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 = {}