Download src/lib/__tests__/export.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 13.4 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/__tests__/export.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/__tests__/export.test.ts
-
curl -L -o export.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/__tests__/export.test.ts
13.4 kB
| import { describe, it, expect } from "vitest"; | |
| import { | |
| formatProfile, | |
| formatCategorySkills, | |
| formatActivity, | |
| formatActivities, | |
| formatDailyLogs, | |
| formatUnlocks, | |
| formatSpacedRepetition, | |
| buildExport, | |
| type RawUser, | |
| type RawActivity, | |
| type RawCategorySkill, | |
| type RawDailyLog, | |
| type RawUnlock, | |
| type RawSpacedRepetitionItem, | |
| } from "@/lib/export"; | |
| // βββ Fixtures βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| const NOW = new Date("2026-03-22T10:00:00.000Z"); | |
| const MOCK_USER: RawUser = { | |
| id: 1, | |
| name: "Player One", | |
| level: 12, | |
| totalXp: 4500, | |
| currentXp: 300, | |
| xpToNextLevel: 500, | |
| title: "Focused Mind", | |
| dailyGoalXp: 100, | |
| streak: 5, | |
| longestStreak: 14, | |
| streakFreezes: 2, | |
| lichessUsername: "testplayer", | |
| chesscomUsername: null, | |
| createdAt: new Date("2026-01-15T08:00:00.000Z"), | |
| }; | |
| const MOCK_SKILLS: RawCategorySkill[] = [ | |
| { category: "chess", level: 3, totalXp: 800, currentXp: 50, xpToNext: 200 }, | |
| { category: "feynman", level: 5, totalXp: 1500, currentXp: 100, xpToNext: 300 }, | |
| { category: "reading", level: 1, totalXp: 0, currentXp: 0, xpToNext: 100 }, | |
| ]; | |
| const MOCK_ACTIVITY: RawActivity = { | |
| id: 42, | |
| category: "feynman", | |
| exerciseType: "explain", | |
| description: "Explained quantum entanglement", | |
| baseXp: 50, | |
| qualityBonus: 15, | |
| isCriticalHit: true, | |
| totalXp: 97, | |
| score: 0.85, | |
| durationSecs: 300, | |
| metadata: JSON.stringify({ domain: "physics", wasTaught: false }), | |
| createdAt: new Date("2026-03-20T14:30:00.000Z"), | |
| }; | |
| const MOCK_ACTIVITY_NO_META: RawActivity = { | |
| id: 43, | |
| category: "logic", | |
| exerciseType: "sudoku", | |
| description: "Completed a medium sudoku", | |
| baseXp: 30, | |
| qualityBonus: 0, | |
| isCriticalHit: false, | |
| totalXp: 30, | |
| score: null, | |
| durationSecs: null, | |
| metadata: null, | |
| createdAt: new Date("2026-03-20T15:00:00.000Z"), | |
| }; | |
| const MOCK_ACTIVITY_BAD_META: RawActivity = { | |
| ...MOCK_ACTIVITY_NO_META, | |
| id: 44, | |
| metadata: "not valid json {", | |
| }; | |
| const MOCK_DAILY_LOGS: RawDailyLog[] = [ | |
| { date: new Date("2026-03-20T00:00:00.000Z"), xpEarned: 150, activityCount: 3, goalMet: true, streakFreezeUsed: false }, | |
| { date: new Date("2026-03-21T00:00:00.000Z"), xpEarned: 50, activityCount: 1, goalMet: false, streakFreezeUsed: true }, | |
| ]; | |
| const MOCK_UNLOCKS: RawUnlock[] = [ | |
| { | |
| unlockedAt: new Date("2026-03-18T10:00:00.000Z"), | |
| node: { category: "feynman", name: "Core Concepts", nodeType: "milestone", levelRequired: 2 }, | |
| }, | |
| { | |
| unlockedAt: new Date("2026-03-19T12:00:00.000Z"), | |
| node: { category: "chess", name: "Opening Fundamentals", nodeType: "skill", levelRequired: 3 }, | |
| }, | |
| ]; | |
| const MOCK_SPACED_REP: RawSpacedRepetitionItem[] = [ | |
| { | |
| topicId: "physics-quantum-entanglement", | |
| topic: "Quantum Entanglement", | |
| domain: "Physics", | |
| difficulty: "intermediate", | |
| stage: 2, | |
| nextReviewAt: new Date("2026-03-25T10:00:00.000Z"), | |
| lastReviewAt: new Date("2026-03-22T10:00:00.000Z"), | |
| lastScore: 0.8, | |
| totalReviews: 3, | |
| successCount: 2, | |
| createdAt: new Date("2026-03-15T08:00:00.000Z"), | |
| }, | |
| ]; | |
| // βββ formatProfile ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| describe("formatProfile", () => { | |
| it("maps user fields correctly", () => { | |
| const p = formatProfile(MOCK_USER); | |
| expect(p.name).toBe("Player One"); | |
| expect(p.level).toBe(12); | |
| expect(p.title).toBe("Focused Mind"); | |
| expect(p.totalXp).toBe(4500); | |
| expect(p.dailyGoalXp).toBe(100); | |
| expect(p.streak).toBe(5); | |
| expect(p.longestStreak).toBe(14); | |
| expect(p.streakFreezes).toBe(2); | |
| }); | |
| it("includes connected accounts", () => { | |
| const p = formatProfile(MOCK_USER); | |
| expect(p.connectedAccounts.lichess).toBe("testplayer"); | |
| expect(p.connectedAccounts.chesscom).toBeNull(); | |
| }); | |
| it("formats memberSince as ISO string", () => { | |
| const p = formatProfile(MOCK_USER); | |
| expect(p.memberSince).toBe("2026-01-15T08:00:00.000Z"); | |
| }); | |
| it("handles user with no connected accounts", () => { | |
| const user: RawUser = { ...MOCK_USER, lichessUsername: null, chesscomUsername: null }; | |
| const p = formatProfile(user); | |
| expect(p.connectedAccounts.lichess).toBeNull(); | |
| expect(p.connectedAccounts.chesscom).toBeNull(); | |
| }); | |
| }); | |
| // βββ formatCategorySkills βββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| describe("formatCategorySkills", () => { | |
| it("maps all skill fields", () => { | |
| const skills = formatCategorySkills(MOCK_SKILLS); | |
| expect(skills).toHaveLength(3); | |
| expect(skills[0]).toEqual({ | |
| category: "chess", | |
| level: 3, | |
| totalXp: 800, | |
| currentXp: 50, | |
| xpToNext: 200, | |
| }); | |
| }); | |
| it("returns empty array for empty input", () => { | |
| expect(formatCategorySkills([])).toEqual([]); | |
| }); | |
| }); | |
| // βββ formatActivity βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| describe("formatActivity", () => { | |
| it("maps activity with full data", () => { | |
| const a = formatActivity(MOCK_ACTIVITY); | |
| expect(a.id).toBe(42); | |
| expect(a.category).toBe("feynman"); | |
| expect(a.exerciseType).toBe("explain"); | |
| expect(a.description).toBe("Explained quantum entanglement"); | |
| expect(a.xp).toEqual({ | |
| base: 50, | |
| qualityBonus: 15, | |
| total: 97, | |
| isCriticalHit: true, | |
| }); | |
| expect(a.score).toBe(0.85); | |
| expect(a.durationSecs).toBe(300); | |
| expect(a.date).toBe("2026-03-20T14:30:00.000Z"); | |
| }); | |
| it("parses valid JSON metadata", () => { | |
| const a = formatActivity(MOCK_ACTIVITY); | |
| expect(a.metadata).toEqual({ domain: "physics", wasTaught: false }); | |
| }); | |
| it("handles null metadata", () => { | |
| const a = formatActivity(MOCK_ACTIVITY_NO_META); | |
| expect(a.metadata).toBeNull(); | |
| }); | |
| it("wraps invalid JSON metadata in raw field", () => { | |
| const a = formatActivity(MOCK_ACTIVITY_BAD_META); | |
| expect(a.metadata).toEqual({ raw: "not valid json {" }); | |
| }); | |
| it("handles null score and durationSecs", () => { | |
| const a = formatActivity(MOCK_ACTIVITY_NO_META); | |
| expect(a.score).toBeNull(); | |
| expect(a.durationSecs).toBeNull(); | |
| }); | |
| }); | |
| // βββ formatActivities βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| describe("formatActivities", () => { | |
| it("maps multiple activities", () => { | |
| const result = formatActivities([MOCK_ACTIVITY, MOCK_ACTIVITY_NO_META]); | |
| expect(result).toHaveLength(2); | |
| expect(result[0].id).toBe(42); | |
| expect(result[1].id).toBe(43); | |
| }); | |
| it("returns empty array for empty input", () => { | |
| expect(formatActivities([])).toEqual([]); | |
| }); | |
| }); | |
| // βββ formatDailyLogs ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| describe("formatDailyLogs", () => { | |
| it("formats dates as YYYY-MM-DD", () => { | |
| const logs = formatDailyLogs(MOCK_DAILY_LOGS); | |
| expect(logs[0].date).toBe("2026-03-20"); | |
| expect(logs[1].date).toBe("2026-03-21"); | |
| }); | |
| it("preserves all log fields", () => { | |
| const logs = formatDailyLogs(MOCK_DAILY_LOGS); | |
| expect(logs[0]).toEqual({ | |
| date: "2026-03-20", | |
| xpEarned: 150, | |
| activityCount: 3, | |
| goalMet: true, | |
| streakFreezeUsed: false, | |
| }); | |
| }); | |
| it("handles streak freeze used", () => { | |
| const logs = formatDailyLogs(MOCK_DAILY_LOGS); | |
| expect(logs[1].streakFreezeUsed).toBe(true); | |
| }); | |
| it("returns empty array for empty input", () => { | |
| expect(formatDailyLogs([])).toEqual([]); | |
| }); | |
| it("pads single-digit months and days", () => { | |
| const logs = formatDailyLogs([ | |
| { date: new Date("2026-01-05T00:00:00.000Z"), xpEarned: 0, activityCount: 0, goalMet: false, streakFreezeUsed: false }, | |
| ]); | |
| expect(logs[0].date).toBe("2026-01-05"); | |
| }); | |
| }); | |
| // βββ formatUnlocks ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| describe("formatUnlocks", () => { | |
| it("maps unlock fields with node data", () => { | |
| const unlocks = formatUnlocks(MOCK_UNLOCKS); | |
| expect(unlocks).toHaveLength(2); | |
| expect(unlocks[0]).toEqual({ | |
| category: "feynman", | |
| nodeName: "Core Concepts", | |
| nodeType: "milestone", | |
| levelRequired: 2, | |
| unlockedAt: "2026-03-18T10:00:00.000Z", | |
| }); | |
| }); | |
| it("returns empty array for empty input", () => { | |
| expect(formatUnlocks([])).toEqual([]); | |
| }); | |
| }); | |
| // βββ formatSpacedRepetition βββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| describe("formatSpacedRepetition", () => { | |
| it("maps all fields with ISO dates", () => { | |
| const items = formatSpacedRepetition(MOCK_SPACED_REP); | |
| expect(items).toHaveLength(1); | |
| expect(items[0]).toEqual({ | |
| topicId: "physics-quantum-entanglement", | |
| topic: "Quantum Entanglement", | |
| domain: "Physics", | |
| difficulty: "intermediate", | |
| stage: 2, | |
| nextReviewAt: "2026-03-25T10:00:00.000Z", | |
| lastReviewAt: "2026-03-22T10:00:00.000Z", | |
| lastScore: 0.8, | |
| totalReviews: 3, | |
| successCount: 2, | |
| createdAt: "2026-03-15T08:00:00.000Z", | |
| }); | |
| }); | |
| it("handles null lastReviewAt", () => { | |
| const item: RawSpacedRepetitionItem = { | |
| ...MOCK_SPACED_REP[0], | |
| lastReviewAt: null, | |
| lastScore: null, | |
| }; | |
| const result = formatSpacedRepetition([item]); | |
| expect(result[0].lastReviewAt).toBeNull(); | |
| expect(result[0].lastScore).toBeNull(); | |
| }); | |
| it("returns empty array for empty input", () => { | |
| expect(formatSpacedRepetition([])).toEqual([]); | |
| }); | |
| }); | |
| // βββ buildExport ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| describe("buildExport", () => { | |
| const fullExportInput = { | |
| user: MOCK_USER, | |
| categorySkills: MOCK_SKILLS, | |
| activities: [MOCK_ACTIVITY, MOCK_ACTIVITY_NO_META], | |
| dailyLogs: MOCK_DAILY_LOGS, | |
| unlocks: MOCK_UNLOCKS, | |
| spacedRepetition: MOCK_SPACED_REP, | |
| exportedAt: NOW, | |
| }; | |
| it("includes export metadata", () => { | |
| const exp = buildExport(fullExportInput); | |
| expect(exp.exportVersion).toBe(1); | |
| expect(exp.app).toBe("rotnot"); | |
| expect(exp.exportedAt).toBe("2026-03-22T10:00:00.000Z"); | |
| }); | |
| it("includes all sections", () => { | |
| const exp = buildExport(fullExportInput); | |
| expect(exp.profile).toBeDefined(); | |
| expect(exp.categorySkills).toHaveLength(3); | |
| expect(exp.activities).toHaveLength(2); | |
| expect(exp.dailyLogs).toHaveLength(2); | |
| expect(exp.unlocks).toHaveLength(2); | |
| expect(exp.spacedRepetition).toHaveLength(1); | |
| }); | |
| it("computes summary correctly", () => { | |
| const exp = buildExport(fullExportInput); | |
| expect(exp.summary).toEqual({ | |
| totalActivities: 2, | |
| totalDaysLogged: 2, | |
| totalUnlocks: 2, | |
| totalReviewItems: 1, | |
| categoriesActive: 2, // chess=800xp, feynman=1500xp, reading=0xp | |
| }); | |
| }); | |
| it("counts only categories with XP > 0 as active", () => { | |
| const allActive = buildExport({ | |
| ...fullExportInput, | |
| categorySkills: MOCK_SKILLS.map((s) => ({ ...s, totalXp: 100 })), | |
| }); | |
| expect(allActive.summary.categoriesActive).toBe(3); | |
| const noneActive = buildExport({ | |
| ...fullExportInput, | |
| categorySkills: MOCK_SKILLS.map((s) => ({ ...s, totalXp: 0 })), | |
| }); | |
| expect(noneActive.summary.categoriesActive).toBe(0); | |
| }); | |
| it("uses current time when exportedAt not provided", () => { | |
| const exp = buildExport({ | |
| user: MOCK_USER, | |
| categorySkills: [], | |
| activities: [], | |
| dailyLogs: [], | |
| unlocks: [], | |
| spacedRepetition: [], | |
| }); | |
| // Should be a valid ISO string (not the fixed NOW) | |
| expect(exp.exportedAt).toMatch(/^\d{4}-\d{2}-\d{2}T/); | |
| expect(exp.exportedAt).not.toBe("2026-03-22T10:00:00.000Z"); | |
| }); | |
| it("handles empty data gracefully", () => { | |
| const exp = buildExport({ | |
| user: MOCK_USER, | |
| categorySkills: [], | |
| activities: [], | |
| dailyLogs: [], | |
| unlocks: [], | |
| spacedRepetition: [], | |
| exportedAt: NOW, | |
| }); | |
| expect(exp.categorySkills).toEqual([]); | |
| expect(exp.activities).toEqual([]); | |
| expect(exp.dailyLogs).toEqual([]); | |
| expect(exp.unlocks).toEqual([]); | |
| expect(exp.spacedRepetition).toEqual([]); | |
| expect(exp.summary).toEqual({ | |
| totalActivities: 0, | |
| totalDaysLogged: 0, | |
| totalUnlocks: 0, | |
| totalReviewItems: 0, | |
| categoriesActive: 0, | |
| }); | |
| }); | |
| it("profile section matches formatProfile output", () => { | |
| const exp = buildExport(fullExportInput); | |
| const direct = formatProfile(MOCK_USER); | |
| expect(exp.profile).toEqual(direct); | |
| }); | |
| }); | |