Skip to content

Commit d03f84d

Browse files
ryansolidClaude via Cursorcursoragent
committed
test(signals): tolerate sub-ms timer skew in attribution wall-clock assertions
`await wait(N)` then `expect(x.settledMs).toBeGreaterThanOrEqual(N)` flaked in CI (29.92 vs 30, 9.80 vs 10): libuv timers are ms-granular, so a setTimeout(N) can fire a hair under N ms of `performance.now()`, the clock the attribution engine stamps with. `wait` in the three affected files now resolves with the time actually waited on that clock, and the eight exact-N assertions compare against it — a strict lower bound on the engine's own interval, no fudge. Assertions that already carried slack are untouched. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 27bb3fa commit d03f84d

3 files changed

Lines changed: 37 additions & 13 deletions

File tree

‎packages/signals/tests/attribution-holds.test.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ afterEach(() => {
3636
vi.restoreAllMocks();
3737
});
3838

39-
const wait = (ms: number) => new Promise(r => setTimeout(r, ms));
39+
// Resolves with the time actually waited on the engine's clock
40+
// (`performance.now()`): a setTimeout(N) can fire a hair under N ms of it
41+
// (libuv timers are ms-granular), so wall-clock assertions compare against
42+
// this rather than N.
43+
const wait = (ms: number) =>
44+
new Promise<number>(r => {
45+
const start = performance.now();
46+
setTimeout(() => r(performance.now() - start), ms);
47+
});
4048

4149
async function until(cond: () => boolean, what: string, timeout = 5000) {
4250
const start = Date.now();
@@ -116,7 +124,7 @@ describe("SILENT_HOLD", () => {
116124
feed.setPage(2);
117125
flush();
118126
expect(feed.shown).toEqual(["a-p1"]); // held: nothing painted
119-
await wait(10);
127+
const waited = await wait(10);
120128
feed.resolve("b");
121129
await until(() => feed.shown.includes("b-p2"), "the held page to land");
122130

@@ -141,7 +149,7 @@ describe("SILENT_HOLD", () => {
141149
paintedDuringHold: 0,
142150
action: false
143151
});
144-
expect(holds[0].holdMs).toBeGreaterThanOrEqual(10);
152+
expect(holds[0].holdMs).toBeGreaterThanOrEqual(waited);
145153
expect(holds[0].flushes).toBeGreaterThanOrEqual(1);
146154
});
147155

@@ -578,20 +586,20 @@ describe("LONG_HOLD", () => {
578586
feed.setPage(2)
579587
);
580588
flush();
581-
await wait(60);
589+
const pause = await wait(60);
582590
OBSERVE!.attribution.withInteraction({ ...CLICK, at: performance.now() }, () =>
583591
feed.setPage(3)
584592
);
585593
flush();
586-
await wait(10);
594+
const tail = await wait(10);
587595
feed.resolve("c");
588596
await until(() => feed.shown.includes("c-p3"), "the final page to land");
589597

590598
const [hold] = attribution.holds();
591599
// holdMs reaches back to the first parked flush even though "page" now
592600
// carries only the second click's record.
593-
expect(hold.holdMs).toBeGreaterThanOrEqual(70);
594-
expect(hold.tailMs).toBeGreaterThanOrEqual(10);
601+
expect(hold.holdMs).toBeGreaterThanOrEqual(pause + tail);
602+
expect(hold.tailMs).toBeGreaterThanOrEqual(tail);
595603
expect(hold.tailMs).toBeLessThan(hold.holdMs - 40);
596604
expect(longEvents).toHaveLength(1);
597605
expect(longEvents[0].message).toContain("after the last input");
@@ -652,10 +660,10 @@ describe("LONG_HOLD", () => {
652660
await until(() => feed.shown.includes("a-p1"), "initial load");
653661
feed.setPage(2);
654662
flush();
655-
await wait(30);
663+
const waited = await wait(30);
656664
feed.resolve("b");
657665
await until(() => feed.shown.includes("b-p2"), "the held page to land");
658666
expect(longEvents).toHaveLength(0);
659-
expect(attribution.holds()[0].tailMs).toBeGreaterThanOrEqual(30);
667+
expect(attribution.holds()[0].tailMs).toBeGreaterThanOrEqual(waited);
660668
});
661669
});

