import { describe, it, expect } from "vitest"; import { generatePattern, validatePattern, scorePattern, cellKey, type PatternCell, type PatternDifficulty, type Shape, type Fill, type Size, type Rotation, } from "../patterns"; // --- Helpers --- const VALID_SHAPES: Shape[] = ["circle", "triangle", "square", "diamond", "star", "hexagon"]; const VALID_FILLS: Fill[] = ["solid", "striped", "empty"]; const VALID_SIZES: Size[] = ["small", "medium", "large"]; const VALID_ROTATIONS: Rotation[] = [0, 45, 90, 135, 180, 225, 270, 315]; function isValidCell(cell: PatternCell): boolean { return ( VALID_SHAPES.includes(cell.shape) && VALID_FILLS.includes(cell.fill) && VALID_SIZES.includes(cell.size) && VALID_ROTATIONS.includes(cell.rotation) ); } // --- cellKey --- describe("cellKey", () => { it("produces a deterministic string from cell attributes", () => { const cell: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 }; expect(cellKey(cell)).toBe("circle-solid-small-0"); }); it("produces different keys for different cells", () => { const a: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 }; const b: PatternCell = { shape: "triangle", fill: "solid", size: "small", rotation: 0 }; expect(cellKey(a)).not.toBe(cellKey(b)); }); it("produces same key for identical cells", () => { const a: PatternCell = { shape: "star", fill: "striped", size: "large", rotation: 90 }; const b: PatternCell = { shape: "star", fill: "striped", size: "large", rotation: 90 }; expect(cellKey(a)).toBe(cellKey(b)); }); }); // --- validatePattern --- describe("validatePattern", () => { it("returns true for matching cells", () => { const cell: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 }; expect(validatePattern(cell, cell)).toBe(true); }); it("returns false for different shapes", () => { const a: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 }; const b: PatternCell = { shape: "square", fill: "solid", size: "small", rotation: 0 }; expect(validatePattern(a, b)).toBe(false); }); it("returns false for different fills", () => { const a: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 }; const b: PatternCell = { shape: "circle", fill: "empty", size: "small", rotation: 0 }; expect(validatePattern(a, b)).toBe(false); }); it("returns false for different sizes", () => { const a: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 }; const b: PatternCell = { shape: "circle", fill: "solid", size: "large", rotation: 0 }; expect(validatePattern(a, b)).toBe(false); }); it("returns false for different rotations", () => { const a: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 }; const b: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 90 }; expect(validatePattern(a, b)).toBe(false); }); }); // --- scorePattern --- describe("scorePattern", () => { it("returns 1.0 for correct answer", () => { const cell: PatternCell = { shape: "diamond", fill: "striped", size: "medium", rotation: 45 }; expect(scorePattern(cell, cell)).toBe(1.0); }); it("returns 0.0 for wrong answer", () => { const a: PatternCell = { shape: "diamond", fill: "striped", size: "medium", rotation: 45 }; const b: PatternCell = { shape: "hexagon", fill: "striped", size: "medium", rotation: 45 }; expect(scorePattern(a, b)).toBe(0.0); }); }); // --- generatePattern: structure --- describe("generatePattern - structure", () => { const difficulties: PatternDifficulty[] = ["easy", "medium", "hard"]; for (const diff of difficulties) { describe(`difficulty=${diff}`, () => { // Generate a few puzzles to test stochastic properties const puzzles = Array.from({ length: 10 }, () => generatePattern(diff)); it("returns a grid of 9 elements", () => { for (const p of puzzles) { expect(p.grid).toHaveLength(9); } }); it("has exactly one null cell (index 8)", () => { for (const p of puzzles) { expect(p.grid[8]).toBeNull(); const nullCount = p.grid.filter((c) => c === null).length; expect(nullCount).toBe(1); } }); it("has valid cells for all non-null grid positions", () => { for (const p of puzzles) { for (let i = 0; i < 8; i++) { const cell = p.grid[i]; expect(cell).not.toBeNull(); expect(isValidCell(cell!)).toBe(true); } } }); it("answer is a valid cell", () => { for (const p of puzzles) { expect(isValidCell(p.answer)).toBe(true); } }); it("correctIndex points to the answer in choices", () => { for (const p of puzzles) { const chosen = p.choices[p.correctIndex]; expect(cellKey(chosen)).toBe(cellKey(p.answer)); } }); it("all choices are valid cells", () => { for (const p of puzzles) { for (const choice of p.choices) { expect(isValidCell(choice)).toBe(true); } } }); it("choices have no duplicates", () => { for (const p of puzzles) { const keys = p.choices.map(cellKey); expect(new Set(keys).size).toBe(keys.length); } }); it("difficulty matches input", () => { for (const p of puzzles) { expect(p.difficulty).toBe(diff); } }); it("has a non-empty hint string", () => { for (const p of puzzles) { expect(p.hint.length).toBeGreaterThan(0); } }); it("activeAttributes is non-empty", () => { for (const p of puzzles) { expect(p.activeAttributes.length).toBeGreaterThan(0); } }); }); } }); // --- generatePattern: difficulty scaling --- describe("generatePattern - difficulty scaling", () => { it("easy puzzles have 2 active attributes", () => { const puzzles = Array.from({ length: 20 }, () => generatePattern("easy")); for (const p of puzzles) { expect(p.activeAttributes).toHaveLength(2); } }); it("medium puzzles have 3 active attributes", () => { const puzzles = Array.from({ length: 20 }, () => generatePattern("medium")); for (const p of puzzles) { expect(p.activeAttributes).toHaveLength(3); } }); it("hard puzzles have 4 active attributes", () => { const puzzles = Array.from({ length: 20 }, () => generatePattern("hard")); for (const p of puzzles) { expect(p.activeAttributes).toHaveLength(4); } }); it("easy puzzles have 4 choices", () => { const puzzles = Array.from({ length: 10 }, () => generatePattern("easy")); for (const p of puzzles) { expect(p.choices).toHaveLength(4); } }); it("medium puzzles have 5 choices", () => { const puzzles = Array.from({ length: 10 }, () => generatePattern("medium")); for (const p of puzzles) { expect(p.choices).toHaveLength(5); } }); it("hard puzzles have 6 choices", () => { const puzzles = Array.from({ length: 10 }, () => generatePattern("hard")); for (const p of puzzles) { expect(p.choices).toHaveLength(6); } }); }); // --- generatePattern: pattern integrity --- describe("generatePattern - pattern integrity", () => { it("row-shift rule produces correct shifting pattern", () => { // Generate many puzzles and check that at least some use row-shift // where each row is a rotation of the same triad const puzzles = Array.from({ length: 50 }, () => generatePattern("medium")); // For each puzzle, verify the answer (cell 8) is derivable from the grid // by checking that the answer + cell 8's row/col contexts are consistent for (const p of puzzles) { // The full grid (with answer placed back) should have consistent patterns const full = [...p.grid.slice(0, 8), p.answer]; // Check: each row should have internally consistent attributes for (let row = 0; row < 3; row++) { const rowCells = [full[row * 3], full[row * 3 + 1], full[row * 3 + 2]]; // All cells should exist expect(rowCells.every((c) => c !== null)).toBe(true); } // Check: each column should have internally consistent attributes for (let col = 0; col < 3; col++) { const colCells = [full[col], full[3 + col], full[6 + col]]; expect(colCells.every((c) => c !== null)).toBe(true); } } }); it("same rule produces identical values across rows", () => { // Generate many easy puzzles — easy mode preferentially uses "same" rule const puzzles = Array.from({ length: 100 }, () => generatePattern("easy")); let foundSameRule = false; for (const p of puzzles) { const full = [...p.grid.slice(0, 8), p.answer]; // Check each attribute: if all 3 values in a row are identical for all 3 rows, // that attribute uses the "same" rule for (const attr of ["shape", "fill", "size", "rotation"] as const) { let allRowsSame = true; for (let row = 0; row < 3; row++) { const vals = [ full[row * 3]![attr], full[row * 3 + 1]![attr], full[row * 3 + 2]![attr], ]; if (vals[0] !== vals[1] || vals[1] !== vals[2]) { allRowsSame = false; break; } } if (allRowsSame) { foundSameRule = true; } } } // With 100 easy puzzles, extremely likely to find at least one "same" rule expect(foundSameRule).toBe(true); }); }); // --- generatePattern: active attributes coverage --- describe("generatePattern - attribute coverage", () => { it("all 4 attributes appear across many puzzles", () => { const seen = new Set(); const puzzles = Array.from({ length: 50 }, () => generatePattern("hard")); for (const p of puzzles) { for (const attr of p.activeAttributes) { seen.add(attr); } } expect(seen).toContain("shape"); expect(seen).toContain("fill"); expect(seen).toContain("size"); expect(seen).toContain("rotation"); }); it("hard puzzles always include all 4 attributes", () => { const puzzles = Array.from({ length: 20 }, () => generatePattern("hard")); for (const p of puzzles) { const attrs = new Set(p.activeAttributes); expect(attrs.has("shape")).toBe(true); expect(attrs.has("fill")).toBe(true); expect(attrs.has("size")).toBe(true); expect(attrs.has("rotation")).toBe(true); } }); }); // --- generatePattern: variety --- describe("generatePattern - variety", () => { it("generates different puzzles (non-deterministic)", () => { const keys = new Set(); for (let i = 0; i < 20; i++) { const p = generatePattern("medium"); // Use grid cell keys as a puzzle fingerprint const fingerprint = p.grid .map((c) => (c ? cellKey(c) : "null")) .join("|"); keys.add(fingerprint); } // Should have at least 2 distinct puzzles out of 20 expect(keys.size).toBeGreaterThan(1); }); it("uses multiple shape types across puzzles", () => { const shapes = new Set(); for (let i = 0; i < 30; i++) { const p = generatePattern("medium"); for (const cell of p.grid) { if (cell) shapes.add(cell.shape); } } // Should see at least 3 different shapes expect(shapes.size).toBeGreaterThanOrEqual(3); }); });