File size: 3,589 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 | 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);
});
});
|