Domaine : tendance et repères du poids, import de pesées, export .ics
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
08b149ff0a
commit
8ca88e27d7
@@ -0,0 +1,101 @@
|
||||
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");
|
||||
});
|
||||
@@ -0,0 +1,85 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { addDays } from "../../app/domain/dates.js";
|
||||
import { parseWeightLines, weightMilestones, weightTrend } from "../../app/domain/weight.js";
|
||||
|
||||
/** Pesées hebdomadaires à partir de `start`, en perdant `perWeek` kg par semaine. */
|
||||
function weekly(start = "2026-01-05", count = 9, from = 110, perWeek = 1) {
|
||||
return Array.from({ length: count }, (_, i) => ({ date: addDays(start, 7 * i), kg: from - perWeek * i }));
|
||||
}
|
||||
|
||||
test("tendance : pente de la régression, en kg par semaine", () => {
|
||||
const trend = weightTrend(weekly());
|
||||
assert.ok(trend);
|
||||
assert.ok(Math.abs(trend.kgPerWeek + 1) < 1e-9);
|
||||
assert.equal(trend.to, "2026-03-02");
|
||||
assert.equal(trend.from, "2026-01-05");
|
||||
assert.equal(trend.count, 9);
|
||||
assert.equal(trend.staleDays, null);
|
||||
});
|
||||
|
||||
test("tendance après une pause de pesées : la fenêtre part de la dernière pesée", () => {
|
||||
// dernière pesée le 2 mars, trois mois sans peser : la tendance reste lisible
|
||||
const trend = weightTrend(weekly(), { today: "2026-06-01" });
|
||||
assert.ok(trend);
|
||||
assert.ok(Math.abs(trend.kgPerWeek + 1) < 1e-9);
|
||||
assert.equal(trend.staleDays, 91);
|
||||
});
|
||||
|
||||
test("tendance : seules les pesées de la fenêtre comptent", () => {
|
||||
// une prise rapide il y a longtemps, puis une baisse régulière
|
||||
const weights = [{ date: "2025-06-01", kg: 90 }, ...weekly("2026-01-05", 9, 110, 0.5)];
|
||||
const trend = weightTrend(weights, { windowDays: 56 });
|
||||
assert.equal(trend?.from, "2026-01-05");
|
||||
assert.ok(Math.abs((trend?.kgPerWeek ?? 0) + 0.5) < 1e-9);
|
||||
assert.equal(weightTrend(weights, { windowDays: 14 })?.count, 3);
|
||||
});
|
||||
|
||||
test("tendance : il faut deux pesées dans la fenêtre", () => {
|
||||
assert.equal(weightTrend([]), null);
|
||||
assert.equal(weightTrend([{ date: "2026-01-01", kg: 100 }]), null);
|
||||
assert.equal(weightTrend([{ date: "2025-01-01", kg: 100 }, { date: "2026-01-01", kg: 99 }]), null);
|
||||
});
|
||||
|
||||
test("repères : la dernière fois sous le poids actuel, et le minimum", () => {
|
||||
const weights = [
|
||||
{ date: "2026-01-01", kg: 100 }, { date: "2026-02-01", kg: 95 }, { date: "2026-03-01", kg: 97 },
|
||||
{ date: "2026-04-01", kg: 99 }, { date: "2026-05-01", kg: 98 },
|
||||
];
|
||||
const m = weightMilestones(weights);
|
||||
assert.deepEqual(m?.current, { date: "2026-05-01", kg: 98 });
|
||||
assert.deepEqual(m?.lastBelow, { date: "2026-03-01", kg: 97 });
|
||||
assert.equal(m?.daysSinceLastBelow, 61);
|
||||
assert.deepEqual(m?.below.map((w) => w.date), ["2026-03-01", "2026-02-01"]);
|
||||
assert.deepEqual(m?.minimum, { date: "2026-02-01", kg: 95 });
|
||||
assert.equal(m?.daysSinceMinimum, 89);
|
||||
});
|
||||
|
||||
test("repères : au plus bas, aucune pesée antérieure n'est plus légère", () => {
|
||||
const m = weightMilestones([{ date: "2026-01-01", kg: 100 }, { date: "2026-02-01", kg: 96 }]);
|
||||
assert.equal(m?.lastBelow, null);
|
||||
assert.equal(m?.daysSinceLastBelow, null);
|
||||
assert.deepEqual(m?.minimum, { date: "2026-02-01", kg: 96 });
|
||||
assert.equal(weightMilestones([{ date: "2026-01-01", kg: 100 }]), null);
|
||||
});
|
||||
|
||||
test("import en texte libre : dates ISO ou françaises, virgule ou point", () => {
|
||||
const text = [
|
||||
"2026-11-18 109,4",
|
||||
"17/11/2026;109.8",
|
||||
" 3.11.26\t110 ",
|
||||
"",
|
||||
"2026-11-01, 111,2",
|
||||
"hier 110",
|
||||
"31/02/2026 110",
|
||||
"2026-11-02 12",
|
||||
].join("\r\n");
|
||||
const { entries, rejected } = parseWeightLines(text);
|
||||
assert.deepEqual(entries, [
|
||||
{ date: "2026-11-18", kg: 109.4 },
|
||||
{ date: "2026-11-17", kg: 109.8 },
|
||||
{ date: "2026-11-03", kg: 110 },
|
||||
{ date: "2026-11-01", kg: 111.2 },
|
||||
]);
|
||||
assert.deepEqual(rejected, ["hier 110", "31/02/2026 110", "2026-11-02 12"]);
|
||||
});
|
||||
Reference in New Issue
Block a user