Skip to content

Commit 0f43dc4

Browse files
committed
test: fix fetch mock typing
1 parent 53ccc78 commit 0f43dc4

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

extensions/msteams/src/graph-upload.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, it, vi } from "vitest";
2+
import { withFetchPreconnect } from "../../../src/test-utils/fetch-mock.js";
23
import { uploadToOneDrive, uploadToSharePoint } from "./graph-upload.js";
34

45
describe("graph upload helpers", () => {
@@ -22,7 +23,7 @@ describe("graph upload helpers", () => {
2223
buffer: Buffer.from("hello"),
2324
filename: "a.txt",
2425
tokenProvider,
25-
fetchFn: fetchFn as typeof fetch,
26+
fetchFn: withFetchPreconnect(fetchFn),
2627
});
2728

2829
expect(fetchFn).toHaveBeenCalledWith(
@@ -59,7 +60,7 @@ describe("graph upload helpers", () => {
5960
filename: "b.txt",
6061
siteId: "site-123",
6162
tokenProvider,
62-
fetchFn: fetchFn as typeof fetch,
63+
fetchFn: withFetchPreconnect(fetchFn),
6364
});
6465

6566
expect(fetchFn).toHaveBeenCalledWith(
@@ -94,7 +95,7 @@ describe("graph upload helpers", () => {
9495
filename: "bad.txt",
9596
siteId: "site-123",
9697
tokenProvider,
97-
fetchFn: fetchFn as typeof fetch,
98+
fetchFn: withFetchPreconnect(fetchFn),
9899
}),
99100
).rejects.toThrow("SharePoint upload response missing required fields");
100101
});

src/agents/model-auth.test.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { streamSimpleOpenAICompletions, type Model } from "@mariozechner/pi-ai";
22
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import { withFetchPreconnect } from "../test-utils/fetch-mock.js";
34
import type { AuthProfileStore } from "./auth-profiles.js";
45
import { CUSTOM_LOCAL_AUTH_MARKER, NON_ENV_SECRETREF_MARKER } from "./model-auth-markers.js";
56
import {
@@ -503,16 +504,18 @@ describe("applyLocalNoAuthHeaderOverride", () => {
503504
const requestSeen = new Promise<void>((resolve) => {
504505
resolveRequest = resolve;
505506
});
506-
globalThis.fetch = vi.fn(async (_input, init) => {
507-
const headers = new Headers(init?.headers);
508-
capturedAuthorization = headers.get("Authorization");
509-
capturedXTest = headers.get("X-Test");
510-
resolveRequest?.();
511-
return new Response(JSON.stringify({ error: { message: "unauthorized" } }), {
512-
status: 401,
513-
headers: { "content-type": "application/json" },
514-
});
515-
}) as typeof fetch;
507+
globalThis.fetch = withFetchPreconnect(
508+
vi.fn(async (_input, init) => {
509+
const headers = new Headers(init?.headers);
510+
capturedAuthorization = headers.get("Authorization");
511+
capturedXTest = headers.get("X-Test");
512+
resolveRequest?.();
513+
return new Response(JSON.stringify({ error: { message: "unauthorized" } }), {
514+
status: 401,
515+
headers: { "content-type": "application/json" },
516+
});
517+
}),
518+
);
516519

517520
const model = applyLocalNoAuthHeaderOverride(
518521
{

src/infra/fetch.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ describe("wrapFetchWithAbortSignal", () => {
293293
});
294294

295295
it("exposes a no-op preconnect when the source fetch has none", () => {
296-
const fetchImpl = vi.fn(async () => ({ ok: true }) as Response) as typeof fetch;
296+
const fetchImpl = withFetchPreconnect(vi.fn(async () => ({ ok: true }) as Response));
297297
const wrapped = wrapFetchWithAbortSignal(fetchImpl) as typeof fetch & {
298298
preconnect: (url: string, init?: { credentials?: RequestCredentials }) => unknown;
299299
};

src/infra/provider-usage.fetch.shared.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import { withFetchPreconnect } from "../test-utils/fetch-mock.js";
23
import {
34
buildUsageErrorSnapshot,
45
buildUsageHttpErrorSnapshot,
@@ -36,7 +37,7 @@ describe("provider usage fetch shared helpers", () => {
3637
async (_input: URL | RequestInfo, init?: RequestInit) =>
3738
new Response(JSON.stringify({ aborted: init?.signal?.aborted ?? false }), { status: 200 }),
3839
);
39-
const fetchFn = fetchFnMock as typeof fetch;
40+
const fetchFn = withFetchPreconnect(fetchFnMock);
4041

4142
const response = await fetchJson(
4243
"https://example.com/usage",
@@ -71,7 +72,7 @@ describe("provider usage fetch shared helpers", () => {
7172
});
7273
}),
7374
);
74-
const fetchFn = fetchFnMock as typeof fetch;
75+
const fetchFn = withFetchPreconnect(fetchFnMock);
7576

7677
const request = fetchJson("https://example.com/usage", {}, 50, fetchFn);
7778
const rejection = expect(request).rejects.toThrow("aborted by timeout");

0 commit comments

Comments
 (0)