sokko-g/tests/holo-flares.test.mjs
Shinuwa f77a0ca577 Prototype lightning, twinkle and firefly particles in the visual lab
The Particles tab of the visual lab now previews three persistent particle
animations for mythic cards, drawn on a canvas inside the illustration or over
the whole card. Lightning bolts grow from the edges towards a random point near
the hero and pass behind a cut-out hero with the prismatic parallax. Twinkles
pop chosen motifs in and out with a light cross at their peak. Fireflies drift
on winding paths, pulse and move away from the pointer. Each animation offers a
palette, a zone and its own sliders, runs only on a large or hovered card, and
stays still under reduced motion. Nothing is saved on the variations yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 16:07:22 +01:00

117 lines
8 KiB
JavaScript

// Rôle : 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, MATCH_HOLO_FLARE_PALETTE, NO_FLARE, resolveFlare } from "../website/src/features/cards/holoFlares.js";
import { boltGeometry, boltPath, TARGET_ZONE } from "../website/src/features/cards/holoLightning.js";
import { motifAngle } from "../website/src/features/cards/flareSprites.js";
import { fireflyFade, fireflyHeading } from "../website/src/features/cards/holoFireflies.js";
import { TWINKLE_MOTIFS, 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), {});
assert.deepEqual(flareEditionFields({ ...NO_FLARE, id: "lightning" }), { flareId: "lightning" });
assert.deepEqual(flareEditionFields({ id: "lightning", paletteId: "cosmic", intensity: 5, level: 1, scope: "card" }), { flareId: "lightning", flarePaletteId: "cosmic", flareIntensity: 5, flareLevel: 1, flareScope: "card" });
assert.deepEqual(flareEditionFields({ ...NO_FLARE, id: "lightning", scope: "art" }), { 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" }), { flareId: "twinkle" });
assert.deepEqual(flareEditionFields({ ...NO_FLARE, id: "twinkle", motifId: "heart", count: 5, level: 1 }), { 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 }), { 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, TWINKLE_MOTIFS[0].id);
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" }), { flareId: "fireflies" });
assert.deepEqual(flareEditionFields({ ...NO_FLARE, id: "fireflies", motifId: "heart", count: 2 }), { 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);
}
});