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 | // LampionClient, against dist/ — the two 2.0.1 fixes.
//
// 1. claimBeats. Lampion browsers put `live: 1` on their "ro" while they host a
// round, so the base lowest-id rule in room.cljs would partition a mixed
// bot/browser room. The truth table below is game5's OWN answer
// (sim/bystander-claim-wins), recorded in the fixture, and every row is
// reached through the wire — the phase is driven by real frames, never by
// poking _phase. The same table is replayed against NeonGridClient, whose
// override is the one this port matched.
//
// 2. World vs local slots. `_slot`, "tk".mv/.fp and "sn".ps carry WORLD slots;
// `level.stairs[r]` holds LOCAL ones, and ring r's world slot is its local
// slot plus rots[r] mod S. The replays feed the exact frames a game5 host
// emits and assert the client's (_ring, _slot, _rots) equals what game5's own
// apply-turn!/apply-move!/apply-fingerprint! produce — the "sn" run also
// covers the trap that "sn".level.rots is EMPTY and the live set rides at the
// frame's top level.
//
// Fixture provenance: test/vectors/generate-lampion.mjs (imports game5's
// compiled client as ground truth).
import test from "node:test";
import assert from "node:assert/strict";
import { EventEmitter } from "node:events";
import { readFileSync } from "node:fs";
import { LampionClient } from "../dist/games/lampion.js";
import { NeonGridClient } from "../dist/games/neon-grid.js";
const V = JSON.parse(readFileSync(new URL("./vectors/lampion-client.json", import.meta.url), "utf8"));
const A = V.actions;
class MockPeer extends EventEmitter {
constructor(id) {
super();
this.id = id;
this.sent = []; // [to, frame]
this.net = {
roomPeers: () => new Set(),
openPeers: () => [],
relayConnected: () => true,
roomId: "testroom",
wake: () => {},
};
}
get myId() {
return this.id;
}
send(to, frame) {
this.sent.push([to, frame]);
}
broadcast(frame) {
this.sent.push(["*", frame]);
}
identityOf() {
return null;
}
close() {
return Promise.resolve();
}
}
/** A client seated at V.seat, following `host`. Returns [client, peer]. */
const seated = (Cls, host, opts = {}) => {
const peer = new MockPeer(V.order[V.seat]);
const client = new Cls(peer, { app: "lampion", roomSecret: "s", nick: "bot", keepAliveMs: 0, ...opts });
peer.emit("message", host, { t: "ro", host, ps: [[host, "gazda", 1, 0]] });
assert.equal(client.hostId, host);
peer.sent.length = 0; // drop the "hi" the roster provoked
return [client, peer];
};
const feed = (peer, host, frame) => peer.emit("message", host, structuredClone(frame));
/** The "in" frames the client sent since the marker, as action codes. */
const actionsSince = (peer, mark) =>
peer.sent.slice(mark).filter(([, f]) => f && f.t === "in").map(([, f]) => f.a);
/* ============================ 1. the claim comparator ====================== */
// The frames that put a client into each phase, per game. Only `t` matters
// beyond the fields each handler reads.
const DRIVE = {
lampion: {
lo: [{ t: "lo" }],
st: [{ t: "st", round: 1, tickMs: 125, ticks: 720, order: [], level: V.level, starts: [] }],
tk: [{ t: "st", round: 1, tickMs: 125, ticks: 720, order: [], level: V.level, starts: [] },
{ t: "tk", n: 1 }],
ov: [{ t: "ov", w: null, tops: [], scores: [] }],
ma: [{ t: "ma", w: "someone", scores: [] }],
},
"neon-grid": {
lo: [{ t: "lo" }],
st: [{ t: "st", round: 1, tickMs: 90, order: [], starts: [] }],
tk: [{ t: "st", round: 1, tickMs: 90, order: [], starts: [] }, { t: "tk", n: 1, h: [] }],
ov: [{ t: "ov", w: null, scores: [] }],
ma: [{ t: "ma", w: "someone", scores: [] }],
},
};
for (const [app, Cls] of [["lampion", LampionClient], ["neon-grid", NeonGridClient]]) {
test(`${app}: claimBeats is live-beats-idle, ties to lowest id (game5's own table)`, () => {
for (const row of V.claims) {
const [client, peer] = seated(Cls, row.host, { app });
for (const f of DRIVE[app][row.drive] ?? []) feed(peer, row.host, f);
const claim = { t: "ro", host: row.from, ps: [] };
if (row.theirLive) claim.live = 1;
assert.equal(
client.claimBeats(row.from, claim), row.out,
`${app} phase=${row.phase} theirLive=${row.theirLive} from=${row.from} host=${row.host}`,
);
}
});
}
test("lampion: an unknown host is always outranked, and the base rule is genuinely overridden", () => {
const peer = new MockPeer(V.order[V.seat]);
const client = new LampionClient(peer, { app: "lampion", roomSecret: "s", nick: "bot", keepAliveMs: 0 });
assert.equal(client.hostId, null);
assert.equal(client.claimBeats("zzz", { t: "ro", host: "zzz", ps: [] }), true);
assert.equal(client.claimBeats("zzz", { t: "ro", host: "zzz", ps: [], live: 1 }), true);
// The rows the plain lowest-id default in room.cljs gets WRONG — proof the
// override is doing work and not just restating the inherited rule.
const differs = V.claims.filter((r) => r.out !== (r.from < r.host));
assert.ok(differs.length >= 6, `expected the tables to diverge, got ${differs.length} rows`);
for (const row of differs) {
const [c, p] = seated(LampionClient, row.host);
for (const f of DRIVE.lampion[row.drive] ?? []) feed(p, row.host, f);
const claim = { t: "ro", host: row.from, ps: [] };
if (row.theirLive) claim.live = 1;
assert.equal(c.claimBeats(row.from, claim), row.out);
assert.notEqual(c.claimBeats(row.from, claim), row.from < row.host);
}
});
/* ============================ 2. world/local slots ======================== */
/** Replay `rows` into a seated client and check it against game5's fold. */
const replay = (client, peer, host, rows, label) => {
for (const row of rows) {
const mark = peer.sent.length;
feed(peer, host, row.frame);
assert.equal(client._ring, row.ring, `${label} n=${row.frame.n}: ring`);
assert.equal(client._slot, row.slot, `${label} n=${row.frame.n}: world slot`);
assert.deepEqual([...client._rots], row.rots, `${label} n=${row.frame.n}: rots`);
const acts = actionsSince(peer, mark);
if (!row.send) {
assert.deepEqual(acts, [], `${label} n=${row.frame.n}: cooling down, must not act`);
} else {
assert.equal(acts.length, 1, `${label} n=${row.frame.n}: exactly one action`);
if (row.send.a !== null) {
assert.equal(acts[0], row.send.a, `${label} n=${row.frame.n}: action`);
}
}
}
};
test('lampion: "st" seeds rots from the level and every "tk" folds rt/mv/fp like game5', () => {
const host = "12D3KooWHostaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const [client, peer] = seated(LampionClient, host);
feed(peer, host, V.fromStart.st);
assert.equal(client._ring, V.fromStart.expectAtStart.ring);
assert.equal(client._slot, V.fromStart.expectAtStart.slot);
assert.deepEqual([...client._rots], V.fromStart.expectAtStart.rots);
assert.notDeepEqual([...client._rots], [0, 0, 0, 0, 0, 0], "the fixture level must have live rotations");
replay(client, peer, host, V.fromStart.ticks, "fromStart");
// and the heuristic actually gets somewhere: a rotation-blind bot walks away
// from its stairs, so climbing at all is the behavioural signal
const rings = new Set(V.fromStart.ticks.map((r) => r.ring));
assert.ok(Math.max(...rings) >= 2, "the recorded climb must leave the base ring");
assert.equal(client._ring, V.fromStart.ticks.at(-1).ring);
});
test('lampion: an "sn" late join takes its live rotations from the frame\'s top level', () => {
const host = "12D3KooWHostaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const [client, peer] = seated(LampionClient, host);
const sn = V.fromSnap.sn;
// the trap this test exists for
assert.deepEqual(sn.level.rots, [], "game5's send-snap leaves level.rots empty");
assert.notDeepEqual(sn.rots, [0, 0, 0, 0, 0, 0]);
feed(peer, host, sn);
assert.equal(client._ring, V.fromSnap.expectAtSnap.ring);
assert.equal(client._slot, V.fromSnap.expectAtSnap.slot);
assert.deepEqual([...client._rots], V.fromSnap.expectAtSnap.rots,
"level.rots is empty — the live set is the top-level one");
replay(client, peer, host, V.fromSnap.ticks, "fromSnap");
const rings = new Set(V.fromSnap.ticks.map((r) => r.ring));
assert.ok(Math.max(...rings) >= 3, "the late joiner must climb too");
});
test('lampion: "tk".rt drags the client around its own ring, in both directions', () => {
const host = "12D3KooWHostaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
// the fixture must actually exercise the drag path, or the assertions above
// could pass on a client that ignores rt for its own position
const dirs = new Set();
let dragged = 0;
for (const src of [V.fromStart, V.fromSnap]) {
let ring = src.expectAtStart ? src.expectAtStart.ring : src.expectAtSnap.ring;
for (const row of src.ticks) {
for (const e of row.frame.rt ?? []) {
dirs.add(e[1]);
if (e[0] === ring) dragged++;
}
ring = row.ring;
}
}
assert.deepEqual([...dirs].sort(), [-1, 1], "both rotation directions must appear");
assert.ok(dragged >= 2, `the client's own ring must turn under it (got ${dragged})`);
// and a direct check: a lone rt on my ring moves my world slot but not my ring
const [client, peer] = seated(LampionClient, host);
feed(peer, host, {
t: "st", round: 1, tickMs: 125, ticks: 720, order: V.order,
level: { stairs: V.level.stairs, rots: [0, 0, 0, 0, 0, 0], auto: V.level.auto },
starts: [5, 0, 0],
});
assert.equal(client._slot, 5);
feed(peer, host, { t: "tk", n: 1, rt: [[0, 1, 1]] });
assert.equal(client._slot, 6);
assert.deepEqual([...client._rots], [1, 0, 0, 0, 0, 0]);
feed(peer, host, { t: "tk", n: 2, rt: [[0, -1, 1], [0, -1, 2]] });
assert.equal(client._slot, 4);
assert.deepEqual([...client._rots], [11, 0, 0, 0, 0, 0]);
// a turn on a ring I am NOT standing on moves the offset only
feed(peer, host, { t: "tk", n: 3, rt: [[2, 1, -1]] });
assert.equal(client._slot, 4);
assert.deepEqual([...client._rots], [11, 0, 1, 0, 0, 0]);
});
test("lampion: the slot the bot aims at is its local stair converted to world, for every offset", () => {
const host = "12D3KooWHostaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const stairsAt = (local) => [[local], [0], [0], [0], [0], []];
const ringDist = (a, b) => {
const d = Math.abs(a - b) % V.S;
return Math.min(d, V.S - d);
};
let onStair = 0;
let offStair = 0;
for (const [off, world, , local] of V.worldLocal) {
// `world` is my world slot; `local` is what it is called on ring 0 under
// this offset (game5's localOf). Standing on the stair => climb.
// +3 is the short way clockwise, +8 the short way counter-clockwise
for (const stair of [local, (local + 3) % V.S, (local + 8) % V.S]) {
const [client, peer] = seated(LampionClient, host);
const mark = peer.sent.length;
feed(peer, host, {
t: "st", round: 1, tickMs: 125, ticks: 720, order: V.order,
level: { stairs: stairsAt(stair), rots: Array(V.RINGS).fill(off), auto: V.level.auto },
starts: [world, 0, 0],
});
feed(peer, host, { t: "tk", n: 1 });
const acts = actionsSince(peer, mark);
assert.equal(acts.length, 1);
if (stair === local) {
onStair++;
assert.equal(acts[0], A.UP, `off=${off} world=${world} stair=${stair}: aligned, must climb`);
} else {
offStair++;
// the target is the stair's WORLD slot; the step is the short way round
const target = (stair + off) % V.S;
const cw = ((target - world) % V.S + V.S) % V.S;
assert.equal(acts[0], cw <= 6 ? A.CW : A.CCW,
`off=${off} world=${world} stair=${stair}: must step toward world slot ${target}`);
assert.ok(ringDist(world, target) > 0);
}
}
}
assert.equal(onStair, V.S * V.S);
assert.equal(offStair, 2 * V.S * V.S);
});
|