RotNot / src /lib /__tests__ /weekly-report.test.ts
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
6.63 kB
import { describe, it, expect } from "vitest";
import {
getWeekStart,
getPreviousWeekStart,
buildReportCacheKey,
getCachedReport,
cacheReport,
buildWeeklyPrompt,
type WeeklyReport,
type CategoryWeekStats,
type WeekOverWeek,
} from "../weekly-report";
// ---------------------------------------------------------------------------
// Date helpers
// ---------------------------------------------------------------------------
describe("getWeekStart", () => {
it("returns Monday for a Wednesday date", () => {
// 2026-03-18 is a Wednesday
const wed = new Date("2026-03-18T14:30:00Z");
const result = getWeekStart(wed);
expect(result.getUTCDay()).toBe(1); // Monday
expect(result.toISOString().slice(0, 10)).toBe("2026-03-16");
expect(result.getUTCHours()).toBe(0);
});
it("returns Monday for a Monday date", () => {
const mon = new Date("2026-03-16T10:00:00Z");
const result = getWeekStart(mon);
expect(result.toISOString().slice(0, 10)).toBe("2026-03-16");
});
it("returns previous Monday for a Sunday date", () => {
// Sunday 2026-03-22
const sun = new Date("2026-03-22T10:00:00Z");
const result = getWeekStart(sun);
expect(result.toISOString().slice(0, 10)).toBe("2026-03-16");
});
});
describe("getPreviousWeekStart", () => {
it("returns the Monday 7 days before the current week start", () => {
const wed = new Date("2026-03-18T14:30:00Z");
const result = getPreviousWeekStart(wed);
expect(result.toISOString().slice(0, 10)).toBe("2026-03-09");
expect(result.getUTCDay()).toBe(1); // Monday
});
});
// ---------------------------------------------------------------------------
// Cache helpers
// ---------------------------------------------------------------------------
describe("buildReportCacheKey", () => {
it("returns same key for same user and same week", () => {
const d1 = new Date("2026-03-18T10:00:00Z");
const d2 = new Date("2026-03-20T22:00:00Z");
expect(buildReportCacheKey(1, d1)).toBe(buildReportCacheKey(1, d2));
});
it("returns different keys for different weeks", () => {
const thisWeek = new Date("2026-03-18T10:00:00Z");
const lastWeek = new Date("2026-03-11T10:00:00Z");
expect(buildReportCacheKey(1, thisWeek)).not.toBe(
buildReportCacheKey(1, lastWeek),
);
});
it("returns different keys for different users", () => {
const d = new Date("2026-03-18T10:00:00Z");
expect(buildReportCacheKey(1, d)).not.toBe(buildReportCacheKey(2, d));
});
});
describe("report cache", () => {
it("returns null for cache miss", () => {
expect(getCachedReport("nonexistent-key")).toBeNull();
});
it("stores and retrieves a report", () => {
const report: WeeklyReport = {
report: "Test report",
highlights: ["h1"],
areasToFocus: ["a1"],
weekOverWeek: {
currentWeekXp: 100,
previousWeekXp: 80,
xpChange: 20,
xpChangePercent: 25,
currentWeekExercises: 10,
previousWeekExercises: 8,
currentWeekActiveDays: 4,
previousWeekActiveDays: 3,
},
generatedAt: new Date().toISOString(),
};
const key = "test-cache-key";
cacheReport(key, report);
const cached = getCachedReport(key);
expect(cached).not.toBeNull();
expect(cached!.report).toBe("Test report");
expect(cached!.highlights).toEqual(["h1"]);
expect(cached!.areasToFocus).toEqual(["a1"]);
});
});
// ---------------------------------------------------------------------------
// Prompt construction
// ---------------------------------------------------------------------------
describe("buildWeeklyPrompt", () => {
const baseWeekOverWeek: WeekOverWeek = {
currentWeekXp: 500,
previousWeekXp: 400,
xpChange: 100,
xpChangePercent: 25,
currentWeekExercises: 15,
previousWeekExercises: 12,
currentWeekActiveDays: 5,
previousWeekActiveDays: 4,
};
const categories: CategoryWeekStats[] = [
{
category: "chess",
label: "Chess",
exercises: 5,
avgScore: 0.75,
totalXp: 200,
previousWeekExercises: 3,
previousWeekAvgScore: 0.6,
},
{
category: "reading",
label: "Deep Reading",
exercises: 3,
avgScore: 0.8,
totalXp: 150,
previousWeekExercises: 2,
previousWeekAvgScore: 0.7,
},
{
category: "feynman",
label: "Feynman",
exercises: 0,
avgScore: 0,
totalXp: 0,
previousWeekExercises: 0,
previousWeekAvgScore: 0,
},
{
category: "logic",
label: "Logic Puzzles",
exercises: 7,
avgScore: 0.65,
totalXp: 150,
previousWeekExercises: 7,
previousWeekAvgScore: 0.65,
},
{
category: "critical-thinking",
label: "Critical Thinking",
exercises: 0,
avgScore: 0,
totalXp: 0,
previousWeekExercises: 0,
previousWeekAvgScore: 0,
},
{
category: "quests",
label: "Skill Quests",
exercises: 0,
avgScore: 0,
totalXp: 0,
previousWeekExercises: 0,
previousWeekAvgScore: 0,
},
];
it("includes active categories with scores", () => {
const prompt = buildWeeklyPrompt(categories, baseWeekOverWeek, 5, 10);
expect(prompt).toContain("Chess: 5 exercises");
expect(prompt).toContain("Deep Reading: 3 exercises");
expect(prompt).toContain("Logic Puzzles: 7 exercises");
});
it("lists inactive categories", () => {
const prompt = buildWeeklyPrompt(categories, baseWeekOverWeek, 5, 10);
expect(prompt).toContain("Inactive this week:");
expect(prompt).toContain("Feynman");
expect(prompt).toContain("Critical Thinking");
expect(prompt).toContain("Skill Quests");
});
it("includes week-over-week numbers", () => {
const prompt = buildWeeklyPrompt(categories, baseWeekOverWeek, 5, 10);
expect(prompt).toContain("XP this week: 500");
expect(prompt).toContain("last week: 400");
expect(prompt).toContain("+100");
expect(prompt).toContain("+25%");
});
it("includes streak data", () => {
const prompt = buildWeeklyPrompt(categories, baseWeekOverWeek, 5, 10);
expect(prompt).toContain("Current streak: 5 days");
expect(prompt).toContain("longest ever: 10");
});
it("instructs JSON-only output", () => {
const prompt = buildWeeklyPrompt(categories, baseWeekOverWeek, 1, 1);
expect(prompt).toContain("Respond ONLY in valid JSON");
expect(prompt).toContain('"report"');
expect(prompt).toContain('"highlights"');
expect(prompt).toContain('"areasToFocus"');
});
});