|
| 1 | +import * as providerAuth from "openclaw/plugin-sdk/provider-auth-runtime"; |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { buildVydraImageGenerationProvider } from "./image-generation-provider.js"; |
| 4 | + |
| 5 | +describe("vydra image-generation provider", () => { |
| 6 | + afterEach(() => { |
| 7 | + vi.unstubAllGlobals(); |
| 8 | + vi.restoreAllMocks(); |
| 9 | + }); |
| 10 | + |
| 11 | + it("posts to the www api and downloads the generated image", async () => { |
| 12 | + vi.spyOn(providerAuth, "resolveApiKeyForProvider").mockResolvedValue({ |
| 13 | + apiKey: "vydra-test-key", |
| 14 | + source: "env", |
| 15 | + mode: "api-key", |
| 16 | + }); |
| 17 | + const fetchMock = vi |
| 18 | + .fn() |
| 19 | + .mockResolvedValueOnce( |
| 20 | + new Response( |
| 21 | + JSON.stringify({ |
| 22 | + jobId: "job-123", |
| 23 | + status: "completed", |
| 24 | + imageUrl: "https://cdn.vydra.ai/generated/test.png", |
| 25 | + }), |
| 26 | + { |
| 27 | + status: 200, |
| 28 | + headers: { "Content-Type": "application/json" }, |
| 29 | + }, |
| 30 | + ), |
| 31 | + ) |
| 32 | + .mockResolvedValueOnce( |
| 33 | + new Response(Buffer.from("png-data"), { |
| 34 | + status: 200, |
| 35 | + headers: { "Content-Type": "image/png" }, |
| 36 | + }), |
| 37 | + ); |
| 38 | + vi.stubGlobal("fetch", fetchMock); |
| 39 | + |
| 40 | + const provider = buildVydraImageGenerationProvider(); |
| 41 | + const result = await provider.generateImage({ |
| 42 | + provider: "vydra", |
| 43 | + model: "grok-imagine", |
| 44 | + prompt: "draw a cat", |
| 45 | + cfg: {}, |
| 46 | + }); |
| 47 | + |
| 48 | + expect(fetchMock).toHaveBeenNthCalledWith( |
| 49 | + 1, |
| 50 | + "https://www.vydra.ai/api/v1/models/grok-imagine", |
| 51 | + expect.objectContaining({ |
| 52 | + method: "POST", |
| 53 | + body: JSON.stringify({ |
| 54 | + prompt: "draw a cat", |
| 55 | + model: "text-to-image", |
| 56 | + }), |
| 57 | + }), |
| 58 | + ); |
| 59 | + const [, init] = fetchMock.mock.calls[0] as [string, RequestInit]; |
| 60 | + const headers = new Headers(init.headers); |
| 61 | + expect(headers.get("authorization")).toBe("Bearer vydra-test-key"); |
| 62 | + expect(result).toEqual({ |
| 63 | + images: [ |
| 64 | + { |
| 65 | + buffer: Buffer.from("png-data"), |
| 66 | + mimeType: "image/png", |
| 67 | + fileName: "image-1.png", |
| 68 | + }, |
| 69 | + ], |
| 70 | + model: "grok-imagine", |
| 71 | + metadata: { |
| 72 | + jobId: "job-123", |
| 73 | + imageUrl: "https://cdn.vydra.ai/generated/test.png", |
| 74 | + status: "completed", |
| 75 | + }, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("polls jobs when the create response is not completed yet", async () => { |
| 80 | + vi.spyOn(providerAuth, "resolveApiKeyForProvider").mockResolvedValue({ |
| 81 | + apiKey: "vydra-test-key", |
| 82 | + source: "env", |
| 83 | + mode: "api-key", |
| 84 | + }); |
| 85 | + const fetchMock = vi |
| 86 | + .fn() |
| 87 | + .mockResolvedValueOnce( |
| 88 | + new Response(JSON.stringify({ jobId: "job-456", status: "queued" }), { |
| 89 | + status: 200, |
| 90 | + headers: { "Content-Type": "application/json" }, |
| 91 | + }), |
| 92 | + ) |
| 93 | + .mockResolvedValueOnce( |
| 94 | + new Response( |
| 95 | + JSON.stringify({ |
| 96 | + jobId: "job-456", |
| 97 | + status: "completed", |
| 98 | + resultUrls: ["https://cdn.vydra.ai/generated/polled.png"], |
| 99 | + }), |
| 100 | + { |
| 101 | + status: 200, |
| 102 | + headers: { "Content-Type": "application/json" }, |
| 103 | + }, |
| 104 | + ), |
| 105 | + ) |
| 106 | + .mockResolvedValueOnce( |
| 107 | + new Response(Buffer.from("png-data"), { |
| 108 | + status: 200, |
| 109 | + headers: { "Content-Type": "image/png" }, |
| 110 | + }), |
| 111 | + ); |
| 112 | + vi.stubGlobal("fetch", fetchMock); |
| 113 | + |
| 114 | + const provider = buildVydraImageGenerationProvider(); |
| 115 | + await provider.generateImage({ |
| 116 | + provider: "vydra", |
| 117 | + model: "grok-imagine", |
| 118 | + prompt: "draw a cat", |
| 119 | + cfg: {}, |
| 120 | + }); |
| 121 | + |
| 122 | + expect(fetchMock).toHaveBeenNthCalledWith( |
| 123 | + 2, |
| 124 | + "https://www.vydra.ai/api/v1/jobs/job-456", |
| 125 | + expect.objectContaining({ method: "GET" }), |
| 126 | + ); |
| 127 | + }); |
| 128 | +}); |
0 commit comments