/** * Tests for GET /api/logic/generate * * Covers: * - Input validation: invalid difficulty, invalid type * - All 6 puzzle types: sudoku, sequence, pattern, wordscramble, constraint, mathequation * - Default behaviour (no params → sudoku/medium) * - Response shape for each type * - Optional sub-params: sequenceType, wordCategory */ import { describe, it, expect, vi, beforeEach } from "vitest"; import { NextRequest } from "next/server"; // --------------------------------------------------------------------------- // Mock all puzzle generators // --------------------------------------------------------------------------- const mockGenerateSudoku = vi.fn(); const mockGenerateSequence = vi.fn(); const mockGeneratePattern = vi.fn(); const mockGenerateWordScramble = vi.fn(); const mockGenerateConstraintPuzzle = vi.fn(); const mockGenerateMathEquation = vi.fn(); vi.mock("@/lib/puzzles/sudoku", () => ({ generateSudoku: (...args: unknown[]) => mockGenerateSudoku(...args), })); vi.mock("@/lib/puzzles/sequences", () => ({ generateSequence: (...args: unknown[]) => mockGenerateSequence(...args), })); vi.mock("@/lib/puzzles/patterns", () => ({ generatePattern: (...args: unknown[]) => mockGeneratePattern(...args), })); vi.mock("@/lib/puzzles/wordscramble", () => ({ generateWordScramble: (...args: unknown[]) => mockGenerateWordScramble(...args), })); vi.mock("@/lib/puzzles/constraint", () => ({ generateConstraintPuzzle: (...args: unknown[]) => mockGenerateConstraintPuzzle(...args), })); vi.mock("@/lib/puzzles/mathequation", () => ({ generateMathEquation: (...args: unknown[]) => mockGenerateMathEquation(...args), })); // --------------------------------------------------------------------------- // Import route handler (after mocks) // --------------------------------------------------------------------------- import { GET } from "../generate/route"; // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- function makeRequest(params: Record = {}): NextRequest { const url = new URL("http://localhost/api/logic/generate"); for (const [key, val] of Object.entries(params)) { url.searchParams.set(key, val); } return new NextRequest(url.toString()); } // Minimal fixture shapes for each puzzle type const SUDOKU_FIXTURE = { puzzle: Array(9).fill(Array(9).fill(0)), solution: Array(9).fill(Array(9).fill(1)), }; const SEQUENCE_FIXTURE = { terms: [1, 2, 3, 4, 5], hiddenIndices: [4], answer: [5], hint: "Arithmetic", sequenceType: "arithmetic", difficulty: "medium", }; const PATTERN_FIXTURE = { grid: [null, null, null, null, null, null, null, null, null], choices: [{ shape: "circle", fill: "solid", size: "medium", rotation: 0 }], correctIndex: 0, activeAttributes: ["shape", "fill"], hint: "Look at the shapes", difficulty: "medium", }; const WORDSCRAMBLE_FIXTURE = { scrambled: "ELPPA", answer: "APPLE", hintIndices: [0], hint: "A fruit", category: "food", length: 5, difficulty: "easy", }; const CONSTRAINT_FIXTURE = { solution: [[1, 2, 3, 4], [2, 1, 4, 3], [3, 4, 1, 2], [4, 3, 2, 1]], given: [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], cages: [{ id: "c1", cells: [{ row: 0, col: 0 }, { row: 0, col: 1 }], target: 3 }], hint: "Fill the grid so each row and column contains 1–4", difficulty: "medium", }; const MATHEQUATION_FIXTURE = { equation: "? + 5 = 10", answer: 5, hint: "Subtract 5 from both sides", operationCount: 1, difficulty: "easy", }; beforeEach(() => { vi.clearAllMocks(); mockGenerateSudoku.mockReturnValue(SUDOKU_FIXTURE); mockGenerateSequence.mockReturnValue(SEQUENCE_FIXTURE); mockGeneratePattern.mockReturnValue(PATTERN_FIXTURE); mockGenerateWordScramble.mockReturnValue(WORDSCRAMBLE_FIXTURE); mockGenerateConstraintPuzzle.mockReturnValue(CONSTRAINT_FIXTURE); mockGenerateMathEquation.mockReturnValue(MATHEQUATION_FIXTURE); }); // ─── Validation ─────────────────────────────────────────────────────────────── describe("GET /api/logic/generate — input validation", () => { it("returns 400 for an invalid difficulty", async () => { const res = await GET(makeRequest({ difficulty: "extreme" })); expect(res.status).toBe(400); const body = await res.json() as { error: string }; expect(body.error).toMatch(/difficulty/i); }); it("returns 400 for an invalid puzzle type", async () => { const res = await GET(makeRequest({ type: "nonexistent", difficulty: "easy" })); expect(res.status).toBe(400); const body = await res.json() as { error: string }; expect(body.error).toMatch(/type/i); }); it("accepts all three valid difficulties without error", async () => { for (const diff of ["easy", "medium", "hard"]) { const res = await GET(makeRequest({ type: "sudoku", difficulty: diff })); expect(res.status).toBe(200); } }); it("accepts all six valid puzzle types without error", async () => { for (const type of ["sudoku", "sequence", "pattern", "wordscramble", "constraint", "mathequation"]) { const res = await GET(makeRequest({ type, difficulty: "medium" })); expect(res.status).toBe(200); } }); }); // ─── Default behaviour ──────────────────────────────────────────────────────── describe("GET /api/logic/generate — defaults", () => { it("defaults to sudoku when no type param is given", async () => { const res = await GET(makeRequest({ difficulty: "medium" })); expect(res.status).toBe(200); expect(mockGenerateSudoku).toHaveBeenCalledWith("medium"); }); it("defaults to medium difficulty when no difficulty param is given", async () => { const res = await GET(makeRequest({ type: "sudoku" })); expect(res.status).toBe(200); expect(mockGenerateSudoku).toHaveBeenCalledWith("medium"); }); it("defaults to sudoku/medium when no params at all", async () => { const res = await GET(makeRequest()); expect(res.status).toBe(200); expect(mockGenerateSudoku).toHaveBeenCalledWith("medium"); }); }); // ─── Sudoku ─────────────────────────────────────────────────────────────────── describe("GET /api/logic/generate — sudoku", () => { it("returns puzzleType=sudoku with puzzle, solution, difficulty, generatedAt", async () => { const res = await GET(makeRequest({ type: "sudoku", difficulty: "hard" })); expect(res.status).toBe(200); const body = await res.json() as Record; expect(body.puzzleType).toBe("sudoku"); expect(body.puzzle).toBeDefined(); expect(body.solution).toBeDefined(); expect(body.difficulty).toBe("hard"); expect(typeof body.generatedAt).toBe("string"); }); it("passes the difficulty to generateSudoku", async () => { await GET(makeRequest({ type: "sudoku", difficulty: "easy" })); expect(mockGenerateSudoku).toHaveBeenCalledWith("easy"); }); it("does not call any other generator", async () => { await GET(makeRequest({ type: "sudoku", difficulty: "medium" })); expect(mockGenerateSequence).not.toHaveBeenCalled(); expect(mockGeneratePattern).not.toHaveBeenCalled(); }); }); // ─── Sequence ───────────────────────────────────────────────────────────────── describe("GET /api/logic/generate — sequence", () => { it("returns puzzleType=sequence with required fields", async () => { const res = await GET(makeRequest({ type: "sequence", difficulty: "medium" })); expect(res.status).toBe(200); const body = await res.json() as Record; expect(body.puzzleType).toBe("sequence"); expect(body.terms).toBeDefined(); expect(body.hiddenIndices).toBeDefined(); expect(body.hint).toBeDefined(); expect(typeof body.generatedAt).toBe("string"); }); it("passes difficulty to generateSequence", async () => { await GET(makeRequest({ type: "sequence", difficulty: "hard" })); expect(mockGenerateSequence).toHaveBeenCalledWith("hard", undefined); }); it("passes sequenceType to generateSequence when provided", async () => { await GET(makeRequest({ type: "sequence", difficulty: "easy", sequenceType: "fibonacci" })); expect(mockGenerateSequence).toHaveBeenCalledWith("easy", "fibonacci"); }); it("passes undefined sequenceType when not provided", async () => { await GET(makeRequest({ type: "sequence", difficulty: "medium" })); expect(mockGenerateSequence).toHaveBeenCalledWith("medium", undefined); }); }); // ─── Pattern ────────────────────────────────────────────────────────────────── describe("GET /api/logic/generate — pattern", () => { it("returns puzzleType=pattern with required fields", async () => { const res = await GET(makeRequest({ type: "pattern", difficulty: "medium" })); expect(res.status).toBe(200); const body = await res.json() as Record; expect(body.puzzleType).toBe("pattern"); expect(body.grid).toBeDefined(); expect(body.choices).toBeDefined(); expect(body.correctIndex).toBeDefined(); expect(body.activeAttributes).toBeDefined(); expect(body.hint).toBeDefined(); expect(body.difficulty).toBe("medium"); expect(typeof body.generatedAt).toBe("string"); }); it("passes difficulty to generatePattern", async () => { await GET(makeRequest({ type: "pattern", difficulty: "hard" })); expect(mockGeneratePattern).toHaveBeenCalledWith("hard"); }); it("does not expose the full puzzle object (only selected fields)", async () => { const res = await GET(makeRequest({ type: "pattern", difficulty: "easy" })); const body = await res.json() as Record; // The route explicitly picks fields — ensure they're all present expect(Object.keys(body)).toContain("grid"); expect(Object.keys(body)).toContain("choices"); expect(Object.keys(body)).toContain("correctIndex"); expect(Object.keys(body)).toContain("activeAttributes"); expect(Object.keys(body)).toContain("hint"); expect(Object.keys(body)).toContain("difficulty"); expect(Object.keys(body)).toContain("generatedAt"); }); }); // ─── Word Scramble ──────────────────────────────────────────────────────────── describe("GET /api/logic/generate — wordscramble", () => { it("returns puzzleType=wordscramble with required fields", async () => { const res = await GET(makeRequest({ type: "wordscramble", difficulty: "easy" })); expect(res.status).toBe(200); const body = await res.json() as Record; expect(body.puzzleType).toBe("wordscramble"); expect(body.scrambled).toBeDefined(); expect(body.answer).toBeDefined(); expect(body.hintIndices).toBeDefined(); expect(body.hint).toBeDefined(); expect(body.category).toBeDefined(); expect(body.length).toBeDefined(); expect(body.difficulty).toBe("easy"); expect(typeof body.generatedAt).toBe("string"); }); it("passes difficulty to generateWordScramble", async () => { await GET(makeRequest({ type: "wordscramble", difficulty: "hard" })); expect(mockGenerateWordScramble).toHaveBeenCalledWith("hard", undefined); }); it("passes wordCategory to generateWordScramble when provided", async () => { await GET(makeRequest({ type: "wordscramble", difficulty: "medium", wordCategory: "science" })); expect(mockGenerateWordScramble).toHaveBeenCalledWith("medium", "science"); }); it("passes undefined wordCategory when not provided", async () => { await GET(makeRequest({ type: "wordscramble", difficulty: "easy" })); expect(mockGenerateWordScramble).toHaveBeenCalledWith("easy", undefined); }); }); // ─── Constraint ─────────────────────────────────────────────────────────────── describe("GET /api/logic/generate — constraint", () => { it("returns puzzleType=constraint with required fields", async () => { const res = await GET(makeRequest({ type: "constraint", difficulty: "medium" })); expect(res.status).toBe(200); const body = await res.json() as Record; expect(body.puzzleType).toBe("constraint"); expect(body.solution).toBeDefined(); expect(body.given).toBeDefined(); expect(body.cages).toBeDefined(); expect(body.hint).toBeDefined(); expect(body.difficulty).toBe("medium"); expect(typeof body.generatedAt).toBe("string"); }); it("passes difficulty to generateConstraintPuzzle", async () => { await GET(makeRequest({ type: "constraint", difficulty: "easy" })); expect(mockGenerateConstraintPuzzle).toHaveBeenCalledWith("easy"); }); }); // ─── Math Equation ──────────────────────────────────────────────────────────── describe("GET /api/logic/generate — mathequation", () => { it("returns puzzleType=mathequation with required fields", async () => { const res = await GET(makeRequest({ type: "mathequation", difficulty: "easy" })); expect(res.status).toBe(200); const body = await res.json() as Record; expect(body.puzzleType).toBe("mathequation"); expect(body.equation).toBeDefined(); expect(body.answer).toBeDefined(); expect(body.hint).toBeDefined(); expect(body.operationCount).toBeDefined(); expect(body.difficulty).toBe("easy"); expect(typeof body.generatedAt).toBe("string"); }); it("passes difficulty to generateMathEquation", async () => { await GET(makeRequest({ type: "mathequation", difficulty: "hard" })); expect(mockGenerateMathEquation).toHaveBeenCalledWith("hard"); }); it("includes all fixture fields in the response", async () => { const res = await GET(makeRequest({ type: "mathequation", difficulty: "easy" })); const body = await res.json() as Record; expect(body.equation).toBe(MATHEQUATION_FIXTURE.equation); expect(body.answer).toBe(MATHEQUATION_FIXTURE.answer); expect(body.hint).toBe(MATHEQUATION_FIXTURE.hint); expect(body.operationCount).toBe(MATHEQUATION_FIXTURE.operationCount); }); }); // ─── generatedAt timestamp ──────────────────────────────────────────────────── describe("GET /api/logic/generate — generatedAt", () => { it("returns a valid ISO timestamp for every puzzle type", async () => { for (const type of ["sudoku", "sequence", "pattern", "wordscramble", "constraint", "mathequation"]) { const res = await GET(makeRequest({ type, difficulty: "medium" })); const body = await res.json() as Record; expect(() => new Date(body.generatedAt as string)).not.toThrow(); expect(new Date(body.generatedAt as string).toISOString()).toBe(body.generatedAt); } }); }); // ─── Isolation — generators don't cross-fire ───────────────────────────────── describe("GET /api/logic/generate — generator isolation", () => { it("only calls generateSequence for type=sequence", async () => { await GET(makeRequest({ type: "sequence", difficulty: "medium" })); expect(mockGenerateSequence).toHaveBeenCalledOnce(); expect(mockGenerateSudoku).not.toHaveBeenCalled(); expect(mockGeneratePattern).not.toHaveBeenCalled(); expect(mockGenerateWordScramble).not.toHaveBeenCalled(); expect(mockGenerateConstraintPuzzle).not.toHaveBeenCalled(); expect(mockGenerateMathEquation).not.toHaveBeenCalled(); }); it("only calls generatePattern for type=pattern", async () => { await GET(makeRequest({ type: "pattern", difficulty: "medium" })); expect(mockGeneratePattern).toHaveBeenCalledOnce(); expect(mockGenerateSudoku).not.toHaveBeenCalled(); expect(mockGenerateSequence).not.toHaveBeenCalled(); expect(mockGenerateWordScramble).not.toHaveBeenCalled(); expect(mockGenerateConstraintPuzzle).not.toHaveBeenCalled(); expect(mockGenerateMathEquation).not.toHaveBeenCalled(); }); it("only calls generateConstraintPuzzle for type=constraint", async () => { await GET(makeRequest({ type: "constraint", difficulty: "medium" })); expect(mockGenerateConstraintPuzzle).toHaveBeenCalledOnce(); expect(mockGenerateSudoku).not.toHaveBeenCalled(); expect(mockGenerateSequence).not.toHaveBeenCalled(); expect(mockGeneratePattern).not.toHaveBeenCalled(); expect(mockGenerateWordScramble).not.toHaveBeenCalled(); expect(mockGenerateMathEquation).not.toHaveBeenCalled(); }); it("only calls generateMathEquation for type=mathequation", async () => { await GET(makeRequest({ type: "mathequation", difficulty: "medium" })); expect(mockGenerateMathEquation).toHaveBeenCalledOnce(); expect(mockGenerateSudoku).not.toHaveBeenCalled(); expect(mockGenerateSequence).not.toHaveBeenCalled(); expect(mockGeneratePattern).not.toHaveBeenCalled(); expect(mockGenerateWordScramble).not.toHaveBeenCalled(); expect(mockGenerateConstraintPuzzle).not.toHaveBeenCalled(); }); });