File size: 8,898 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
import { describe, it, expect } from "vitest";
import {
  selectDifficulty,
  getRecommendedSettings,
} from "../difficulty";
import type { PerformanceSummary } from "../performance";

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

function makeSummary(
  overrides: Partial<PerformanceSummary> = {},
): PerformanceSummary {
  return {
    categories: [],
    overallScore: 0,
    streakDays: 1,
    longestStreak: 1,
    totalExercises: 0,
    activeDays: 0,
    lastActivityAt: null,
    ...overrides,
  };
}

function makeCategory(
  category: Parameters<typeof getRecommendedSettings>[0]["categories"][number]["category"],
  opts: {
    attempts?: number;
    successRate?: number;
    level?: number;
    trend?: "new" | "improving" | "declining" | "stable";
    avgScore?: number;
  } = {},
): PerformanceSummary["categories"][number] {
  return {
    category,
    label: category,
    attempts: opts.attempts ?? 10,
    successRate: opts.successRate ?? 50,
    avgScore: opts.avgScore ?? 0.5,
    avgDurationSecs: null,
    totalXp: 100,
    level: opts.level ?? 1,
    trend: opts.trend ?? "stable",
  };
}

// ---------------------------------------------------------------------------
// selectDifficulty β€” success rate bands
// ---------------------------------------------------------------------------

describe("selectDifficulty", () => {
  it("returns 'easy' for 0% success rate", () => {
    expect(selectDifficulty(0)).toBe("easy");
  });

  it("returns 'easy' for 25% success rate", () => {
    expect(selectDifficulty(25)).toBe("easy");
  });

  it("returns 'easy' just below the easy/medium boundary (39.9%)", () => {
    expect(selectDifficulty(39.9)).toBe("easy");
  });

  it("returns 'medium' at the easy/medium boundary (40%)", () => {
    expect(selectDifficulty(40)).toBe("medium");
  });

  it("returns 'medium' for 50% success rate", () => {
    expect(selectDifficulty(50)).toBe("medium");
  });

  it("returns 'medium' just below the medium/hard boundary (69.9%)", () => {
    expect(selectDifficulty(69.9)).toBe("medium");
  });

  it("returns 'hard' at the medium/hard boundary (70%)", () => {
    expect(selectDifficulty(70)).toBe("hard");
  });

  it("returns 'hard' for 75% success rate", () => {
    expect(selectDifficulty(75)).toBe("hard");
  });

  it("returns 'hard' for 100% success rate", () => {
    expect(selectDifficulty(100)).toBe("hard");
  });
});

// ---------------------------------------------------------------------------
// getRecommendedSettings β€” streak effects
// ---------------------------------------------------------------------------

describe("getRecommendedSettings β€” streak effects", () => {
  it("boosts difficulty on a hot streak (β‰₯5 days): medium β†’ hard", () => {
    const summary = makeSummary({
      streakDays: 5,
      totalExercises: 10,
      categories: [makeCategory("chess", { successRate: 50, trend: "stable" })],
    });
    const { recommendations } = getRecommendedSettings(summary);
    // 50% β†’ medium, then hot streak bumps to hard
    expect(recommendations.chess.difficulty).toBe("hard");
    expect(recommendations.chess.adjustmentReason).toMatch(/hot streak/);
  });

  it("boosts difficulty on a hot streak (β‰₯5 days): easy β†’ medium", () => {
    const summary = makeSummary({
      streakDays: 7,
      totalExercises: 10,
      categories: [makeCategory("logic", { successRate: 20, trend: "stable" })],
    });
    const { recommendations } = getRecommendedSettings(summary);
    // 20% β†’ easy, then hot streak bumps to medium
    expect(recommendations.logic.difficulty).toBe("medium");
  });

  it("does not boost above hard on hot streak", () => {
    const summary = makeSummary({
      streakDays: 10,
      totalExercises: 10,
      categories: [makeCategory("reading", { successRate: 90, trend: "stable" })],
    });
    const { recommendations } = getRecommendedSettings(summary);
    expect(recommendations.reading.difficulty).toBe("hard");
  });

  it("lowers difficulty on a broken streak (0 days) for existing user", () => {
    const summary = makeSummary({
      streakDays: 0,
      totalExercises: 10,
      categories: [makeCategory("feynman", { successRate: 75, trend: "stable" })],
    });
    const { recommendations } = getRecommendedSettings(summary);
    // 75% β†’ hard, then cold streak bumps down to medium
    expect(recommendations.feynman.difficulty).toBe("medium");
    expect(recommendations.feynman.adjustmentReason).toMatch(/break/);
  });

  it("does not lower below easy on cold streak", () => {
    const summary = makeSummary({
      streakDays: 0,
      totalExercises: 10,
      categories: [makeCategory("chess", { successRate: 10, trend: "stable" })],
    });
    const { recommendations } = getRecommendedSettings(summary);
    expect(recommendations.chess.difficulty).toBe("easy");
  });

  it("does not lower difficulty for brand-new user with 0 exercises on streak 0", () => {
    // totalExercises === 0 means brand new β€” cold streak rule should not trigger
    const summary = makeSummary({
      streakDays: 0,
      totalExercises: 0,
      categories: [makeCategory("chess", { attempts: 0 })],
    });
    const { recommendations } = getRecommendedSettings(summary);
    expect(recommendations.chess.hasHistory).toBe(false);
    expect(recommendations.chess.difficulty).toBe("medium");
  });
});

