test: tighten image generation assertions

This commit is contained in:
Peter Steinberger
2026-05-09 22:10:33 +01:00
parent 8140465765
commit 21dd96cdff
2 changed files with 50 additions and 42 deletions

View File

@@ -73,6 +73,15 @@ function requireFirstRequestHeaders(mock: ReturnType<typeof vi.fn>): Headers {
return headers;
}
function requireFirstCallArg(mock: ReturnType<typeof vi.fn>): unknown {
const call = (mock.mock.calls as unknown as Array<[unknown] | undefined>)[0];
const arg = call?.[0];
if (!arg) {
throw new Error("expected mock call argument");
}
return arg;
}
function createProvider(overrides: Partial<OpenAiCompatibleImageProviderOptions> = {}) {
return createOpenAiCompatibleImageGenerationProvider({
id: "sample",
@@ -179,33 +188,36 @@ describe("OpenAI-compatible image provider helper", () => {
},
} as never);
expect(resolveApiKeyForProviderMock).toHaveBeenCalledWith(
expect.objectContaining({ provider: "sample" }),
);
const apiKeyParams = requireFirstCallArg(resolveApiKeyForProviderMock) as { provider?: string };
expect(apiKeyParams.provider).toBe("sample");
expect(sanitizeConfiguredModelProviderRequestMock).toHaveBeenCalledWith({
allowPrivateNetwork: true,
});
expect(postJsonRequestMock).toHaveBeenCalledWith(
expect.objectContaining({
url: "https://sample.example/v1/images/generations",
allowPrivateNetwork: true,
ssrfPolicy: { allowRfc2544BenchmarkRange: true },
dispatcherPolicy: { request: { allowPrivateNetwork: true } },
body: {
model: "custom-image",
prompt: "draw a square",
n: 2,
size: "512x512",
response_format: "b64_json",
},
}),
);
const jsonRequest = requireFirstCallArg(postJsonRequestMock) as {
url?: string;
allowPrivateNetwork?: boolean;
ssrfPolicy?: unknown;
dispatcherPolicy?: unknown;
body?: unknown;
};
expect(jsonRequest.url).toBe("https://sample.example/v1/images/generations");
expect(jsonRequest.allowPrivateNetwork).toBe(true);
expect(jsonRequest.ssrfPolicy).toEqual({ allowRfc2544BenchmarkRange: true });
expect(jsonRequest.dispatcherPolicy).toEqual({ request: { allowPrivateNetwork: true } });
expect(jsonRequest.body).toEqual({
model: "custom-image",
prompt: "draw a square",
n: 2,
size: "512x512",
response_format: "b64_json",
});
const headers = requireFirstRequestHeaders(postJsonRequestMock);
expect(headers.get("Content-Type")).toBe("application/json");
expect(result).toMatchObject({
model: "custom-image",
images: [{ mimeType: "image/png", fileName: "image-1.png", revisedPrompt: "revised" }],
});
expect(result.model).toBe("custom-image");
expect(result.images).toHaveLength(1);
expect(result.images[0]?.mimeType).toBe("image/png");
expect(result.images[0]?.fileName).toBe("image-1.png");
expect(result.images[0]?.revisedPrompt).toBe("revised");
expect(release).toHaveBeenCalledOnce();
});
@@ -221,12 +233,12 @@ describe("OpenAI-compatible image provider helper", () => {
cfg: {} as never,
});
expect(postMultipartRequestMock).toHaveBeenCalledWith(
expect.objectContaining({
url: "https://sample.example/v1/images/edits",
body: expect.any(FormData),
}),
);
const multipartRequest = requireFirstCallArg(postMultipartRequestMock) as {
url?: string;
body?: unknown;
};
expect(multipartRequest.url).toBe("https://sample.example/v1/images/edits");
expect(multipartRequest.body).toBeInstanceOf(FormData);
const headers = requireFirstRequestHeaders(postMultipartRequestMock);
expect(headers.has("Content-Type")).toBe(false);
});
@@ -259,8 +271,7 @@ describe("OpenAI-compatible image provider helper", () => {
deadline: { timeoutMs: 123, label: "Sample image generation" },
defaultTimeoutMs: 60_000,
});
expect(postJsonRequestMock).toHaveBeenCalledWith(
expect.objectContaining({ timeoutMs: 60_000 }),
);
const timeoutRequest = requireFirstCallArg(postJsonRequestMock) as { timeoutMs?: number };
expect(timeoutRequest.timeoutMs).toBe(60_000);
});
});

View File

@@ -488,17 +488,14 @@ describe("image-generation runtime", () => {
resolution: undefined,
});
expect(result.ignoredOverrides).toStrictEqual([]);
expect(result.normalization).toMatchObject({
aspectRatio: {
applied: "16:9",
derivedFrom: "size",
},
});
expect(result.metadata).toMatchObject({
requestedSize: "1280x720",
normalizedAspectRatio: "16:9",
aspectRatioDerivedFromSize: "16:9",
});
if (!result.normalization || !result.metadata) {
throw new Error("Expected image-generation normalization metadata");
}
expect(result.normalization.aspectRatio?.applied).toBe("16:9");
expect(result.normalization.aspectRatio?.derivedFrom).toBe("size");
expect(result.metadata.requestedSize).toBe("1280x720");
expect(result.metadata.normalizedAspectRatio).toBe("16:9");
expect(result.metadata.aspectRatioDerivedFromSize).toBe("16:9");
});
it("lists runtime image-generation providers through the provider registry", () => {