From: Ardis Lu Date: Fri, 1 Dec 2023 05:48:34 +0000 (-0800) Subject: Add e2e tests for main() logic X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=e0460f18b132c17f6a41d4cb6c5e7606f5a84f40;p=seed-phrase-transposer Add e2e tests for main() logic --- diff --git a/main.ts b/main.ts index 1887144..701b203 100644 --- a/main.ts +++ b/main.ts @@ -63,7 +63,7 @@ export function getLowerFactors(num: number): Array { 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(); +} diff --git a/main_test.ts b/main_test.ts index b05ce32..24d6032 100644 --- a/main_test.ts +++ b/main_test.ts @@ -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(); +});