test(ai): avoid leaking fetch stubs

This commit is contained in:
DarkPhilosophy
2026-06-13 20:36:34 +03:00
parent 8224b5e1bb
commit 45af89e1b7
2 changed files with 6 additions and 4 deletions
@@ -97,7 +97,7 @@ describe("azure openai responses streaming", () => {
it("sends an async onPayload replacement body", async () => {
let capturedBody: Record<string, unknown> | undefined;
global.fetch = vi.fn(async (_input: string | URL | Request, init?: RequestInit) => {
const fetchMock = vi.fn(async (_input: string | URL | Request, init?: RequestInit) => {
capturedBody = typeof init?.body === "string" ? (JSON.parse(init.body) as Record<string, unknown>) : undefined;
return createSseResponse([
{
@@ -113,13 +113,14 @@ describe("azure openai responses streaming", () => {
},
},
]);
}) as unknown as typeof fetch;
});
const result = await streamAzureOpenAIResponses(
azureModel,
{ messages: [{ role: "user", content: "Say hello", timestamp: Date.now() }] },
{
apiKey: "test-key",
fetch: fetchMock as unknown as typeof fetch,
azureBaseUrl: azureModel.baseUrl,
azureApiVersion: "v1",
onPayload: async payload => ({
+3 -2
View File
@@ -299,16 +299,17 @@ describe("openai-codex streaming", () => {
const context = createCodexTestContext();
const model = { ...createCodexTestModel("https://chatgpt.com/backend-api"), preferWebsockets: false };
let capturedBody: Record<string, unknown> | undefined;
global.fetch = vi.fn(async (_input: string | URL, init?: RequestInit) => {
const fetchMock = vi.fn(async (_input: string | URL, init?: RequestInit) => {
capturedBody = typeof init?.body === "string" ? (JSON.parse(init.body) as Record<string, unknown>) : undefined;
return new Response(createCompletedCodexSse("Hello"), {
status: 200,
headers: { "content-type": "text/event-stream" },
});
}) as unknown as typeof fetch;
});
const result = await streamOpenAICodexResponses(model, context, {
apiKey: token,
fetch: fetchMock as unknown as typeof fetch,
onPayload: async payload => ({
...(payload as Record<string, unknown>),
input: [{ role: "user", content: [{ type: "input_text", text: "replacement" }] }],