Tests: fix fetch mock typings for type-aware checks

This commit is contained in:
Shakker
2026-02-17 14:34:41 +00:00
parent dd0b789669
commit 6bb9b0656f
2 changed files with 29 additions and 17 deletions

View File

@@ -14,11 +14,12 @@ vi.mock("../agents/model-auth.js", () => ({
}));
const createFetchMock = () => {
const fetchMock = vi.fn(async () => ({
ok: true,
status: 200,
json: async () => ({ data: [{ embedding: [0.1, 0.2, 0.3] }] }),
}));
const fetchMock = vi.fn(async (_input: RequestInfo | URL, _init?: RequestInit) => {
return new Response(JSON.stringify({ data: [{ embedding: [0.1, 0.2, 0.3] }] }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
});
return withFetchPreconnect(fetchMock) as typeof fetch & typeof fetchMock;
};
@@ -94,13 +95,17 @@ describe("voyage embedding provider", () => {
it("passes input_type=document for embedBatch", async () => {
const fetchMock = withFetchPreconnect(
vi.fn(async () => ({
ok: true,
status: 200,
json: async () => ({
data: [{ embedding: [0.1, 0.2] }, { embedding: [0.3, 0.4] }],
}),
})),
vi.fn(async (_input: RequestInfo | URL, _init?: RequestInit) => {
return new Response(
JSON.stringify({
data: [{ embedding: [0.1, 0.2] }, { embedding: [0.3, 0.4] }],
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
}),
);
vi.stubGlobal("fetch", fetchMock);

View File

@@ -1,6 +1,13 @@
export const withFetchPreconnect = <T extends (...args: unknown[]) => unknown>(
type FetchWithPreconnect = {
preconnect: (url: string, init?: { credentials?: RequestCredentials }) => void;
};
export function withFetchPreconnect<T extends typeof fetch>(fn: T): T & FetchWithPreconnect;
export function withFetchPreconnect<T extends object>(
fn: T,
): typeof fetch =>
Object.assign(fn, {
preconnect: () => {},
}) as unknown as typeof fetch;
): T & FetchWithPreconnect & typeof fetch;
export function withFetchPreconnect(fn: object) {
return Object.assign(fn, {
preconnect: (_url: string, _init?: { credentials?: RequestCredentials }) => {},
});
}