File size: 7,907 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 | /**
* 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<string, unknown> | 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);
});
});
|