test: fix fetch mock typing

This commit is contained in:
Peter Steinberger
2026-03-15 21:07:05 -07:00
parent 53ccc78c63
commit 0f43dc4680
4 changed files with 21 additions and 16 deletions

View File

@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from "vitest";
import { withFetchPreconnect } from "../../../src/test-utils/fetch-mock.js";
import { uploadToOneDrive, uploadToSharePoint } from "./graph-upload.js";
describe("graph upload helpers", () => {
@@ -22,7 +23,7 @@ describe("graph upload helpers", () => {
buffer: Buffer.from("hello"),
filename: "a.txt",
tokenProvider,
fetchFn: fetchFn as typeof fetch,
fetchFn: withFetchPreconnect(fetchFn),
});
expect(fetchFn).toHaveBeenCalledWith(
@@ -59,7 +60,7 @@ describe("graph upload helpers", () => {
filename: "b.txt",
siteId: "site-123",
tokenProvider,
fetchFn: fetchFn as typeof fetch,
fetchFn: withFetchPreconnect(fetchFn),
});
expect(fetchFn).toHaveBeenCalledWith(
@@ -94,7 +95,7 @@ describe("graph upload helpers", () => {
filename: "bad.txt",
siteId: "site-123",
tokenProvider,
fetchFn: fetchFn as typeof fetch,
fetchFn: withFetchPreconnect(fetchFn),
}),
).rejects.toThrow("SharePoint upload response missing required fields");
});

View File

@@ -1,5 +1,6 @@
import { streamSimpleOpenAICompletions, type Model } from "@mariozechner/pi-ai";
import { afterEach, describe, expect, it, vi } from "vitest";
import { withFetchPreconnect } from "../test-utils/fetch-mock.js";
import type { AuthProfileStore } from "./auth-profiles.js";
import { CUSTOM_LOCAL_AUTH_MARKER, NON_ENV_SECRETREF_MARKER } from "./model-auth-markers.js";
import {
@@ -503,16 +504,18 @@ describe("applyLocalNoAuthHeaderOverride", () => {
const requestSeen = new Promise<void>((resolve) => {
resolveRequest = resolve;
});
globalThis.fetch = vi.fn(async (_input, init) => {
const headers = new Headers(init?.headers);
capturedAuthorization = headers.get("Authorization");
capturedXTest = headers.get("X-Test");
resolveRequest?.();
return new Response(JSON.stringify({ error: { message: "unauthorized" } }), {
status: 401,
headers: { "content-type": "application/json" },
});
}) as typeof fetch;
globalThis.fetch = withFetchPreconnect(
vi.fn(async (_input, init) => {
const headers = new Headers(init?.headers);
capturedAuthorization = headers.get("Authorization");
capturedXTest = headers.get("X-Test");
resolveRequest?.();
return new Response(JSON.stringify({ error: { message: "unauthorized" } }), {
status: 401,
headers: { "content-type": "application/json" },
});
}),
);
const model = applyLocalNoAuthHeaderOverride(
{

View File

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

View File

@@ -1,4 +1,5 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { withFetchPreconnect } from "../test-utils/fetch-mock.js";
import {
buildUsageErrorSnapshot,
buildUsageHttpErrorSnapshot,
@@ -36,7 +37,7 @@ describe("provider usage fetch shared helpers", () => {
async (_input: URL | RequestInfo, init?: RequestInit) =>
new Response(JSON.stringify({ aborted: init?.signal?.aborted ?? false }), { status: 200 }),
);
const fetchFn = fetchFnMock as typeof fetch;
const fetchFn = withFetchPreconnect(fetchFnMock);
const response = await fetchJson(
"https://example.com/usage",
@@ -71,7 +72,7 @@ describe("provider usage fetch shared helpers", () => {
});
}),
);
const fetchFn = fetchFnMock as typeof fetch;
const fetchFn = withFetchPreconnect(fetchFnMock);
const request = fetchJson("https://example.com/usage", {}, 50, fetchFn);
const rejection = expect(request).rejects.toThrow("aborted by timeout");