primordial-soup/test/undo.test.js

233 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";
import { recordMove } from "../engine/undo.js";
const act = (s, type, extra = {}) => {
const action = E.legalMoves(s).find(
(m) =>
m.type === type &&
Object.entries(extra).every(([key, value]) => m[key] === value),
);
assert.ok(action, `Missing ${type} in ${JSON.stringify(s.tasks[0])}`);
return E.move(s, action, E.currentPlayer(s));
};
const undo = (s, p = 0) => E.move(s, { type: "undo" }, p);
const publicPosition = (s) => {
const view = E.stripSecret(s, 0);
delete view.undoPlayer;
return view;
};
test("100 drift/undo cycles restore the board without growing history, journal or time", () => {
const initial = prepared({ genes: ["energy"] });
initial.toxins = Array(25).fill(0);
initial.toxins[7] = 1;
let s = initial;
for (let i = 0; i < 100; i++) {
s = act(s, "drift");
assert.equal(E.amoeba(s, "0-1").cell, 7);
assert.equal(s.players[0].bp, initial.players[0].bp + 1);
assert.equal(E.isLiveUpdate(s), true);
assert.equal(E.canUndo(s, 0), true);
assert.throws(() => undo(s, 1));
assert.throws(() => E.move(s, { type: "undo", player: 0 }, 0));
const view = E.stripSecret(s, 1);
for (const key of [
"_undo",
"_pendingMoves",
"_counter",
"_timeIncrements",
"_moveDepth",
])
assert.ok(!(key in view));
s = undo(s);
assert.deepEqual(publicPosition(s), publicPosition(initial));
assert.deepEqual(s.log, initial.log);
assert.equal(E.logLength(s), 0);
assert.deepEqual(E.logSlice(s), []);
assert.deepEqual(E.timeIncrements(s), [1, 0, 0, 0]);
assert.equal(E.canUndo(s, 0), false);
}
assert.equal(initial._undo, undefined);
});
test("multiple deterministic actions undo in reverse, then commit only the final sequence", () => {
const initial = prepared();
let s = act(initial, "drift");
const drifted = s;
s = act(s, "eat");
assert.equal(E.amoeba(s, "0-1").cell, 7);
s = undo(s);
assert.deepEqual(publicPosition(s), publicPosition(drifted));
s = undo(s);
assert.deepEqual(publicPosition(s), publicPosition(initial));
s = act(act(act(act(s, "drift"), "eat"), "drift"), "eat");
assert.equal(E.canUndo(s, 0), false);
assert.equal(E.isLiveUpdate(s), false);
assert.deepEqual(
E.logSlice(s).map((x) => x.move.type),
["drift", "eat", "drift", "eat"],
);
assert.deepEqual(E.timeIncrements(s), [4, 0, 0, 0]);
assert.throws(() => undo(s));
});
test("dice rolls commit preceding actions and cannot be cancelled or rerolled", () => {
let s = act(act(prepared(), "drift"), "eat");
assert.equal(E.logLength(s), 0);
s = act(s, "swim");
const rolled = s;
assert.equal(E.logLength(s), 3);
assert.equal(E.canUndo(s, 0), false);
assert.equal(E.isLiveUpdate(s), false);
assert.throws(() => undo(s));
s = act(s, "direction");
s = undo(s);
assert.deepEqual(publicPosition(s), publicPosition(rolled));
assert.equal(s._counter, rolled._counter);
assert.deepEqual(s.tasks[0].dice, rolled.tasks[0].dice);
assert.deepEqual(s.history, rolled.history);
assert.throws(() => undo(s));
});
test("Movement II swimming and direction choices refund fully because no dice are rolled", () => {
const initial = prepared({ genes: ["movement2"] });
let s = act(act(initial, "swim"), "direction");
assert.equal(s.players[0].bp, initial.players[0].bp - 1);
assert.equal(s._counter, initial._counter);
s = undo(undo(s));
assert.deepEqual(publicPosition(s), publicPosition(initial));
assert.deepEqual(E.timeIncrements(s), [2, 0, 0, 0]);
});
test("gene purchases and division can be cancelled with prices refunded", () => {
for (const [phase, task, type] of [
[2, "genes", "buy"],
[3, "division", "divide"],
]) {
const initial = prepared({ phase, task: { kind: task, p: 0 } });
initial.tasks.push({ kind: task, p: 1 });
let s = act(initial, type);
assert.ok(s.players[0].bp < initial.players[0].bp);
s = undo(s);
assert.deepEqual(publicPosition(s), publicPosition(initial));
s = act(act(s, type), "done");
assert.equal(E.currentPlayer(s), 1);
assert.equal(E.canUndo(s, 0), false);
assert.equal(E.isLiveUpdate(s), false);
assert.deepEqual(
s.history.map((x) => x.move.type),
[type, "done"],
);
assert.throws(() => undo(s));
}
});
test("opponent Holding decisions are a handoff boundary even without randomness", () => {
let s = prepared();
s.players[1].genes = [{ id: "holding", round: 1 }];
E.amoeba(s, "1-1").cell = 6;
s = act(s, "drift");
assert.equal(E.currentPlayer(s), 1);
assert.equal(s.tasks[0].kind, "follow");
assert.equal(E.logLength(s), 1);
assert.equal(E.canUndo(s, 0), false);
assert.throws(() => undo(s));
});
test("a shorter replacement turn does not consume another player's time credit", () => {
let s = prepared({ phase: 2, bp: 30, task: { kind: "genes", p: 0 } });
s.tasks.push({ kind: "genes", p: 1 });
s = act(act(act(s, "buy"), "buy"), "buy");
s = undo(undo(undo(s)));
s = act(s, "done");
const credits = E.timeIncrements(s);
s = act(s, "buy");
assert.equal(E.timeIncrements(s)[0], credits[0]);
assert.equal(E.timeIncrements(s)[1], credits[1] + 1);
});
test("hidden information and phase boundaries commit even when the player stays the same", () => {
const initial = prepared();
for (const change of [
(s) => {
s._counter++;
},
(s) => {
s._deck.shift();
},
(s) => {
s._knowledge = { 0: structuredClone(s._deck[0]) };
},
(s) => {
s.newGeneMarket = { available: ["social"], _deck: [], revealedRound: 2 };
},
(s) => {
s.phase++;
},
(s) => {
s.round++;
},
]) {
const after = structuredClone(initial);
change(after);
const s = recordMove(initial, after, { type: "done" }, 0, true);
assert.equal(E.canUndo(s, 0), false);
assert.equal(E.isLiveUpdate(s), false);
assert.equal(E.logLength(s), 1);
}
});
test("Tentacle cargo adjustments give no time; cancelling removes all pending actions", () => {
const initial = prepared({ genes: ["tentacle"] });
let s = act(initial, "drift");
const increments = E.timeIncrements(s);
for (let i = 0; i < 100; i++) {
s = act(s, "carry");
s = undo(s);
assert.deepEqual(E.timeIncrements(s), increments);
assert.equal(E.logLength(s), 0);
}
s = act(act(s, "carry"), "return-food");
assert.deepEqual(E.timeIncrements(s), increments);
s = undo(undo(undo(s)));
assert.deepEqual(publicPosition(s), publicPosition(initial));
});
test("dropping a player commits pending moves and invalidates undo", () => {
const pending = act(prepared(), "drift");
const s = E.dropPlayer(pending, 1);
assert.equal(s.players[1].dropped, true);
assert.equal(E.canUndo(s, 0), false);
assert.equal(E.logLength(s), 1);
assert.deepEqual(E.timeIncrements(s), E.timeIncrements(pending));
});
test("a whole game keeps published history append-only and time counts monotonic", () => {
let s = E.init(3, [], { newGenes: true }, "undo-replay");
let steps = 0;
while (!E.ended(s) && steps++ < 3000) {
const before = s;
s = E.move(s, E.chooseAI(s), E.currentPlayer(s));
assert.deepEqual(s.history.slice(0, before.history.length), before.history);
E.timeIncrements(s).forEach((value, p) =>
assert.ok(value >= E.timeIncrements(before)[p]),
);
const p = E.currentPlayer(s);
if (E.canUndo(s, p)) {
const next = s;
s = undo(s, p);
assert.deepEqual(E.timeIncrements(s), E.timeIncrements(next));
assert.deepEqual(s.history, next.history);
assert.deepEqual(publicPosition(s), publicPosition(before));
s = E.move(s, E.chooseAI(s), p);
}
}
assert.ok(s.finished);
assert.equal(s._pendingMoves, undefined);
const replay = E.replay(s);
assert.deepEqual(publicPosition(replay), publicPosition(s));
assert.deepEqual(replay.history, s.history);
});