From 017cd73e87d07d4bceed3e85681f89393404aa89 Mon Sep 17 00:00:00 2001 From: "zhou.han" Date: Wed, 11 Nov 2026 18:50:11 +0800 Subject: [PATCH] catpaw: tests put back the fetch they found, and the rewrite keeps a Request's method, headers and body (#877, #988) Fixes #877 (catpaw half). Test-only; no version bump. Change: catpaw's index.test.mjs saves the fetch it found and puts it back in afterAll (in a finally, so a failed server stop still restores it). Its URL-rewriting wrapper now builds `new Request(patched, input)` when handed a Request, so the method, headers and body survive the rewrite; init still applies on top. fake.mjs records each model-types request's method, an x-test-marker header and body, and a new test sends a POST Request through the wrapper and checks all three reach the fake. Kiro: its test files (usage, builderid, accounts, apikey, ...) already save and restore globalThis.fetch on main, unchanged since f379bf90; the per-file sweep below finds no kiro file leaving fetch changed. Ran (WSL magpie-desktop, Bun 1.4.2, HOME=$(mktemp -d)), at edecd806 and at edecd806 merged onto origin/main 80c9b495: - bun test packages/catpaw: 74 pass (main: 73). - bun test packages/catpaw packages/inkstone/api.test.mjs: 89 pass; bun test packages/kiro packages/inkstone/api.test.mjs: 198 pass. Both also pass on main, because inkstone now passes headers and body through init (#847), so this order no longer shows the leak. - #877 reproduced on main with a probe file run after catpaw (packages/zzprobe, not committed): globalThis.fetch is catpaw's wrapper, and fetch(new Request(url, {method: "POST"}), {body}) throws "fetch() request with GET/HEAD method cannot have body". At the PR head, and merged, the probe passes (native fetch, POST kept). - bun scripts/check.mjs: pass. - Root bun test on the merge: 8207 pass, 19 skip, 0 fail, 581 files. origin/main has since gained c00c4b9f (agy-cli test timing only, no fetch), not rerun with it. - Per-file sweep on main (each of 579 test files alone, with a preload that compares fetch, setTimeout, Date.now, Math.random and WebSocket at the end): catpaw is the only file that leaves a rewriting wrapper. The other 88 that leave fetch changed are the deliberate loopback-only guards (cursor, trae, minimax, inkstone, sensenova, mclaw nonet.mjs; gcp-ai test/network.mjs), which hand input and init through unchanged. Broken by hand: - Restore removed: catpaw's own suite stays green (74 pass); the probe after it goes red (fetch is the wrapper). Nothing inside the package guards the restore; only a later file sees it. - Request-preserving rewrite reverted to realFetch(patched, init): the new test goes red (401, the cookie header is lost along with the method). Not tried: init overriding a Request's fields through the wrapper (the PR says it does; no test sends both); macOS (mote), where #877 was seen. Co-authored-by: zhou.han <18181754+yetihan@users.noreply.github.com> --- packages/catpaw/fake.mjs | 3 +++ packages/catpaw/index.test.mjs | 33 +++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/packages/catpaw/fake.mjs b/packages/catpaw/fake.mjs index 7012c08..bceb48c 100644 --- a/packages/catpaw/fake.mjs +++ b/packages/catpaw/fake.mjs @@ -43,6 +43,7 @@ export function fakeCatPaw() { // never heard of (type 120 is a fixture, not the vendor's number). modelTypes: defaultModelTypes(), modelTypeCalls: [], + modelTypeRequests: [], modelTypesFail: false, // the web sign-in: sids awaiting their token, like the real vendor webSids: new Map(), // sid -> token to hand out @@ -66,6 +67,7 @@ export function fakeCatPaw() { state.balancePlan = null state.modelTypes = defaultModelTypes() state.modelTypeCalls = [] + state.modelTypeRequests = [] state.modelTypesFail = false state.webSids = new Map() state.webTokens = new Set() @@ -126,6 +128,7 @@ export function fakeCatPaw() { if (url.pathname === "/api/agent/maas/model-types") { state.modelTypeCalls.push(body) + state.modelTypeRequests.push({ method: req.method, marker: req.headers.get("x-test-marker"), body }) if (state.modelTypesFail) return json(500, { code: 500, msg: "boom" }) return json(200, { code: 0, message: "success", data: state.modelTypes, errorCode: null }) } diff --git a/packages/catpaw/index.test.mjs b/packages/catpaw/index.test.mjs index fdd24bf..f8d5d30 100644 --- a/packages/catpaw/index.test.mjs +++ b/packages/catpaw/index.test.mjs @@ -18,6 +18,7 @@ let fake let plugin let saved = {} let tmpDir +let realFetch const client = { auth: { set: async ({ body }) => (saved = body) } } const getAuth = async () => saved @@ -34,18 +35,22 @@ beforeAll(async () => { // the plugin reads ORIGIN at call time only inside gatewayHeaders/fetch, so // monkey-patching the global fetch to route to the fake works: we swap the // global fetch for one that rewrites the URL host to the fake server - const realFetch = globalThis.fetch + realFetch = globalThis.fetch globalThis.fetch = (input, init) => { const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url const patched = url.replace("https://ai.catpaw.meituan.com", fake.url).replace("https://catx.nocode.cn", fake.url) - return realFetch(patched, init) + return realFetch(input instanceof Request ? new Request(patched, input) : patched, init) } plugin = await CatPawAuthPlugin({ client }) }) afterAll(() => { - fake.server.stop(true) - rmSync(tmpDir, { recursive: true, force: true }) + try { + fake.server.stop(true) + rmSync(tmpDir, { recursive: true, force: true }) + } finally { + globalThis.fetch = realFetch + } }) beforeEach(() => { @@ -53,6 +58,26 @@ beforeEach(() => { saved = { type: "oauth", access: fake.state.token, expires: 0, source: "desktop", accountId: "Tester" } }) +describe("test fetch isolation", () => { + test("rewriting a Request keeps its method, headers and body", async () => { + const response = await globalThis.fetch(new Request("https://ai.catpaw.meituan.com/api/agent/maas/model-types", { + method: "POST", + headers: { + cookie: `X-Passport-Token=${fake.state.token}`, + "content-type": "application/json", + "m-appkey": "com.sankuai.mcopilot.cli", + "x-test-marker": "kept", + }, + body: JSON.stringify({ probe: true }), + })) + + expect(response.status).toBe(200) + expect(fake.state.modelTypeRequests).toEqual([ + { method: "POST", marker: "kept", body: { probe: true } }, + ]) + }) +}) + describe("config", () => { test("lists the desktop client's models", async () => { const cfg = {}