125 lines
7.7 KiB
JavaScript
125 lines
7.7 KiB
JavaScript
/**
|
|
* @file Vérifie les correspondances des guides, la mise à jour d'une checklist depuis la page
|
|
* et la validité des associations persistantes.
|
|
*/
|
|
|
|
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { aniimoChecklistEntries, matchGuideChecklist, mergeGuideChecklistResults, normalizeGuideLabel, toGuideChecklistResults, toggleGuideChecklistRows, validGuideAssociations } from "../website/src/features/games/guideChecklist.js";
|
|
|
|
const variants = [{ key: "1:base", name: "emberpup", form: "base" }, { key: "1:mountain", name: "emberpup", form: "mountain" }];
|
|
const entries = aniimoChecklistEntries(variants, { emberpup: "Loufeuteau", base: "Forme de base", mountain: "Montagnes" }, { emberpup: "Emberpup", base: "Basic form", mountain: "Mountain form" });
|
|
|
|
test("full names normalize accents, case and whitespace without approximate matching", () => {
|
|
assert.equal(normalizeGuideLabel(" ŒUF Écarlate \n"), "oeuf ecarlate");
|
|
const result = matchGuideChecklist([{ key: "egg", labels: ["Œuf écarlate"] }], { items: [
|
|
{ label: "OEUF ECARLATE", qtyCurrent: 1 }, { label: "Œuf", qtyCurrent: 0 }
|
|
] });
|
|
assert.equal(result.count, 1);
|
|
assert.ok(result.completed.has("egg"));
|
|
assert.equal(matchGuideChecklist(entries, { items: [{ label: "Loufeu", qtyCurrent: 1 }] }).count, 0);
|
|
});
|
|
|
|
test("species completion covers forms, but a specific French or English row takes priority", () => {
|
|
const data = { sections: [{ items: [
|
|
{ label: "EMBERPUP", qtyCurrent: 1 },
|
|
{ label: "Loufeuteau — forme de base", qtyCurrent: 0 }
|
|
] }] };
|
|
const result = matchGuideChecklist(entries, data);
|
|
assert.equal(result.count, 2);
|
|
assert.deepEqual([...result.completed], ["1:mountain"]);
|
|
const english = matchGuideChecklist(entries, { items: [{ label: "Emberpup — Basic form", qtyCurrent: 1 }] });
|
|
assert.equal(english.count, 1);
|
|
assert.deepEqual([...english.completed], ["1:base"]);
|
|
});
|
|
|
|
test("all duplicate lines must reach their own target, including translated aliases", () => {
|
|
const data = { items: [
|
|
{ label: "Loufeuteau", qtyTarget: 3, qtyCurrent: 2 },
|
|
{ label: "Emberpup", qtyCurrent: 1 },
|
|
{ label: "Unrelated", qtyCurrent: 1 }
|
|
] };
|
|
assert.equal(matchGuideChecklist(entries, data).completed.size, 0);
|
|
data.items[0].qtyCurrent = 3;
|
|
assert.equal(matchGuideChecklist(entries, data).completed.size, 2);
|
|
data.items.push({ label: "Loufeuteau" });
|
|
assert.equal(matchGuideChecklist(entries, data).completed.size, 0);
|
|
assert.equal(matchGuideChecklist(entries, {}).count, 0);
|
|
});
|
|
|
|
test("card toggles update every exact matching row to its target or zero", () => {
|
|
const data = { sections: [{ id: "s1", items: [
|
|
{ id: "a", label: "Loufeuteau", qtyTarget: 3, qtyCurrent: 1 },
|
|
{ id: "b", label: "Emberpup", qtyTarget: 2, qtyCurrent: 0 },
|
|
{ id: "c", label: "Unrelated", qtyCurrent: 1 }
|
|
] }] };
|
|
const match = matchGuideChecklist(entries, data);
|
|
assert.deepEqual(match.rowIds.get("1:base"), ["a", "b"]);
|
|
const completed = toggleGuideChecklistRows(data, match.rowIds.get("1:base"), true);
|
|
assert.deepEqual(completed.sections[0].items.map((item) => item.qtyCurrent), [3, 2, 1]);
|
|
const cleared = toggleGuideChecklistRows(completed, match.rowIds.get("1:base"), false);
|
|
assert.deepEqual(cleared.sections[0].items.map((item) => item.qtyCurrent), [0, 0, 1]);
|
|
assert.equal(data.sections[0].items[0].qtyCurrent, 1);
|
|
});
|
|
|
|
test("persistent associations require the current linked toolbox and an existing checklist", () => {
|
|
const toolbox = { id: "tb", modules: [{ id: "capture", type: "checklist" }, { id: "notes", type: "notepad" }] };
|
|
const association = { toolboxId: "tb", moduleId: "capture", hideCompleted: true };
|
|
const state = { "aniimo/aniimos": association, "mhwilds/monsters": { ...association, moduleId: "notes" }, unknown: association };
|
|
assert.deepEqual(validGuideAssociations(state, [toolbox], { aniimo: "tb", mhwilds: "tb" }), { "aniimo/aniimos": { ...association, onlyListed: false } });
|
|
assert.equal(validGuideAssociations({ "aniimo/aniimos": { ...association, onlyListed: true } }, [toolbox], { aniimo: "tb" })["aniimo/aniimos"].onlyListed, true);
|
|
assert.deepEqual(validGuideAssociations(state, [toolbox], { aniimo: "other" }), {});
|
|
assert.deepEqual(validGuideAssociations(state, [], { aniimo: "tb" }), {});
|
|
assert.deepEqual(validGuideAssociations({ "aniimo/aniimos": null }, [toolbox], { aniimo: "tb" }), {});
|
|
});
|
|
|
|
const sequence = () => {
|
|
let next = 0;
|
|
return (prefix) => `${prefix}-${++next}`;
|
|
};
|
|
const result = (label, ...aliases) => ({ key: label, label, labels: [label, ...aliases] });
|
|
|
|
test("page results keep progress of known rows and insert new ones in page order", () => {
|
|
const data = { hideCompletedItems: true, items: [{ id: "flat" }], sections: [{ id: "s1", title: "", items: [
|
|
{ id: "a", label: "Arkveld", qtyTarget: 2, qtyCurrent: 2 },
|
|
{ id: "c", label: "Chatacabra", qtyCurrent: 0 },
|
|
{ id: "z", label: "Note perso", qtyCurrent: 1 }
|
|
] }] };
|
|
const { data: merged, summary } = mergeGuideChecklistResults(data, [result("Ajarakan"), result("ARKVELD"), result("Balahara"), result("Chatacabra")], { uid: sequence() });
|
|
assert.deepEqual(merged.sections[0].items.map((item) => item.label), ["Ajarakan", "Arkveld", "Balahara", "Chatacabra", "Note perso"]);
|
|
assert.deepEqual(merged.sections[0].items.slice(0, 2), [{ id: "item-1", label: "Ajarakan", qtyTarget: 1, qtyCurrent: 0 }, data.sections[0].items[0]]);
|
|
assert.equal(merged.hideCompletedItems, true);
|
|
assert.equal("items" in merged, false);
|
|
assert.deepEqual(summary, { added: 2, kept: 2, completed: 1, missing: 1, missingCompleted: 1, removed: 0 });
|
|
assert.equal(data.sections[0].items.length, 3);
|
|
});
|
|
|
|
test("missing rows are removed only on request and empty sections disappear", () => {
|
|
const data = { sections: [
|
|
{ id: "s1", title: "Monstres", items: [{ id: "a", label: "Arkveld", qtyCurrent: 1 }] },
|
|
{ id: "s2", title: "Vieux", items: [{ id: "o", label: "Obsolète", qtyCurrent: 1 }] }
|
|
] };
|
|
const { data: merged, summary } = mergeGuideChecklistResults(data, [result("Arkveld"), result("Balahara")], { removeMissing: true, uid: sequence() });
|
|
assert.deepEqual(merged.sections.map((section) => [section.title, section.items.map((item) => item.label)]), [["Monstres", ["Arkveld", "Balahara"]]]);
|
|
assert.equal(summary.removed, 1);
|
|
});
|
|
|
|
test("translated aliases match, duplicate results add one row, empty checklists get a section", () => {
|
|
const aliases = mergeGuideChecklistResults({ sections: [{ id: "s", items: [{ id: "e", label: "Emberpup", qtyCurrent: 1 }] }] }, [result("Loufeuteau", "Emberpup")], { uid: sequence() });
|
|
assert.deepEqual(aliases.summary, { added: 0, kept: 1, completed: 1, missing: 0, missingCompleted: 0, removed: 0 });
|
|
const empty = mergeGuideChecklistResults(undefined, [result("Arkveld"), result("Arkveld")], { uid: sequence() });
|
|
assert.deepEqual(empty.data.sections, [{ id: "section-2", title: "", items: [{ id: "item-1", label: "Arkveld", qtyTarget: 1, qtyCurrent: 0 }] }]);
|
|
assert.equal(empty.summary.added, 1);
|
|
});
|
|
|
|
test("Aniimo results follow the page grouping with French and English labels", () => {
|
|
const aniimo = { translations: { emberpup: "loufeuteau", base: "forme de base", mountain: "montagnes" }, englishTranslations: { emberpup: "Emberpup", base: "Basic form" } };
|
|
const items = variants.map((variant) => ({ ...variant, aniimoUid: "1" }));
|
|
assert.deepEqual(toGuideChecklistResults("aniimo/aniimos", items, { aniimo, filters: { aniimo: { grouped: true } } }), [
|
|
{ key: "1", label: "Loufeuteau", labels: ["Loufeuteau", "Emberpup"] }
|
|
]);
|
|
assert.deepEqual(toGuideChecklistResults("aniimo/aniimos", items, { aniimo, filters: { aniimo: { grouped: false } } }).map((entry) => entry.labels), [
|
|
["Loufeuteau — Forme de base", "Emberpup — Basic form"],
|
|
["Loufeuteau — Montagnes"]
|
|
]);
|
|
});
|