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 | /**
* The harness's own gate.
*
* det-clock and effect-log are what every characterization vector in 3c–3e is
* measured with. A silently wrong clock — one that fires timers out of order, or
* reports the same `Date.now()` for two deadlines a second apart, or virtualizes
* microtasks by accident — would not fail: it would produce a plausible
* transcript, get committed as the golden value, and pin the wrong behaviour
* forever. Six gates in this suite's history reported success while testing
* nothing. So the instrument is tested before anything is measured with it.
*/
import test from "node:test";
import assert from "node:assert/strict";
import { installClock, realSetTimeout, makeSink } from "./helpers/det-clock.mjs";
import { effectLog, effectFn, summarize } from "./helpers/effect-log.mjs";
/** Install, run, uninstall — never leave a clock on the runner's globals. */
async function on(opts, fn) {
const clock = installClock(opts);
try {
return await fn(clock);
} finally {
clock.uninstall();
}
}
test("the clock follows the queue: Date.now() is the firing timer's deadline", async () => {
await on({ t0: 1000 }, async (clock) => {
const seen = [];
setTimeout(() => seen.push(["a", Date.now()]), 300);
setTimeout(() => seen.push(["b", Date.now()]), 100);
setTimeout(() => seen.push(["c", Date.now()]), 100);
assert.equal(Date.now(), 1000);
await clock.advance(500);
// deadline order, ties by scheduling order — and each callback saw ITS OWN
// deadline, not 1500 and not 1000. This is the property the whole phase
// rests on: a backoff ladder is only reproducible if two timers 1s apart
// observe two different clocks.
assert.deepEqual(seen, [["b", 1100], ["c", 1100], ["a", 1300]]);
assert.equal(clock.now(), 1500);
});
});
test("tick advances the clock and fires nothing (the pre-3b contract)", async () => {
await on({ t0: 0 }, async (clock) => {
let fired = 0;
setTimeout(() => fired++, 10);
clock.tick(1_000_000);
assert.equal(Date.now(), 1_000_000);
await clock.drain();
assert.equal(fired, 0, "tick must not fire — every pre-3b vector depends on it");
assert.equal(clock.pending().length, 1);
// ...and the timer is now overdue, so the next advance(0) still fires it
await clock.advance(0);
assert.equal(fired, 1);
});
});
test("advance fires only what is due, runNext fires exactly one", async () => {
await on({ t0: 0 }, async (clock) => {
const seen = [];
setTimeout(() => seen.push(1), 100);
setTimeout(() => seen.push(2), 200);
setTimeout(() => seen.push(3), 300);
await clock.advance(200);
assert.deepEqual(seen, [1, 2]);
assert.deepEqual(clock.pending(), [{ id: 3, at: 300, delay: 300, kind: "timeout", label: null }]);
const shot = await clock.runNext();
assert.deepEqual(shot, { id: 3, at: 300, delay: 300, kind: "timeout", label: null });
assert.deepEqual(seen, [1, 2, 3]);
assert.equal(await clock.runNext(), null);
});
});
test("microtasks are NOT virtualized — promise chains still run untouched", async () => {
await on({ t0: 0 }, async (clock) => {
const order = [];
// a promise-chain mutex, exactly the shape media's peer.queue and log's
// _updateChain use. If a fake microtask queue existed, the harness could
// reorder these — and reordering is what the media mutex test exists to
// detect, so the instrument must not be able to do it.
let chain = Promise.resolve();
chain = chain.then(() => order.push("a1")).then(() => order.push("a2"));
chain = chain.then(() => order.push("b1")).then(() => order.push("b2"));
await chain;
assert.deepEqual(order, ["a1", "a2", "b1", "b2"]);
assert.equal(clock.pending().length, 0, "a promise must never become a queued macrotask");
});
});
test("the real timer survives the swap and is exported", async () => {
const captured = realSetTimeout;
await on({ t0: 0 }, async (clock) => {
assert.notEqual(globalThis.setTimeout, captured, "the global really is virtual inside");
// the real one still comes back on its own — this is what settle() and
// node:test rely on, and swapping it without handing it back deadlocks the
// runner with no useful error
await new Promise((r) => captured(r, 0));
assert.equal(clock.pending().length, 0);
});
assert.equal(globalThis.setTimeout, captured, "uninstall must restore the global");
});
test("clearTimeout removes the timer and lands in the transcript", async () => {
await on({ t0: 0 }, async (clock) => {
let fired = 0;
const id = setTimeout(() => fired++, 1000);
const keep = setTimeout(() => fired += 10, 2000);
clearTimeout(id);
await clock.advance(5000);
assert.equal(fired, 10, "the cleared timer must not fire; the other must");
assert.deepEqual(clock.ops(), [
["setTimeout", 1000, 1],
["setTimeout", 2000, 2],
["clearTimeout", 1],
["fire", 2],
]);
assert.equal(keep, 2);
});
});
test("setInterval reschedules; clearInterval stops it", async () => {
await on({ t0: 0 }, async (clock) => {
const at = [];
const id = setInterval(() => at.push(Date.now()), 250);
await clock.advance(1000);
assert.deepEqual(at, [250, 500, 750, 1000]);
clearInterval(id);
await clock.advance(1000);
assert.deepEqual(at, [250, 500, 750, 1000]);
});
});
test("requestAnimationFrame is a virtual macrotask carrying a virtual timestamp", async () => {
await on({ t0: 5000, rafMs: 16 }, async (clock) => {
const frames = [];
requestAnimationFrame((t) => frames.push(t));
assert.deepEqual(clock.pending(), [{ id: 1, at: 5016, delay: 16, kind: "raf", label: null }]);
await clock.advance(20);
assert.deepEqual(frames, [16]);
});
});
test("performance.now is virtual and monotonic with the clock", async () => {
await on({ t0: 1_700_000_000_000 }, async (clock) => {
assert.equal(performance.now(), 0);
clock.tick(1234);
assert.equal(performance.now(), 1234);
});
});
test("dateCtor pins `new Date()` too, and only the no-arg form", async () => {
await on({ t0: 1_700_000_000_000, dateCtor: true }, async () => {
assert.equal(new Date().getTime(), 1_700_000_000_000);
assert.equal(new Date(0).getTime(), 0);
assert.equal(Date.now(), 1_700_000_000_000);
});
assert.ok(Math.abs(new Date().getTime() - Date.now()) < 1000, "uninstall must restore Date");
});
// ------------------------------------------------------------- effect-log --
test("effectLog records every call, in order, and delegates", () => {
const sink = makeSink();
const real = { puts: [], async putEntryBlock(hash, bytes) { this.puts.push([hash, bytes.length]); return "ok"; } };
const log = effectLog(real, "log", sink);
log.putEntryBlock("zdpuHASH", new Uint8Array(4));
log.putEntryBlock("zdpuOTHER", new Uint8Array(9));
assert.deepEqual(sink, [
["log.putEntryBlock", "zdpuHASH", 4],
["log.putEntryBlock", "zdpuOTHER", 9],
]);
assert.deepEqual(real.puts, [["zdpuHASH", 4], ["zdpuOTHER", 9]], "and the real method really ran");
});
test("one sink interleaves clock ops with effects", async () => {
const sink = makeSink();
await on({ t0: 0, sink }, async (clock) => {
const client = effectLog({ deposit: (b) => b.length }, "client", sink);
const onRetry = effectFn("onRetry", sink);
client.deposit(new Uint8Array(36));
setTimeout(() => { onRetry(1); client.deposit(new Uint8Array(2)); }, 1000);
await clock.advance(1000);
assert.deepEqual(sink, [
["client.deposit", 36],
["setTimeout", 1000, 1],
["fire", 1],
["onRetry", 1],
["client.deposit", 2],
]);
// the clock's own view is the same events, minus the effects
assert.deepEqual(clock.ops(), [["setTimeout", 1000, 1], ["fire", 1]]);
});
});
test("summarize is JSON-stable for the argument shapes these doubles see", () => {
assert.equal(summarize("s"), "s");
assert.equal(summarize(7), 7);
assert.equal(summarize(false), false);
assert.equal(summarize(null), null);
assert.equal(summarize(undefined), "undefined");
assert.equal(summarize(new Uint8Array(12)), 12);
assert.equal(summarize(() => {}), "fn");
assert.deepEqual(summarize([1, new Uint8Array(2), "x"]), [1, 2, "x"]);
assert.deepEqual(summarize({ t: "entry", hash: "h" }), { t: "entry", hash: "h" });
assert.equal(summarize(new Error("boom")), "Error: boom");
// a CID-ish object: its own toString wins over a structural dump
class Fake { toString() { return "bafyFAKE"; } }
assert.equal(summarize(new Fake()), "bafyFAKE");
});
test("the harness gate is not vacuous", async () => {
// Each perturbation is one of the ways a silently-wrong clock would still
// produce a plausible transcript.
const sink = makeSink();
await on({ t0: 0, sink }, async (clock) => {
setTimeout(() => {}, 50);
setTimeout(() => {}, 10);
await clock.advance(100);
});
assert.deepEqual(sink, [["setTimeout", 50, 1], ["setTimeout", 10, 2], ["fire", 2], ["fire", 1]]);
// fired in DEADLINE order, not scheduling order
assert.notDeepEqual(sink, [["setTimeout", 50, 1], ["setTimeout", 10, 2], ["fire", 1], ["fire", 2]]);
// a dropped clear, a halved delay and a swapped pair are all visible
const halved = JSON.parse(JSON.stringify(sink));
halved[0][1] = 25;
assert.notDeepEqual(halved, sink);
const swapped = JSON.parse(JSON.stringify(sink));
[swapped[2], swapped[3]] = [swapped[3], swapped[2]];
assert.notDeepEqual(swapped, sink);
const missing = JSON.parse(JSON.stringify(sink)).slice(0, 3);
assert.notDeepEqual(missing, sink);
});
|