Files
oh-my-pi/packages/coding-agent/test/tools/image-gen.test.ts
T
can1357 f1f6516056 refactor: reorganized exports and removed obsolete helper branches
- Removed export leakage by demoting many helper and const symbols to module-local scope.
- Renamed underscore-prefixed internals and cache fields, then updated related references and `satisfies never` checks.
- Deleted obsolete logic branches and helpers, including harmony-stream interruption flow and unused benchmark runtime helpers.
- Updated Biome config and manifests by broadening lint coverage and removing an unused `@napi-rs/cli` dev dependency.
- Adjusted tests and utilities to use renamed test helpers and remove redundant private test-only helpers/locals.
2026-05-14 04:36:19 +02:00

93 lines
3.4 KiB
TypeScript

import { afterEach, describe, expect, it } from "bun:test";
import * as fs from "node:fs/promises";
import type { Model } from "@oh-my-pi/pi-ai";
import type { ModelRegistry } from "@oh-my-pi/pi-coding-agent/config/model-registry";
import type { CustomToolContext } from "@oh-my-pi/pi-coding-agent/extensibility/custom-tools";
import type { ReadonlySessionManager } from "@oh-my-pi/pi-coding-agent/session/session-manager";
import { imageGenTool, setPreferredImageProvider } from "@oh-my-pi/pi-coding-agent/tools/image-gen";
const originalFetch = global.fetch;
const originalOpenRouterKey = Bun.env.OPENROUTER_API_KEY;
const generatedImagePaths: string[] = [];
afterEach(async () => {
await Promise.all(generatedImagePaths.splice(0).map(imagePath => fs.rm(imagePath, { force: true })));
global.fetch = originalFetch;
if (originalOpenRouterKey === undefined) {
delete Bun.env.OPENROUTER_API_KEY;
} else {
Bun.env.OPENROUTER_API_KEY = originalOpenRouterKey;
}
setPreferredImageProvider("auto");
});
describe("imageGenTool", () => {
it("e2e writes OpenAI Responses image_generation WebP output to a temp file", async () => {
let requestUrl: string | undefined;
let requestBody: unknown;
const fetchMock: typeof fetch = (async (input: string | URL | Request, init?: RequestInit) => {
requestUrl = input.toString();
requestBody = JSON.parse(String(init?.body));
return new Response(
JSON.stringify({
output: [
{
type: "image_generation_call",
result: Buffer.from("fake-webp").toString("base64"),
revised_prompt: "A crisp tabby cat portrait.",
status: "completed",
},
],
usage: { input_tokens: 10, output_tokens: 20, total_tokens: 30 },
}),
{ status: 200, headers: { "content-type": "application/json" } },
);
}) as unknown as typeof fetch;
fetchMock.preconnect = originalFetch.preconnect;
global.fetch = fetchMock;
const model = {
api: "openai-responses",
provider: "openai",
id: "gpt-5.5",
name: "GPT 5.5",
baseUrl: "https://api.openai.com/v1",
} as Model;
const ctx: CustomToolContext = {
sessionManager: {
getCwd: () => "/tmp",
getSessionId: () => "test-session",
} as unknown as ReadonlySessionManager,
modelRegistry: {
getApiKey: async () => "test-openai-key",
getApiKeyForProvider: async () => undefined,
} as unknown as ModelRegistry,
model,
isIdle: () => true,
hasQueuedMessages: () => false,
abort: () => {},
};
const result = await imageGenTool.execute("call-1", { subject: "a cat", aspect_ratio: "16:9" }, undefined, ctx);
generatedImagePaths.push(...(result.details?.imagePaths ?? []));
expect(requestUrl).toBe("https://api.openai.com/v1/responses");
expect(requestBody).toMatchObject({
model: "gpt-5.5",
tools: [{ type: "image_generation", output_format: "webp", size: "1536x1024", action: "generate" }],
tool_choice: { type: "image_generation" },
store: false,
});
expect(result.details?.provider).toBe("openai");
expect(result.details?.imageCount).toBe(1);
expect(result.details?.images[0]?.mimeType).toBe("image/webp");
expect(result.details?.revisedPrompt).toBe("A crisp tabby cat portrait.");
expect(result.details?.imagePaths).toHaveLength(1);
const savedPath = result.details?.imagePaths[0];
if (!savedPath) throw new Error("Expected generated image path");
expect(savedPath.endsWith(".webp")).toBe(true);
expect(await Bun.file(savedPath).bytes()).toEqual(Buffer.from("fake-webp"));
});
});