]> git.ardis.lu - seed-phrase-transposer/commitdiff
Add e2e tests for main() logic
authorArdis Lu <ardisglu@gmail.com>
Fri, 1 Dec 2023 05:48:34 +0000 (21:48 -0800)
committerArdis Lu <ardisglu@gmail.com>
Fri, 1 Dec 2023 05:48:34 +0000 (21:48 -0800)
main.ts
main_test.ts

diff --git a/main.ts b/main.ts
index 18871441cc1c7dc1e9461463ffa6d278323a7353..701b203c043b0d0563f13985fdf8a7a875cda7a5 100644 (file)
--- a/main.ts
+++ b/main.ts
@@ -63,7 +63,7 @@ export function getLowerFactors(num: number): Array<number> {
   return lowerFactors;
 }
 
-if (import.meta.main) {
+export function main() {
   const input = prompt("Seed phrase:");
   const words = parseInput(input);
   const factors = getLowerFactors(words.length);
@@ -78,3 +78,7 @@ if (import.meta.main) {
   console.log("\n");
   permutations.forEach((permutation) => console.log(permutation));
 }
+
+if (import.meta.main) {
+  main();
+}
index b05ce3258bc56703de517d03032c9c10ba15a1d7..24d6032ae9a24345efb2ed3f19ccb2ea0fdbe6b5 100644 (file)
@@ -1,10 +1,15 @@
-import { getLowerFactors, getPermutations, parseInput } from "./main.ts";
+import { getLowerFactors, getPermutations, main, parseInput } from "./main.ts";
 import {
   assertArrayIncludes,
   assertEquals,
   assertFalse,
   assertThrows,
 } from "https://deno.land/std@0.208.0/assert/mod.ts";
+import {
+  assertSpyCalls,
+  spy,
+  stub,
+} from "https://deno.land/std@0.208.0/testing/mock.ts";
 
 Deno.test("parseInput throws on null", () => {
   assertThrows(() => parseInput(null));
@@ -177,3 +182,33 @@ Deno.test("getLowerFactors returns correct factors of 24", () => {
   assertFalse(factors.includes(8));
   assertFalse(factors.includes(12));
 });
+
+Deno.test("main logs permutations of a 12-word seed phrase", () => {
+  const consoleSpy = spy(console, "log");
+  const promptStub = stub(
+    globalThis,
+    "prompt",
+    (_) => "1 4 7 10 2 5 8 11 3 6 9 12",
+  );
+
+  main();
+  assertSpyCalls(consoleSpy, 5); // console.log was called exactly 5 times
+
+  consoleSpy.restore();
+  promptStub.restore();
+});
+
+Deno.test("main exits cleanly on a malformed seed phrase", () => {
+  const consoleSpy = spy(console, "log");
+  const promptStub = stub(
+    globalThis,
+    "prompt",
+    (_) => "1 2 3 4 5",
+  );
+
+  assertThrows(main); // Calling Deno.exit() within a test throws an error
+  assertSpyCalls(consoleSpy, 1); // console.log was called exactly 1 time
+
+  consoleSpy.restore();
+  promptStub.restore();
+});
This page took 0.037959 seconds and 4 git commands to generate.