Download src/lib/puzzles/__tests__/sequences.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 10.8 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/puzzles/__tests__/sequences.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/puzzles/__tests__/sequences.test.ts
-
curl -L -o sequences.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/puzzles/__tests__/sequences.test.ts
10.8 kB
| import { describe, it, expect } from "vitest"; | |
| import { | |
| generateSequence, | |
| validateSequence, | |
| scoreSequence, | |
| sievePrimes, | |
| TYPES_BY_DIFFICULTY, | |
| HINTS, | |
| HIDDEN_COUNT, | |
| type SequenceDifficulty, | |
| type SequenceType, | |
| } from "../sequences"; | |
| // ---------- sievePrimes ---------- | |
| describe("sievePrimes", () => { | |
| it("returns primes up to the given limit", () => { | |
| const primes = sievePrimes(30); | |
| expect(primes).toEqual([2, 3, 5, 7, 11, 13, 17, 19, 23, 29]); | |
| }); | |
| it("returns empty for limit < 2", () => { | |
| expect(sievePrimes(1)).toEqual([]); | |
| expect(sievePrimes(0)).toEqual([]); | |
| }); | |
| it("returns [2] for limit 2", () => { | |
| expect(sievePrimes(2)).toEqual([2]); | |
| }); | |
| it("handles larger limits", () => { | |
| const primes = sievePrimes(100); | |
| expect(primes.length).toBe(25); // 25 primes below 100 | |
| expect(primes[0]).toBe(2); | |
| expect(primes[primes.length - 1]).toBe(97); | |
| }); | |
| }); | |
| // ---------- constants ---------- | |
| describe("TYPES_BY_DIFFICULTY", () => { | |
| it("easy includes arithmetic and geometric", () => { | |
| expect(TYPES_BY_DIFFICULTY.easy).toContain("arithmetic"); | |
| expect(TYPES_BY_DIFFICULTY.easy).toContain("geometric"); | |
| }); | |
| it("medium includes fibonacci, triangular, square, geometric", () => { | |
| expect(TYPES_BY_DIFFICULTY.medium).toContain("fibonacci"); | |
| expect(TYPES_BY_DIFFICULTY.medium).toContain("triangular"); | |
| expect(TYPES_BY_DIFFICULTY.medium).toContain("square"); | |
| }); | |
| it("hard includes prime, quadratic, alternating", () => { | |
| expect(TYPES_BY_DIFFICULTY.hard).toContain("prime"); | |
| expect(TYPES_BY_DIFFICULTY.hard).toContain("quadratic"); | |
| expect(TYPES_BY_DIFFICULTY.hard).toContain("alternating"); | |
| }); | |
| }); | |
| describe("HINTS", () => { | |
| it("has a hint for every sequence type", () => { | |
| const allTypes: SequenceType[] = [ | |
| "arithmetic", | |
| "geometric", | |
| "fibonacci", | |
| "triangular", | |
| "square", | |
| "prime", | |
| "quadratic", | |
| "alternating", | |
| ]; | |
| for (const type of allTypes) { | |
| expect(HINTS[type]).toBeDefined(); | |
| expect(HINTS[type].length).toBeGreaterThan(0); | |
| } | |
| }); | |
| }); | |
| describe("HIDDEN_COUNT", () => { | |
| it("easy hides 1, medium 2, hard 3", () => { | |
| expect(HIDDEN_COUNT.easy).toBe(1); | |
| expect(HIDDEN_COUNT.medium).toBe(2); | |
| expect(HIDDEN_COUNT.hard).toBe(3); | |
| }); | |
| }); | |
| // ---------- generateSequence ---------- | |
| describe("generateSequence", () => { | |
| const difficulties: SequenceDifficulty[] = ["easy", "medium", "hard"]; | |
| for (const diff of difficulties) { | |
| describe(`difficulty: ${diff}`, () => { | |
| it("returns a valid puzzle structure", () => { | |
| const puzzle = generateSequence(diff); | |
| expect(puzzle.difficulty).toBe(diff); | |
| expect(puzzle.sequence.length).toBe(puzzle.length); | |
| expect(puzzle.solution.length).toBe(puzzle.length); | |
| expect(puzzle.hiddenIndices.length).toBe(HIDDEN_COUNT[diff]); | |
| expect(puzzle.hint).toBe(HINTS[puzzle.type]); | |
| expect(TYPES_BY_DIFFICULTY[diff]).toContain(puzzle.type); | |
| }); | |
| it("hidden indices are within bounds", () => { | |
| const puzzle = generateSequence(diff); | |
| for (const idx of puzzle.hiddenIndices) { | |
| expect(idx).toBeGreaterThanOrEqual(0); | |
| expect(idx).toBeLessThan(puzzle.length); | |
| } | |
| }); | |
| it("hidden indices are sorted ascending", () => { | |
| const puzzle = generateSequence(diff); | |
| for (let i = 1; i < puzzle.hiddenIndices.length; i++) { | |
| expect(puzzle.hiddenIndices[i]).toBeGreaterThan( | |
| puzzle.hiddenIndices[i - 1], | |
| ); | |
| } | |
| }); | |
| it("sequence has nulls exactly at hidden indices", () => { | |
| const puzzle = generateSequence(diff); | |
| for (let i = 0; i < puzzle.sequence.length; i++) { | |
| if (puzzle.hiddenIndices.includes(i)) { | |
| expect(puzzle.sequence[i]).toBeNull(); | |
| } else { | |
| expect(puzzle.sequence[i]).toBe(puzzle.solution[i]); | |
| } | |
| } | |
| }); | |
| it("solution contains only numbers (no nulls)", () => { | |
| const puzzle = generateSequence(diff); | |
| for (const val of puzzle.solution) { | |
| expect(typeof val).toBe("number"); | |
| expect(Number.isFinite(val)).toBe(true); | |
| } | |
| }); | |
| it("generates at least 6 terms", () => { | |
| const puzzle = generateSequence(diff); | |
| expect(puzzle.length).toBeGreaterThanOrEqual(6); | |
| }); | |
| }); | |
| } | |
| it("respects requestedType when it matches difficulty", () => { | |
| const puzzle = generateSequence("easy", "arithmetic"); | |
| expect(puzzle.type).toBe("arithmetic"); | |
| }); | |
| it("ignores requestedType if not in difficulty pool", () => { | |
| // "prime" is not in easy pool | |
| const puzzle = generateSequence("easy", "prime" as SequenceType); | |
| expect(TYPES_BY_DIFFICULTY.easy).toContain(puzzle.type); | |
| }); | |
| it("defaults to medium difficulty", () => { | |
| const puzzle = generateSequence(); | |
| expect(puzzle.difficulty).toBe("medium"); | |
| }); | |
| }); | |
| // ---------- specific sequence type validation ---------- | |
| describe("arithmetic sequences", () => { | |
| it("has constant differences", () => { | |
| const puzzle = generateSequence("easy", "arithmetic"); | |
| const sol = puzzle.solution; | |
| const diff = sol[1] - sol[0]; | |
| for (let i = 2; i < sol.length; i++) { | |
| expect(sol[i] - sol[i - 1]).toBe(diff); | |
| } | |
| }); | |
| }); | |
| describe("geometric sequences", () => { | |
| it("has constant ratios", () => { | |
| const puzzle = generateSequence("easy", "geometric"); | |
| const sol = puzzle.solution; | |
| if (sol[0] !== 0) { | |
| const ratio = sol[1] / sol[0]; | |
| for (let i = 2; i < sol.length; i++) { | |
| expect(sol[i] / sol[i - 1]).toBeCloseTo(ratio, 5); | |
| } | |
| } | |
| }); | |
| }); | |
| describe("fibonacci sequences", () => { | |
| it("each term equals sum of previous two", () => { | |
| const puzzle = generateSequence("medium", "fibonacci"); | |
| const sol = puzzle.solution; | |
| for (let i = 2; i < sol.length; i++) { | |
| expect(sol[i]).toBe(sol[i - 1] + sol[i - 2]); | |
| } | |
| }); | |
| }); | |
| describe("square sequences", () => { | |
| it("all terms are perfect squares", () => { | |
| const puzzle = generateSequence("medium", "square"); | |
| for (const val of puzzle.solution) { | |
| const sqrt = Math.sqrt(val); | |
| expect(Math.round(sqrt) ** 2).toBe(val); | |
| } | |
| }); | |
| }); | |
| describe("triangular sequences", () => { | |
| it("all terms are triangular numbers", () => { | |
| const puzzle = generateSequence("medium", "triangular"); | |
| for (const val of puzzle.solution) { | |
| // n*(n+1)/2 = val → n^2+n-2val=0 → n = (-1+sqrt(1+8val))/2 | |
| const n = (-1 + Math.sqrt(1 + 8 * val)) / 2; | |
| expect(Math.abs(n - Math.round(n))).toBeLessThan(0.001); | |
| } | |
| }); | |
| }); | |
| describe("prime sequences", () => { | |
| it("all terms are prime", () => { | |
| const puzzle = generateSequence("hard", "prime"); | |
| for (const val of puzzle.solution) { | |
| expect(isPrime(val)).toBe(true); | |
| } | |
| }); | |
| it("terms are consecutive primes", () => { | |
| const puzzle = generateSequence("hard", "prime"); | |
| const allPrimes = sievePrimes(200); | |
| const sol = puzzle.solution; | |
| const startIdx = allPrimes.indexOf(sol[0]); | |
| expect(startIdx).toBeGreaterThanOrEqual(0); | |
| for (let i = 0; i < sol.length; i++) { | |
| expect(sol[i]).toBe(allPrimes[startIdx + i]); | |
| } | |
| }); | |
| }); | |
| describe("alternating sequences", () => { | |
| it("even-indexed and odd-indexed terms each form arithmetic sequences", () => { | |
| const puzzle = generateSequence("hard", "alternating"); | |
| const sol = puzzle.solution; | |
| const evens = sol.filter((_, i) => i % 2 === 0); | |
| const odds = sol.filter((_, i) => i % 2 === 1); | |
| // Check constant differences in each sub-sequence | |
| if (evens.length >= 3) { | |
| const d = evens[1] - evens[0]; | |
| for (let i = 2; i < evens.length; i++) { | |
| expect(evens[i] - evens[i - 1]).toBe(d); | |
| } | |
| } | |
| if (odds.length >= 3) { | |
| const d = odds[1] - odds[0]; | |
| for (let i = 2; i < odds.length; i++) { | |
| expect(odds[i] - odds[i - 1]).toBe(d); | |
| } | |
| } | |
| }); | |
| }); | |
| // ---------- validateSequence ---------- | |
| describe("validateSequence", () => { | |
| it("returns true for correct answers", () => { | |
| const puzzle = generateSequence("medium"); | |
| const answers = puzzle.hiddenIndices.map((i) => puzzle.solution[i]); | |
| expect(validateSequence(answers, puzzle.solution, puzzle.hiddenIndices)).toBe( | |
| true, | |
| ); | |
| }); | |
| it("returns false for wrong answers", () => { | |
| const puzzle = generateSequence("medium"); | |
| const answers = puzzle.hiddenIndices.map((i) => puzzle.solution[i] + 1); | |
| expect(validateSequence(answers, puzzle.solution, puzzle.hiddenIndices)).toBe( | |
| false, | |
| ); | |
| }); | |
| it("returns false for mismatched answer count", () => { | |
| const puzzle = generateSequence("medium"); | |
| expect(validateSequence([], puzzle.solution, puzzle.hiddenIndices)).toBe( | |
| false, | |
| ); | |
| }); | |
| it("returns false for partially correct answers", () => { | |
| const puzzle = generateSequence("hard"); | |
| const answers = puzzle.hiddenIndices.map((i) => puzzle.solution[i]); | |
| // Corrupt the last answer | |
| answers[answers.length - 1] += 999; | |
| expect(validateSequence(answers, puzzle.solution, puzzle.hiddenIndices)).toBe( | |
| false, | |
| ); | |
| }); | |
| }); | |
| // ---------- scoreSequence ---------- | |
| describe("scoreSequence", () => { | |
| it("returns 1.0 for all correct", () => { | |
| const puzzle = generateSequence("hard"); | |
| const answers = puzzle.hiddenIndices.map((i) => puzzle.solution[i]); | |
| expect(scoreSequence(answers, puzzle.solution, puzzle.hiddenIndices)).toBe( | |
| 1.0, | |
| ); | |
| }); | |
| it("returns 0 for all wrong", () => { | |
| const puzzle = generateSequence("hard"); | |
| const answers = puzzle.hiddenIndices.map( | |
| (i) => puzzle.solution[i] + 999999, | |
| ); | |
| expect(scoreSequence(answers, puzzle.solution, puzzle.hiddenIndices)).toBe( | |
| 0, | |
| ); | |
| }); | |
| it("returns partial credit", () => { | |
| const solution = [1, 2, 3, 4, 5, 6]; | |
| const hidden = [2, 4, 5]; | |
| // 2 out of 3 correct | |
| const answers = [3, 999, 6]; | |
| expect(scoreSequence(answers, solution, hidden)).toBeCloseTo(2 / 3, 5); | |
| }); | |
| it("returns 0 for empty answers", () => { | |
| expect(scoreSequence([], [1, 2, 3], [1])).toBe(0); | |
| }); | |
| it("returns 0 for empty hidden indices", () => { | |
| expect(scoreSequence([1], [1, 2, 3], [])).toBe(0); | |
| }); | |
| it("handles single hidden term", () => { | |
| const solution = [2, 4, 6, 8, 10]; | |
| expect(scoreSequence([8], solution, [3])).toBe(1.0); | |
| expect(scoreSequence([7], solution, [3])).toBe(0); | |
| }); | |
| }); | |
| // ---------- helper ---------- | |
| function isPrime(n: number): boolean { | |
| if (n < 2) return false; | |
| if (n < 4) return true; | |
| if (n % 2 === 0 || n % 3 === 0) return false; | |
| for (let i = 5; i * i <= n; i += 6) { | |
| if (n % i === 0 || n % (i + 2) === 0) return false; | |
| } | |
| return true; | |
| } | |