return lowerFactors;
}
-if (import.meta.main) {
+export function main() {
const input = prompt("Seed phrase:");
const words = parseInput(input);
const factors = getLowerFactors(words.length);
console.log("\n");
permutations.forEach((permutation) => console.log(permutation));
}
+
+if (import.meta.main) {
+ main();
+}
-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));
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();
+});