sokko-g/tests/aniimo.test.mjs
Shinuwa 243465720f
All checks were successful
Validate and deploy Sokko G / validate (push) Successful in 18m14s
Validate and deploy Sokko G / deploy (push) Successful in 10s
Add Aniimo elemental matchup calculator
2026-10-31 09:43:04 +01:00

192 lines
12 KiB
JavaScript

// Rôle : vérifie les données Aniimo et le filtrage des variantes avant regroupement.
import { readFile, access } from "node:fs/promises";
import assert from "node:assert/strict";
import { test } from "node:test";
import { createAniimoFilters, filterAniimoVariants, groupAniimoVariants, prepareAniimoData } from "../website/src/features/games/aniimo/utils.js";
import { loadAniimoData } from "../website/src/features/games/loaders.js";
import { aniimoChecklistEntries } from "../website/src/features/games/guideChecklist.js";
import { validateAniimoData } from "./helpers/data-validation.mjs";
const readJson = async (path) => JSON.parse(await readFile(`website/public/data/${path}`, "utf8"));
const [aniimos, fr, en] = await Promise.all([readJson("aniimo/aniimos.json"), readJson("aniimo/i18n/fr.json"), readJson("aniimo/i18n/en.json")]);
const data = prepareAniimoData(aniimos, fr, en);
function readWebpCanvasSize(webp) {
assert.equal(webp.toString("ascii", 12, 16), "VP8X");
return [webp.readUIntLE(24, 3) + 1, webp.readUIntLE(27, 3) + 1];
}
test("Aniimo attributes have valid stats and matching totals", () => {
validateAniimoData(aniimos, [fr, en]);
const invalid = structuredClone(aniimos[0]);
invalid.variations[0].attributes.total++;
assert.throws(() => validateAniimoData([invalid], [fr, en]), /Stats total/);
invalid.variations[0].attributes.stats.hp = -1;
assert.throws(() => validateAniimoData([invalid], [fr, en]), /nonnegative integers/);
});
const filter = (overrides, variants = data.variants) => filterAniimoVariants(variants, { ...createAniimoFilters(), ...overrides });
test("regional identities keep repeated numbers distinct before and after filtering", () => {
assert.equal(new Set(data.variants.map((v) => v.key)).size, data.variants.length);
const sameNumber = data.variants.filter((v) => v.aniimoId === "001");
assert.deepEqual(groupAniimoVariants(sameNumber).map(([v]) => v.name), ["emberpup", "reefish"]);
assert.deepEqual(data.filterOptions.regions, ["breezy_plains", "whisperwake_isles", "special"]);
assert.ok(aniimos.slice(0, -9).every((aniimo) => aniimo.region !== "special"));
assert.ok(aniimos.slice(-9).every((aniimo) => aniimo.region === "special"));
assert.equal(groupAniimoVariants(filter({ regions: ["whisperwake_isles"] })).length, 29);
assert.equal(groupAniimoVariants(filter({ regions: ["breezy_plains", "whisperwake_isles"] })).length, 111);
assert.deepEqual(filter({ regions: ["whisperwake_isles"], name: "emberpup" }), []);
assert.deepEqual(filter({ regions: ["whisperwake_isles"], name: "reefish", elements: ["water"] }).map((v) => v.key), ["whisperwake_isles:001:basic_form"]);
assert.ok(filter({ regions: ["whisperwake_isles"], prismanaOnly: true }).every((v) => v.form === "prismana_form" && v.region === "whisperwake_isles"));
const reordered = prepareAniimoData([aniimos.at(-1), ...aniimos.slice(0, -1)], fr, en).variants;
assert.equal(reordered.at(-1).region, "special");
assert.ok(reordered.slice(0, reordered.findIndex((variant) => variant.region === "special")).every((variant) => variant.region !== "special"));
});
test("validation requires regional uids and explicitly documented unknown roles", () => {
const entry = structuredClone(aniimos[0]);
entry.uid = entry.id;
assert.throws(() => validateAniimoData([entry], [fr, en]), /uid must combine/);
assert.throws(() => validateAniimoData([aniimos[0], aniimos[0]], [fr, en]), /Duplicate Aniimo uid/);
const pending = structuredClone(aniimos[0]);
pending.variations[0].role = null;
pending.variations[0].verification = "pending";
pending.variations[0].data_gaps = ["role_not_verified"];
validateAniimoData([pending], [fr, en]);
const pendingData = prepareAniimoData([pending], fr, en);
delete pending.variations.find((v) => v.role === null).data_gaps;
assert.throws(() => validateAniimoData([pending], [fr, en]), /Unknown role must/);
assert.ok(!pendingData.filterOptions.roles.includes(null));
assert.ok(filter({ roles: pendingData.filterOptions.roles }, pendingData.variants).every((v) => v.role !== null));
assert.ok(filter({}, pendingData.variants).some((v) => v.role === null));
});
test("Sparkelf uses only its current French and English names", () => {
const variants = filter({ name: "Elfe solidaire" });
assert.equal(variants.length, 2);
assert.ok(variants.every((v) => v.name === "sparkelf"));
assert.deepEqual(filter({ name: "Sparkelf" }), variants);
assert.deepEqual(filter({ name: "Little Fire Spirit" }), []);
assert.equal(Object.hasOwn(fr, "little_fire_spirit"), false);
assert.equal(Object.hasOwn(en, "little_fire_spirit"), false);
const entries = aniimoChecklistEntries(variants, fr, en);
assert.ok(entries[0].labels.includes("Elfe solidaire — Forme de base"));
assert.ok(entries[0].labels.includes("Sparkelf — Basic Form"));
});
test("Aniimo data has valid variants, translations, portraits and icons", async () => {
validateAniimoData(aniimos, [fr, en]);
const game = (await readJson("games.json")).games.find((game) => game.id === "aniimo");
assert.ok(game);
assert.equal(game.images.aniimosCover, "/static/img/aniimo/emberpup__basic_form.webp");
const assets = new Set([game.images.cardCover, game.images.heroBg, game.images.aniimosCover, ...data.variants.map((variant) => variant.image).filter(Boolean)]);
for (const option of [...data.filterOptions.roles, ...data.filterOptions.elements, ...data.filterOptions.abilities]) assets.add(`/static/img/aniimo/${option}.webp`);
await Promise.all([...assets].map((asset) => access(`website/public${asset}`)));
for (const variant of data.variants) {
if (variant.image === null) continue;
const webp = await readFile(`website/public${variant.image}`);
assert.equal(webp.toString("ascii", 0, 4), "RIFF");
assert.equal(webp.toString("ascii", 8, 12), "WEBP");
const [width, height] = readWebpCanvasSize(webp);
assert.ok(width >= 280 && height > width, `Portrait must be a usable vertical image: ${variant.image}`);
}
});
test("validation rejects duplicate forms, missing translations and invalid levels", () => {
const entry = structuredClone(aniimos[0]);
entry.variations.push(entry.variations[0]);
assert.throws(() => validateAniimoData([entry], [fr, en]), /Duplicate form/);
assert.throws(() => validateAniimoData(aniimos, [{}]), /must be a string/);
for (const level of [0, 5, 1.5, "3"]) {
const invalid = structuredClone(aniimos[0]);
invalid.variations[0].homeland_abilities[0].level = level;
assert.throws(() => validateAniimoData([invalid], [fr, en]), /integer between 1 and 4/);
}
});
test("name searches accept French, English, accents and case", () => {
assert.deepEqual(filter({ name: " LOUFEUTEAU " }).map((v) => v.key), filter({ name: "Emberpup" }).map((v) => v.key));
assert.ok(filter({ name: "celestia" }).some((variant) => variant.name === "celestis"));
assert.ok(filter({ name: "pomoeuf" }).some((variant) => variant.name === "pomegg"));
assert.deepEqual(filter({ name: "inexistant-xyz" }), []);
});
test("Prismana-only filtering combines with other criteria before grouping", () => {
assert.equal(createAniimoFilters().prismanaOnly, false);
const expected = data.variants.filter((variant) => variant.form === "prismana_form");
assert.ok(expected.length > 0);
assert.deepEqual(filter({ prismanaOnly: true }), expected);
const variant = expected[0];
const criteria = { name: variant.name, elements: variant.elements, roles: [variant.role], abilities: Object.fromEntries(variant.homeland_abilities.map(({ type, level }) => [type, level])) };
assert.deepEqual(filter({ ...criteria, prismanaOnly: true }), [variant]);
assert.equal(groupAniimoVariants(expected).length, expected.length);
assert.deepEqual(filter({ prismanaOnly: true, name: "emberpup" }), []);
assert.equal(filter({ prismanaOnly: false }).length, data.variants.length);
});
test("Prismana remains the final variation in data and grouped results", () => {
for (const group of groupAniimoVariants(data.variants)) {
if (group.some((variant) => variant.form === "prismana_form")) assert.equal(group.at(-1).form, "prismana_form");
}
const entry = structuredClone(aniimos.find((aniimo) => aniimo.variations.length > 1 && aniimo.variations.at(-1).form === "prismana_form"));
entry.variations.unshift(entry.variations.pop());
assert.throws(() => validateAniimoData([entry], [fr, en]), /Prismana must be last/);
});
const fixture = prepareAniimoData([{
id: "001", uid: "breezy_plains:001", region: "breezy_plains", name: "example", elements: ["rock", "water"], role: "support",
variations: [
{ form: "rock_form", elements: ["rock"], role: "dps", homeland_abilities: [{ type: "carry", level: 3 }] },
{ form: "water_form", elements: ["water"], role: "heal", homeland_abilities: [{ type: "crafting", level: 2 }] },
{ form: "both_form", elements: ["rock", "water"], role: "dps", homeland_abilities: [{ type: "carry", level: 4 }, { type: "crafting", level: 1 }] }
]
}], {}, {}).variants;
test("element AND never borrows elements or roles from the parent or sibling variants", () => {
assert.deepEqual(filter({ elements: ["rock", "water"] }, fixture).map((v) => v.form), ["both_form"]);
assert.equal(filter({ elements: ["rock", "water"], elementsLogic: "or" }, fixture).length, 3);
assert.equal(filter({ roles: ["support"] }, fixture).length, 0);
assert.equal(filter({ roles: ["dps", "heal"] }, fixture).length, 3);
});
test("ability thresholds remain independent, inclusive and constrained to a single variant", () => {
assert.equal(filter({ abilities: { carry: 3, crafting: 2 } }, fixture).length, 0);
assert.deepEqual(filter({ abilities: { carry: 3, crafting: 1 } }, fixture).map((v) => v.form), ["both_form"]);
assert.deepEqual(filter({ abilities: { carry: 3 }, abilityExact: true }, fixture).map((v) => v.form), ["rock_form"]);
assert.deepEqual(filter({ abilities: { carry: 4 }, abilityExact: true }, fixture).map((v) => v.form), ["both_form"]);
assert.equal(filter({ abilities: { carry: 2, crafting: 1 }, abilityExact: true }, fixture).length, 0);
assert.equal(filter({ elements: ["rock"], roles: ["heal"], abilities: { carry: 1, crafting: 1 } }, fixture).length, 0);
});
test("grouping preserves order and contains only variants that pass all filters", () => {
const visible = filter({ elements: ["water"] }, fixture);
const groups = groupAniimoVariants(visible);
assert.equal(groups.length, 1);
assert.deepEqual(groups[0].map((v) => v.form), ["water_form", "both_form"]);
assert.deepEqual(groupAniimoVariants([]), []);
assert.equal(groupAniimoVariants(data.variants).length, aniimos.length);
});
test("loader passes cancellation to all requests and reports failed responses", async (context) => {
const calls = [];
const controller = new AbortController();
const listIndex = await readJson("aniimo/lists/index.json");
const regions = await readJson("aniimo/lists/regions.json");
const elementChart = await readJson("aniimo/elements.json");
const bodies = [aniimos, fr, en, listIndex, elementChart, regions];
context.mock.method(globalThis, "fetch", async (url, options) => {
calls.push({ url, options });
const body = bodies[calls.length - 1];
return { ok: true, json: async () => body };
});
const result = await loadAniimoData(controller.signal);
assert.equal(result.variants.length, data.variants.length);
assert.equal(result.lists.length, 1);
assert.equal(result.lists[0].id, "regions");
assert.equal(result.lists[0].itemCount, 14);
assert.deepEqual(result.elementChart, elementChart);
assert.equal(calls.length, 6);
assert.ok(calls.every((call) => call.options.signal === controller.signal));
globalThis.fetch.mock.mockImplementation(async () => ({ ok: false }));
await assert.rejects(loadAniimoData(controller.signal), /indisponibles/);
});