File size: 18,091 Bytes
9580dce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | /**
* 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<string, string> = {}): 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<string, unknown>;
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<string, unknown>;
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<string, unknown>;
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<string, unknown>;
// 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<string, unknown>;
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<string, unknown>;
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<string, unknown>;
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<string, unknown>;
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<string, unknown>;
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();
});
});
|