primordial-soup/test/automatic-phases.test.js

223 lines
7.9 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, details = {}) => {
const move = E.legalMoves(s).find(
(m) =>
m.type === type &&
Object.entries(details).every(([key, value]) => m[key] === value),
);
assert.ok(move, `Missing ${type}`);
return E.move(s, move, E.currentPlayer(s));
};
function beforeTurn(kind, { players = 4, bp = 0, genes = [] } = {}) {
const s = prepared({ players, bp, genes, phase: kind === "genes" ? 2 : 3 });
s.players.forEach((p, i) => {
p.name = `Colony ${i + 1}`;
p.score = players - i;
});
// Passing the preceding turn enters the turn under test through settle().
s.tasks = [
{ kind, p: players - 1 },
{ kind, p: 0 },
{ kind, p: 1 },
];
return s;
}
const own = (s, p, ...ids) =>
(s.players[p].genes = ids.map((id) => ({ id, round: 1 })));
function fillColony(s, p) {
s.amoebas = s.amoebas.filter((a) => a.p !== p);
for (let n = 1; n <= E.amoebaLimit(s.players.length); n++)
s.amoebas.push({ id: `${p}-${n}`, p, n, cell: E.CELLS[n], hp: 0 });
}
test("empty gene and division turns skip on entry without synthetic moves or time credits", () => {
for (const players of [3, 4, 5, 6]) {
for (const kind of ["genes", "division"]) {
const initial = beforeTurn(kind, { players });
const s = act(initial, "done");
assert.equal(E.currentPlayer(s), 1);
assert.equal(s.tasks[0].kind, kind);
assert.deepEqual(s.history, [
{ player: players - 1, move: { type: "done" } },
]);
assert.deepEqual(
E.timeIncrements(s),
s.players.map((_, p) => Number(p === players - 1)),
);
assert.equal(
s.log.filter(
(line) =>
line.text ===
(kind === "genes"
? "Colony 1 didn't buy any genes."
: "Colony 1 finishes cell division."),
).length,
1,
);
assert.equal(E.canUndo(s, players - 1), false);
}
}
});
test("gene skips account for supply, discounts, prerequisites and free exchanges", () => {
const exhausted = beforeTurn("genes", { bp: 2 });
own(exhausted, 1, "intelligence");
own(exhausted, 2, "intelligence");
assert.equal(E.currentPlayer(act(exhausted, "done")), 1);
const discounted = beforeTurn("genes", { bp: 1, genes: ["flexible"] });
const entered = act(discounted, "done");
assert.equal(E.currentPlayer(entered), 0);
assert.ok(
E.legalMoves(entered).some(
(m) => m.type === "buy" && m.gene === "intelligence",
),
);
const exchange = act(beforeTurn("genes", { genes: ["adaptable"] }), "done");
assert.equal(E.currentPlayer(exchange), 0);
assert.ok(!E.legalMoves(exchange).some((m) => m.type === "buy"));
assert.ok(E.legalMoves(exchange).some((m) => m.type === "exchange"));
const prerequisite = beforeTurn("genes", {
bp: 3,
genes: ["intelligence", "movement1", "spores"],
});
prerequisite.players[0].genes[0].round = prerequisite.round;
prerequisite.newGeneMarket = {
available: ["phd"],
_deck: [],
revealedRound: prerequisite.round,
};
assert.equal(E.currentPlayer(act(prerequisite, "done")), 1);
prerequisite.players[0].genes[0].round--;
const upgrade = act(prerequisite, "done");
assert.equal(E.currentPlayer(upgrade), 0);
assert.ok(E.legalMoves(upgrade).some((m) => m.gene === "phd"));
});
test("a skipped leader reveals new genes after passing, before the next colony's choices", () => {
const initial = beforeTurn("genes");
initial.options.newGenes = true;
initial.newGeneMarket = {
available: ["social", "population"],
_deck: ["glutton", "alarm", "energy"],
revealedRound: 0,
};
const s = act(initial, "done");
assert.equal(E.currentPlayer(s), 1);
assert.deepEqual(s.newGeneMarket.available, [
"social",
"population",
"glutton",
"alarm",
]);
assert.deepEqual(s.newGeneMarket._deck, ["energy"]);
assert.equal(s.log.at(-2).text, "Colony 1 didn't buy any genes.");
assert.match(s.log.at(-1).text, /^New genes available:/);
assert.ok(
E.legalMoves(s).some((m) => m.type === "buy" && m.gene === "glutton"),
);
assert.equal(E.canUndo(s, 3), false);
assert.deepEqual(act(s, "done").newGeneMarket._deck, ["energy"]);
});
test("division does not skip healing, injury transfers or free extinction recovery", () => {
for (const [genes, bp, expected] of [
[["healing1"], 3, "heal"],
[["healing2"], 0, "transfer-injury"],
[[], 0, "divide"],
]) {
const initial = beforeTurn("division", { genes, bp });
if (genes.length) {
E.amoeba(initial, "0-1").hp = 1;
E.amoeba(initial, "1-1").cell = E.amoeba(initial, "0-1").cell;
} else initial.amoebas = initial.amoebas.filter((a) => a.p !== 0);
const s = act(initial, "done");
assert.equal(E.currentPlayer(s), 0);
assert.ok(E.legalMoves(s).some((m) => m.type === expected));
}
const full = beforeTurn("division", { bp: 99 });
fillColony(full, 0);
assert.equal(E.currentPlayer(act(full, "done")), 1);
});
test("spending the final BP or taking the final action still keeps Done and Undo", () => {
for (const [kind, bp, genes, type, details] of [
["genes", 2, [], "buy", { gene: "intelligence" }],
[
"genes",
0,
["adaptable"],
"exchange",
{ gene: "intelligence", exchange: "adaptable" },
],
["division", 6, [], "divide", {}],
["division", 3, ["healing1"], "heal", {}],
["division", 0, ["healing2"], "transfer-injury", { target: "1-1" }],
]) {
const initial = beforeTurn(kind, { bp, genes });
if (type === "heal" || type === "transfer-injury") {
E.amoeba(initial, "0-1").hp = 1;
E.amoeba(initial, "1-1").cell = E.amoeba(initial, "0-1").cell;
}
const entered = act(initial, "done");
let s = act(entered, type, details);
assert.equal(E.currentPlayer(s), 0);
assert.deepEqual(E.legalMoves(s), [{ type: "done" }]);
assert.equal(E.canUndo(s, 0), true);
s = E.move(s, { type: "undo" }, 0);
assert.deepEqual(s.players, entered.players);
assert.deepEqual(s.amoebas, entered.amoebas);
assert.deepEqual(s.tasks, entered.tasks);
assert.deepEqual(s.history, entered.history);
assert.deepEqual(s.log, entered.log);
s = act(act(s, type, details), "done");
assert.equal(E.currentPlayer(s), 1);
assert.equal(E.canUndo(s, 0), false);
}
});
test("Population Explosion's income choice remains undoable even when division is empty", () => {
for (const type of ["take-income", "population-explosion"]) {
const initial = beforeTurn("division", { genes: ["population"] });
fillColony(initial, 0);
initial.tasks.splice(1, 0, { kind: "income", p: 0 });
const entered = act(initial, "done");
assert.equal(entered.tasks[0].kind, "population");
const chosen = act(entered, type);
assert.equal(chosen.tasks[0].kind, "division");
assert.deepEqual(E.legalMoves(chosen), [{ type: "done" }]);
assert.equal(E.canUndo(chosen, 0), true);
const undone = E.move(chosen, { type: "undo" }, 0);
assert.deepEqual(undone.players, entered.players);
assert.deepEqual(undone.tasks, entered.tasks);
}
});
test("automatic income and empty division chain through to the next round", () => {
const initial = beforeTurn("genes");
initial.players.forEach((p, i) => {
p.bp = 0;
fillColony(initial, i);
});
initial.tasks = [
{ kind: "genes", p: 0 },
...[1, 2, 3].map((p) => ({ kind: "genes", p })),
];
const s = act(initial, "done");
assert.equal(s.round, initial.round + 1);
assert.equal(s.phase, 0);
assert.equal(s.tasks[0].kind, "movement");
assert.ok(s.players.every((p) => p.bp === 10));
assert.deepEqual(s.history, [{ player: 0, move: { type: "done" } }]);
assert.deepEqual(E.timeIncrements(s), [1, 0, 0, 0]);
assert.equal(
s.log.filter((l) => l.text.includes("finishes cell division")).length,
4,
);
});