Download src/lib/__tests__/difficulty.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 8.9 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/__tests__/difficulty.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/__tests__/difficulty.test.ts
-
curl -L -o difficulty.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/__tests__/difficulty.test.ts
8.9 kB
| 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/); | |
| }); | |
| }); | |