File size: 6,332 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
import { describe, it, expect } from "vitest";
import { checkLevelUp, shouldStreakBreak } from "@/lib/progression";
import { xpForLevel } from "@/config/xp";

// ─── checkLevelUp ────────────────────────────────────────────────────────────

describe("checkLevelUp β€” no level-up", () => {
  it("returns no level up when xp is insufficient", () => {
    const result = checkLevelUp({ currentXp: 50, xpToNextLevel: 100, level: 1 });
    expect(result.levelsGained).toBe(0);
    expect(result.newLevel).toBe(1);
    expect(result.remainingXp).toBe(50);
  });

  it("returns no level up when xp is exactly 0", () => {
    const result = checkLevelUp({ currentXp: 0, xpToNextLevel: 100, level: 5 });
    expect(result.levelsGained).toBe(0);
    expect(result.newLevel).toBe(5);
    expect(result.remainingXp).toBe(0);
  });

  it("returns no level up when xp is one short of threshold", () => {
    const result = checkLevelUp({ currentXp: 99, xpToNextLevel: 100, level: 3 });
    expect(result.levelsGained).toBe(0);
    expect(result.newLevel).toBe(3);
    expect(result.remainingXp).toBe(99);
  });
});

describe("checkLevelUp β€” single level-up", () => {
  it("levels up when xp exactly meets threshold", () => {
    const result = checkLevelUp({ currentXp: 100, xpToNextLevel: 100, level: 1 });
    expect(result.levelsGained).toBe(1);
    expect(result.newLevel).toBe(2);
    expect(result.remainingXp).toBe(0);
  });

  it("levels up when xp exceeds threshold; excess XP carries over", () => {
    const result = checkLevelUp({ currentXp: 150, xpToNextLevel: 100, level: 1 });
    expect(result.levelsGained).toBe(1);
    expect(result.newLevel).toBe(2);
    expect(result.remainingXp).toBeLessThan(result.newXpToNextLevel);
    expect(result.remainingXp).toBeGreaterThan(0);
  });

  it("remaining XP is correct after one level-up", () => {
    // 100 xp needed, given 150 β†’ 50 left over
    const result = checkLevelUp({ currentXp: 150, xpToNextLevel: 100, level: 1 });
    // After levelling to 2, next threshold is xpForLevel(3)
    expect(result.remainingXp).toBe(150 - 100);
    expect(result.newXpToNextLevel).toBe(xpForLevel(3));
  });

  it("newXpToNextLevel is set to the threshold for the new level", () => {
    const result = checkLevelUp({ currentXp: 100, xpToNextLevel: 100, level: 5 });
    expect(result.newLevel).toBe(6);
    expect(result.newXpToNextLevel).toBe(xpForLevel(7));
  });
});

describe("checkLevelUp β€” multi-level jumps", () => {
  it("levels up multiple times when XP is very high", () => {
    const result = checkLevelUp({ currentXp: 500, xpToNextLevel: 100, level: 1 });
    expect(result.levelsGained).toBeGreaterThan(1);
    expect(result.newLevel).toBeGreaterThan(2);
  });

  it("remaining XP is always less than new xpToNextLevel after multi-level", () => {
    const result = checkLevelUp({ currentXp: 1000, xpToNextLevel: 100, level: 1 });
    expect(result.remainingXp).toBeGreaterThanOrEqual(0);
    expect(result.remainingXp).toBeLessThan(result.newXpToNextLevel);
  });

  it("levelsGained + input level equals newLevel", () => {
    const result = checkLevelUp({ currentXp: 800, xpToNextLevel: 100, level: 10 });
    expect(result.newLevel).toBe(10 + result.levelsGained);
  });

  it("handles exact multi-level threshold with zero remainder", () => {
    // Give exactly enough XP to advance precisely one level from level 1
    const xpNeeded = xpForLevel(2); // should be 100
    const result = checkLevelUp({
      currentXp: xpNeeded,
      xpToNextLevel: xpNeeded,
      level: 1,
    });
    expect(result.levelsGained).toBe(1);
    expect(result.remainingXp).toBe(0);
    expect(result.newLevel).toBe(2);
  });
});

describe("checkLevelUp β€” output invariants", () => {
  it("remainingXp is always non-negative", () => {
    const cases = [
      { currentXp: 0, xpToNextLevel: 100, level: 1 },
      { currentXp: 50, xpToNextLevel: 100, level: 5 },
      { currentXp: 500, xpToNextLevel: 100, level: 3 },
    ];
    for (const input of cases) {
      const result = checkLevelUp(input);
      expect(result.remainingXp).toBeGreaterThanOrEqual(0);
    }
  });

  it("newXpToNextLevel is always positive", () => {
    const cases = [
      { currentXp: 0, xpToNextLevel: 100, level: 1 },
      { currentXp: 999, xpToNextLevel: 100, level: 2 },
    ];
    for (const input of cases) {
      const result = checkLevelUp(input);
      expect(result.newXpToNextLevel).toBeGreaterThan(0);
    }
  });

  it("newLevel is always >= input level", () => {
    const result = checkLevelUp({ currentXp: 0, xpToNextLevel: 100, level: 7 });
    expect(result.newLevel).toBeGreaterThanOrEqual(7);
  });

  it("levelsGained is always non-negative", () => {
    const result = checkLevelUp({ currentXp: 0, xpToNextLevel: 200, level: 3 });
    expect(result.levelsGained).toBeGreaterThanOrEqual(0);
  });
});

// ─── shouldStreakBreak ───────────────────────────────────────────────────────

describe("shouldStreakBreak", () => {
  it("returns false when goal was met today (no freeze needed)", () => {
    expect(shouldStreakBreak({ goalMet: true, hasStreakFreeze: false })).toBe(false);
  });

  it("returns false when goal was met and freeze is available (freeze unused)", () => {
    expect(shouldStreakBreak({ goalMet: true, hasStreakFreeze: true })).toBe(false);
  });

  it("returns false when goal not met but freeze is available (freeze protects streak)", () => {
    expect(shouldStreakBreak({ goalMet: false, hasStreakFreeze: true })).toBe(false);
  });

  it("returns true when goal not met and no freeze available", () => {
    expect(shouldStreakBreak({ goalMet: false, hasStreakFreeze: false })).toBe(true);
  });

  it("goal=true always prevents break regardless of freeze state", () => {
    expect(shouldStreakBreak({ goalMet: true, hasStreakFreeze: false })).toBe(false);
    expect(shouldStreakBreak({ goalMet: true, hasStreakFreeze: true })).toBe(false);
  });

  it("with no freeze and no goal, streak always breaks", () => {
    expect(shouldStreakBreak({ goalMet: false, hasStreakFreeze: false })).toBe(true);
  });
});