// ---------------------------------------------------------------------------
// getRecommendedSettings β€” edge cases
// ---------------------------------------------------------------------------

describe("getRecommendedSettings β€” edge cases", () => {
  it("returns hasHistory=false for a category with 0 attempts", () => {
    const summary = makeSummary({
      totalExercises: 5,
      categories: [makeCategory("chess", { attempts: 0 })],
    });
    const { recommendations } = getRecommendedSettings(summary);
    expect(recommendations.chess.hasHistory).toBe(false);
    expect(recommendations.chess.difficulty).toBe("medium");
    expect(recommendations.chess.adjustmentReason).toMatch(/No activity yet/);
  });

  it("returns hasHistory=true for a category with attempts > 0", () => {
    const summary = makeSummary({
      totalExercises: 10,
      categories: [makeCategory("chess", { attempts: 5, successRate: 60 })],
    });
    const { recommendations } = getRecommendedSettings(summary);
    expect(recommendations.chess.hasHistory).toBe(true);
  });

  it("fills in all 6 CATEGORIES even when summary.categories is empty", () => {
    const summary = makeSummary({ categories: [] });
    const { recommendations } = getRecommendedSettings(summary);
    const cats = Object.keys(recommendations);
    expect(cats).toContain("chess");
    expect(cats).toContain("reading");
    expect(cats).toContain("feynman");
    expect(cats).toContain("logic");
    expect(cats).toContain("critical-thinking");
    expect(cats).toContain("quests");
  });

  it("includes updatedAt as a valid ISO date string", () => {
    const summary = makeSummary({ categories: [] });
    const { updatedAt } = getRecommendedSettings(summary);
    expect(new Date(updatedAt).toISOString()).toBe(updatedAt);
  });

  it("includes nextSkill in every recommendation", () => {
    const summary = makeSummary({
      totalExercises: 5,
      categories: [makeCategory("logic", { attempts: 5, level: 3 })],
    });
    const { recommendations } = getRecommendedSettings(summary);
    expect(typeof recommendations.logic.nextSkill).toBe("string");
    expect(recommendations.logic.nextSkill.length).toBeGreaterThan(0);
  });
});

// ---------------------------------------------------------------------------
// getRecommendedSettings β€” trend adjustments
// ---------------------------------------------------------------------------

describe("getRecommendedSettings β€” trend adjustments", () => {
  it("bumps difficulty up on improving trend (medium β†’ hard)", () => {
    const summary = makeSummary({
      streakDays: 1,
      totalExercises: 10,
      categories: [makeCategory("chess", { successRate: 50, trend: "improving" })],
    });
    const { recommendations } = getRecommendedSettings(summary);
    expect(recommendations.chess.difficulty).toBe("hard");
    expect(recommendations.chess.adjustmentReason).toMatch(/upward trend/);
  });

  it("bumps difficulty down on declining trend (medium β†’ easy)", () => {
    const summary = makeSummary({
      streakDays: 1,
      totalExercises: 10,
      categories: [makeCategory("reading", { successRate: 50, trend: "declining" })],
    });
    const { recommendations } = getRecommendedSettings(summary);
    expect(recommendations.reading.difficulty).toBe("easy");
    expect(recommendations.reading.adjustmentReason).toMatch(/declining trend/);
  });
});