/** * Tests for skill tree auto-unlock logic. * * These test the pure logic of processSkillUnlocks by mocking the Prisma * transaction client. We verify correct unlock decisions without a real DB. */ import { describe, it, expect, vi, beforeEach } from "vitest"; import { processSkillUnlocks } from "@/lib/skill-unlock"; // ---- Helpers to build mock Prisma transaction ---- type MockNode = { id: number; category: string; name: string; description: string; levelRequired: number; nodeType: string; parentNodeId: number | null; }; type MockUnlock = { nodeId: number }; function buildMockTx( nodes: MockNode[], existingUnlocks: MockUnlock[], ) { const createdUnlocks: Array<{ userId: number; nodeId: number }> = []; let userUpdateData: Record | null = null; const tx = { skillTreeNode: { findMany: vi.fn().mockResolvedValue(nodes), }, unlock: { findMany: vi.fn().mockResolvedValue(existingUnlocks), create: vi.fn().mockImplementation(({ data }) => { createdUnlocks.push(data); return Promise.resolve(data); }), }, user: { update: vi.fn().mockImplementation(({ data }) => { userUpdateData = data; return Promise.resolve({}); }), }, _createdUnlocks: createdUnlocks, _getUserUpdateData: () => userUpdateData, }; return tx; } // ---- Sample skill tree (simplified chess tree) ---- const CHESS_TREE: MockNode[] = [ { id: 1, category: "chess", name: "Chess Initiate", description: "", levelRequired: 1, nodeType: "difficulty", parentNodeId: null }, { id: 2, category: "chess", name: "Puzzle Warrior", description: "", levelRequired: 5, nodeType: "difficulty", parentNodeId: 1 }, { id: 3, category: "chess", name: "Endgame Student", description: "", levelRequired: 5, nodeType: "exercise-type", parentNodeId: 1 }, { id: 4, category: "chess", name: "Tactical Eye", description: "", levelRequired: 15, nodeType: "multiplier", parentNodeId: 2 }, { id: 5, category: "chess", name: "Streak Shield I", description: "", levelRequired: 10, nodeType: "streak-shield", parentNodeId: 3 }, { id: 6, category: "chess", name: "Puzzle Master", description: "", levelRequired: 20, nodeType: "difficulty", parentNodeId: 2 }, { id: 7, category: "chess", name: "Grandmaster Path", description: "", levelRequired: 30, nodeType: "multiplier", parentNodeId: 6 }, { id: 8, category: "chess", name: "Grandthinker", description: "", levelRequired: 50, nodeType: "cosmetic", parentNodeId: 7 }, ]; describe("processSkillUnlocks", () => { beforeEach(() => { vi.clearAllMocks(); }); it("unlocks root node at level 1 with no prior unlocks", async () => { const tx = buildMockTx(CHESS_TREE, []); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = await processSkillUnlocks(tx as any, 1, "chess", 1); expect(result.unlocked).toEqual(["Chess Initiate"]); expect(result.streakFreezesGranted).toBe(0); expect(tx._createdUnlocks).toHaveLength(1); expect(tx._createdUnlocks[0]).toEqual({ userId: 1, nodeId: 1 }); }); it("unlocks root + children when level is high enough", async () => { const tx = buildMockTx(CHESS_TREE, []); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = await processSkillUnlocks(tx as any, 1, "chess", 5); expect(result.unlocked).toContain("Chess Initiate"); expect(result.unlocked).toContain("Puzzle Warrior"); expect(result.unlocked).toContain("Endgame Student"); expect(result.unlocked).toHaveLength(3); expect(result.streakFreezesGranted).toBe(0); }); it("skips already-unlocked nodes", async () => { // Root already unlocked const tx = buildMockTx(CHESS_TREE, [{ nodeId: 1 }]); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = await processSkillUnlocks(tx as any, 1, "chess", 5); expect(result.unlocked).not.toContain("Chess Initiate"); expect(result.unlocked).toContain("Puzzle Warrior"); expect(result.unlocked).toContain("Endgame Student"); expect(result.unlocked).toHaveLength(2); }); it("does not unlock child if parent level not met", async () => { // Level 3: root is unlockable (L1) but children need L5 const tx = buildMockTx(CHESS_TREE, []); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = await processSkillUnlocks(tx as any, 1, "chess", 3); expect(result.unlocked).toEqual(["Chess Initiate"]); }); it("does not unlock child if parent not unlocked (even if level high enough)", async () => { // Nodes without root node available (simulating a broken chain) const orphanTree: MockNode[] = [ { id: 10, category: "chess", name: "Orphan", description: "", levelRequired: 1, nodeType: "difficulty", parentNodeId: 999 }, ]; const tx = buildMockTx(orphanTree, []); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = await processSkillUnlocks(tx as any, 1, "chess", 50); expect(result.unlocked).toEqual([]); }); it("grants streak freezes for streak-shield nodes", async () => { // Level 10: root + L5 children already unlocked, Streak Shield I (L10) now eligible const tx = buildMockTx(CHESS_TREE, [ { nodeId: 1 }, // Chess Initiate { nodeId: 2 }, // Puzzle Warrior { nodeId: 3 }, // Endgame Student ]); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = await processSkillUnlocks(tx as any, 1, "chess", 10); expect(result.unlocked).toContain("Streak Shield I"); expect(result.streakFreezesGranted).toBe(1); expect(tx.user.update).toHaveBeenCalledWith({ where: { id: 1 }, data: { streakFreezes: { increment: 1 } }, }); }); it("handles deep chain unlock in a single call", async () => { // Level 50: everything should unlock from scratch const tx = buildMockTx(CHESS_TREE, []); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = await processSkillUnlocks(tx as any, 1, "chess", 50); expect(result.unlocked).toHaveLength(8); // All 8 nodes expect(result.unlocked).toContain("Grandthinker"); expect(result.streakFreezesGranted).toBe(1); }); it("returns empty when no new nodes to unlock", async () => { // Everything already unlocked const allUnlocked = CHESS_TREE.map((n) => ({ nodeId: n.id })); const tx = buildMockTx(CHESS_TREE, allUnlocked); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = await processSkillUnlocks(tx as any, 1, "chess", 50); expect(result.unlocked).toEqual([]); expect(result.streakFreezesGranted).toBe(0); expect(tx.unlock.create).not.toHaveBeenCalled(); }); it("returns empty when level is too low for any node", async () => { // Level 0 (theoretically impossible but tests edge case) const tx = buildMockTx(CHESS_TREE, []); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = await processSkillUnlocks(tx as any, 1, "chess", 0); expect(result.unlocked).toEqual([]); }); it("unlocks incrementally — only newly eligible nodes", async () => { // Was level 5 (root + 2 children unlocked), now level 15 const tx = buildMockTx(CHESS_TREE, [ { nodeId: 1 }, // Chess Initiate { nodeId: 2 }, // Puzzle Warrior { nodeId: 3 }, // Endgame Student ]); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = await processSkillUnlocks(tx as any, 1, "chess", 15); // Should unlock: Tactical Eye (L15, parent=2), Streak Shield I (L10, parent=3) expect(result.unlocked).toContain("Tactical Eye"); expect(result.unlocked).toContain("Streak Shield I"); expect(result.unlocked).toHaveLength(2); expect(result.streakFreezesGranted).toBe(1); }); });