import { expect, test } from "@playwright/test"; import { serverLoader } from "../helpers/canvas-download-server-loader"; const { canUseImageFallback } = serverLoader({ "@/lib/ai/dynamic-provider": {}, "@/lib/ai/providers/fal-image": {}, "./image-media": {}, })("app/(chat)/api/chat/_lib/image-failover.ts"); const candidate = { provider: { id: "account", type: "vip98k" }, model: { modelId: "nano-banana-2-2k-cl", supportsVision: true, imageGenConfig: { supportedImageSizes: ["2K"], supportedAspectRatios: ["1:1", "3:4"], maxReferenceImages: 2, maxImages: 1 }, }, }; const params = { imageSize: "2K", aspectRatio: "3:4", imageCount: 1 }; const messages = [{ role: "user", parts: [ { type: "text", text: "Replace the bookshelf" }, { type: "file", mediaType: "image/png", url: "https://fixture.invalid/source.png" }, ] }]; test("a fixed 2K fallback never receives a 1K or 4K request", () => { for (const imageSize of ["1K", "4K"]) { expect(canUseImageFallback(candidate, { ...params, imageSize }, messages, new Set())).toBe(false); } expect(canUseImageFallback(candidate, params, messages, new Set())).toBe(true); }); test("fallback preserves image ratio, quantity and all reference images", () => { expect(canUseImageFallback(candidate, { ...params, aspectRatio: "16:9" }, messages, new Set())).toBe(false); expect(canUseImageFallback(candidate, { ...params, imageCount: 2 }, messages, new Set())).toBe(false); expect(canUseImageFallback(candidate, params, [messages[0], messages[0], messages[0]], new Set())).toBe(false); const textOnly = { ...candidate, model: { ...candidate.model, supportsVision: false } }; expect(canUseImageFallback(textOnly, params, messages, new Set())).toBe(false); expect(canUseImageFallback(textOnly, params, [], new Set())).toBe(true); expect(canUseImageFallback(candidate, { ...params, aspectRatio: "auto" }, messages, new Set())).toBe(true); }); test("an exhausted account is skipped across models, while a compatible independent account is eligible", () => { const blocked = new Set(["account"]); expect(canUseImageFallback(candidate, params, messages, blocked)).toBe(false); expect(canUseImageFallback({ ...candidate, model: { ...candidate.model, modelId: "another-model" } }, params, messages, blocked)).toBe(false); expect(canUseImageFallback({ ...candidate, provider: { ...candidate.provider, id: "other-account" } }, params, messages, blocked)).toBe(true); });