From 731f15e63cd387771b0e90906292dd05e73a498e Mon Sep 17 00:00:00 2001 From: Agnik47 <140933190+Agnik47@users.noreply.github.com> Date: Fri, 18 Sep 2026 22:29:49 +0530 Subject: [PATCH] test(site-memory): stop racing two clocks in the public-IP timeout bound `bounds public-IP lookup and omits the field on timeout` asserted `Date.now() - started >= 2000` against a 2000ms `AbortSignal.timeout`. The timer and `Date.now()` are not the same clock source, so the observed elapsed time can land just under the configured timeout. The Bun compatibility job has already failed on it: AssertionError: expected 1999 to be greater than or equal to 2000 Assert the mechanism instead: capture the abort reason and require it to be a `TimeoutError`, which only the lookup's own `AbortSignal.timeout` produces. The upper bound stays, so an unbounded hang is still caught. --- src/site-memory/environment.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/site-memory/environment.test.ts b/src/site-memory/environment.test.ts index e12d74e99..46296acec 100644 --- a/src/site-memory/environment.test.ts +++ b/src/site-memory/environment.test.ts @@ -87,8 +87,10 @@ describe('candidate environment provenance', () => { }); it('bounds public-IP lookup and omits the field on timeout', async () => { + let abortReason: unknown; const fetch = vi.fn((_input: RequestInfo | URL, init?: RequestInit) => new Promise((_resolve, reject) => { init?.signal?.addEventListener('abort', () => { + abortReason = init.signal?.reason; reject(init.signal?.reason ?? new Error('aborted')); }); })); @@ -105,7 +107,11 @@ describe('candidate environment provenance', () => { expect(env.publicIp).toBeUndefined(); expect(fetch).toHaveBeenCalledTimes(1); - expect(Date.now() - started).toBeGreaterThanOrEqual(2000); + // The lookup is bounded by its own AbortSignal.timeout, not by the caller + // giving up. Assert that mechanism rather than a wall-clock floor: the + // timer clock and Date.now() are different sources, so elapsed time can + // land a millisecond under the configured timeout. + expect((abortReason as Error | undefined)?.name).toBe('TimeoutError'); expect(Date.now() - started).toBeLessThan(4000); }); });