285 lines
9.8 KiB
JavaScript
285 lines
9.8 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { readFile } from "node:fs/promises";
|
|
import { createTranslator, languages } from "../viewer/localization/runtime.js";
|
|
import {
|
|
journalHtml,
|
|
journalEntryHtml,
|
|
renderJournalSentence,
|
|
} from "../viewer/journal.js";
|
|
|
|
const players = [
|
|
{ name: "Maya", color: "red" },
|
|
{ name: "Maya_2", color: "blue" },
|
|
];
|
|
|
|
test("swimming rolls show each die and its direction without treating combat rolls as movement", () => {
|
|
const directions = ["West", "North", "East", "South", "Stay", "Choose"];
|
|
for (let value = 1; value <= 6; value++) {
|
|
const html = journalEntryHtml(
|
|
{ phase: 0, text: `Maya #1 rolls ${value}.` },
|
|
players,
|
|
);
|
|
assert.match(
|
|
html,
|
|
new RegExp(`aria-label="Rolled ${value}: ${directions[value - 1]}"`),
|
|
);
|
|
assert.match(html, /aria-label="Maya, amoeba 1"/);
|
|
}
|
|
const html = journalEntryHtml(
|
|
{ phase: 0, text: "Maya #2 rolls 3 and 6." },
|
|
players,
|
|
);
|
|
assert.equal((html.match(/class="journal-die"/g) ?? []).length, 2);
|
|
assert.match(html, /Rolled 3: East/);
|
|
assert.match(html, /Rolled 6: Choose/);
|
|
for (const text of [
|
|
"Maya rolls 2; Maya_2 rolls 4.",
|
|
"Maya #1 rolls 3; Maya_2 #2 rolls 5.",
|
|
"Maya #1 rolls 4 for Camouflage I: defenses are bypassed.",
|
|
]) {
|
|
for (const phase of [0, 4]) {
|
|
assert.doesNotMatch(
|
|
journalEntryHtml({ phase, text }, players),
|
|
/journal-die/,
|
|
);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("journal preserves amoeba identities, coordinates and resource amounts", () => {
|
|
const html = journalHtml(
|
|
[
|
|
{ round: 1, phase: 0, text: "Maya #1 moves to D2." },
|
|
{
|
|
round: 1,
|
|
phase: 0,
|
|
text: "Maya_2 #2 excretes two blue nutrients at D2.",
|
|
},
|
|
],
|
|
players,
|
|
);
|
|
assert.match(html, /aria-label="Maya, amoeba 1"/);
|
|
assert.match(html, /aria-label="Maya_2, amoeba 2"/);
|
|
assert.match(html, /title="Space D2"/);
|
|
assert.match(html, /aria-label="2 blue nutrients"/);
|
|
assert.doesNotMatch(html.replace(/<[^>]*>/g, ""), /Maya #1|Maya_2 #2/);
|
|
});
|
|
|
|
test("gene causes appear as chips on the affected journal entry and the last-move line", () => {
|
|
const line = {
|
|
round: 2,
|
|
phase: 0,
|
|
text: "Maya_2 #1 moves to B3.",
|
|
genes: ["holding"],
|
|
};
|
|
for (const html of [
|
|
journalHtml([line], players),
|
|
journalEntryHtml(line, players),
|
|
]) {
|
|
assert.match(html, /aria-label="Maya_2, amoeba 1"/);
|
|
assert.match(html, /title="Space B3"/);
|
|
assert.match(html, /class="gene-label"[^>]*>.*<span>Holding<\/span>/);
|
|
assert.equal((html.match(/class="gene-label"/g) ?? []).length, 1);
|
|
assert.match(html, /data-localized-rich-text="Holding"/);
|
|
}
|
|
});
|
|
|
|
test("consecutive amoeba actions share one portrait while preserving dice, resources and gene causes", () => {
|
|
const lines = [
|
|
{ text: "Maya #1 rolls 2 and 3.", genes: ["movement1"] },
|
|
{ text: "Maya #1 chooses east." },
|
|
{ text: "Maya #1 moves to C2.", genes: ["holding"] },
|
|
{ text: "Maya #1 feeds on 1 blue nutrient at C2." },
|
|
{ text: "Maya #1 excretes 2 red nutrients at C2." },
|
|
].map((line) => ({ round: 2, phase: 0, ...line }));
|
|
const original = structuredClone(lines);
|
|
const html = journalHtml(lines, players);
|
|
assert.equal((html.match(/journal-event--combined/g) ?? []).length, 1);
|
|
assert.equal((html.match(/class="journal-amoeba"/g) ?? []).length, 1);
|
|
assert.equal((html.match(/class="journal-action"/g) ?? []).length, 5);
|
|
assert.equal((html.match(/class="journal-die"/g) ?? []).length, 2);
|
|
assert.equal((html.match(/class="gene-label"/g) ?? []).length, 2);
|
|
assert.equal((html.match(/class="food-amount"/g) ?? []).length, 2);
|
|
assert.deepEqual(lines, original);
|
|
assert.equal(
|
|
(
|
|
journalHtml(lines.slice(0, 2), players).match(
|
|
/class="journal-action"/g,
|
|
) ?? []
|
|
).length,
|
|
2,
|
|
);
|
|
});
|
|
|
|
test("journal grouping stops at other actors, standalone events, phases and rounds", () => {
|
|
const html = journalHtml(
|
|
[
|
|
{ round: 1, phase: 0, text: "Maya #1 moves to C2." },
|
|
{ round: 1, phase: 0, text: "Maya #2 moves to C2." },
|
|
{ round: 1, phase: 0, text: "Maya #1 feeds." },
|
|
{
|
|
round: 1,
|
|
phase: 0,
|
|
text: "Maya_2 #1 moves to C2.",
|
|
genes: ["holding"],
|
|
},
|
|
{ round: 1, phase: 0, text: "Maya #1 feeds." },
|
|
{ round: 1, phase: 0, text: "Tie — roll again." },
|
|
{ round: 1, phase: 0, text: "Maya #1 feeds." },
|
|
{ round: 1, phase: 4, text: "Maya #1 feeds." },
|
|
{ round: 2, phase: 4, text: "Maya #1 feeds." },
|
|
],
|
|
players,
|
|
);
|
|
assert.doesNotMatch(html, /journal-event--combined/);
|
|
assert.equal((html.match(/class="journal-event"/g) ?? []).length, 9);
|
|
});
|
|
|
|
test("grouped translations omit only their shared subject, retaining other amoeba references", () => {
|
|
const source = "Maya #1 attacks Maya_2 #2 with Aggression for 1 BP.";
|
|
const html = renderJournalSentence(
|
|
source,
|
|
players,
|
|
(text) =>
|
|
text === source
|
|
? "Maya_2 #2 is attacked by Maya #1 with Aggression for 1 BP."
|
|
: text,
|
|
{ name: "Maya", number: "1" },
|
|
);
|
|
assert.doesNotMatch(html, /aria-label="Maya, amoeba 1"/);
|
|
assert.match(html, /aria-label="Maya_2, amoeba 2"/);
|
|
assert.match(html, /class="gene-label"/);
|
|
assert.match(html, /class="resource-amount bp-amount"/);
|
|
});
|
|
|
|
test("journal uses one round divider across phase changes and retains historical environments", () => {
|
|
const html = journalHtml(
|
|
[
|
|
{ round: 1, phase: 0, text: "Maya #1 feeds." },
|
|
{ round: 1, phase: 1, text: "The current is west. Ozone layer: 12." },
|
|
{ round: 1, phase: 2, text: "Maya evolves Movement II for 5 BP." },
|
|
{ round: 2, phase: 1, text: "The current is still. Ozone layer: 14." },
|
|
],
|
|
players,
|
|
);
|
|
assert.equal((html.match(/>Round 1</g) ?? []).length, 1);
|
|
assert.equal((html.match(/>Round 2</g) ?? []).length, 1);
|
|
assert.match(html, /aria-label="Current west\. Ozone layer 12\."/);
|
|
assert.match(html, /aria-label="Current still\. Ozone layer 14\."/);
|
|
assert.match(html, />Movement II<\/span>/);
|
|
assert.match(html, /aria-label="5 biological points"/);
|
|
});
|
|
|
|
test("player names are treated as literal text and safely escaped in journal markup", () => {
|
|
const name = 'Ada.+<img src=x onerror="oops">';
|
|
const html = journalHtml(
|
|
[
|
|
{
|
|
round: 0,
|
|
phase: "setup-first",
|
|
text: `${name} places amoeba #2 at C2.`,
|
|
},
|
|
{
|
|
round: 1,
|
|
phase: 0,
|
|
text: `${name} #2 feeds. <script>alert(1)</script>`,
|
|
},
|
|
{ round: 1, phase: 0, text: "NotMaya #1 feeds." },
|
|
],
|
|
[...players, { name, color: "green" }],
|
|
);
|
|
assert.match(
|
|
html,
|
|
/Ada\.\+<img src=x onerror="oops">, amoeba 2/,
|
|
);
|
|
assert.doesNotMatch(html, /<img|<script/);
|
|
assert.match(html, /<script>/);
|
|
assert.match(html, /NotMaya #1 feeds/);
|
|
});
|
|
|
|
test("journal translates the complete sentence before decorating names and coordinates", () => {
|
|
const source = "Maya #1 moves to D2.";
|
|
const html = renderJournalSentence(
|
|
source,
|
|
players,
|
|
(text) =>
|
|
({
|
|
[source]: "Nach D2 zieht Maya #1.",
|
|
"Space D2": "Feld D2",
|
|
"Maya, amoeba 1": "Maya, Amöbe 1",
|
|
})[text] ?? text,
|
|
);
|
|
assert.match(html, /^Nach <span/);
|
|
assert.match(html, /title="Feld D2"/);
|
|
assert.match(html, /aria-label="Maya, Amöbe 1"/);
|
|
assert.doesNotMatch(html, /moves to/);
|
|
});
|
|
|
|
test("detailed cargo and meal entries retain nutrient icons, gene labels and amoeba badges in every language", async () => {
|
|
for (const locale of Object.keys(languages)) {
|
|
const catalog = JSON.parse(
|
|
await readFile(
|
|
new URL(`../viewer/localization/${locale}.json`, import.meta.url),
|
|
"utf8",
|
|
),
|
|
);
|
|
const translator = createTranslator({ [locale]: catalog }, locale);
|
|
translator.setNames(players.map((p) => p.name));
|
|
for (const source of [
|
|
"Maya #1 carries 2 blue nutrients, 1 red nutrient from B2 to C2 with Tentacle.",
|
|
"Maya #1 feeds on 1 red nutrient, 1 blue nutrient at B2 with Fast Food.",
|
|
]) {
|
|
const html = renderJournalSentence(source, players, translator.translate);
|
|
assert.equal(
|
|
(html.match(/class="food-amount"/g) ?? []).length,
|
|
2,
|
|
`${locale}: ${source}`,
|
|
);
|
|
assert.equal(
|
|
(html.match(/class="journal-amoeba"/g) ?? []).length,
|
|
1,
|
|
locale,
|
|
);
|
|
assert.match(html, /class="gene-label"/, locale);
|
|
assert.match(html, /class="journal-cell"/, locale);
|
|
const grouped = renderJournalSentence(
|
|
source,
|
|
players,
|
|
translator.translate,
|
|
{ name: "Maya", number: "1" },
|
|
);
|
|
assert.doesNotMatch(grouped, /class="journal-amoeba"/, locale);
|
|
assert.match(grouped, /class="gene-label"/, locale);
|
|
assert.equal(
|
|
(grouped.match(/class="food-amount"/g) ?? []).length,
|
|
2,
|
|
locale,
|
|
);
|
|
if (locale !== "en")
|
|
assert.notEqual(translator.translate(source), source, locale);
|
|
}
|
|
for (const source of [
|
|
"Maya #1 attacks Maya_2 #2 with Aggression for 1 BP.",
|
|
"Maya #1 attacks Maya_2 #2 with Persistence for 0 BP.",
|
|
"Maya_2 #2 uses Defense for 1 BP.",
|
|
"Maya_2 #2 uses Persistence for 0 BP.",
|
|
"Maya_2 #2 swims with Escape for 0 BP.",
|
|
"Maya #1 pays 1 BP to overcome Hard Crust.",
|
|
"Maya buys Movement II for 5 BP.",
|
|
"Maya buys Social Gene from Maya_2 for 2 BP.",
|
|
"Maya discards Speed; remaining deficit: 0 BP.",
|
|
]) {
|
|
const html = renderJournalSentence(source, players, translator.translate);
|
|
assert.equal(
|
|
(html.match(/class="resource-amount bp-amount"/g) ?? []).length,
|
|
1,
|
|
`${locale}: ${source}`,
|
|
);
|
|
assert.match(html, /class="journal-(?:amoeba|player)"/, locale);
|
|
assert.match(html, /class="gene-label"/, locale);
|
|
if (locale !== "en")
|
|
assert.notEqual(translator.translate(source), source, locale);
|
|
}
|
|
}
|
|
});
|