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 | /**
* In-memory room hub + a deterministic clock, shared by the black-box tests
* and by test/vectors/generate.mjs.
*
* `Hub`/`MockPeer` stand in for a RoomPeer over a relay: `send`/`broadcast`
* deliver synchronously (structured-cloned, so no accidental aliasing) and
* `net` answers the handful of queries GameRoom and HostDriver make.
*
* `FakeClock` replaces the global timer functions and the two clocks the
* clients read (`Date.now`, `performance.now`), so a hosted match produces the
* SAME wire trace on every run — which is what makes the driver traces usable
* as golden vectors.
*/
import { EventEmitter } from "node:events";
export class MockPeer extends EventEmitter {
constructor(hub, id) {
super();
this.hub = hub;
this.id = id;
this.sent = []; // [to|"*", frame]
this.net = {
roomPeers: () => new Set([...hub.peers.keys()].filter((p) => p !== id)),
openPeers: () => [...hub.peers.keys()].filter((p) => p !== id),
relayConnected: () => true,
roomId: "testroom",
wake: () => {},
};
}
get myId() {
return this.id;
}
send(to, frame) {
this.sent.push([to, frame]);
this.hub.peers.get(to)?.emit("message", this.id, structuredClone(frame));
}
broadcast(frame) {
this.sent.push(["*", frame]);
for (const [pid, p] of this.hub.peers) {
if (pid !== this.id) p.emit("message", this.id, structuredClone(frame));
}
}
identityOf() {
return null;
}
close() {
return Promise.resolve();
}
}
export class Hub {
peers = new Map();
join(id) {
const p = new MockPeer(this, id);
this.peers.set(id, p);
return p;
}
/** Open streams both ways between every pair (fires peerOpen). */
openAll() {
for (const [id, p] of this.peers) {
for (const [oid] of this.peers) if (oid !== id) p.emit("peerOpen", oid);
}
}
}
/** Await pending microtasks/immediates — real setImmediate is never faked. */
export const drain = async (rounds = 12) => {
for (let i = 0; i < rounds; i++) await new Promise((r) => setImmediate(r));
};
export class FakeClock {
#now = 1_700_000_000_000;
#seq = 0;
#timers = new Map(); // id -> {at, fn, every|null, seq}
#saved = null;
constructor({ install = true } = {}) {
if (install) this.install();
}
install() {
if (this.#saved) return;
this.#saved = {
setTimeout: globalThis.setTimeout,
clearTimeout: globalThis.clearTimeout,
setInterval: globalThis.setInterval,
clearInterval: globalThis.clearInterval,
dateNow: Date.now,
perfNow: globalThis.performance?.now?.bind(globalThis.performance),
};
const mk = (fn, delay, every) => {
const id = { id: ++this.#seq, unref: () => id, ref: () => id };
this.#timers.set(id, { at: this.#now + Math.max(0, delay | 0), fn, every, seq: this.#seq });
return id;
};
globalThis.setTimeout = (fn, delay) => mk(fn, delay, null);
globalThis.setInterval = (fn, delay) => mk(fn, delay, Math.max(1, delay | 0));
globalThis.clearTimeout = (id) => this.#timers.delete(id);
globalThis.clearInterval = (id) => this.#timers.delete(id);
Date.now = () => this.#now;
if (globalThis.performance) globalThis.performance.now = () => this.#now;
}
uninstall() {
if (!this.#saved) return;
globalThis.setTimeout = this.#saved.setTimeout;
globalThis.clearTimeout = this.#saved.clearTimeout;
globalThis.setInterval = this.#saved.setInterval;
globalThis.clearInterval = this.#saved.clearInterval;
Date.now = this.#saved.dateNow;
if (globalThis.performance && this.#saved.perfNow) globalThis.performance.now = this.#saved.perfNow;
this.#saved = null;
}
get now() {
return this.#now;
}
/** Advance the clock, firing due callbacks in (time, creation) order. */
tick(ms) {
const until = this.#now + ms;
for (;;) {
let next = null;
for (const [id, t] of this.#timers) {
if (t.at > until) continue;
if (!next || t.at < next[1].at || (t.at === next[1].at && t.seq < next[1].seq)) next = [id, t];
}
if (!next) break;
const [id, t] = next;
this.#now = t.at;
if (t.every === null) this.#timers.delete(id);
else t.at = this.#now + t.every;
t.fn();
}
this.#now = until;
}
/** Settle promise chains without advancing time (real setImmediate). */
drain(rounds) {
return drain(rounds);
}
}
|