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 | // The late-joiner snapshot and the client's apply path. Fixtures recorded off
// the real sendSnap/applyStart/applyTick/applySnap while the TS was canon — a
// mid-round joiner must see exactly the host's hill, and every client must
// track the host's rings and lanterns tick for tick.
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 { snapshots, clientRuns } = JSON.parse(
readFileSync(new URL("./vectors/snapshot.json", import.meta.url), "utf8"),
);
const { S } = JSON.parse(readFileSync(new URL("./vectors/sim.json", import.meta.url), "utf8"));
const plain = (x) => JSON.parse(JSON.stringify(x));
for (const s of snapshots) {
test(`snapshot ${s.name}: sendSnap serializes byte-identically to TS`, () => {
const f = s.frame;
assert.equal(
JSON.stringify(
lib.mkSn(f.round, f.tickMs, f.ticks, f.n, f.order, lib.mkLevel(f.level.stairs, f.level.rots, f.level.auto), f.rots, f.ps),
),
s.json,
);
});
test(`snapshot ${s.name}: level.rots is empty — the live offsets ride top level`, () => {
// the one shape trap in this frame: a late joiner that read level.rots
// would rebuild the hill at the round's OPENING rotations, not the current
assert.deepEqual(s.frame.level.rots, []);
assert.equal(s.frame.rots.length, 6);
assert.deepEqual(s.applied.rots, s.frame.rots);
assert.deepEqual(s.applied.rotc, s.frame.rots);
assert.deepEqual(s.applied.visRots, s.frame.rots);
});
test(`snapshot ${s.name}: the rebuilt view matches the host's climbers`, () => {
assert.equal(s.applied.spectator, true, "a late joiner spectates until the next song");
assert.equal(s.applied.phase, "playing");
assert.equal(s.applied.tickN, s.frame.n);
assert.deepEqual(s.applied.order, s.frame.order);
assert.deepEqual(s.applied.stairs, s.frame.level.stairs);
s.applied.pg.forEach((p, i) => {
const [ring, slot, alive] = s.frame.ps[i];
assert.equal(p.ring, ring, `seat ${i} ring`);
assert.equal(p.slot, slot, `seat ${i} slot`);
assert.equal(p.alive, Boolean(alive), `seat ${i} alive`);
assert.equal(p.tgtA, slot, `seat ${i} render angle starts at its slot`);
});
assert.equal(s.applied.clock, lib.clockText(s.frame.ticks - s.frame.n, s.frame.tickMs));
});
}
/* ---------------- the client apply path ---------------- */
// applyStart's pg construction, and the roster the generator seeded, both
// re-declared 1:1 with the extraction harness.
const ROSTER_NICKS = ["zed", "rex"];
function startPg(st) {
return st.order.map((id, i) => ({
id,
color: i,
nick: ROSTER_NICKS[i],
ring: 0,
slot: st.starts[i],
alive: true,
tgtA: st.starts[i],
}));
}
for (const run of clientRuns) {
test(`client apply: ${run.name}`, () => {
const st = run.st;
const pg = startPg(st);
const rots = [...st.level.rots];
const rotc = [...st.level.rots];
let clock = lib.clockText(st.ticks, st.tickMs);
const state = () => ({
rots: [...rots],
rotc: [...rotc],
pg: pg.map((p) => ({
id: p.id, color: p.color, nick: p.nick,
ring: p.ring, slot: p.slot, alive: p.alive, tgtA: p.tgtA,
})),
clock,
});
// the generator's own record of the state right after "st"
assert.deepEqual(state().rots, run.after_start.rots, "rots after start");
assert.deepEqual(state().rotc, run.after_start.rotc, "rotc after start");
assert.deepEqual(state().pg, run.after_start.pg, "pg after start");
assert.equal(clock, run.after_start.clock, "clock after start");
for (const { frame, state: want } of run.ticks) {
for (const [r, dir] of frame.rt ?? []) lib.applyTurn(pg, rots, rotc, r, dir);
for (const [seat, ring, slot] of frame.mv ?? []) lib.applyMove(pg, seat, ring, slot);
if (frame.fp) lib.applyFingerprint(pg, frame.fp);
if (frame.n % 8 === 0) clock = lib.clockText(st.ticks - frame.n, st.tickMs);
const got = state();
assert.deepEqual(got.rots, want.rots, `tick ${frame.n} rots`);
assert.deepEqual(got.rotc, want.rotc, `tick ${frame.n} rotc`);
assert.deepEqual(got.pg, want.pg, `tick ${frame.n} pg`);
assert.equal(got.clock, want.clock, `tick ${frame.n} clock`);
}
});
}
test("applyMove reports climbs, steps and skipped seats", () => {
const pg = [
{ id: "a", ring: 0, slot: 0, alive: true, tgtA: 0 },
{ id: "b", ring: 0, slot: 4, alive: false, tgtA: 4 },
];
assert.equal(lib.applyMove(pg, 0, 0, 1), 0, "a step around the ring");
assert.equal(lib.applyMove(pg, 0, 1, 1), 1, "a change of ring");
assert.equal(lib.applyMove(pg, 1, 0, 5), -1, "a gutted lantern is skipped");
assert.equal(lib.applyMove(pg, 7, 0, 5), -1, "an empty seat is skipped");
assert.deepEqual(pg[1], { id: "b", ring: 0, slot: 4, alive: false, tgtA: 4 }, "skipped seats untouched");
});
test("applyTurn drags only the live climbers on that ring", () => {
const pg = [
{ id: "a", ring: 1, slot: 2, alive: true, tgtA: 2 },
{ id: "b", ring: 1, slot: 11, alive: true, tgtA: 11 },
{ id: "c", ring: 1, slot: 5, alive: false, tgtA: 5 },
{ id: "d", ring: 0, slot: 5, alive: true, tgtA: 5 },
];
const rots = [0, 0, 0, 0, 0, 0];
const rotc = [0, 0, 0, 0, 0, 0];
lib.applyTurn(pg, rots, rotc, 1, 1);
assert.deepEqual(rots, [0, 1, 0, 0, 0, 0]);
assert.deepEqual(rotc, [0, 1, 0, 0, 0, 0]);
assert.deepEqual(pg.map((p) => [p.slot, p.tgtA]), [[3, 3], [0, 12], [5, 5], [5, 5]]);
// rotc accumulates past the seam so the render angle never snaps
for (let i = 0; i < 12; i++) lib.applyTurn(pg, rots, rotc, 1, -1);
assert.equal(rots[1], 1);
assert.equal(rotc[1], -11);
});
test("applyFingerprint heals drift and revives/guts by the host's truth", () => {
const pg = [
{ id: "a", ring: 0, slot: 0, alive: true, tgtA: 0 },
{ id: "b", ring: 3, slot: 7, alive: false, tgtA: 7 },
];
lib.applyFingerprint(pg, [[2, 11, 1], [3, 7, 1]]);
assert.deepEqual(pg[0], { id: "a", ring: 2, slot: 11, alive: true, tgtA: -1 }, "0 -> 11 wraps backwards");
assert.deepEqual(pg[1], { id: "b", ring: 3, slot: 7, alive: true, tgtA: 7 }, "same slot: no angle nudge");
lib.applyFingerprint(pg, [[2, 11, 0], [3, 7, 0]]);
assert.equal(pg[0].alive, false);
assert.equal(pg[1].alive, false);
lib.applyFingerprint(pg, [[0, 0, 1], [0, 0, 1], [0, 0, 1]]); // more seats than climbers must not throw
});
test("wrapDelta takes the short way round, and pins the half-turn edge", () => {
assert.equal(lib.wrapDelta(0, 1), 1);
assert.equal(lib.wrapDelta(1, 0), -1);
assert.equal(lib.wrapDelta(0, 11), -1);
assert.equal(lib.wrapDelta(11, 0), 1);
assert.equal(lib.wrapDelta(0, 0), 0);
// exactly half a turn: S/2 is NOT reduced (d > S/2 is strict), -S/2 is not either
assert.equal(lib.wrapDelta(0, 6), 6);
assert.equal(lib.wrapDelta(6, 0), -6);
assert.equal(lib.wrapDelta(0, 7), -5);
assert.equal(lib.wrapDelta(7, 0), 5);
for (let a = 0; a < S; a++) {
for (let b = 0; b < S; b++) {
const d = lib.wrapDelta(a, b);
assert.ok(Math.abs(d) <= S / 2, `|${a}->${b}| = ${d}`);
assert.equal(((a + d) % S + S) % S, b, `${a} + ${d} == ${b}`);
}
}
});
|