test: guard deepinfra provider mock calls

This commit is contained in:
Peter Steinberger
2026-05-11 23:54:27 +01:00
parent b596c484e4
commit 7453ba6381
4 changed files with 48 additions and 8 deletions

View File

@@ -46,6 +46,22 @@ afterAll(() => {
vi.resetModules();
});
function requireFirstMockArg(mock: ReturnType<typeof vi.fn>, label: string): unknown {
const [call] = mock.mock.calls;
if (!call) {
throw new Error(`expected ${label}`);
}
return call[0];
}
function requireFirstMockObjectArg(mock: ReturnType<typeof vi.fn>, label: string): object {
const value = requireFirstMockArg(mock, label);
if (!value || typeof value !== "object" || Array.isArray(value)) {
throw new Error(`expected ${label}`);
}
return value;
}
describe("deepinfra image generation provider", () => {
afterEach(() => {
assertOkOrThrowHttpErrorMock.mockClear();
@@ -113,7 +129,7 @@ describe("deepinfra image generation provider", () => {
],
]);
expect(postJsonRequestMock).toHaveBeenCalledOnce();
const [jsonRequest] = postJsonRequestMock.mock.calls[0] ?? [];
const jsonRequest = requireFirstMockArg(postJsonRequestMock, "DeepInfra JSON image request");
const jsonRequestHeaders = Reflect.get(jsonRequest ?? {}, "headers");
expect(jsonRequestHeaders).toBeInstanceOf(Headers);
expect(Object.fromEntries((jsonRequestHeaders as Headers).entries())).toEqual({
@@ -175,10 +191,10 @@ describe("deepinfra image generation provider", () => {
});
expect(postMultipartRequestMock).toHaveBeenCalledOnce();
const [multipartRequest] = postMultipartRequestMock.mock.calls[0] ?? [];
if (!multipartRequest) {
throw new Error("Expected DeepInfra multipart request");
}
const multipartRequest = requireFirstMockObjectArg(
postMultipartRequestMock,
"DeepInfra multipart image request",
);
const multipartHeaders = Reflect.get(multipartRequest, "headers");
expect(multipartHeaders).toBeInstanceOf(Headers);
expect(Object.fromEntries((multipartHeaders as Headers).entries())).toEqual({

View File

@@ -66,6 +66,14 @@ async function withFetchPathTest(
}
}
function requireFirstFetchCall(mockFetch: ReturnType<typeof vi.fn>): [unknown, unknown] {
const [call] = mockFetch.mock.calls;
if (!call) {
throw new Error("expected DeepInfra models fetch call");
}
return call as [unknown, unknown];
}
describe("discoverDeepInfraModels", () => {
it("returns static catalog in test environment", async () => {
const models = await discoverDeepInfraModels();
@@ -89,7 +97,7 @@ describe("discoverDeepInfraModels", () => {
await withFetchPathTest(mockFetch, async () => {
const models = await discoverDeepInfraModels();
expect(mockFetch).toHaveBeenCalledOnce();
const [fetchUrl, fetchInit] = mockFetch.mock.calls[0] ?? [];
const [fetchUrl, fetchInit] = requireFirstFetchCall(mockFetch);
const fetchSignal = Reflect.get(fetchInit ?? {}, "signal");
expect(fetchUrl).toBe(DEEPINFRA_MODELS_URL);
expect(fetchSignal).toBeInstanceOf(AbortSignal);

View File

@@ -24,6 +24,14 @@ afterAll(() => {
vi.resetModules();
});
function requireFirstPostJsonRequest(): unknown {
const [call] = postJsonRequestMock.mock.calls;
if (!call) {
throw new Error("expected DeepInfra speech request");
}
return call[0];
}
describe("deepinfra speech provider", () => {
afterEach(() => {
assertOkOrThrowHttpErrorMock.mockClear();
@@ -108,7 +116,7 @@ describe("deepinfra speech provider", () => {
],
]);
expect(postJsonRequestMock).toHaveBeenCalledOnce();
const [postRequest] = postJsonRequestMock.mock.calls[0] ?? [];
const postRequest = requireFirstPostJsonRequest();
const postRequestHeaders = Reflect.get(postRequest ?? {}, "headers");
expect(postRequestHeaders).toBeInstanceOf(Headers);
expect(Object.fromEntries((postRequestHeaders as Headers).entries())).toEqual({

View File

@@ -15,6 +15,14 @@ beforeAll(async () => {
installProviderHttpMockCleanup();
function requireFirstPostJsonRequest(): unknown {
const [call] = postJsonRequestMock.mock.calls;
if (!call) {
throw new Error("expected DeepInfra video request");
}
return call[0];
}
describe("deepinfra video generation provider", () => {
it("declares explicit mode capabilities", () => {
expectExplicitVideoGenerationCapabilities(buildDeepInfraVideoGenerationProvider());
@@ -66,7 +74,7 @@ describe("deepinfra video generation provider", () => {
],
]);
expect(postJsonRequestMock).toHaveBeenCalledOnce();
const [postRequest] = postJsonRequestMock.mock.calls[0] ?? [];
const postRequest = requireFirstPostJsonRequest();
const postRequestHeaders = Reflect.get(postRequest ?? {}, "headers");
expect(postRequestHeaders).toBeInstanceOf(Headers);
expect(Object.fromEntries((postRequestHeaders as Headers).entries())).toEqual({