File size: 8,312 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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);
  });
});