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 | // Golden-vector tests for the referee: host-claim seniority, the "ro"
// takeover decision table, seat grants, host loss and the frame-authority
// gate. Fixtures recorded off the real src/game/game.ts while it was canon —
// the whole-brain scenarios were captured by driving handleMsg through a fake
// NetLike, and each one is reduced here to the pure decision it turns on.
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 v = JSON.parse(readFileSync(new URL("./vectors/referee.json", import.meta.url), "utf8"));
const MY_ID = "12D3KooWAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
test("the age slack the whole seniority rule turns on is still 10 s", () => {
assert.equal(lib.ageSlackS, v.ageSlackS);
assert.equal(lib.ageSlackS, 10);
});
test("incumbentWins: older host wins, near-ties break to the lowest id", () => {
for (const c of v.incumbentWins) {
assert.equal(
lib.incumbentWins(c.myAge, c.theirAge, c.myId, c.theirId),
c.wins,
`${c.myAge} vs ${c.theirAge}, ${c.myId.slice(-4)} vs ${c.theirId.slice(-4)}`,
);
}
});
test("normInst maps any wire value onto a legal instrument", () => {
for (const c of v.normInst) {
const input = c.in === "NaN" ? NaN : c.in === "Infinity" ? Infinity : c.in;
assert.equal(lib.normInst(input), c.out, `normInst(${c.in})`);
}
});
test("clampBpm holds the transport inside 60..200 and truncates", () => {
for (const c of v.clampBpm) assert.equal(lib.clampBpm(c.in), c.out, `clampBpm(${c.in})`);
});
test("freeColor picks the first unused stage light, then wraps by roster size", () => {
assert.equal(v.colorsLength, 12);
for (const c of v.freeColor) {
assert.equal(lib.freeColorOf(c.used, c.size), c.free, `used=${JSON.stringify(c.used)}`);
}
});
test("musicianCount ignores the audience", () => {
for (const c of v.musicianCount) {
assert.equal(lib.musicianCountOf(c.insts), c.count, JSON.stringify(c.insts));
}
});
test("the hello seat grant sends late arrivals to the house, incumbents keep theirs", () => {
assert.equal(v.maxMusicians, 8);
for (const c of v.helloGrant) {
if (c.granted === null) continue; // a refused hello (blank nick / not host)
const musicians = c.seats.filter((i) => i !== 4).length + (c.seatFrom !== null && c.seatFrom !== 4 ? 1 : 0);
assert.equal(
lib.grantInst(c.wanted, c.seatFrom, musicians),
c.granted,
`${c.name}: wanted ${c.wanted} with ${musicians} on stage`,
);
}
});
test("a blank stage name and a hello at a non-host are both ignored", () => {
for (const name of ["blank-nick", "not-host"]) {
const c = v.helloGrant.find((e) => e.name === name);
assert.ok(c, name);
assert.equal(c.granted, null);
assert.deepEqual(c.emitted, []);
}
});
// Each recorded "ro" scenario is mapped back to the branch the TS took, from
// what it emitted and where hostship landed. roAction must agree.
const actionOf = (c) => {
if (c.emitted.some(([to, t]) => to === "*" && t === "ro")) return "reassert";
if (c.emitted.some(([, t]) => t === "hi")) {
return c.before.hostId === MY_ID ? "concede" : "adopt";
}
if (c.hostAfter === c.frame.host) return "apply";
return "ignore";
};
test("the roster/takeover decision table matches the TS branch for branch", () => {
for (const c of v.rosterClaims) {
const amHost = c.before.hostId === MY_ID;
const hostInRoom = c.before.hostId !== null && c.inRoom.includes(c.before.hostId);
assert.equal(
lib.roAction(c.frame.from, c.frame.host, MY_ID, c.before.hostId, amHost, hostInRoom,
c.before.myAge, c.frame.age),
actionOf(c),
c.name,
);
}
});
test("every branch of the decision table is actually exercised", () => {
const seen = new Set(v.rosterClaims.map(actionOf));
assert.deepEqual([...seen].sort(), ["adopt", "apply", "concede", "ignore", "reassert"]);
});
test("host loss elects the lowest surviving id", () => {
for (const c of v.hostLoss) {
if (c.hostBefore !== c.gone) continue; // a non-host departure elects nobody
assert.equal(lib.electHost(MY_ID, [...c.open], c.gone), c.hostAfter, c.name);
}
const nonHost = v.hostLoss.find((c) => c.name === "non-host-departs");
assert.equal(nonHost.hostAfter, nonHost.hostBefore);
});
test("a departing peer's loops and theremin voice go with them", () => {
for (const c of v.hostLoss) {
assert.deepEqual(c.loopsAfter, ["y.1"], c.name); // x.1 was the departed peer's
assert.deepEqual(c.thereminAfter, [], c.name);
}
});
test("setInstrument's stage-full guard only gates stepping up from the audience", () => {
for (const c of v.setInstrument) {
const musicians = c.seats.filter((i) => i !== 4).length + (c.myInstBefore !== 4 ? 1 : 0);
const allowed = lib.canTakeSeat(c.want, c.myInstBefore, musicians);
const tookIt = c.myInstAfter === c.want;
// "same-instrument-noop" never reaches the guard at all
if (c.want === c.myInstBefore) {
assert.equal(tookIt, true, c.name);
continue;
}
assert.equal(allowed, tookIt, c.name);
assert.equal(c.toasts.includes("toast.stage-full"), !allowed, c.name);
}
});
test('"lp" admission: bar counts outside {1,2,4} fall back to 2', () => {
for (const c of v.loopAdmission) {
const inst = lib.normInst(c.i);
const admitted = inst !== 4 && c.n >= 0 && c.n <= 256;
assert.equal(c.stored.length === 1, admitted, c.name);
if (admitted) {
assert.equal(lib.loopLenOrDefault(c.len), c.stored[0][3], c.name);
assert.equal(inst, c.stored[0][2], c.name);
}
}
});
test('"lx" only removes the sender\'s own loop, and clears its local mute', () => {
for (const c of v.loopRemoval) {
const removed = c.loopsAfter.length === 0;
assert.equal(removed, c.name === "owner-removes");
// remove-loop! drops the mute with the loop; a refused removal keeps it
assert.deepEqual(c.mutedAfter, removed ? [] : ["BBBBBB.1"], c.name);
}
});
test("transport and pong frames are trusted only from the host or my loopback", () => {
const gate = (name) => v.hostGate.find((e) => e.name === name);
assert.equal(lib.fromHost("B", "B", "A"), true);
assert.equal(lib.fromHost("A", "B", "A"), true);
assert.equal(lib.fromHost("C", "B", "A"), false);
// and the fixture agrees about the effect
assert.equal(gate("tr-from-host").bpm, 200); // clamped from 5000
assert.equal(gate("tr-from-self").bpm, 200);
assert.equal(gate("tr-from-stranger").bpm, 100); // untouched
assert.equal(gate("po-from-host").synced, true);
assert.equal(gate("po-from-stranger").synced, false);
assert.equal(gate("po-from-stranger").rtt, null); // Infinity, through JSON
});
test("stale theremin voices are reaped after 5 s of silence", () => {
// 594_999 is older than the 5 s cutoff at now=600_000; 595_001 is not
assert.equal(v.reapTheremins.kept.length, 1);
assert.ok(v.reapTheremins.kept[0].endsWith("CCCC"));
});
test("room-full is the only relay error the game treats as fatal", () => {
assert.deepEqual(v.relayError.roomFull, [["showFatal", "fatal.room-full", "fatal.room-full.sub"]]);
assert.deepEqual(v.relayError.other, []);
});
|