import { test } from "node:test"; import assert from "node:assert/strict"; import { emptyState } from "../../app/domain/state.js"; import { DEFAULT_PROGRAM } from "../../app/domain/default-program.js"; import { weekPlan } from "../../app/domain/planning.js"; import { buildIcs, foldLine, icsEscape } from "../../app/domain/ics.js"; const MONDAY = "2026-11-16"; const NOW = new Date("2026-11-18T17:05:00Z"); /** Une semaine complète : force, cardio et une activité au nom accentué. */ function fullWeek() { const s = emptyState(); s.program = structuredClone(DEFAULT_PROGRAM); s.program.activities = [{ id: "randonnee", name: "Randonnée en forêt, dénivelé élevé ; « été »", zones: ["cardio", "lower"], intensity: "moderate", minutes: 180, }]; s.planSettings = { days: [0, 1, 2, 3, 4, 5], locks: { 6: "activity:randonnee" } }; return s; } /** @param {string} text */ const physicalLines = (text) => text.split("\r\n").slice(0, -1); /** @param {string} text */ const unfold = (text) => text.replace(/\r\n /g, "").split("\r\n").slice(0, -1); /** @param {string} text */ const events = (text) => text.split("BEGIN:VEVENT").slice(1); test("pliage .ics avec accents : 75 octets au plus, jamais au milieu d'un caractère", () => { const line = "SUMMARY:" + "é".repeat(100) + "€🏋"; const folded = foldLine(line); assert.ok(folded.length > 1); for (const l of folded) assert.ok(Buffer.byteLength(l, "utf8") <= 75, `${Buffer.byteLength(l, "utf8")} octets`); assert.ok(folded.slice(1).every((l) => l.startsWith(" "))); assert.equal(folded.map((l, i) => (i ? l.slice(1) : l)).join(""), line); // en caractères, la première ligne fait bien moins de 75 : c'est l'octet qui compte assert.ok(folded[0].length < 75); assert.deepEqual(foldLine("UID:court"), ["UID:court"]); }); test("le fichier entier respecte le pliage et se déplie à l'identique", () => { const s = fullWeek(); const text = buildIcs(s, weekPlan(s, MONDAY), { hour: 18, minute: 0, now: NOW }); assert.ok(text.startsWith("BEGIN:VCALENDAR\r\nVERSION:2.0\r\n")); assert.ok(text.endsWith("END:VCALENDAR\r\n")); for (const l of physicalLines(text)) assert.ok(Buffer.byteLength(l, "utf8") <= 75, l); const summaries = unfold(text).filter((l) => l.startsWith("SUMMARY:")); assert.ok(summaries.includes("SUMMARY:Randonnée en forêt\\, dénivelé élevé \\; « été »")); }); test("UID distincts : un par créneau, la nature du créneau en fait partie", () => { const s = fullWeek(); const plan = weekPlan(s, MONDAY); const text = buildIcs(s, plan, { hour: 18, minute: 0, now: NOW }); const uids = unfold(text).filter((l) => l.startsWith("UID:")); assert.equal(uids.length, plan.slots.length); assert.equal(new Set(uids).size, uids.length); assert.ok(uids.some((u) => u.includes("-cardio-"))); assert.ok(uids.includes("UID:2026-11-22-activity-randonnee@ia-fit")); }); test("chaque événement a son horaire, sa durée et un rappel 30 min avant", () => { const s = fullWeek(); const text = buildIcs(s, weekPlan(s, MONDAY), { hour: 18, minute: 30, now: NOW }); const first = events(text)[0]; assert.match(first, /DTSTAMP:20261118T170500Z/); assert.match(first, /DTSTART:20261116T183000\r\n/); assert.match(first, /DTEND:20261116T191500\r\n/, "séance A : 45 minutes"); assert.match(first, /SUMMARY:Séance A — Bas du corps \+ dos/); assert.match(first, /BEGIN:VALARM\r\nTRIGGER:-PT30M\r\nACTION:DISPLAY/); assert.match(unfold(text).join("\n"), /1\. Squat barre — 4 × 8 — charge à définir \(repos 2 min\)/); // une fin après minuit passe au lendemain const late = buildIcs(s, weekPlan(s, MONDAY), { hour: 23, minute: 0, now: NOW }); assert.match(events(late).at(-1) ?? "", /DTSTART:20261122T230000\r\nDTEND:20261123T020000/); }); test("créneaux faits ou marqués non faits exclus de l'export", () => { const s = fullWeek(); const plan = weekPlan(s, MONDAY); s.skippedDays = [plan.slots[1].date]; s.sessions = [{ id: "x", loggedAt: "2026-11-16T19:00:00Z", date: "2026-11-16", kind: "strength", ref: plan.slots[0].sessionKey ?? "", name: "A", warmup: null, exercises: [], }]; const text = buildIcs(s, plan, { hour: 18, minute: 0, now: NOW }); assert.equal(events(text).length, plan.slots.length - 2); assert.ok(!text.includes("DTSTART:20261116")); assert.ok(!text.includes("DTSTART:20261117")); }); test("sans programme, un calendrier sans événement", () => { const s = fullWeek(); const plan = weekPlan(s, MONDAY); s.program = /** @type {any} */ (null); const text = buildIcs(s, plan, { hour: 18, minute: 0, now: NOW }); assert.equal(events(text).length, 0); assert.match(text, /X-WR-CALNAME:IA-Fit/); }); test("échappement des caractères spéciaux", () => { assert.equal(icsEscape("a,b;c\\d\ne"), "a\\,b\\;c\\\\d\\ne"); });