primordial-soup/test/journal-actions.test.js

451 lines
16 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import * as E from "../engine/index.js";
import { prepared } from "../viewer/tutorials.js";
const act = (s, type, props = {}) => {
const move = E.legalMoves(s).find(
(m) =>
m.type === type && Object.entries(props).every(([k, v]) => m[k] === v),
);
assert.ok(move, `Missing ${type}`);
return E.move(s, move, E.currentPlayer(s));
};
const text = (s) => s.log.map((l) => l.text).join("\n");
const own = (s, p, ...genes) => {
s.players[p].genes = genes.map((id) => ({ id, round: 1 }));
};
test("Tentacle logs only confirmed cargo, aggregates colours, and undo removes the entry", () => {
for (const players of [3, 4, 5, 6]) {
let s = act(prepared({ players, genes: ["tentacle"] }), "drift");
const before = structuredClone(s);
for (let i = 0; i < 10; i++) s = act(act(s, "carry"), "return-food");
assert.deepEqual(s.log, before.log);
assert.equal(E.logLength(s), E.logLength(before));
const colour = players >= 5 ? 4 : 1;
s = act(s, "carry", { color: colour });
s = act(s, "carry", { color: colour });
const picked = s;
s = act(s, "travel");
assert.equal(s.log.filter((l) => l.text.includes("carries")).length, 1);
assert.match(
text(s),
new RegExp(
`carries 2 ${E.COLORS[colour]} nutrients from B2 to C2 with Tentacle`,
),
);
assert.equal(s.food[7][colour], before.food[7][colour] + 2);
s = E.move(s, { type: "undo" }, 0);
assert.deepEqual(s.log, picked.log);
assert.deepEqual(s.food, picked.food);
s = act(act(s, "return-food"), "return-food");
s = act(s, "travel");
assert.doesNotMatch(text(s), /carries/);
}
});
test("feeding identifies the meal, Suction source, Parasitism payer and Fast Food timing", () => {
let s = prepared({ genes: ["suction1", "parasitism", "fast-food"] });
s.amoebas.find((a) => a.p === 1).cell = 6;
s = act(s, "eat-first");
const meal = E.legalMoves(s).find(
(m) => m.type === "eat" && m.source === 7 && m.host === 1,
);
assert.ok(meal);
s = E.move(s, meal, 0);
assert.match(text(s), /takes .* from C2 with Suction I/);
assert.match(text(s), /You #1 uses Parasitism; Maya pays 1 BP/);
assert.match(text(s), /You #1 feeds on .* at B2 with Fast Food/);
for (let p = 0; p < s.players.length; p++) {
if (meal.food[p])
assert.ok(text(s).includes(`${meal.food[p]} ${E.COLORS[p]} nutrient`));
}
assert.equal(s.players[1].bp, 5);
assert.equal(s.tasks[0].kind, "movement");
const normal = act(prepared({ task: { kind: "feeding", a: "0-1" } }), "eat");
assert.match(
text(normal),
/feeds on 1 red nutrient, 1 blue nutrient, 1 yellow nutrient at B2\./,
);
});
test("excretion reports actual supply-limited amounts, including zero with Toxic Excretions", () => {
for (const supply of [0, 1, 2]) {
let s = prepared({ genes: ["toxic"], task: { kind: "feeding", a: "0-1" } });
s.supply[0] = supply;
s = act(s, "eat");
assert.match(
text(s),
new RegExp(
`excretes ${supply} green nutrient${supply === 1 ? "" : "s"} and one toxin at B2`,
),
);
assert.equal(s.toxins[6], 1);
}
});
test("Holding, normal swimming, Streamline and Speed explain the chosen movement and fee", () => {
let s = act(prepared({ genes: ["holding"] }), "hold");
assert.equal(s.log[0].text, "You #1 stays at B2.");
assert.deepEqual(s.log[0].genes, ["holding"]);
s = prepared();
own(s, 1, "holding");
s.amoebas.find((a) => a.p === 1).cell = 6;
s = act(act(s, "drift"), "follow");
assert.deepEqual(
s.log.map((l) => [l.text, l.genes]),
[
["You #1 moves to C2.", undefined],
["Maya #1 moves to C2.", ["holding"]],
],
);
s = act(prepared(), "swim");
assert.match(text(s), /swims for 1 BP/);
assert.match(s.log.at(-1).text, /rolls/);
s = act(prepared({ genes: ["movement2", "streamline", "speed"] }), "swim");
assert.match(text(s), /swims for 0 BP/);
assert.deepEqual(s.log[0].genes, ["streamline"]);
s = act(act(s, "direction"), "swim-again");
assert.match(text(s), /swims again for 0 BP with Speed/);
assert.doesNotMatch(text(s), /rolls/);
});
test("Holding tags the confirmed movement after Tentacle choices and undo removes both", () => {
let s = prepared({ genes: ["holding", "tentacle"] });
s.amoebas.find((a) => a.id === "0-2").cell = 6;
s = act(act(act(s, "drift"), "travel"), "follow");
assert.equal(s.log.length, 1);
s = act(s, "carry");
const before = structuredClone(s);
s = act(s, "travel");
assert.match(s.log.at(-2).text, /with Tentacle/);
assert.deepEqual(s.log.at(-1).genes, ["holding"]);
assert.equal(s.log.at(-1).text, "You #2 moves to C2.");
s = E.move(s, { type: "undo" }, 0);
assert.deepEqual(s.log, before.log);
});
test("movement and excretion entries identify the genes that cause them", () => {
let s = act(prepared({ genes: ["movement1"] }), "swim");
assert.deepEqual(s.log.at(-1).genes, ["movement1"]);
for (const swim of [false, true]) {
s = prepared({ genes: ["rebound", "movement2"] });
s.amoebas[0].cell = 11;
s.environment.direction = 2;
if (swim) s = act(s, "swim");
s = act(s, swim ? "direction" : "drift", { cell: 13 });
assert.deepEqual(s.log.at(-1).genes, ["rebound"]);
}
s = prepared({
genes: ["cleanliness", "toxic"],
task: { kind: "feeding", a: "0-1" },
});
s = act(act(s, "eat"), "excrete", { cell: 7 });
assert.deepEqual(s.log.find((l) => l.text.includes("excretes")).genes, [
"toxic",
"cleanliness",
]);
});
test("feeding chips credit only genes needed by the chosen meal", () => {
for (const players of [3, 4, 5, 6]) {
for (const genes of [
[],
["frugality"],
["substitution"],
["frugality", "substitution"],
["glutton"],
]) {
const s = prepared({
players,
genes,
task: { kind: "feeding", a: "0-1" },
});
s.food[6].fill(8);
for (const meal of E.legalMoves(s).filter((m) => m.type === "eat")) {
const next = E.move(s, meal, 0);
const entry = next.log.find((l) => l.text.includes("feeds on"));
for (const id of genes) {
const without = structuredClone(s);
own(without, 0, ...genes.filter((g) => g !== id));
const possibleWithout = E.legalMoves(without).some(
(m) =>
m.type === "eat" && m.food.every((n, p) => n === meal.food[p]),
);
assert.equal(
entry.genes?.includes(id) ?? false,
id === "glutton" || !possibleWithout,
`${players} players, ${genes}, meal ${meal.food}, gene ${id}`,
);
}
}
}
}
});
test("division chips explain discounts and remote placement without claiming unused genes", () => {
for (const id of ["division", "short-life"]) {
let s = prepared({
genes: [id, "spores"],
phase: 3,
task: { kind: "division", p: 0 },
});
const remote = E.legalMoves(s).find(
(m) =>
m.type === "divide" &&
!E.colony(s, 0).some((a) => E.adjacent(a.cell).includes(m.cell)),
);
s = E.move(s, remote, 0);
assert.deepEqual(s.log[0].genes, [id, "spores"]);
assert.match(s.log[0].text, /for 4 BP/);
}
let s = prepared({
genes: ["division", "spores"],
phase: 3,
task: { kind: "division", p: 0 },
});
s.amoebas = s.amoebas.filter((a) => a.p !== 0);
s = act(s, "divide");
assert.equal(s.log[0].genes, undefined);
assert.match(s.log[0].text, /for 0 BP/);
});
function combat(defenderGenes, attackerGenes = ["struggle"], seed) {
const s = prepared({
genes: attackerGenes,
task: { kind: "feeding", a: "0-1" },
...(seed ? { seed } : {}),
});
s.tasks.push({ kind: "movement", a: "0-2" });
s.food[6].fill(0);
s.amoebas.find((a) => a.p === 1).cell = 6;
own(s, 1, ...defenderGenes);
return s;
}
test("combat records the gene, defender cost, escape attempt and Hard Crust payment", () => {
let s = act(combat(["defense"]), "attack");
assert.match(text(s), /attacks Maya #1 with Struggle for Survival for 1 BP/);
s = act(s, "defend");
assert.match(text(s), /Maya #1 uses Defense for 1 BP/);
s = act(act(combat(["escape", "movement2"]), "attack"), "escape");
assert.match(text(s), /Maya #1 swims with Escape for 1 BP/);
s = act(
act(combat(["escape", "movement2", "streamline"]), "attack"),
"escape",
);
assert.match(text(s), /Maya #1 swims with Escape for 0 BP/);
assert.equal(s.players[1].bp, 6);
s = act(act(combat(["hard-crust"]), "attack"), "pay-crust");
assert.match(text(s), /You #1 pays 1 BP to overcome Hard Crust/);
assert.equal(s.players[0].bp, 10);
s = act(
act(combat(["hard-crust"], ["struggle", "persistence"]), "attack"),
"abandon-attack",
);
assert.match(text(s), /fails to attack Maya #1/);
const retried = act(s, "retry-attack");
assert.match(text(retried), /attacks Maya #1 with Persistence for 0 BP/);
assert.equal(retried.players[0].bp, s.players[0].bp);
s = act(s, "stop-attack");
assert.equal(
s.log.filter((l) => l.text.includes("fails to attack")).length,
1,
);
});
test("tied combat rolls remain visible and a Persistence defense reroll costs no additional BP", () => {
let s = act(
combat(["defense", "persistence"], ["struggle"], "journal-combat-6"),
"attack",
);
s = act(s, "defend");
assert.deepEqual(
s.log.slice(2).map((l) => l.text),
[
"You #1 rolls 1; Maya #1 rolls 1.",
"Tie — roll again.",
"You #1 rolls 6; Maya #1 rolls 6.",
"Tie — roll again.",
"You #1 rolls 3; Maya #1 rolls 2.",
],
);
const bp = s.players[1].bp;
s = act(s, "retry-defense");
assert.match(text(s), /Maya #1 uses Persistence for 0 BP/);
assert.equal(s.players[1].bp, bp);
});
test("upgrades and exchanges name every returned gene; Social Gene names its paid owner", () => {
const genesTurn = (genes = []) => {
const s = prepared({ genes, phase: 2, task: { kind: "genes", p: 0 } });
s.options.newGenes = true;
s.newGeneMarket = {
available: ["social", "phd", "population"],
_deck: [],
revealedRound: 0,
};
return s;
};
let s = act(genesTurn(), "buy", { gene: "movement1" });
assert.equal(s.log[0].text, "You buys Movement I for 3 BP.");
assert.equal(s.players[0].bp, 9);
s = act(genesTurn(["movement1"]), "buy", { gene: "movement2" });
assert.match(text(s), /returns Movement I to the gene pool/);
assert.match(text(s), /buys Movement II for 5 BP/);
s = act(genesTurn(["adaptable", "intelligence", "movement1"]), "exchange", {
gene: "phd",
exchange: "movement1",
});
assert.match(text(s), /returns Intelligence to the gene pool/);
assert.match(text(s), /returns Movement I to the gene pool/);
assert.equal(s.log.filter((l) => l.text.includes("returns")).length, 2);
assert.deepEqual(s.log.at(-1).genes, ["adaptable"]);
s = genesTurn(["flexible"]);
s.players[0].score = 0;
own(s, 1, "social");
s = act(s, "buy", { gene: "social" });
assert.equal(s.log[0].text, "You buys Social Gene from Maya for 1 BP.");
assert.equal(s.players[0].bp, 11);
assert.equal(s.players[1].bp, 7);
assert.deepEqual(s.log[0].genes, ["flexible"]);
});
test("gene passes distinguish purchases, free exchanges, and an undone purchase", () => {
const turn = (genes = []) => {
const s = prepared({ genes, phase: 2, task: { kind: "genes", p: 0 } });
s.tasks.push({ kind: "genes", p: 1 });
return s;
};
assert.equal(act(turn(), "done").log[0].text, "You didn't buy any genes.");
const bought = act(turn(), "buy", { gene: "movement1" });
assert.equal(
act(bought, "done").log.at(-1).text,
"You finishes buying genes.",
);
const undone = E.move(bought, { type: "undo" }, 0);
const passed = act(undone, "done");
assert.equal(passed.log.length, 1);
assert.equal(passed.log[0].text, "You didn't buy any genes.");
const exchanged = act(turn(["adaptable"]), "exchange", {
gene: "intelligence",
exchange: "adaptable",
});
assert.match(text(exchanged), /buys Intelligence for 0 BP/);
assert.equal(
act(exchanged, "done").log.at(-1).text,
"You finishes buying genes.",
);
});
test("deaths show actual released food and Bang identifies each injured amoeba", () => {
let s = prepared({ phase: 3, task: { kind: "division", p: 0 } });
s.amoebas[0].hp = 2;
s.supply = [0, 1, 2, 0];
s = act(s, "done");
assert.match(
text(s),
/You #1 returns to the soup at B2; food: 1 red nutrient, 2 blue nutrients\./,
);
s = prepared({ genes: ["bang"], phase: 3, task: { kind: "division", p: 0 } });
s.supply.fill(0);
s.amoebas[0].hp = 2;
s.amoebas.find((a) => a.p === 1).cell = 6;
s.amoebas.find((a) => a.p === 2).cell = 6;
s = act(s, "done");
assert.match(text(s), /returns to the soup at B2; food: none/);
for (const victim of ["Maya", "Leo"])
assert.ok(
text(s).includes(
`You #1 gives ${victim} #1 one injury with Go Out With a Bang.`,
),
);
});
test("gene defects and Population Explosion report the actual BP still needed or surrendered", () => {
let s = prepared({
genes: ["short-life"],
phase: 1,
task: { kind: "defect", p: 0, debt: 6 },
});
s = act(s, "discard", { gene: "short-life" });
assert.match(text(s), /discards Short Life Span; remaining deficit: 2 BP/);
assert.equal(s.players[0].bp, 12);
s = prepared({
genes: ["population"],
bp: 5,
phase: 3,
task: { kind: "population", p: 0, target: 3 },
});
s.tasks.push({ kind: "division", p: 0 });
s = act(s, "population-explosion");
assert.match(text(s), /gives up 5 BP and grows toward 3 amoebas/);
assert.equal(s.players[0].bp, 0);
});
test("scoring breaks down amoeba and gene points while showing the final position after occupied spaces", () => {
let s = prepared({
genes: ["movement2", "speed"],
phase: 4,
task: { kind: "aggression", p: 0 },
});
s.players.forEach((p, i) => (p.score = [4, 5, 6, 7][i]));
s.amoebas.push({ id: "0-3", p: 0, n: 3, cell: 1, hp: 0 });
s = act(s, "done");
assert.match(
text(s),
/You advances 2 spaces to 9 \(1 from amoebas \+ 1 from genes\)/,
);
assert.equal(s.players[0].score, 9);
});
test("phase 5 Aggression and Defense journal the BP paid by each colony", () => {
let s = combat(["defense"], ["aggression"]);
s.phase = 4;
s.tasks = [
{ kind: "aggression", p: 0 },
{ kind: "aggression", p: 2 },
];
const initialBP = s.players.map((p) => p.bp);
s = act(s, "attack", { attacker: "0-1", victim: "1-1" });
assert.equal(s.players[0].bp, initialBP[0] - 1);
assert.equal(s.players[1].bp, initialBP[1]);
assert.deepEqual(s.log[0], {
round: s.round,
phase: 4,
text: "You #1 attacks Maya #1 with Aggression for 1 BP.",
cell: 6,
});
s = act(s, "defend");
assert.equal(s.players[0].bp, initialBP[0] - 1);
assert.equal(s.players[1].bp, initialBP[1] - 1);
assert.equal(s.log[1].phase, 4);
assert.equal(s.log[1].text, "Maya #1 uses Defense for 1 BP.");
assert.match(s.log[2].text, /rolls/);
});
test("Healing shows its price or the recipient of the injury before that recipient dies", () => {
let s = prepared({
genes: ["healing1"],
phase: 3,
task: { kind: "division", p: 0 },
});
s.amoebas[0].hp = 1;
s = act(s, "heal");
assert.match(text(s), /You #1 removes one injury with Healing I for 3 BP/);
s = prepared({
genes: ["healing2"],
phase: 3,
task: { kind: "division", p: 0 },
});
s.amoebas[0].hp = 1;
s.amoebas.find((a) => a.p === 1).cell = 6;
s = act(s, "transfer-injury", { target: "1-1" });
assert.match(
s.log[0].text,
/You #1 transfers one injury to Maya #1 with Healing II/,
);
assert.match(s.log[1].text, /Maya #1 returns to the soup/);
assert.equal(s.players[0].bp, 12);
});