46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import {
|
|
buildLevelProgress,
|
|
totalXpForLevel,
|
|
xpRequiredForLevel,
|
|
} from "@/lib/leaderboard/level-progress";
|
|
|
|
describe("leaderboard level formula", () => {
|
|
test("matches the bot thresholds", () => {
|
|
expect(xpRequiredForLevel(0)).toBe(BigInt(500));
|
|
expect(xpRequiredForLevel(1)).toBe(BigInt(625));
|
|
expect(totalXpForLevel(1)).toBe(BigInt(500));
|
|
expect(totalXpForLevel(2)).toBe(BigInt(1_125));
|
|
});
|
|
|
|
test("calculates progress at level boundaries", () => {
|
|
expect(buildLevelProgress(0, "499")).toMatchObject({
|
|
xpIntoLevel: "499",
|
|
xpRequired: "500",
|
|
xpToNextLevel: "1",
|
|
percentage: 99.8,
|
|
});
|
|
expect(buildLevelProgress(1, "500")).toMatchObject({
|
|
xpIntoLevel: "0",
|
|
xpRequired: "625",
|
|
xpToNextLevel: "625",
|
|
percentage: 0,
|
|
});
|
|
});
|
|
|
|
test("clamps inconsistent and very large totals without losing precision", () => {
|
|
expect(buildLevelProgress(12, "1").percentage).toBe(0);
|
|
|
|
const huge = buildLevelProgress(12, "9999999999999999999999999999999999999999");
|
|
expect(huge.percentage).toBe(100);
|
|
expect(huge.totalXp).toBe("9999999999999999999999999999999999999999");
|
|
});
|
|
|
|
test("marks level 200 as max level and caps the formula", () => {
|
|
const maxLevel = buildLevelProgress(200, totalXpForLevel(200).toString());
|
|
|
|
expect(maxLevel.isMaxLevel).toBeTrue();
|
|
expect(xpRequiredForLevel(200)).toBe(xpRequiredForLevel(199));
|
|
});
|
|
});
|