1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 | // Golden-vector tests for the host simulation core and the referee decision
// rules — fixtures extracted from src/game/game.ts while the TS was canon.
import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import * as lib from "../test-dist/testlib.js";
const read = (n) => JSON.parse(readFileSync(new URL(`./vectors/${n}`, import.meta.url), "utf8"));
const sim = read("sim.json");
const referee = read("referee.json");
const scoring = read("scoring.json");
const { GW, GH, ROUND_TICKS, BLOOM_EVERY, MAX_BLOOMS } = sim;
const plain = (x) => JSON.parse(JSON.stringify(x));
test("spawn ring matches TS for every roster size", () => {
for (const [n, starts] of Object.entries(sim.spawns)) {
assert.deepEqual(plain(lib.spawnStarts(Number(n))), starts, `N=${n}`);
}
});
test("splash footprint matches TS, wrapping at every edge", () => {
for (const [at, cells] of Object.entries(sim.splashes)) {
const [x, y] = at.split(",").map(Number);
assert.deepEqual(plain(lib.splashCells(x, y)), cells, at);
}
});
// The scenarios' initial conditions are re-declared here 1:1 with the
// generator script, and the CLJS sim must reproduce the recorded per-tick
// heads/splashes and the resulting pilgrims + mosaic.
const SCENARIOS = {
straight: [{ id: "12D3KooWA", x: 10, y: 10, dir: 1, pend: [] }],
turn: [{ id: "12D3KooWA", x: 10, y: 10, dir: 1, pend: [2] }],
reverse: [{ id: "12D3KooWA", x: 10, y: 10, dir: 1, pend: [3] }],
"pend-drops-same-dir": [{ id: "12D3KooWA", x: 10, y: 10, dir: 1, pend: [1, 1, 2] }],
"wrap-west": [{ id: "12D3KooWA", x: 0, y: 5, dir: 3, pend: [] }],
"wrap-east": [{ id: "12D3KooWA", x: GW - 1, y: 5, dir: 1, pend: [] }],
"wrap-north": [{ id: "12D3KooWA", x: 5, y: 0, dir: 0, pend: [] }],
"wrap-south": [{ id: "12D3KooWA", x: 5, y: GH - 1, dir: 2, pend: [] }],
crossing: [
{ id: "12D3KooWA", x: 10, y: 10, dir: 1, pend: [] },
{ id: "12D3KooWB", x: 12, y: 10, dir: 3, pend: [] },
],
"dead-stays": [
{ id: "12D3KooWA", x: 10, y: 10, dir: 1, pend: [], alive: false },
{ id: "12D3KooWB", x: 20, y: 20, dir: 1, pend: [] },
],
"bloom-splash": [{ id: "12D3KooWA", x: 10, y: 10, dir: 1, pend: [] }],
};
const BLOOMS = { "bloom-splash": [[11, 10]] };
for (const fixture of sim.scenarios) {
test(`tick scenario: ${fixture.name}`, () => {
const spec = SCENARIOS[fixture.name];
assert.ok(spec, `unknown scenario ${fixture.name}`);
const grid = new Uint8Array(GW * GH);
const pilgrims = spec.map((p, i) => ({
id: p.id, x: p.x, y: p.y, dir: p.dir, pend: [...p.pend],
alive: p.alive !== false, color: i,
}));
const seatOf = new Map(pilgrims.map((p, i) => [p.id, i]));
const blooms = new Set((BLOOMS[fixture.name] ?? []).map(([x, y]) => y * GW + x));
pilgrims.forEach((p, i) => { if (p.alive) grid[p.y * GW + p.x] = i + 1; });
for (const expected of fixture.heads) {
const [h, sp] = lib.tickHeads(grid, pilgrims, seatOf, blooms);
assert.deepEqual(plain(h), expected.h, `tick ${expected.n} heads`);
assert.deepEqual(sp.length ? plain(sp) : null, expected.sp, `tick ${expected.n} splashes`);
}
assert.deepEqual(
pilgrims.map((p) => ({ id: p.id, x: p.x, y: p.y, dir: p.dir, alive: p.alive, pend: p.pend, color: p.color })),
fixture.pilgrims,
"final pilgrims",
);
const cells = [];
for (let i = 0; i < grid.length; i++) if (grid[i]) cells.push([i % GW, (i / GW) | 0, grid[i]]);
assert.deepEqual(cells, fixture.gridCells, "final mosaic");
});
}
test("bloom-spawn gate matches TS (cadence, cap, and the sunset cutoff)", () => {
for (const c of sim.bloomGate) {
assert.equal(Boolean(lib.bloomSpawns(c.n, c.blooms)), c.spawns, JSON.stringify(c));
}
// the constants the gate is built from must not drift
assert.equal(BLOOM_EVERY, 24);
assert.equal(MAX_BLOOMS, 3);
assert.equal(ROUND_TICKS, 360);
});
test("host input queue: cap 3, mask to 0..3, stopped pilgrims ignored", () => {
const p = { alive: true, pend: [] };
for (const d of [0, 1, 2, 3]) lib.pushInput(p, d);
assert.deepEqual(p.pend, [0, 1, 2], "capped at 3");
const masked = { alive: true, pend: [] };
lib.pushInput(masked, 7);
assert.deepEqual(masked.pend, [3], "masked with &3");
const dead = { alive: false, pend: [] };
lib.pushInput(dead, 1);
assert.deepEqual(dead.pend, [], "dead pilgrims ignored");
lib.pushInput(undefined, 1); // an id with no seat must not throw
});
test("referee: incumbent decision table", () => {
for (const c of referee.incumbent) {
assert.equal(Boolean(lib.claimDecision(c.myLive, c.theirLive, c.myId, c.from)), c.iWin, JSON.stringify(c));
}
});
test("referee: bystander adoption table", () => {
for (const c of referee.bystander) {
assert.equal(
Boolean(lib.bystanderClaimWins(c.theirLive, c.incumbentLive, c.from, c.hostId)),
c.challengerWins,
JSON.stringify(c),
);
}
});
test("referee: host-loss election", () => {
for (const c of referee.election) {
assert.equal(lib.electHost(c.myId, [...c.open], c.gone), c.next, JSON.stringify(c));
}
});
test("free color picks the first unused index", () => {
for (const c of scoring.freeColors) {
assert.equal(lib.freeColorOf([...c.used]), c.out, JSON.stringify(c));
}
});
test("the round tally matches TS, ties included", () => {
for (const c of scoring.tallies) {
const grid = new Uint8Array(GW * GH);
let at = 0;
for (const [seat, n] of c.paint) for (let k = 0; k < n; k++) grid[at++] = seat + 1;
const [counts, seat] = lib.tally(grid, c.counts.length);
assert.deepEqual(plain(counts), c.counts, `${c.name} counts`);
// the fixture records the winner's peer id; seats are ids' positions
const expectedSeat = c.w === null ? -1 : c.scores.findIndex(([id]) => id === c.w);
assert.equal(seat, expectedSeat, `${c.name} winner seat`);
}
});
test("the daylight clock formats as TS did", () => {
for (const c of scoring.clocks) {
assert.equal(lib.clockText(c.left, c.ms), c.text, JSON.stringify(c));
}
});
|