import { describe, it, expect, vi } from "vitest"; import { parseMultiplierPercent, getMultiplierPercent, type MultiplierTx } from "@/lib/multiplier"; // ── parseMultiplierPercent ────────────────────────────────────────────── describe("parseMultiplierPercent", () => { it("parses +10% from description", () => { expect(parseMultiplierPercent("+10% Chess XP")).toBe(10); }); it("parses +25% from description", () => { expect(parseMultiplierPercent("+25% Reading XP")).toBe(25); }); it("parses +100% from description", () => { expect(parseMultiplierPercent("+100% Logic XP")).toBe(100); }); it("returns 0 for non-multiplier description", () => { expect(parseMultiplierPercent("Unlock medium difficulty")).toBe(0); }); it("returns 0 for empty string", () => { expect(parseMultiplierPercent("")).toBe(0); }); it("returns 0 when + is not at the start", () => { expect(parseMultiplierPercent("Earn +10% bonus")).toBe(0); }); it("returns 0 for description without %", () => { expect(parseMultiplierPercent("+10 Chess XP")).toBe(0); }); it("handles single-digit percentages", () => { expect(parseMultiplierPercent("+5% Quest XP")).toBe(5); }); }); // ── getMultiplierPercent ──────────────────────────────────────────────── describe("getMultiplierPercent", () => { function mockTx(unlocks: Array<{ node: { description: string } }>): MultiplierTx { return { unlock: { findMany: vi.fn().mockResolvedValue(unlocks), }, skillTreeNode: { findMany: vi.fn().mockResolvedValue([]), }, }; } it("returns 0 when no multiplier nodes are unlocked", async () => { const tx = mockTx([]); const result = await getMultiplierPercent(tx, 1, "chess"); expect(result).toBe(0); }); it("returns single multiplier percent", async () => { const tx = mockTx([ { node: { description: "+10% Chess XP" } }, ]); const result = await getMultiplierPercent(tx, 1, "chess"); expect(result).toBe(10); }); it("sums multiple multiplier nodes", async () => { const tx = mockTx([ { node: { description: "+10% Chess XP" } }, { node: { description: "+25% Chess XP" } }, ]); const result = await getMultiplierPercent(tx, 1, "chess"); expect(result).toBe(35); }); it("ignores unparseable descriptions", async () => { const tx = mockTx([ { node: { description: "+10% Chess XP" } }, { node: { description: "Some other node" } }, ]); const result = await getMultiplierPercent(tx, 1, "chess"); expect(result).toBe(10); }); it("queries with correct userId and category filter", async () => { const tx = mockTx([]); await getMultiplierPercent(tx, 42, "reading"); expect(tx.unlock.findMany).toHaveBeenCalledWith({ where: { userId: 42, node: { category: "reading", nodeType: "multiplier", }, }, include: { node: { select: { description: true }, }, }, }); }); it("handles three stacked multipliers", async () => { const tx = mockTx([ { node: { description: "+5% Logic XP" } }, { node: { description: "+10% Logic XP" } }, { node: { description: "+25% Logic XP" } }, ]); const result = await getMultiplierPercent(tx, 1, "logic"); expect(result).toBe(40); }); });