RotNot / src /config /__tests__ /xp.test.ts
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
8.31 kB
import { describe, it, expect } from "vitest";
import {
xpForLevel,
totalXpForLevel,
EXERCISE_XP,
CRITICAL_HIT_CHANCE,
CRITICAL_HIT_MULTIPLIER,
CATEGORIES,
CATEGORY_LABELS,
CATEGORY_COLORS,
type Category,
} from "@/config/xp";
// ─── xpForLevel ──────────────────────────────────────────────────────────────
describe("xpForLevel", () => {
it("returns 100 for level 2", () => {
expect(xpForLevel(2)).toBe(100);
});
it("scales with level (monotonically non-decreasing)", () => {
for (let lvl = 2; lvl < 50; lvl++) {
expect(xpForLevel(lvl + 1)).toBeGreaterThanOrEqual(xpForLevel(lvl));
}
});
it("returns approximately 1500 at level 50", () => {
const xp = xpForLevel(50);
expect(xp).toBeGreaterThanOrEqual(1200);
expect(xp).toBeLessThanOrEqual(1800);
});
it("returns a positive integer for level 1", () => {
const xp = xpForLevel(1);
expect(xp).toBeGreaterThanOrEqual(0);
expect(Number.isInteger(xp)).toBe(true);
});
it("always returns a non-negative integer", () => {
for (const lvl of [1, 2, 5, 10, 25, 50, 100]) {
const xp = xpForLevel(lvl);
expect(xp).toBeGreaterThanOrEqual(0);
expect(Number.isInteger(xp)).toBe(true);
}
});
it("grows sub-linearly (xp at level 100 is less than 50x xp at level 2)", () => {
// The formula is x^0.8 β€” sub-linear growth. Level 100 XP is ~22x level 2 XP,
// not 50x as linear growth would produce. This keeps the game accessible at
// higher levels (you don't need 50x as much XP per level as at level 2).
expect(xpForLevel(100)).toBeLessThan(50 * xpForLevel(2));
// But it still grows β€” level 100 > level 2
expect(xpForLevel(100)).toBeGreaterThan(xpForLevel(2));
});
it("returns a reasonable value at level 10 (~350 XP)", () => {
const xp = xpForLevel(10);
expect(xp).toBeGreaterThanOrEqual(200);
expect(xp).toBeLessThanOrEqual(500);
});
});
// ─── totalXpForLevel ─────────────────────────────────────────────────────────
describe("totalXpForLevel", () => {
it("returns 0 for level 1 (already at starting level)", () => {
expect(totalXpForLevel(1)).toBe(0);
});
it("returns xpForLevel(2) for level 2 (one level's worth)", () => {
expect(totalXpForLevel(2)).toBe(xpForLevel(2));
});
it("returns sum of xpForLevel(2) + xpForLevel(3) for level 3", () => {
const expected = xpForLevel(2) + xpForLevel(3);
expect(totalXpForLevel(3)).toBe(expected);
});
it("is strictly increasing with level", () => {
for (let lvl = 2; lvl < 20; lvl++) {
expect(totalXpForLevel(lvl + 1)).toBeGreaterThan(totalXpForLevel(lvl));
}
});
it("equals the sum of all xpForLevel values from 2 to N", () => {
for (const targetLevel of [5, 10, 20]) {
let manual = 0;
for (let i = 2; i <= targetLevel; i++) {
manual += xpForLevel(i);
}
expect(totalXpForLevel(targetLevel)).toBe(manual);
}
});
it("returns a positive integer for level 10", () => {
const xp = totalXpForLevel(10);
expect(xp).toBeGreaterThan(0);
expect(Number.isInteger(xp)).toBe(true);
});
it("totalXpForLevel(N+1) - totalXpForLevel(N) === xpForLevel(N+1)", () => {
for (const lvl of [3, 7, 15, 30]) {
expect(totalXpForLevel(lvl) - totalXpForLevel(lvl - 1)).toBe(
xpForLevel(lvl),
);
}
});
});
// ─── EXERCISE_XP ─────────────────────────────────────────────────────────────
describe("EXERCISE_XP", () => {
it("has entries for all categories", () => {
for (const cat of CATEGORIES) {
expect(EXERCISE_XP[cat]).toBeDefined();
}
});
it("has positive baseXp for all categories", () => {
for (const cat of CATEGORIES) {
expect(EXERCISE_XP[cat].baseXp).toBeGreaterThan(0);
}
});
it("has non-negative maxQualityBonus for all categories", () => {
for (const cat of CATEGORIES) {
expect(EXERCISE_XP[cat].maxQualityBonus).toBeGreaterThanOrEqual(0);
}
});
it("critical-thinking has the highest base XP (50)", () => {
const maxBase = Math.max(
...CATEGORIES.map((c) => EXERCISE_XP[c].baseXp),
);
expect(EXERCISE_XP["critical-thinking"].baseXp).toBe(maxBase);
});
it("logic has the lowest base XP (15)", () => {
const minBase = Math.min(
...CATEGORIES.map((c) => EXERCISE_XP[c].baseXp),
);
expect(EXERCISE_XP["logic"].baseXp).toBe(minBase);
});
it("max total XP per exercise (base + max bonus) is always positive", () => {
for (const cat of CATEGORIES) {
const max =
EXERCISE_XP[cat].baseXp + EXERCISE_XP[cat].maxQualityBonus;
expect(max).toBeGreaterThan(0);
}
});
});
// ─── critical hit constants ──────────────────────────────────────────────────
describe("critical hit", () => {
it("has 10% chance", () => {
expect(CRITICAL_HIT_CHANCE).toBe(0.1);
});
it("has 2x multiplier", () => {
expect(CRITICAL_HIT_MULTIPLIER).toBe(2);
});
it("chance is between 0 and 1 exclusive", () => {
expect(CRITICAL_HIT_CHANCE).toBeGreaterThan(0);
expect(CRITICAL_HIT_CHANCE).toBeLessThan(1);
});
it("multiplier is greater than 1", () => {
expect(CRITICAL_HIT_MULTIPLIER).toBeGreaterThan(1);
});
});
// ─── CATEGORY_LABELS ────────────────────────────────────────────────────────
describe("CATEGORY_LABELS", () => {
it("has an entry for every category", () => {
for (const cat of CATEGORIES) {
expect(CATEGORY_LABELS[cat]).toBeDefined();
}
});
it("all labels are non-empty strings", () => {
for (const cat of CATEGORIES) {
expect(typeof CATEGORY_LABELS[cat]).toBe("string");
expect(CATEGORY_LABELS[cat].length).toBeGreaterThan(0);
}
});
it("all labels are unique", () => {
const labels = CATEGORIES.map((c) => CATEGORY_LABELS[c]);
const unique = new Set(labels);
expect(unique.size).toBe(CATEGORIES.length);
});
it("chess label is 'Chess'", () => {
expect(CATEGORY_LABELS["chess"]).toBe("Chess");
});
it("critical-thinking label is 'Critical Thinking'", () => {
expect(CATEGORY_LABELS["critical-thinking"]).toBe("Critical Thinking");
});
});
// ─── CATEGORY_COLORS ────────────────────────────────────────────────────────
describe("CATEGORY_COLORS", () => {
it("has an entry for every category", () => {
for (const cat of CATEGORIES) {
expect(CATEGORY_COLORS[cat]).toBeDefined();
}
});
it("all colors are valid hex strings (#rrggbb format)", () => {
const hexPattern = /^#[0-9a-f]{6}$/i;
for (const cat of CATEGORIES) {
expect(CATEGORY_COLORS[cat]).toMatch(hexPattern);
}
});
it("all colors are unique", () => {
const colors = CATEGORIES.map((c) => CATEGORY_COLORS[c]);
const unique = new Set(colors);
expect(unique.size).toBe(CATEGORIES.length);
});
});
// ─── CATEGORIES array ───────────────────────────────────────────────────────
describe("CATEGORIES", () => {
it("contains exactly 7 categories", () => {
expect(CATEGORIES.length).toBe(7);
});
it("contains all expected exercise categories", () => {
const expected: Category[] = [
"chess",
"reading",
"feynman",
"logic",
"critical-thinking",
"quests",
"coding",
];
for (const cat of expected) {
expect(CATEGORIES).toContain(cat);
}
});
it("contains no duplicate entries", () => {
const unique = new Set(CATEGORIES);
expect(unique.size).toBe(CATEGORIES.length);
});
});