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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 | // Golden-vector tests for the host simulation core, the hill's own rules and
// the referee decisions — fixtures extracted from src/game/{game,state,ui}.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 plain = (x) => JSON.parse(JSON.stringify(x));
const A = sim.actions;
/* ---------------- constants ---------------- */
test("the constants the whole simulation is built from have not drifted", () => {
assert.equal(sim.S, 12);
assert.deepEqual(sim.LENS, [12, 9, 8, 6, 5, 1]);
assert.equal(sim.RINGS, 6);
assert.equal(sim.TOP, 5);
assert.deepEqual(sim.STAIR_COUNT, [2, 2, 2, 1, 1]);
assert.equal(sim.TICK_MS, 125);
assert.equal(sim.ROUND_TICKS, 720);
assert.equal(sim.WIN_ROUNDS, 3);
assert.equal(sim.MOVE_EVERY, 2);
assert.equal(sim.ROT_CD, 24);
assert.deepEqual(sim.AUTO_PERIOD, [56, 44, 36, 30, 24, 0]);
assert.deepEqual(A, { A_CCW: 0, A_CW: 1, A_UP: 2, A_DOWN: 3, A_ROT_CCW: 4, A_ROT_CW: 5 });
});
/* ---------------- local <-> world, and the hill's rules ---------------- */
test("worldOf / localOf match TS for every ring, slot and rotation set", () => {
for (const [ri, r, v, world, back] of sim.worldLocal) {
const rots = sim.rotSets[ri];
assert.equal(lib.worldOf(v, r, rots), world, `worldOf ${v} ring ${r} rots ${ri}`);
assert.equal(lib.localOf(v, r, rots), back, `localOf ${v} ring ${r} rots ${ri}`);
}
// and they are inverses
for (const rots of sim.rotSets) {
for (let r = 0; r < sim.RINGS; r++) {
for (let v = 0; v < sim.S; v++) {
assert.equal(lib.localOf(lib.worldOf(v, r, rots), r, rots), v);
}
}
}
});
test("canStand matches TS, off-ring indices included", () => {
for (const [ri, r, w, out] of sim.standTable) {
assert.equal(Boolean(lib.canStand(r, w, sim.rotSets[ri])), Boolean(out), `r=${r} w=${w} rots=${ri}`);
}
});
test("canClimb matches TS — a stair only connects when both rings line up", () => {
for (const [ri, si, r, w, out] of sim.climbTable) {
assert.equal(
Boolean(lib.canClimb(r, w, sim.stairSets[si], sim.rotSets[ri])),
Boolean(out),
`r=${r} w=${w} stairs=${si} rots=${ri}`,
);
}
});
test("canDescend matches TS", () => {
for (const [ri, si, r, w, out] of sim.descendTable) {
assert.equal(
Boolean(lib.canDescend(r, w, sim.stairSets[si], sim.rotSets[ri])),
Boolean(out),
`r=${r} w=${w} stairs=${si} rots=${ri}`,
);
}
});
/* ---------------- round setup ---------------- */
test("spawn slots match TS for every roster size", () => {
for (const [n, starts] of Object.entries(sim.spawns)) {
assert.deepEqual(plain(lib.spawnStarts(Number(n))), starts, `N=${n}`);
}
});
// The same mulberry32 the extraction harness installed as Math.random, so the
// CLJS roll replays the TS draw order exactly: STAIR_COUNT draws per ring,
// then RINGS rotation draws, then two per turning ring.
function mulberry32(seed) {
let a = seed >>> 0;
return () => {
a = (a + 0x6d2b79f5) >>> 0;
let t = a;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
test("rollLevel draws the same hill as TS, call for call", () => {
for (const { seed, level } of sim.levels) {
const [stairs, rots, auto] = plain(lib.rollLevel(mulberry32(seed)));
assert.deepEqual(stairs, level.stairs, `seed ${seed} stairs`);
assert.deepEqual(rots, level.rots, `seed ${seed} rots`);
assert.deepEqual(auto, level.auto, `seed ${seed} auto`);
}
});
test("rollLevel's invariants hold for any seed", () => {
for (let seed = 1; seed <= 200; seed++) {
const [stairs, rots, auto] = plain(lib.rollLevel(mulberry32(seed)));
assert.equal(stairs.length, sim.RINGS);
assert.deepEqual(stairs[sim.TOP], [], "the summit stage has no stairs of its own");
for (let r = 0; r < sim.TOP; r++) {
assert.equal(stairs[r].length, sim.STAIR_COUNT[r], `ring ${r} stair count`);
assert.deepEqual(stairs[r], [...stairs[r]].sort((a, b) => a - b), `ring ${r} sorted`);
assert.equal(new Set(stairs[r]).size, stairs[r].length, `ring ${r} distinct`);
for (const s of stairs[r]) assert.ok(s >= 0 && s < sim.LENS[r], `ring ${r} stair ${s} on the arc`);
}
assert.equal(rots.length, sim.RINGS);
for (const v of rots) assert.ok(Number.isInteger(v) && v >= 0 && v < sim.S);
assert.deepEqual(auto[sim.TOP], [0, 1], "the summit never turns on its own");
for (let r = 0; r < sim.TOP; r++) {
assert.ok(auto[r][0] >= 8, `ring ${r} period floor`);
assert.ok([1, -1].includes(auto[r][1]), `ring ${r} direction`);
}
}
});
/* ---------------- host input queue ---------------- */
test("host input queue: cap 3, only the six action codes, dead lanterns ignored", () => {
for (const c of sim.inputQueue) {
const p = { alive: c.alive, pend: [...c.init], ring: 0, slot: 0 };
for (const a of c.feed) lib.pushInput(p, a);
assert.deepEqual(p.pend, c.pend, c.name);
}
lib.pushInput(undefined, 1); // an id with no seat must not throw
lib.pushInput(null, 1);
});
/* ---------------- the host tick ---------------- */
const hcOf = (o) => ({
id: o.id,
ring: o.ring ?? 0,
slot: o.slot ?? 0,
pend: [...(o.pend ?? [])],
alive: o.alive ?? true,
lastMove: o.lastMove ?? -99,
lastRot: o.lastRot ?? -99,
best: o.best ?? 0,
bestAt: o.bestAt ?? 0,
});
const snapHC = (p) => ({
id: p.id,
ring: p.ring,
slot: p.slot,
pend: [...p.pend],
alive: p.alive,
lastMove: p.lastMove,
lastRot: p.lastRot,
best: p.best,
bestAt: p.bestAt,
});
for (const sc of sim.scenarios) {
test(`host tick: ${sc.name}`, () => {
const climbers = sc.spec.climbers.map(hcOf);
const stairs = sc.spec.stairs.map((a) => [...a]);
const rots = [...sc.spec.rots];
const auto = sc.spec.auto.map((a) => [...a]);
// the client view the host also keeps, rebuilt exactly as applyStart does
const pg = climbers.map((p, i) => ({
id: p.id, color: i, nick: `n${i}`,
ring: p.ring, slot: p.slot, alive: p.alive, tgtA: p.slot,
}));
const rotc = [...rots];
const clientRots = [...rots];
let ov = null;
for (const expected of sc.ticks) {
const n = expected.n;
const [mv, rt] = lib.tickStep(climbers, stairs, rots, auto, n);
const fp = lib.heartbeat(climbers, n);
assert.deepEqual(mv.length ? plain(mv) : null, expected.mv, `tick ${n} mv`);
assert.deepEqual(rt.length ? plain(rt) : null, expected.rt, `tick ${n} rt`);
assert.deepEqual(fp == null ? null : plain(fp), expected.fp, `tick ${n} fp`);
// the client applies the same delta and must land on the host's truth
for (const [r, dir] of rt) lib.applyTurn(pg, clientRots, rotc, r, dir);
for (const [seat, ring, slot] of mv) lib.applyMove(pg, seat, ring, slot);
if (fp) lib.applyFingerprint(pg, fp);
if (ov === null && lib.roundOver(climbers, n)) {
const w = lib.roundWinner(climbers);
ov = { w: w ?? null, tops: climbers.map((p) => p.best) };
}
}
assert.deepEqual(climbers.map(snapHC), sc.climbers, "final climbers");
assert.deepEqual(rots, sc.rots, "final host rotations");
assert.deepEqual(clientRots, sc.rots, "the client's rotations track the host's");
assert.deepEqual(
pg.map((p) => ({
id: p.id, color: p.color, nick: p.nick,
ring: p.ring, slot: p.slot, alive: p.alive, tgtA: p.tgtA,
})),
sc.pg,
"final client view",
);
if (sc.ov) {
assert.ok(ov, `${sc.name} should have ended the round`);
assert.equal(ov.w, sc.ov.w, "round winner");
assert.deepEqual(ov.tops, sc.ov.tops, "round tops");
} else {
assert.equal(ov, null, `${sc.name} should NOT have ended the round`);
}
});
}
test("the fp heartbeat fires on every 16th tick and nowhere else", () => {
const climbers = [hcOf({ id: "a", ring: 1, slot: 2 })];
for (let n = 1; n <= 48; n++) {
const fp = lib.heartbeat(climbers, n);
assert.equal(fp == null, n % 16 !== 0, `n=${n}`);
}
});
test("roundOver fires on a live summiteer, or when the song runs out", () => {
const at = (ring, alive = true) => [hcOf({ id: "a", ring, alive })];
assert.equal(Boolean(lib.roundOver(at(sim.TOP), 1)), true, "summit");
assert.equal(Boolean(lib.roundOver(at(sim.TOP, false), 1)), false, "a gutted lantern on the summit does not end it");
assert.equal(Boolean(lib.roundOver(at(3), sim.ROUND_TICKS - 1)), false);
assert.equal(Boolean(lib.roundOver(at(3), sim.ROUND_TICKS)), true, "the song ended");
});
/* ---------------- referee ---------------- */
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) {
const wins = Boolean(lib.bystanderClaimWins(c.theirLive, c.incumbentLive, c.from, c.hostId));
// the recorded outcome also adopts when the incumbent has left the room;
// the pure rule is the precedence half of that decision
assert.equal(wins || !c.incumbentPresent, c.challengerWins, JSON.stringify(c));
if (c.incumbentPresent) assert.equal(wins, 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));
}
});
/* ---------------- scoring, colours, clock ---------------- */
test("free colour picks the first unused index", () => {
for (const c of scoring.freeColors) {
assert.equal(lib.freeColorOf([...c.used]), c.out, JSON.stringify(c));
}
});
test("the moon clock formats as TS did", () => {
for (const c of scoring.clocks) {
assert.equal(lib.clockText(c.left, c.ms), c.text, JSON.stringify(c));
}
});
|