File size: 6,633 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 | 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"');
});
});
|