primordial-soup/test/new-gene-effects.test.js

602 lines
20 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import test from "node:test";
import assert from "node:assert/strict";
import * as E from "../engine/index.js";
import { NEW_GENES } from "../engine/new-genes.js";
import { prepared } from "../viewer/tutorials.js";
import { geneSummary } from "../viewer/gene-summary.js";
import { moveLabel } from "../viewer/ui.js";
const own = (s, p, ...ids) =>
(s.players[p].genes = ids.map((id) => ({ id, round: 1 })));
const a = (p, n, cell = 6, hp = 0) => ({ id: `${p}-${n}`, p, n, cell, hp });
function fixture({
genes = [],
task = { kind: "movement", a: "0-1" },
phase = 0,
...rest
} = {}) {
const s = prepared({ genes, task, phase, ...rest });
s.options = { newGenes: true };
s.newGeneMarket = {
available: NEW_GENES.map((g) => g.id),
_deck: [],
revealedRound: 0,
};
s.toxins = Array(25).fill(0);
s.alarmPlayers = [];
s._knowledge = {};
// A sentinel decision keeps assertions inside the phase being tested.
s.tasks.push({ kind: "genes", p: 3 });
return s;
}
function act(s, type, props = {}) {
const m = E.legalMoves(s).find(
(m) =>
m.type === type && Object.entries(props).every(([k, v]) => m[k] === v),
);
assert.ok(
m,
`${type} ${JSON.stringify(props)} missing at ${JSON.stringify(s.tasks[0])}`,
);
return E.move(s, m, E.currentPlayer(s));
}
const opts = (s) => E.legalMoves(s).map((m) => m.type);
function clearFood(s, cell) {
s.food[cell].forEach((n, p) => (s.supply[p] += n));
s.food[cell].fill(0);
}
function invariant(s) {
for (let p = 0; p < s.players.length; p++) {
const carried = s.tasks
.filter((t) => t.kind === "carry")
.reduce((n, t) => n + t.cargo.filter((c) => c === p).length, 0);
assert.equal(s.food.reduce((n, v) => n + v[p], s.supply[p]) + carried, 55);
assert.ok(s.food.every((v) => v[p] >= 0));
assert.ok(s.players[p].bp >= 0);
const own = E.colony(s, p);
assert.ok(own.length <= E.amoebaLimit(s.players.length));
assert.equal(new Set(own.map((a) => a.n)).size, own.length);
assert.ok(own.every((a) => E.CELLS.includes(a.cell) && a.hp >= 0));
}
for (const g of E.ALL_GENES) {
const count = s.players.reduce(
(n, p) => n + p.genes.filter((x) => x.id === g.id).length,
0,
);
assert.ok(count <= E.copiesOf(g, s.players.length), `${g.id} exhausted`);
}
assert.ok(s.toxins.every((n) => Number.isInteger(n) && n >= 0));
}
test("all 26 effects have descriptions; the option works independently with 3–6 players", () => {
assert.equal(NEW_GENES.length, 26);
for (const g of NEW_GENES) {
assert.ok(g.implemented);
assert.ok(g.text);
assert.ok(geneSummary(g.id, 4));
assert.equal(E.geneFor(g.id, 6), g);
}
for (const n of [3, 4, 5, 6]) {
const s = E.init(n, [], { newGenes: true }, "option");
assert.equal(
E.genesInGame(s).filter((g) => NEW_GENES.includes(g)).length,
2,
);
assert.equal(s.newGeneMarket._deck.length, 24);
assert.deepEqual(s, E.init(n, [], { newGenes: true }, "option"));
assert.ok(!E.init(n).newGeneMarket);
}
assert.throws(() => E.init(3, [], { newGenes: "false" }));
});
test("the market reveals after the actual leader finishes, never before or twice", () => {
let s = fixture({ phase: 2, task: { kind: "genes", p: 0 } });
s.newGeneMarket = {
available: ["social", "population"],
_deck: ["energy", "glutton", "alarm"],
revealedRound: 0,
};
s.tasks = [
{ kind: "genes", p: 0 },
{ kind: "genes", p: 1 },
{ kind: "genes", p: 2 },
];
assert.ok(!E.legalMoves(s).some((m) => m.gene === "energy"));
s = act(s, "done");
assert.match(s.log.at(-2).text, /didn't buy any genes/);
assert.match(s.log.at(-1).text, /^New genes available:/);
assert.deepEqual(s.newGeneMarket.available, [
"social",
"population",
"energy",
"glutton",
]);
assert.ok(E.legalMoves(s).some((m) => m.gene === "energy"));
s = act(s, "done");
assert.equal(s.newGeneMarket._deck.length, 1);
assert.equal(E.stripSecret(s).newGeneMarket.remaining, 1);
assert.ok(!JSON.stringify(E.stripSecret(s)).includes('"_deck"'));
});
test("Glutton forces an extra foreign nutrient, including combined reduced and substituted meals", () => {
for (const players of [3, 4, 5, 6]) {
const s = fixture({ players, genes: ["glutton"] });
const meals = E.menus(s, s.amoebas[0]);
assert.ok(meals.length);
assert.ok(
meals.every(
(m) =>
m.food[0] === 0 &&
m.food.reduce((a, b) => a + b, 0) === (players >= 5 ? 5 : 4),
),
);
own(s, 0, "glutton", "frugality", "substitution", "parasitism");
s.amoebas.push(a(1, 6));
assert.ok(
E.menus(s, s.amoebas[0]).some(
(m) =>
m.host === 1 &&
m.food.reduce((a, b) => a + b, 0) === (players >= 5 ? 3 : 2),
),
);
assert.equal(E.mutation(s, 0), -2 + 5 + 4 + 5);
}
});
test("Suction borrows from one legal space, conserves food, and enables otherwise impossible meals", () => {
let s = fixture({ genes: ["suction1"], task: { kind: "feeding", a: "0-1" } });
s.supply[1] += s.food[6][1];
s.food[6][1] = 0;
let m = E.legalMoves(s).find((m) => m.type === "eat" && m.source === 7);
assert.ok(m);
assert.equal(
m.borrowed.reduce((a, b) => a + b, 0),
1,
);
s = E.move(s, m, 0);
invariant(s);
assert.equal(s.food[7][1], 1);
s = fixture({ genes: ["suction2"], task: { kind: "feeding", a: "0-1" } });
for (const p of [1, 2]) {
s.supply[p] += s.food[6][p];
s.food[6][p] = 0;
}
m = E.legalMoves(s).find((m) => m.source === 2);
assert.ok(m);
assert.equal(
m.borrowed.reduce((a, b) => a + b, 0),
2,
);
assert.ok(!E.legalMoves(s).some((m) => m.source === 18));
s = E.move(s, m, 0);
invariant(s);
const bad = fixture({ genes: ["suction2"] });
clearFood(bad, 6);
assert.equal(E.menus(bad, bad.amoebas[0]).length, 0); // Cannot borrow all three.
});
test("Cleanliness and Toxic Excretions share the chosen space; the next entrant absorbs all toxins", () => {
let s = fixture({
genes: ["cleanliness", "toxic"],
task: { kind: "feeding", a: "0-1" },
});
s.toxins[7] = 2;
s = act(s, "eat");
assert.equal(s.tasks[0].kind, "excrete");
assert.ok(!E.legalMoves(s).some((m) => m.cell === 18));
s = act(s, "excrete", { cell: 7 });
assert.equal(s.food[7][0], 4);
assert.equal(s.toxins[7], 3);
invariant(s);
s.tasks = [
{ kind: "movement", a: "0-1" },
{ kind: "genes", p: 3 },
];
s = act(s, "drift");
assert.equal(s.amoebas[0].hp, 3);
assert.equal(s.toxins[7], 0);
assert.ok(s.amoebas[0]); // Toxin damage waits for the normal survival phase.
});
test("Fast Food completes feeding once, then movement and Speed, without a second meal", () => {
let s = fixture({
genes: ["fast-food", "movement2", "speed", "energy", "cleanliness"],
});
s = act(s, "eat-first");
s = act(s, "eat");
s = act(s, "excrete", { cell: 6 });
assert.equal(s.tasks[0].kind, "movement");
assert.ok(!opts(s).includes("eat-first"));
const before = s.players[0].bp;
s = act(s, "drift");
assert.equal(s.players[0].bp, before + 1);
assert.equal(s.tasks[0].kind, "genes");
s = fixture({ genes: ["fast-food", "movement2", "speed"] });
s = act(s, "eat-first");
s = act(s, "eat");
s = act(s, "swim");
s = act(s, "direction", { direction: 2 });
s = act(s, "swim-again");
s = act(s, "direction", { direction: 3 });
assert.equal(s.tasks[0].kind, "genes");
s = fixture({
genes: ["fast-food", "struggle"],
amoebas: [a(0, 1), a(1, 1)],
});
clearFood(s, 6);
s = act(s, "eat-first");
s = act(s, "attack", { victim: "1-1" });
assert.equal(s.tasks[0].kind, "movement");
assert.ok(s.tasks[0].fed);
});
test("Rebound crosses only a single obstacle; Energy Conservation also rewards blocked drift", () => {
let s = fixture({ genes: ["rebound", "energy"], amoebas: [a(0, 1, 11)] });
assert.deepEqual(
E.legalMoves(s)
.filter((m) => m.type === "drift")
.map((m) => m.cell),
[11, 13],
);
s = act(s, "drift", { cell: 13 });
assert.equal(s.amoebas[0].cell, 13);
assert.equal(s.players[0].bp, 13);
s = fixture({ genes: ["rebound", "energy"], amoebas: [a(0, 1, 9)] });
assert.deepEqual(
E.legalMoves(s)
.filter((m) => m.type === "drift")
.map((m) => m.cell),
[9],
);
s = act(s, "drift");
assert.equal(s.players[0].bp, 13);
});
test("Hard Crust charges after defenses; refusing or being unable to pay fails the attack", () => {
for (const pay of [true, false]) {
let s = fixture({
genes: ["struggle"],
bp: pay ? 2 : 1,
amoebas: [a(0, 1), a(1, 1)],
task: { kind: "feeding", a: "0-1" },
});
own(s, 1, "hard-crust");
clearFood(s, 6);
s = act(s, "attack");
assert.equal(s.tasks[0].kind, "crust");
assert.equal(E.currentPlayer(s), 0);
s = act(s, pay ? "pay-crust" : "abandon-attack");
assert.equal(!!E.amoeba(s, "1-1"), !pay);
assert.equal(E.amoeba(s, "0-1").hp, pay ? 0 : 1);
}
});
test("Alarm enables Escape only after an actual meal, and Mouthful prevents that meal's death and nutrients", () => {
let s = fixture({
genes: ["struggle"],
amoebas: [a(0, 1), a(0, 2), a(1, 1), a(1, 2)],
task: { kind: "feeding", a: "0-1" },
});
s.tasks = [
{ kind: "feeding", a: "0-1" },
{ kind: "feeding", a: "0-2" },
{ kind: "genes", p: 3 },
];
own(s, 1, "alarm");
clearFood(s, 6);
s = act(s, "attack", { victim: "1-1" });
assert.deepEqual(s.alarmPlayers, [1]);
clearFood(s, 6);
s = act(s, "attack", { victim: "1-2" });
assert.equal(s.tasks[0].kind, "combat");
assert.ok(opts(s).includes("escape"));
s = fixture({
genes: ["struggle"],
amoebas: [a(0, 1), a(1, 1)],
task: { kind: "feeding", a: "0-1" },
});
own(s, 1, "alarm", "mouthful");
clearFood(s, 6);
s = act(s, "attack");
assert.equal(E.amoeba(s, "1-1").hp, 1);
assert.equal(E.amoeba(s, "0-1").hp, 0);
assert.deepEqual(s.food[6], [0, 0, 0, 0]);
assert.deepEqual(s.alarmPlayers, []);
});
test("Threat Display checks amoeba numbers for both forms of attack", () => {
let s = fixture({
genes: ["struggle", "aggression"],
amoebas: [a(0, 3), a(1, 2), a(1, 4)],
task: { kind: "feeding", a: "0-3" },
});
own(s, 1, "threat");
clearFood(s, 6);
assert.deepEqual(
E.legalMoves(s)
.filter((m) => m.type === "attack")
.map((m) => m.victim),
["1-4"],
);
s.phase = 4;
s.tasks = [{ kind: "aggression", p: 0 }];
assert.deepEqual(
E.legalMoves(s)
.filter((m) => m.type === "attack")
.map((m) => m.victim),
["1-4"],
);
});
test("Camouflage I rolls once; II bypasses active defense but not Armor or Hard Crust", () => {
const observed = new Set();
for (let seed = 0; seed < 30; seed++) {
let s = fixture({
genes: ["struggle", "camouflage1"],
amoebas: [a(0, 1), a(1, 1)],
task: { kind: "feeding", a: "0-1" },
seed: String(seed),
});
own(s, 1, "defense", "escape");
clearFood(s, 6);
s = act(s, "attack");
observed.add(s.tasks[0].kind === "combat");
assert.equal(
s.log.filter((l) => l.text.includes("for Camouflage I")).length,
1,
);
}
assert.equal(observed.size, 2);
let s = fixture({
phase: 4,
genes: ["aggression", "camouflage2"],
amoebas: [a(0, 1), a(1, 1)],
task: { kind: "aggression", p: 0 },
});
own(s, 1, "armor");
s = act(s, "attack");
assert.equal(E.amoeba(s, "1-1").hp, 1);
s = fixture({
genes: ["struggle", "camouflage2"],
amoebas: [a(0, 1), a(1, 1)],
task: { kind: "feeding", a: "0-1" },
});
own(s, 1, "defense", "escape", "hard-crust");
clearFood(s, 6);
s = act(s, "attack");
assert.equal(s.tasks[0].kind, "crust");
});
test("Go Out With a Bang handles lethal chains once per amoeba, including the attacker", () => {
let s = fixture({
genes: ["struggle"],
amoebas: [a(0, 1, 6, 1), a(1, 1), a(1, 2, 6, 1), a(2, 1, 6)],
task: { kind: "feeding", a: "0-1" },
});
own(s, 1, "bang");
clearFood(s, 6);
s = act(s, "attack", { victim: "1-1" });
assert.equal(s.amoebas.length, 0);
assert.equal(s.tasks[0].kind, "genes");
assert.equal(
s.log.filter((l) => l.text.includes("returns to the soup")).length,
4,
);
invariant(s);
});
test("Short Life Span changes death threshold, division cost and defect relief", () => {
let s = fixture({
genes: ["short-life"],
phase: 3,
task: { kind: "division", p: 0 },
});
assert.equal(E.injuriesToDie(s, 0), 1);
s = act(s, "divide");
assert.equal(s.players[0].bp, 8);
own(s, 0, "short-life", "longevity");
assert.equal(E.injuriesToDie(s, 0), 2);
s.phase = 1;
s.tasks = [
{ kind: "defect", p: 0, debt: 5 },
{ kind: "genes", p: 3 },
];
s = act(s, "discard", { gene: "short-life" });
assert.equal(s.tasks[0].debt, 1);
s = fixture({
genes: ["short-life"],
phase: 3,
amoebas: [a(0, 1, 6, 1), a(1, 1)],
task: { kind: "division", p: 0 },
});
s.tasks = [s.tasks[0]];
s = act(s, "done");
assert.ok(!E.amoeba(s, "0-1"));
});
test("Healing I can save a dying amoeba; Healing II transfers an injury and immediately kills the recipient", () => {
let s = fixture({
genes: ["healing1"],
phase: 3,
amoebas: [a(0, 1, 6, 2)],
task: { kind: "division", p: 0 },
});
s = act(s, "heal");
assert.equal(s.amoebas[0].hp, 1);
assert.equal(s.players[0].bp, 9);
s = act(s, "heal");
assert.equal(s.amoebas[0].hp, 0);
assert.ok(!opts(s).includes("heal"));
s = fixture({
genes: ["healing2"],
bp: 0,
phase: 3,
amoebas: [a(0, 1, 6, 1), a(1, 1)],
task: { kind: "division", p: 0 },
});
own(s, 1, "armor", "threat");
s = act(s, "transfer-injury", { target: "1-1" });
assert.equal(s.amoebas[0].hp, 0);
assert.ok(!E.amoeba(s, "1-1"));
assert.equal(s.players[0].bp, 0);
invariant(s);
});
test("Population Explosion replaces income and savings with limited free legal placement", () => {
let s = fixture({
genes: ["population"],
phase: 2,
task: { kind: "genes", p: 3 },
});
s.players[0].score = 1;
s.players[1].score = 9;
s.players[3].score = 4;
s.amoebas = [a(0, 1), a(1, 1, 7), a(1, 2, 8), a(1, 3, 9), a(1, 4, 13)];
s.tasks = [
{ kind: "genes", p: 3 },
{ kind: "income", p: 0 },
{ kind: "division", p: 0 },
{ kind: "genes", p: 3 },
];
s = act(s, "done");
assert.equal(s.tasks[0].kind, "population");
s = act(s, "population-explosion");
assert.equal(s.players[0].bp, 0);
while (E.colony(s, 0).length < 4) {
assert.ok(!opts(s).includes("done"));
s = act(s, "divide");
}
assert.ok(!opts(s).includes("divide"));
assert.ok(opts(s).includes("done"));
invariant(s);
});
test("Social Gene transfers only to last place and collects BP from colonies six spaces ahead", () => {
let s = fixture({ phase: 2, task: { kind: "genes", p: 0 } });
own(s, 1, "social");
assert.ok(!E.legalMoves(s).some((m) => m.gene === "social"));
s.players[0].score = 0;
const before = s.players[1].bp;
s = act(s, "buy", { gene: "social" });
assert.equal(s.players[0].bp, 10);
assert.equal(s.players[1].bp, before + 2);
assert.ok(!E.has(s, 1, "social"));
s.players.forEach((pl, p) => (pl.score = [0, 5, 6, 12][p]));
s.players[3].bp = 0;
s.phase = 4;
s.tasks = [{ kind: "aggression", p: 1 }];
const money = s.players.map((p) => p.bp);
s = act(s, "done");
assert.equal(s.players[0].bp, money[0] + 1);
assert.equal(s.players[1].bp, money[1]);
assert.equal(s.players[2].bp, money[2] - 1);
});
test("Flexible and Highly Adaptable price exchanges, return cards, and preserve advanced prerequisites", () => {
let s = fixture({
genes: ["flexible", "adaptable", "intelligence"],
phase: 2,
task: { kind: "genes", p: 0 },
});
s = act(s, "buy", { gene: "movement1" });
assert.equal(s.players[0].bp, 10); // cost 3 - 1
const before = s.players[0].bp;
s = act(s, "exchange", {
gene: "phd",
exchange: "intelligence",
base: "intelligence",
});
assert.equal(s.players[0].bp, before);
assert.ok(!E.has(s, 0, "intelligence"));
assert.ok(E.has(s, 0, "phd"));
assert.equal(E.GENE.phd.points, 2);
assert.ok(!E.legalMoves(s).some((m) => m.gene === "movement2")); // bought this round
const cost = E.genePurchaseCost(s, 0, "energy", "adaptable");
assert.equal(cost, 1);
s = act(s, "exchange", { gene: "energy", exchange: "adaptable" });
assert.ok(!opts(s).includes("exchange"));
});
test("Migration and Environmental Sensing reveal only to their owner at their respective times", () => {
let s = fixture({
genes: ["migration"],
phase: 5,
task: { kind: "genes", p: 3 },
});
s.tasks = [s.tasks[0]];
const next = structuredClone(s._deck[0]);
s = act(s, "done");
assert.deepEqual(E.stripSecret(s, 0).foreseenEnvironment, next);
for (const p of [undefined, 1, 2, 3])
assert.ok(!E.stripSecret(s, p).foreseenEnvironment);
assert.ok(!JSON.stringify(E.stripSecret(s, 0)).includes('"_knowledge"'));
own(s, 0, "environmental");
s.phase = 0;
s.tasks = [{ kind: "movement", a: "0-1" }];
s = act(s, "drift");
s = act(s, "eat");
assert.deepEqual(s.environment, next);
assert.deepEqual(E.stripSecret(s, 0).foreseenEnvironment, s._deck[0]);
assert.ok(!JSON.stringify(s.log).includes("Private forecast"));
});
test("Doris & Frank records who chooses the next game", () => {
let s = fixture({
genes: ["doris-frank"],
phase: 4,
task: { kind: "aggression", p: 1 },
});
s.tasks = [s.tasks[0]];
s._deck = [];
s = act(s, "done");
assert.ok(s.finished);
assert.equal(s.nextGameChooser, 0);
});
test("new action labels and player-count prices are readable", () => {
const s = fixture({
phase: 2,
task: { kind: "genes", p: 0 },
genes: ["flexible", "adaptable"],
});
s.actor = 0;
for (const m of E.legalMoves(s))
assert.ok(!moveLabel(m, s).includes("undefined"));
const six = fixture({ players: 6, phase: 2, task: { kind: "genes", p: 0 } });
six.actor = 0;
assert.ok(moveLabel({ type: "buy", gene: "frugality" }, six).includes("5"));
});
test("complete New genes games at all player counts conserve resources and replay deterministically", () => {
for (const n of [3, 4, 5, 6])
for (const seed of ["new-effects", "interactions"]) {
let s = E.init(n, [], { newGenes: true }, seed + ":" + n),
steps = 0;
while (!s.finished && steps++ < 2500) {
invariant(s);
s = E.move(s, E.chooseAI(s), E.currentPlayer(s));
if (steps % 101 === 0) s = JSON.parse(JSON.stringify(s));
}
invariant(s);
assert.ok(s.finished, `${n} players, ${seed} stalled`);
assert.deepEqual(E.replay(s), s);
}
});
test("Hard Crust also protects Armor against Aggression without bypassing the injury limit", () => {
for (const pay of [true, false]) {
let s = fixture({
genes: ["aggression"],
phase: 4,
task: { kind: "aggression", p: 0 },
amoebas: [a(0, 1), a(1, 1)],
});
own(s, 1, "armor", "hard-crust");
s = act(s, "attack");
assert.equal(E.amoeba(s, "1-1").hp, 0);
s = act(s, pay ? "pay-crust" : "abandon-attack");
assert.equal(E.amoeba(s, "1-1").hp, pay ? 1 : 0);
}
});
test("random choices with all extra genes in the pool preserve invariants across uncommon interactions", () => {
for (let run = 0; run < 8; run++) {
const n = 3 + (run % 4);
let s = E.init(n, [], { newGenes: true }, `new-random-${run}`),
rng = run + 1,
steps = 0;
// Make late advanced combinations reachable without waiting for the reveal deck.
s.newGeneMarket.available = NEW_GENES.map((g) => g.id);
s.newGeneMarket._deck = [];
const random = (n) => {
rng = (Math.imul(rng, 1664525) + 1013904223) >>> 0;
return rng % n;
};
while (!s.finished && steps++ < 4000) {
const moves = E.legalMoves(s);
assert.ok(moves.length);
const buys = moves.filter((m) => m.type === "buy");
const choices = buys.length && random(3) ? buys : moves;
s = E.move(s, choices[random(choices.length)], E.currentPlayer(s));
invariant(s);
}
assert.ok(s.finished, `random New genes run ${run} must finish`);
}
});