sokko-g/tests/holo-flares.test.mjs
Shinuwa 22b397ebfa Document Sokko G's architecture and folders, and move JavaScript file headers to JSDoc @file blocks
Add ARCHITECTURE.md (layers, flows, folder map, glossary), CONTRIBUTING.md
(setup and step-by-step recipes) and a README in each main folder, with an
index of docs/. Convert the 511 JavaScript headers from "// Rôle :" lines to
JSDoc @file blocks, define a three-level header template in website/AGENTS.md
and check the header in tests/file-headers.test.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:42:13 +01:00

157 lines
11 KiB
JavaScript

/**
* @file Vérifie les particules persistantes : tracé fractal et arrivées des éclairs, cycle de vie des
* scintillements et des lucioles, et résolution des réglages de particules d'une édition (animation,
* motif, palette héritée ou explicite, zone, paliers).
*/
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import { flareEditionFields, flareOfEdition, MATCH_HOLO_FLARE_PALETTE, NO_FLARE, resolveFlare, sameFlare } from "../website/src/features/cards/holoFlares.js";
import { boltGeometry, boltPath, TARGET_ZONE } from "../website/src/features/cards/holoLightning.js";
import { canvasBacking, motifAngle } from "../website/src/features/cards/flareSprites.js";
import { FIREFLY_COUNTS, fireflyFade, fireflyHeading } from "../website/src/features/cards/holoFireflies.js";
import { TWINKLE_COUNTS, twinkleState } from "../website/src/features/cards/holoTwinkle.js";
const catalog = JSON.parse(await readFile(new URL("../website/public/data/cards/catalog.json", import.meta.url), "utf8"));
const seeded = (seed = 7) => () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };
const palette = (id) => catalog.effectPalettes.find((item) => item.id === id);
const visual = { effectPreset: catalog.effectPresets[0], palette: palette("solar") };
test("a bolt path keeps its ends and subdivides into 2^depth segments", () => {
const points = boltPath({ x: 0, y: 0 }, { x: 100, y: 40 }, seeded());
assert.equal(points.length, 33);
assert.deepEqual([points[0], points.at(-1)], [{ x: 0, y: 0 }, { x: 100, y: 40 }]);
assert.equal(boltPath({ x: 0, y: 0 }, { x: 10, y: 0 }, seeded(), { depth: 3 }).length, 9);
});
test("bolts end anywhere in a central zone, sparing the face of a hero that is not cut out", () => {
const random = seeded(3);
const offset = (geometry) => ({ x: (geometry.main.at(-1).x - geometry.center.x) / 300, y: (geometry.main.at(-1).y - geometry.center.y) / 400 });
const ends = { flat: [], behind: [] };
for (let index = 0; index < 200; index++) {
const angle = random() * Math.PI * 2;
const flat = boltGeometry(300, 400, angle, random);
ends.flat.push(offset(flat));
ends.behind.push(offset(boltGeometry(300, 400, angle, random, { behindSubject: true })));
assert.ok(flat.branches.length <= 2 && flat.branches.every((branch) => branch.length === 17));
}
const distance = ({ x, y }) => Math.hypot(x, y / 0.9);
assert.ok(ends.flat.every((end) => distance(end) >= TARGET_ZONE.flat[0] - 1e-9 && distance(end) <= TARGET_ZONE.flat[1] + 1e-9));
assert.ok(ends.behind.every((end) => distance(end) <= TARGET_ZONE.behind[1] + 1e-9));
// Derrière un héros détouré, des éclairs vont jusqu'au cœur ; et les arrivées se répartissent tout autour du centre.
assert.ok(ends.behind.some((end) => distance(end) < 0.08));
for (const [x, y] of [[1, 1], [1, -1], [-1, 1], [-1, -1]]) assert.ok(ends.flat.some((end) => Math.sign(end.x) === x && Math.sign(end.y) === y));
});
test("on the whole card, bolts start from the card edges and still aim at the illustration", () => {
const random = seeded(11);
const focus = { x: 10, y: 52, width: 280, height: 220 };
for (let index = 0; index < 100; index++) {
const geometry = boltGeometry(300, 420, random() * Math.PI * 2, random, { focus });
assert.deepEqual(geometry.center, { x: 150, y: 52 + 220 * 0.48 });
const end = geometry.main.at(-1);
assert.ok(Math.abs(end.x - geometry.center.x) <= 280 * TARGET_ZONE.flat[1] + 1e-9 && Math.abs(end.y - geometry.center.y) <= 220 * TARGET_ZONE.flat[1] + 1e-9);
}
});
test("flare settings travel on the edition without their default values", () => {
assert.deepEqual(flareEditionFields(NO_FLARE, catalog), {});
assert.deepEqual(flareEditionFields({ ...NO_FLARE, id: "lightning" }, catalog), { flareId: "lightning" });
assert.deepEqual(flareEditionFields({ id: "lightning", paletteId: "cosmic", intensity: 5, level: 1, scope: "card" }, catalog), { flareId: "lightning", flarePaletteId: "cosmic", flareIntensity: 5, flareLevel: 1, flareScope: "card" });
assert.deepEqual(flareEditionFields({ ...NO_FLARE, id: "lightning", scope: "art" }, catalog), { flareId: "lightning" });
assert.equal(MATCH_HOLO_FLARE_PALETTE, NO_FLARE.paletteId);
});
test("a flare inherits the holo palette unless one is chosen, and needs a holographic finish", () => {
assert.equal(resolveFlare({ flareId: "comet" }, catalog, visual), null);
assert.equal(resolveFlare({ flareId: "lightning" }, catalog, { effectPreset: null, palette: null }), null);
const inherited = resolveFlare({ flareId: "lightning" }, catalog, visual);
assert.deepEqual([inherited.colors, inherited.core, inherited.intensity, inherited.level, inherited.scope], [palette("solar").colors, palette("solar").highlight, 3, 3, "art"]);
assert.equal(resolveFlare({ flareId: "lightning", flareScope: "card" }, catalog, visual).scope, "card");
assert.equal(resolveFlare({ flareId: "lightning", flareScope: "sky" }, catalog, visual).scope, "art");
const chosen = resolveFlare({ flareId: "lightning", flarePaletteId: "cosmic", flareIntensity: 5, flareLevel: 2 }, catalog, visual);
assert.deepEqual([chosen.colors, chosen.intensity, chosen.level], [palette("cosmic").colors, 5, 2]);
});
test("a twinkle grows past its size, flashes at its peak, then shrinks away", () => {
assert.deepEqual(twinkleState(0), { scale: 0, alpha: 0, peak: 0 });
// Léger dépassement pendant l'apparition.
assert.ok([0.2, 0.25, 0.3].some((t) => twinkleState(t).scale > 1));
assert.ok(twinkleState(0.45).peak > 0.95);
assert.equal(twinkleState(0.3).peak, 0);
const end = twinkleState(1);
assert.ok(end.alpha < 1e-6 && end.scale < 0.3);
});
test("twinkle settings carry their motif, count and speed, defaults omitted", () => {
assert.deepEqual(flareEditionFields({ ...NO_FLARE, id: "twinkle" }, catalog), { flareId: "twinkle" });
assert.deepEqual(flareEditionFields({ ...NO_FLARE, id: "twinkle", motifId: "heart", count: 5, level: 1 }, catalog), { flareId: "twinkle", flareMotifId: "heart", flareLevel: 1, flareCount: 5 });
// Les éclairs n'ont ni motif ni nombre : ces réglages ne voyagent pas avec eux.
assert.deepEqual(flareEditionFields({ ...NO_FLARE, id: "lightning", motifId: "heart", count: 5 }, catalog), { flareId: "lightning" });
const twinkle = resolveFlare({ flareId: "twinkle", flareMotifId: "snowflake", flareCount: 4 }, catalog, visual);
assert.deepEqual([twinkle.motifId, twinkle.count, twinkle.level], ["snowflake", 4, 3]);
assert.equal(resolveFlare({ flareId: "twinkle", flareMotifId: "comet" }, catalog, visual).motifId, "four-point-star");
assert.equal(resolveFlare({ flareId: "lightning" }, catalog, visual).motifId, null);
});
test("a firefly fades in, glows, fades out, and wanders around its heading", () => {
assert.deepEqual([0, 0.1, 0.2, 0.5, 0.75, 0.875, 1].map((t) => Number(fireflyFade(t).toFixed(3))), [0, 0.5, 1, 1, 1, 0.5, 0]);
const fly = { heading: 0, wobble: [0.5, 1.2], phase: [0, 1] };
const headings = Array.from({ length: 40 }, (_, index) => fireflyHeading(fly, index * 0.25));
// Le cap ondule autour de la direction de base, sans jamais dépasser l'amplitude des deux sinusoïdes.
assert.ok(headings.every((heading) => Math.abs(heading) <= 1.7 + 1e-9));
assert.ok(Math.max(...headings) - Math.min(...headings) > 1);
});
test("fireflies carry their own motif list, count and speed", () => {
assert.deepEqual(flareEditionFields({ ...NO_FLARE, id: "fireflies" }, catalog), { flareId: "fireflies" });
assert.deepEqual(flareEditionFields({ ...NO_FLARE, id: "fireflies", motifId: "heart", count: 2 }, catalog), { flareId: "fireflies", flareMotifId: "heart", flareCount: 2 });
// Le reflet n'existe que pour les scintillements : les lucioles reprennent leur motif par défaut.
assert.equal(resolveFlare({ flareId: "fireflies", flareMotifId: "glint" }, catalog, visual).motifId, "firefly");
});
test("a bubble never turns and a heart stays upright, other motifs take any angle", () => {
for (const angle of [0, 1, 2.5, 4, 6]) {
assert.equal(motifAngle("bubble", angle), 0);
assert.ok(Math.abs(motifAngle("heart", angle)) <= 0.25);
assert.equal(motifAngle("snowflake", angle), angle);
}
});
test("the catalog animations match their render engines", () => {
const effect = (id) => catalog.flareEffects.find((item) => item.id === id);
assert.deepEqual(catalog.flareEffects.map((item) => item.id), ["lightning", "twinkle", "fireflies"]);
// Les libellés du curseur « Nombre » annoncent les nombres que les moteurs visent réellement.
assert.deepEqual(effect("twinkle").tunings.find((tuning) => tuning.key === "count").levels, TWINKLE_COUNTS.map((count) => `≈ ${count}`));
assert.deepEqual(effect("fireflies").tunings.find((tuning) => tuning.key === "count").levels, FIREFLY_COUNTS.map((count) => `≈ ${count}`));
const drawable = new Set(["four-point-star", "glint", "star", "diamond", "heart", "snowflake", "bubble", "firefly"]);
assert.ok(catalog.flareEffects.flatMap((item) => item.motifs || []).every((motif) => drawable.has(motif.id)));
assert.deepEqual(catalog.flareScopes.map((scope) => scope.id), ["art", "card"]);
});
test("a published edition reads back as editable settings, compared by what they publish", () => {
assert.deepEqual(flareOfEdition({ id: "x" }, catalog), NO_FLARE);
const flare = flareOfEdition({ flareId: "twinkle", flareMotifId: "heart", flareScope: "card", flareCount: 5 }, catalog);
assert.deepEqual(flare, { id: "twinkle", motifId: "heart", paletteId: MATCH_HOLO_FLARE_PALETTE, scope: "card", intensity: 3, level: 3, count: 5 });
assert.deepEqual(flareEditionFields(flare, catalog), { flareId: "twinkle", flareMotifId: "heart", flareScope: "card", flareCount: 5 });
// Le premier motif et le motif vide publient la même chose ; un palier que l'animation n'a pas ne compte pas.
assert.ok(sameFlare({ ...NO_FLARE, id: "twinkle", motifId: "" }, { ...NO_FLARE, id: "twinkle", motifId: "four-point-star" }, catalog));
assert.ok(sameFlare({ ...NO_FLARE, id: "lightning", count: 5 }, { ...NO_FLARE, id: "lightning" }, catalog));
assert.ok(!sameFlare({ ...NO_FLARE, id: "lightning" }, NO_FLARE, catalog));
});
test("a particle canvas holds pixels only while it animates", () => {
const canvas = { width: 300, height: 150 };
const backing = canvasBacking(canvas);
// Un canvas neuf rend aussitôt sa surface par défaut (300 x 150) ; un redimensionnement au repos n'en réserve pas.
assert.deepEqual([canvas.width, canvas.height], [0, 0]);
backing.resize(280, 240, 2);
assert.deepEqual([canvas.width, canvas.height], [0, 0]);
backing.attach();
assert.deepEqual([canvas.width, canvas.height], [560, 480]);
backing.resize(100, 50, 1.5);
assert.deepEqual([canvas.width, canvas.height], [150, 75]);
backing.release();
assert.deepEqual([canvas.width, canvas.height], [0, 0]);
});