97 lines
4.7 KiB
JavaScript
97 lines
4.7 KiB
JavaScript
// Les modules purs de l'interface : magasin, routeur, mise en forme, couleurs, accueil.
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { createEvent } from "../../app/domain/events.js";
|
|
import { foldEvents } from "../../app/domain/state.js";
|
|
import { createStore } from "../../app/ui/store.js";
|
|
import { parseRoute, href } from "../../app/ui/router.js";
|
|
import { clock, kg, localDay, parseAmount, plural, restLabel } from "../../app/ui/format.js";
|
|
import { contrastRatio, inkOn } from "../../app/ui/color.js";
|
|
import { estimateMinutes, lastDoneByKey, nextByRotation } from "../../app/ui/home-model.js";
|
|
import { PROGRAM } from "./fixtures.js";
|
|
|
|
const device = "test";
|
|
const adopted = createEvent("ProgramAdopted", { program: PROGRAM, source: "ai" }, { device, at: "2026-11-01T10:00:00.000Z" });
|
|
/** @param {string} key @param {string} date */
|
|
const done = (key, date) => createEvent("SessionLogged", { date, kind: "strength", ref: key, name: key, warmup: null, exercises: [] }, { device });
|
|
|
|
test("dispatch crée un événement, recalcule l'état et prévient les abonnés", () => {
|
|
const appended = [];
|
|
const store = createStore({ events: [adopted], device: "d1", onAppend: (e) => appended.push(e) });
|
|
let calls = 0;
|
|
const unsubscribe = store.subscribe(() => calls++);
|
|
const e = store.dispatch("WorkingLoadSet", { key: "A-squat", loadKg: 62.5 });
|
|
assert.equal(e.device, "d1");
|
|
assert.equal(store.getState().workingLoads["A-squat"].loadKg, 62.5);
|
|
assert.equal(store.getEvents().length, 2);
|
|
assert.deepEqual(appended, [e]);
|
|
assert.equal(calls, 1);
|
|
unsubscribe();
|
|
store.dispatch("ModeChanged", { mode: "recovery" });
|
|
assert.equal(calls, 1);
|
|
});
|
|
|
|
test("dispatch refuse un événement invalide sans toucher au journal", () => {
|
|
const store = createStore();
|
|
assert.throws(() => store.dispatch("WeightRecorded", { date: "2026-11-18", kg: 5 }));
|
|
assert.equal(store.getEvents().length, 0);
|
|
assert.match(store.device, /^browser-/);
|
|
});
|
|
|
|
test("routeur : une adresse par écran, #import= et #pair= réservés", () => {
|
|
assert.deepEqual(parseRoute(""), { name: "home" });
|
|
assert.deepEqual(parseRoute("#/"), { name: "home" });
|
|
assert.deepEqual(parseRoute("#/seance/A"), { name: "session", key: "A" });
|
|
assert.deepEqual(parseRoute(href.session("B2")), { name: "session", key: "B2" });
|
|
assert.deepEqual(parseRoute("#/historique"), { name: "history" });
|
|
assert.deepEqual(parseRoute("#/historique/"), { name: "history" });
|
|
assert.deepEqual(parseRoute(href.historyEdit("abc-1")), { name: "history-edit", id: "abc-1" });
|
|
assert.deepEqual(parseRoute("#import=xyz"), { name: "reserved" });
|
|
assert.deepEqual(parseRoute("#pair=ABCD"), { name: "reserved" });
|
|
assert.deepEqual(parseRoute("#/nimporte/quoi/ici"), { name: "not-found" });
|
|
});
|
|
|
|
test("mise en forme française", () => {
|
|
assert.equal(clock(90), "1:30");
|
|
assert.equal(clock(5), "0:05");
|
|
assert.equal(clock(-3), "0:00");
|
|
assert.equal(restLabel(45), "45 s");
|
|
assert.equal(restLabel(120), "2 min");
|
|
assert.equal(restLabel(90), "1 min 30");
|
|
assert.equal(kg(62.5), "62,5");
|
|
assert.equal(parseAmount("62,5"), 62.5);
|
|
assert.equal(parseAmount(" "), null);
|
|
assert.equal(parseAmount("abc"), undefined);
|
|
assert.equal(parseAmount("-2"), undefined);
|
|
assert.equal(plural(1, "série"), "1 série");
|
|
assert.equal(plural(3, "série"), "3 séries");
|
|
assert.equal(localDay(new Date(2026, 0, 5, 23, 30)), "2026-01-05");
|
|
});
|
|
|
|
test("le texte posé sur une couleur de séance reste lisible (AA)", () => {
|
|
for (const color of ["#3A7FCB", "#E3B505", "#C93A3F", "#3FA06A", "#000000", "#FFFFFF", "#8A9199"])
|
|
assert.ok(contrastRatio(inkOn(color), color) >= 4.5, `${color} : ${contrastRatio(inkOn(color), color).toFixed(2)}`);
|
|
});
|
|
|
|
test("rotation : une séance jamais faite d'abord, sinon la moins récente", () => {
|
|
assert.equal(nextByRotation(foldEvents([adopted]))?.key, "A");
|
|
const state = foldEvents([adopted, done("A", "2026-11-10"), done("B", "2026-11-12"), done("C", "2026-11-14")]);
|
|
assert.equal(nextByRotation(state)?.key, "A");
|
|
assert.deepEqual(lastDoneByKey(state), { A: "2026-11-10", B: "2026-11-12", C: "2026-11-14" });
|
|
const later = foldEvents([adopted, done("A", "2026-11-16"), done("B", "2026-11-12"), done("C", "2026-11-14")]);
|
|
assert.equal(nextByRotation(later)?.key, "B");
|
|
const partial = foldEvents([adopted, done("A", "2026-11-16")]);
|
|
assert.equal(nextByRotation(partial)?.key, "B");
|
|
});
|
|
|
|
test("rotation sans programme : rien à proposer", () => {
|
|
const state = { ...foldEvents([]), program: /** @type {any} */ (null) };
|
|
assert.equal(nextByRotation(state), null);
|
|
});
|
|
|
|
test("durée estimée d'une séance, arrondie à 5 minutes", () => {
|
|
const m = estimateMinutes(PROGRAM.sessions[0]);
|
|
assert.equal(m % 5, 0);
|
|
assert.ok(m >= 20 && m <= 60, String(m));
|
|
});
|