primordial-soup/test/replay.test.js

179 lines
6 KiB
JavaScript
Raw 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 { applyReplayChange } from "../engine/replay-view.js";
import { createReplay } from "../viewer/replay.js";
function game(players = 3, complete = false) {
let state = E.init(players, [], { newGenes: true }, "replay-support");
state.players.forEach((p, i) => (p.name = `Colony ${i + 1}`));
for (let i = 0; !state.finished && i < (complete ? 2000 : 25); i++)
state = E.moveAI(state, E.currentPlayer(state));
return state;
}
function publicFrame(state, position = state.history.length) {
const view = E.stripSecret(state);
delete view.undoPlayer;
view.moves = [];
view.historyLength = position;
return view;
}
function at(packet, position) {
return packet.steps
.slice(0, position - packet.start)
.reduce(applyReplayChange, packet.state);
}
function harness() {
const renders = [],
requests = [],
infos = [],
modes = [];
const replay = createReplay({
render: (state) => renders.push(state),
fetchLog: (range) => requests.push(range),
setReplayInfo: (info) => infos.push(info),
setReplayMode: (mode) => modes.push(mode),
});
return { replay, renders, requests, infos, modes };
}
test("completed games replay setup, intermediate positions and final scores for 3–6 colonies", () => {
for (const count of [3, 4, 5, 6]) {
const state = game(count, true);
assert.equal(state.finished, true);
const original = structuredClone(state);
const packet = E.replayFrames(state);
for (const position of [
0,
1,
count * 3,
Math.floor(state.history.length / 2),
state.history.length,
]) {
assert.deepEqual(
at(packet, position),
publicFrame(E.replay(state, { to: position })),
);
}
assert.deepEqual(at(packet, state.history.length), publicFrame(state));
assert.deepEqual(state, original, "Replay must not mutate the saved game");
assert.equal(
packet.steps.reduce(
(n, step) => n + step.log.length,
packet.state.log.length,
),
state.log.length,
);
}
});
test("bounded log slices use inclusive endpoints and expose only public replay data", () => {
const state = game();
state._knowledge = { 0: state._deck[0], 1: state._deck[1] };
const first = E.logSlice(state, { start: 0, end: 0 });
assert.deepEqual(first.log, state.history.slice(0, 1));
assert.equal(first.replay.steps.length, 1);
const part = E.logSlice(state, { start: 3, end: 7, player: 0 });
assert.deepEqual(part.log, state.history.slice(3, 8));
assert.equal(part.replay.start, 3);
assert.equal(part.replay.steps.length, 5);
assert.deepEqual(at(part.replay, 8), publicFrame(E.replay(state, { to: 8 })));
function safe(value) {
if (!value || typeof value !== "object") return;
for (const [key, item] of Object.entries(value)) {
assert.ok(!key.startsWith("_"), `Secret ${key} leaked`);
assert.ok(
!["history", "foreseenEnvironment", "undoPlayer"].includes(key),
key,
);
safe(item);
}
}
safe(part.replay);
assert.deepEqual(
E.logSlice(state),
state.history,
"Normal move notifications remain unchanged",
);
assert.throws(() => E.replayFrames(state, { start: -1 }), /range/);
});
test("replay excludes undoable actions and records only the replacement once committed", () => {
let state = E.init(3, [], {}, "pending-replay");
while (state.round === 0)
state = E.move(state, E.chooseAI(state), E.currentPlayer(state));
const player = E.currentPlayer(state);
const drift = E.legalMoves(state).find((m) => m.type === "drift");
const before = E.replayFrames(state);
const pending = E.move(state, drift, player);
assert.equal(E.canUndo(pending, player), true);
assert.equal(E.stripSecret(pending).historyLength, state.history.length);
assert.deepEqual(E.replayFrames(pending), before);
const undone = E.move(pending, { type: "undo" }, player);
assert.deepEqual(E.replayFrames(undone), before);
state = E.moveAI(undone, player);
assert.deepEqual(
at(E.replayFrames(state), state.history.length),
publicFrame(state),
);
});
test("live replay preserves its cursor as moves arrive and restores the newest live state", () => {
const h = harness();
let state = game();
h.replay.update(E.stripSecret(state, 0));
h.replay.start();
assert.equal(h.replay.active, true);
assert.deepEqual(h.requests.at(-1), {
start: 0,
end: state.history.length - 1,
});
h.replay.receive(E.logSlice(state, h.requests.at(-1)));
h.replay.to(3);
const displayed = h.renders.at(-1);
assert.equal(displayed.historyLength, 3);
state = E.moveAI(state, E.currentPlayer(state));
const latest = E.stripSecret(state, 0);
h.replay.update(latest);
assert.deepEqual(h.renders.at(-1), displayed);
assert.deepEqual(h.infos.at(-1), {
start: 0,
current: 3,
end: state.history.length,
});
assert.equal(
h.requests.length,
1,
"Live updates do not refetch history while browsing an older position",
);
h.replay.to(state.history.length);
const response = E.logSlice(state, h.requests.at(-1));
h.replay.end();
assert.equal(h.renders.at(-1), latest);
const count = h.renders.length;
h.replay.receive(response);
assert.equal(
h.renders.length,
count,
"A late replay response must not repaint the live board",
);
assert.deepEqual(h.modes, [true, false]);
h.replay.start();
h.replay.to(-10);
assert.equal(h.infos.at(-1).current, 0);
h.replay.to(1e9);
assert.equal(h.infos.at(-1).current, state.history.length);
});
test("archived, cancelled and empty games use local history without a server request", () => {
for (const state of [game(3, true), game(), E.init(3, [], {}, "empty")]) {
const h = harness();
h.replay.update(state);
h.replay.start();
h.replay.to(0);
assert.equal(h.requests.length, 0);
assert.equal(h.infos.at(-1).current, 0);
h.replay.end();
assert.equal(h.renders.at(-1), state);
}
});