| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import { beforeEach, describe, expect, it, vi } from "vitest";
- import { generateImages } from "../src/images.ts";
- import type { ImagesContext, ImagesModel } from "../src/types.ts";
- const mockState = vi.hoisted(() => ({
- clientOptions: [] as unknown[],
- lastParams: undefined as unknown,
- lastRequestOptions: undefined as unknown,
- }));
- vi.mock("openai", () => {
- class FakeOpenAI {
- constructor(options: unknown) {
- mockState.clientOptions.push(options);
- }
- chat = {
- completions: {
- create: (params: unknown, requestOptions?: unknown) => {
- mockState.lastParams = params;
- mockState.lastRequestOptions = requestOptions;
- const signal = (requestOptions as { signal?: AbortSignal } | undefined)?.signal;
- if (signal?.aborted) {
- const error = new Error("Request aborted");
- return {
- withResponse: async () => {
- throw error;
- },
- };
- }
- const response = {
- id: "img-1",
- usage: {
- prompt_tokens: 12,
- completion_tokens: 34,
- prompt_tokens_details: { cached_tokens: 0 },
- },
- choices: [
- {
- message: {
- content: "Here is your image.",
- images: [{ image_url: "data:image/png;base64,ZmFrZS1wbmc=" }],
- },
- },
- ],
- };
- const promise = Promise.resolve(response) as Promise<typeof response> & {
- withResponse: () => Promise<{
- data: typeof response;
- response: { status: number; headers: Headers };
- }>;
- };
- promise.withResponse = async () => ({
- data: response,
- response: { status: 200, headers: new Headers() },
- });
- return promise;
- },
- },
- };
- }
- return { default: FakeOpenAI };
- });
- describe("openrouter images", () => {
- beforeEach(() => {
- mockState.clientOptions = [];
- mockState.lastParams = undefined;
- mockState.lastRequestOptions = undefined;
- });
- it("returns text plus images in final output", async () => {
- const model: ImagesModel<"openrouter-images"> = {
- id: "google/gemini-3.1-flash-image-preview",
- name: "Gemini 3.1 Flash Image Preview",
- api: "openrouter-images",
- provider: "openrouter",
- baseUrl: "https://openrouter.ai/api/v1",
- input: ["text", "image"],
- output: ["text", "image"],
- cost: { input: 0.015, output: 0.03, cacheRead: 0, cacheWrite: 0 },
- headers: { "HTTP-Referer": "https://example.com" },
- };
- const context: ImagesContext = {
- input: [{ type: "text", text: "Generate a dog" }],
- };
- const output = await generateImages(model, context, { apiKey: "test" });
- expect(output.stopReason).toBe("stop");
- expect(output.responseId).toBe("img-1");
- expect(output.output[0]).toMatchObject({ type: "text", text: "Here is your image." });
- expect(output.output[1]).toMatchObject({ type: "image", mimeType: "image/png", data: "ZmFrZS1wbmc=" });
- const params = mockState.lastParams as {
- stream?: boolean;
- modalities?: string[];
- messages?: [{ content?: [{ type: string; text?: string }] }];
- };
- expect(params.stream).toBe(false);
- expect(params.modalities).toEqual(["image", "text"]);
- expect(params.messages?.[0]?.content?.[0]).toMatchObject({ type: "text", text: "Generate a dog" });
- });
- it("passes custom fetch to the OpenAI image SDK client", async () => {
- const model: ImagesModel<"openrouter-images"> = {
- id: "google/gemini-3.1-flash-image-preview",
- name: "Gemini 3.1 Flash Image Preview",
- api: "openrouter-images",
- provider: "openrouter",
- baseUrl: "https://openrouter.ai/api/v1",
- input: ["text"],
- output: ["image"],
- cost: { input: 0.015, output: 0.03, cacheRead: 0, cacheWrite: 0 },
- };
- const fetchImpl = (async () => new Response()) as typeof globalThis.fetch;
- await generateImages(model, { input: [{ type: "text", text: "Generate a dog" }] }, { apiKey: "test", fetch: fetchImpl });
- expect(mockState.clientOptions[0]).toEqual(expect.objectContaining({ fetch: fetchImpl }));
- });
- it("passes through abort signal and returns aborted result", async () => {
- const model: ImagesModel<"openrouter-images"> = {
- id: "black-forest-labs/flux.2-pro",
- name: "FLUX.2 Pro",
- api: "openrouter-images",
- provider: "openrouter",
- baseUrl: "https://openrouter.ai/api/v1",
- input: ["text", "image"],
- output: ["image"],
- cost: { input: 0.015, output: 0.03, cacheRead: 0, cacheWrite: 0 },
- };
- const context: ImagesContext = {
- input: [{ type: "text", text: "Generate a dog" }],
- };
- const controller = new AbortController();
- controller.abort();
- const output = await generateImages(model, context, { apiKey: "test", signal: controller.signal });
- expect(output.stopReason).toBe("aborted");
- expect(output.errorMessage).toBe("Request aborted");
- expect(mockState.lastRequestOptions).toMatchObject({ signal: controller.signal });
- });
- it("generateImages resolves the final assistant images result", async () => {
- const model: ImagesModel<"openrouter-images"> = {
- id: "black-forest-labs/flux.2-pro",
- name: "FLUX.2 Pro",
- api: "openrouter-images",
- provider: "openrouter",
- baseUrl: "https://openrouter.ai/api/v1",
- input: ["text", "image"],
- output: ["image"],
- cost: { input: 0.015, output: 0.03, cacheRead: 0, cacheWrite: 0 },
- };
- const context: ImagesContext = {
- input: [{ type: "text", text: "Generate a dog" }],
- };
- const output = await generateImages(model, context, { apiKey: "test" });
- expect(output.output.some((item) => item.type === "image")).toBe(true);
- });
- });
|