‎packages/signals/tests/attribution-interactions.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ describe("InteractionEvent", () => {
186186
flush();
187187
expect(feed.shown).toEqual(["a-p1"]); // held
188188
expect(interactions()).toHaveLength(0);
189+
// Bracket the wait on the engine's own clock: a 10ms timer can fire a
190+
// hair under 10ms of `performance.now()`.
191+
const armed = performance.now();
189192
await wait(10);
193+
const waited = performance.now() - armed;
190194
feed.resolve("b");
191195
await until(() => feed.shown.includes("b-p2"), "the held page to land");
192196

@@ -199,7 +203,7 @@ describe("InteractionEvent", () => {
199203
// The hold's clock starts at the click; the interaction settles at the commit.
200204
expect(hold.at).toBe(e.at);
201205
expect(e.settledMs).toBeGreaterThanOrEqual(hold.holdMs - 1);
202-
expect(e.settledMs).toBeGreaterThanOrEqual(10);
206+
expect(e.settledMs).toBeGreaterThanOrEqual(waited);
203207
// Bottom-up delivery: the hold before the interaction that waited on it.
204208
const order = delivered.filter(d => d.type !== "rerun").map(d => d.type);
205209
expect(order).toEqual(["hold", "interaction"]);

‎packages/signals/tests/attribution-navigation.test.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,17 @@ describe("navigations() — one settled record per frame", () => {
276276
expect(app.shown).toEqual(["alice@/users"]); // held: nothing painted
277277
const [nav] = attribution.navigations();
278278
expect(nav.outcome).toBeUndefined(); // still waiting on the page
279+
// Bracket the wait on the engine's own clock: a 10ms timer can fire a
280+
// hair under 10ms of `performance.now()`.
281+
const armed = performance.now();
279282
await wait(10);
283+
const waited = performance.now() - armed;
280284
app.resolve("alice");
281285
await until(() => app.shown.includes("alice@/users/42"), "the held page to land");
282286

283287
expect(nav.outcome).toBe("held");
284288
expect(nav.hold).toBeDefined();
285-
expect(nav.settledMs).toBeGreaterThanOrEqual(10);
289+
expect(nav.settledMs).toBeGreaterThanOrEqual(waited);
286290
const [hold] = attribution.holds();
287291
expect(nav.hold).toBe(hold);
288292
// The hold names the navigation, and joins to it by identity.
@@ -390,7 +394,11 @@ describe("navigations() — one settled record per frame", () => {
390394
() => app.setLocation("/users/3")
391395
);
392396
flush();
397+
// Bracket the wait on the engine's own clock: a 10ms timer can fire a
398+
// hair under 10ms of `performance.now()`.
399+
const armed = performance.now();
393400
await wait(10);
401+
const waited = performance.now() - armed;
394402
app.resolve("b");
395403
await until(() => app.shown.includes("b@/users/3"), "the last page to land");
396404

@@ -405,7 +413,7 @@ describe("navigations() — one settled record per frame", () => {
405413
// heldMs is the hold's own clock: it opened with the superseded
406414
// navigation's write and the surviving one inherited it, so it can run
407415
// longer than the survivor's own request-to-settle time.
408-
expect(row.heldMs).toBeGreaterThanOrEqual(10);
416+
expect(row.heldMs).toBeGreaterThanOrEqual(waited);
409417
expect(row.settledMs).toBeGreaterThan(0);
410418
expect(row.worstMs).toBeGreaterThan(0);
411419
});
@@ -478,6 +486,9 @@ describe("redirects — one navigation, several destinations", () => {
478486
OBSERVE!.attribution.withOrigin(NAV, () => app.setLocation("/users/42"))
479487
);
480488
flush();
489+
// Bracket both waits on the engine's own clock: a 10ms timer can fire a
490+
// hair under 10ms of `performance.now()`.
491+
const armed = performance.now();
481492
await wait(10);
482493
// The guard behind /users/:id sends the user to /login — the click is long
483494
// gone, and the router knows only that a navigation is pending.
@@ -488,6 +499,7 @@ describe("redirects — one navigation, several destinations", () => {
488499
const [nav] = attribution.navigations();
489500
expect(nav.outcome).toBeUndefined();
490501
await wait(10);
502+
const waited = performance.now() - armed;
491503
app.resolve("b");
492504
await until(() => app.shown.includes("b@/login"), "the redirect target to land");
493505

@@ -508,7 +520,7 @@ describe("redirects — one navigation, several destinations", () => {
508520
// Timing runs from the user's request, not the hop.
509521
expect(nav.at).toBeGreaterThanOrEqual(before);
510522
expect(nav.at).toBeLessThan(hopAt);
511-
expect(nav.settledMs).toBeGreaterThanOrEqual(20);
523+
expect(nav.settledMs).toBeGreaterThanOrEqual(waited);
512524
// One hold, joined by identity, named by the whole chain.
513525
const [hold] = attribution.holds();
514526
expect(nav.hold).toBe(hold);

0 commit comments

Comments
 (0)