Download src/app/api/stats/__tests__/personal-bests.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 16.4 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/stats/__tests__/personal-bests.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/app/api/stats/__tests__/personal-bests.test.ts
-
curl -L -o personal-bests.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/stats/__tests__/personal-bests.test.ts
16.4 kB
| /** | |
| * Tests for GET /api/stats/personal-bests | |
| * | |
| * Covers: | |
| * - empty records when no activities exist | |
| * - empty records when all activities have null durationSecs | |
| * - empty records when all activities have score β€ 0 | |
| * - correctly identifies personal best (fastest correct solve per type+difficulty) | |
| * - accumulates solveCount across multiple correct solves | |
| * - ignores activities with null durationSecs | |
| * - ignores activities with score = 0 (wrong answers) | |
| * - ignores activities with score = null | |
| * - difficulty extracted from description: "easy", "medium", "hard" | |
| * - activities with no recognisable difficulty are skipped | |
| * - response is ordered: exercise type order, then easy β medium β hard | |
| * - all 6 logic exercise types appear when data exists for each | |
| * - achievedAt is the ISO timestamp of the fastest solve | |
| * - response shape: { records: [...] } | |
| * - DB errors propagate as 500 | |
| */ | |
| import { describe, it, expect, vi, beforeEach } from "vitest"; | |
| import { NextRequest } from "next/server"; | |
| // --------------------------------------------------------------------------- | |
| // Mock prisma | |
| // --------------------------------------------------------------------------- | |
| const mockActivityFindMany = vi.fn(); | |
| vi.mock("@/lib/db", () => ({ | |
| prisma: { | |
| activity: { findMany: (...args: unknown[]) => mockActivityFindMany(...args) }, | |
| }, | |
| })); | |
| import { GET } from "../personal-bests/route"; | |
| // --------------------------------------------------------------------------- | |
| // Helpers | |
| // --------------------------------------------------------------------------- | |
| function makeRequest() { | |
| return new NextRequest("http://localhost/api/stats/personal-bests"); | |
| } | |
| function makeActivity(overrides: { | |
| exerciseType: string; | |
| description: string; | |
| durationSecs: number | null; | |
| score: number | null; | |
| createdAt?: Date; | |
| }) { | |
| return { | |
| exerciseType: overrides.exerciseType, | |
| description: overrides.description, | |
| durationSecs: overrides.durationSecs, | |
| score: overrides.score, | |
| createdAt: overrides.createdAt ?? new Date("2026-01-15T10:00:00Z"), | |
| }; | |
| } | |
| async function json(res: Response) { | |
| return res.json() as Promise<Record<string, unknown>>; | |
| } | |
| beforeEach(() => { | |
| mockActivityFindMany.mockResolvedValue([]); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // Empty / no-data cases | |
| // --------------------------------------------------------------------------- | |
| describe("GET /api/stats/personal-bests β empty data", () => { | |
| it("returns empty records array when no activities exist", async () => { | |
| mockActivityFindMany.mockResolvedValue([]); | |
| const res = await GET(makeRequest()); | |
| expect(res.status).toBe(200); | |
| const body = await json(res); | |
| expect(body.records).toEqual([]); | |
| }); | |
| it("returns empty records when prisma returns no activities (DB filters applied)", async () => { | |
| // The DB query filters durationSecs NOT NULL and score > 0. | |
| // When those filters exclude everything, prisma returns []. | |
| mockActivityFindMany.mockResolvedValue([]); | |
| const res = await GET(makeRequest()); | |
| const body = await json(res); | |
| expect(body.records).toEqual([]); | |
| }); | |
| it("skips activities with no recognisable difficulty in description", async () => { | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: "sudoku", description: "Completed Sudoku in 60s", durationSecs: 60, score: 1 }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const body = await json(res); | |
| expect(body.records).toEqual([]); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // Correct extraction | |
| // --------------------------------------------------------------------------- | |
| describe("GET /api/stats/personal-bests β PB extraction", () => { | |
| it("returns one record for a single correct solve", async () => { | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 120s", durationSecs: 120, score: 1 }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const body = await json(res); | |
| const records = body.records as Array<Record<string, unknown>>; | |
| expect(records).toHaveLength(1); | |
| const r = records[0]; | |
| expect(r.exerciseType).toBe("sudoku"); | |
| expect(r.difficulty).toBe("easy"); | |
| expect(r.bestSecs).toBe(120); | |
| expect(r.solveCount).toBe(1); | |
| }); | |
| it("picks the fastest solve as bestSecs (activities pre-ordered by durationSecs asc)", async () => { | |
| // Simulate prisma returning in ascending order (fastest first) | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 90s", durationSecs: 90, score: 1, createdAt: new Date("2026-01-20T10:00:00Z") }), | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 120s", durationSecs: 120, score: 1 }), | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 240s", durationSecs: 240, score: 1 }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const body = await json(res); | |
| const records = body.records as Array<Record<string, unknown>>; | |
| expect(records).toHaveLength(1); | |
| expect(records[0].bestSecs).toBe(90); | |
| }); | |
| it("accumulates solveCount across multiple correct solves for same type+difficulty", async () => { | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 90s", durationSecs: 90, score: 1 }), | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 120s", durationSecs: 120, score: 1 }), | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 150s", durationSecs: 150, score: 1 }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const body = await json(res); | |
| const records = body.records as Array<Record<string, unknown>>; | |
| expect(records[0].solveCount).toBe(3); | |
| }); | |
| it("keeps separate records for different difficulties of the same exercise type", async () => { | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 90s", durationSecs: 90, score: 1 }), | |
| makeActivity({ exerciseType: "sudoku", description: "Completed medium Sudoku in 200s", durationSecs: 200, score: 1 }), | |
| makeActivity({ exerciseType: "sudoku", description: "Completed hard Sudoku in 450s", durationSecs: 450, score: 1 }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const body = await json(res); | |
| const records = body.records as Array<Record<string, unknown>>; | |
| expect(records).toHaveLength(3); | |
| expect(records.map((r) => r.difficulty)).toEqual(["easy", "medium", "hard"]); | |
| expect(records.map((r) => r.bestSecs)).toEqual([90, 200, 450]); | |
| }); | |
| it("keeps separate records for different exercise types", async () => { | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 90s", durationSecs: 90, score: 1 }), | |
| makeActivity({ exerciseType: "sequence", description: "Completed easy number sequence in 25s", durationSecs: 25, score: 1 }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const body = await json(res); | |
| const records = body.records as Array<Record<string, unknown>>; | |
| expect(records).toHaveLength(2); | |
| const types = records.map((r) => r.exerciseType); | |
| expect(types).toContain("sudoku"); | |
| expect(types).toContain("sequence"); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // Difficulty extraction from description | |
| // --------------------------------------------------------------------------- | |
| describe("GET /api/stats/personal-bests β difficulty extraction", () => { | |
| it('extracts "easy" from description', async () => { | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: "mathequation", description: "Solved easy math equation in 20s", durationSecs: 20, score: 1 }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const records = ((await json(res)).records) as Array<Record<string, unknown>>; | |
| expect(records[0].difficulty).toBe("easy"); | |
| }); | |
| it('extracts "medium" from description', async () => { | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: "pattern", description: "Completed medium pattern matrix in 40s", durationSecs: 40, score: 1 }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const records = ((await json(res)).records) as Array<Record<string, unknown>>; | |
| expect(records[0].difficulty).toBe("medium"); | |
| }); | |
| it('extracts "hard" from description', async () => { | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: "constraint", description: "Completed hard constraint grid in 180s", durationSecs: 180, score: 1 }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const records = ((await json(res)).records) as Array<Record<string, unknown>>; | |
| expect(records[0].difficulty).toBe("hard"); | |
| }); | |
| it("is case-insensitive for difficulty matching", async () => { | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: "wordscramble", description: "Completed EASY word scramble in 18s", durationSecs: 18, score: 1 }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const records = ((await json(res)).records) as Array<Record<string, unknown>>; | |
| expect(records[0].difficulty).toBe("easy"); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // exerciseLabel | |
| // --------------------------------------------------------------------------- | |
| describe("GET /api/stats/personal-bests β exerciseLabel", () => { | |
| const labelCases: Array<[string, string]> = [ | |
| ["sudoku", "Sudoku"], | |
| ["sequence", "Number Sequence"], | |
| ["pattern", "Pattern Matrix"], | |
| ["wordscramble", "Word Scramble"], | |
| ["constraint", "Constraint Grid"], | |
| ["mathequation", "Math Equation"], | |
| ]; | |
| for (const [type, expectedLabel] of labelCases) { | |
| it(`returns correct exerciseLabel for ${type}`, async () => { | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: type, description: `Completed easy ${type} in 60s`, durationSecs: 60, score: 1 }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const records = ((await json(res)).records) as Array<Record<string, unknown>>; | |
| expect(records[0].exerciseLabel).toBe(expectedLabel); | |
| }); | |
| } | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // Ordering | |
| // --------------------------------------------------------------------------- | |
| describe("GET /api/stats/personal-bests β ordering", () => { | |
| it("orders exercise types in canonical order (sudoku, sequence, pattern, wordscramble, constraint, mathequation)", async () => { | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: "mathequation", description: "Solved easy math equation in 20s", durationSecs: 20, score: 1 }), | |
| makeActivity({ exerciseType: "pattern", description: "Completed easy pattern matrix in 30s", durationSecs: 30, score: 1 }), | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 90s", durationSecs: 90, score: 1 }), | |
| makeActivity({ exerciseType: "sequence", description: "Completed easy number sequence in 25s", durationSecs: 25, score: 1 }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const records = ((await json(res)).records) as Array<Record<string, unknown>>; | |
| const types = records.map((r) => r.exerciseType); | |
| expect(types).toEqual(["sudoku", "sequence", "pattern", "mathequation"]); | |
| }); | |
| it("orders difficulties easy β medium β hard within each exercise type", async () => { | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: "sudoku", description: "Completed hard Sudoku in 450s", durationSecs: 450, score: 1 }), | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 90s", durationSecs: 90, score: 1 }), | |
| makeActivity({ exerciseType: "sudoku", description: "Completed medium Sudoku in 200s", durationSecs: 200, score: 1 }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const records = ((await json(res)).records) as Array<Record<string, unknown>>; | |
| expect(records.map((r) => r.difficulty)).toEqual(["easy", "medium", "hard"]); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // achievedAt | |
| // --------------------------------------------------------------------------- | |
| describe("GET /api/stats/personal-bests β achievedAt", () => { | |
| it("achievedAt is an ISO string", async () => { | |
| const date = new Date("2026-03-15T14:30:00Z"); | |
| mockActivityFindMany.mockResolvedValue([ | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 90s", durationSecs: 90, score: 1, createdAt: date }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const records = ((await json(res)).records) as Array<Record<string, unknown>>; | |
| expect(records[0].achievedAt).toBe(date.toISOString()); | |
| }); | |
| it("achievedAt corresponds to the fastest (first) solve entry", async () => { | |
| const fastDate = new Date("2026-03-20T10:00:00Z"); | |
| const slowDate = new Date("2026-03-10T10:00:00Z"); | |
| mockActivityFindMany.mockResolvedValue([ | |
| // Ordered by durationSecs asc β fastest first | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 90s", durationSecs: 90, score: 1, createdAt: fastDate }), | |
| makeActivity({ exerciseType: "sudoku", description: "Completed easy Sudoku in 150s", durationSecs: 150, score: 1, createdAt: slowDate }), | |
| ]); | |
| const res = await GET(makeRequest()); | |
| const records = ((await json(res)).records) as Array<Record<string, unknown>>; | |
| expect(records[0].achievedAt).toBe(fastDate.toISOString()); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // DB query validation | |
| // --------------------------------------------------------------------------- | |
| describe("GET /api/stats/personal-bests β DB query", () => { | |
| it("queries only logic puzzle exercise types", async () => { | |
| mockActivityFindMany.mockResolvedValue([]); | |
| await GET(makeRequest()); | |
| expect(mockActivityFindMany).toHaveBeenCalledWith( | |
| expect.objectContaining({ | |
| where: expect.objectContaining({ | |
| exerciseType: expect.objectContaining({ | |
| in: expect.arrayContaining(["sudoku", "sequence", "pattern", "wordscramble", "constraint", "mathequation"]), | |
| }), | |
| }), | |
| }) | |
| ); | |
| }); | |
| it("filters to userId=1", async () => { | |
| mockActivityFindMany.mockResolvedValue([]); | |
| await GET(makeRequest()); | |
| expect(mockActivityFindMany).toHaveBeenCalledWith( | |
| expect.objectContaining({ | |
| where: expect.objectContaining({ userId: 1 }), | |
| }) | |
| ); | |
| }); | |
| it("filters durationSecs not null", async () => { | |
| mockActivityFindMany.mockResolvedValue([]); | |
| await GET(makeRequest()); | |
| const callArg = mockActivityFindMany.mock.calls[0][0] as Record<string, unknown>; | |
| const where = callArg.where as Record<string, unknown>; | |
| expect(where.durationSecs).toEqual({ not: null }); | |
| }); | |
| it("filters score > 0", async () => { | |
| mockActivityFindMany.mockResolvedValue([]); | |
| await GET(makeRequest()); | |
| const callArg = mockActivityFindMany.mock.calls[0][0] as Record<string, unknown>; | |
| const where = callArg.where as Record<string, unknown>; | |
| expect(where.score).toEqual({ gt: 0 }); | |
| }); | |
| it("orders by durationSecs ascending", async () => { | |
| mockActivityFindMany.mockResolvedValue([]); | |
| await GET(makeRequest()); | |
| expect(mockActivityFindMany).toHaveBeenCalledWith( | |
| expect.objectContaining({ | |
| orderBy: { durationSecs: "asc" }, | |
| }) | |
| ); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // Error handling | |
| // --------------------------------------------------------------------------- | |
| describe("GET /api/stats/personal-bests β error handling", () => { | |
| it("propagates DB errors as 500", async () => { | |
| mockActivityFindMany.mockRejectedValue(new Error("DB connection lost")); | |
| const res = await GET(makeRequest()); | |
| expect(res.status).toBe(500); | |
| }); | |
| }); | |