From 59ce15a419ae350046f4270f33f176a92faa9a95 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Sun, 15 Nov 2026 23:18:54 -0600 Subject: [PATCH] dispatch tests: restore the fetch stub so server.test.ts cannot leak into later files (#10609) --- pm-stack/dispatch/test/server.test.ts | 41 ++++++++++++++----- .../dispatch/test/zz_fetch_hygiene.test.ts | 22 ++++++++++ 2 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 pm-stack/dispatch/test/zz_fetch_hygiene.test.ts diff --git a/pm-stack/dispatch/test/server.test.ts b/pm-stack/dispatch/test/server.test.ts index ce6d2b8c62..968f453392 100644 --- a/pm-stack/dispatch/test/server.test.ts +++ b/pm-stack/dispatch/test/server.test.ts @@ -2,7 +2,7 @@ // Drives the exported handler with a stubbed-JWKS OIDC token: no network, // no queue, no GitHub — any non-JWKS fetch throws, so a test that tried // to create an issue/task/session would fail loudly instead of 400ing. -import { describe, expect, test } from "bun:test"; +import { afterAll, beforeAll, describe, expect, test } from "bun:test"; import { createSign, generateKeyPairSync } from "node:crypto"; const { publicKey, privateKey } = generateKeyPairSync("rsa", { modulusLength: 2048 }); @@ -14,15 +14,36 @@ jwk.alg = "RS256"; // old bogus .json URL is exactly how code#4886 stayed hidden by tests. import { GITHUB_JWKS_URL as JWKS_URL } from "../src/auth.ts"; const realFetch = globalThis.fetch; -globalThis.fetch = (async (input: unknown) => { - if (String(input) === JWKS_URL) { - return new Response(JSON.stringify({ keys: [jwk] }), { - status: 200, - headers: { "content-type": "application/json" }, - }); - } - throw new Error(`unexpected network fetch in test: ${String(input)}`); -}) as typeof fetch; + +// #10609: install the throw-on-network stub inside this file's own hook and +// restore the captured snapshot in afterAll. At module scope it leaked into +// every later file in the same Bun process: when opencode.test.ts ran after +// this file, its real http://127.0.0.1 requests hit this guard and 4 tests +// failed only when the file order happened to be that way (CI run 43807605419 +// vs. a green re-dispatch of the identical head). installFetchStub stays a +// function so no assignment happens at import time. +function installFetchStub() { + globalThis.fetch = (async (input: unknown) => { + if (String(input) === JWKS_URL) { + return new Response(JSON.stringify({ keys: [jwk] }), { + status: 200, + headers: { "content-type": "application/json" }, + }); + } + throw new Error(`unexpected network fetch in test: ${String(input)}`); + }) as typeof fetch; +} + +beforeAll(installFetchStub); + +afterAll(() => { + globalThis.fetch = realFetch; + // The runtime's own fetch is native code; a JS function still installed + // here is the leaked stub (or a stub another file left for us to capture). + // Failing in this hook is deliberate: it is the explicit restore assertion + // for #10609, so removing the restore above cannot pass silently. + expect(Function.prototype.toString.call(globalThis.fetch)).toContain("[native code]"); +}); function b64url(data: Buffer | string): string { return Buffer.from(data).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); diff --git a/pm-stack/dispatch/test/zz_fetch_hygiene.test.ts b/pm-stack/dispatch/test/zz_fetch_hygiene.test.ts new file mode 100644 index 0000000000..f14059c95b --- /dev/null +++ b/pm-stack/dispatch/test/zz_fetch_hygiene.test.ts @@ -0,0 +1,22 @@ +// #10609: a fetch stub installed by a test file and never restored leaks into +// every test file that runs after it in the same Bun process. server.test.ts +// used to overwrite globalThis.fetch at module scope to make any non-JWKS +// network access throw; when opencode.test.ts ran after it, its real +// http://127.0.0.1:/api/session* requests hit that guard and 4 +// OpenCodeClient tests failed — only under the file order that happened to +// put server.test.ts first (CI run 43807605419; the same head was green on a +// re-dispatch, and the dispatch tree was identical to a green earlier head). +// +// Bun runs test files in filename order by default, so the zz_ prefix puts +// this file after every stubbing one. Under `--randomize` the check is +// best-effort: it still fails whenever it runs after a leaked stub, which is +// the property that matters. It cannot weaken server.test.ts's own restore +// assertion (that lives in the file's afterAll). +import { describe, expect, test } from "bun:test"; + +describe("global fetch hygiene (#10609)", () => { + test("globalThis.fetch is the runtime's native fetch, not a leaked test stub", () => { + const source = Function.prototype.toString.call(globalThis.fetch); + expect(source).toContain("[native code]"); + }); +});