|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { buildRunwayVideoGenerationProvider } from "./video-generation-provider.js"; |
| 3 | + |
| 4 | +const { |
| 5 | + resolveApiKeyForProviderMock, |
| 6 | + postJsonRequestMock, |
| 7 | + fetchWithTimeoutMock, |
| 8 | + assertOkOrThrowHttpErrorMock, |
| 9 | + resolveProviderHttpRequestConfigMock, |
| 10 | +} = vi.hoisted(() => ({ |
| 11 | + resolveApiKeyForProviderMock: vi.fn(async () => ({ apiKey: "runway-key" })), |
| 12 | + postJsonRequestMock: vi.fn(), |
| 13 | + fetchWithTimeoutMock: vi.fn(), |
| 14 | + assertOkOrThrowHttpErrorMock: vi.fn(async () => {}), |
| 15 | + resolveProviderHttpRequestConfigMock: vi.fn((params) => ({ |
| 16 | + baseUrl: params.baseUrl ?? params.defaultBaseUrl, |
| 17 | + allowPrivateNetwork: false, |
| 18 | + headers: new Headers(params.defaultHeaders), |
| 19 | + dispatcherPolicy: undefined, |
| 20 | + })), |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock("openclaw/plugin-sdk/provider-auth-runtime", () => ({ |
| 24 | + resolveApiKeyForProvider: resolveApiKeyForProviderMock, |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock("openclaw/plugin-sdk/provider-http", () => ({ |
| 28 | + assertOkOrThrowHttpError: assertOkOrThrowHttpErrorMock, |
| 29 | + fetchWithTimeout: fetchWithTimeoutMock, |
| 30 | + postJsonRequest: postJsonRequestMock, |
| 31 | + resolveProviderHttpRequestConfig: resolveProviderHttpRequestConfigMock, |
| 32 | +})); |
| 33 | + |
| 34 | +describe("runway video generation provider", () => { |
| 35 | + afterEach(() => { |
| 36 | + resolveApiKeyForProviderMock.mockClear(); |
| 37 | + postJsonRequestMock.mockReset(); |
| 38 | + fetchWithTimeoutMock.mockReset(); |
| 39 | + assertOkOrThrowHttpErrorMock.mockClear(); |
| 40 | + resolveProviderHttpRequestConfigMock.mockClear(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("submits a text-to-video task, polls it, and downloads the output", async () => { |
| 44 | + postJsonRequestMock.mockResolvedValue({ |
| 45 | + response: { |
| 46 | + json: async () => ({ |
| 47 | + id: "task-1", |
| 48 | + }), |
| 49 | + }, |
| 50 | + release: vi.fn(async () => {}), |
| 51 | + }); |
| 52 | + fetchWithTimeoutMock |
| 53 | + .mockResolvedValueOnce({ |
| 54 | + json: async () => ({ |
| 55 | + id: "task-1", |
| 56 | + status: "SUCCEEDED", |
| 57 | + output: ["https://example.com/out.mp4"], |
| 58 | + }), |
| 59 | + headers: new Headers(), |
| 60 | + }) |
| 61 | + .mockResolvedValueOnce({ |
| 62 | + arrayBuffer: async () => Buffer.from("mp4-bytes"), |
| 63 | + headers: new Headers({ "content-type": "video/mp4" }), |
| 64 | + }); |
| 65 | + |
| 66 | + const provider = buildRunwayVideoGenerationProvider(); |
| 67 | + const result = await provider.generateVideo({ |
| 68 | + provider: "runway", |
| 69 | + model: "gen4.5", |
| 70 | + prompt: "a tiny lobster DJ under neon lights", |
| 71 | + cfg: {}, |
| 72 | + durationSeconds: 4, |
| 73 | + aspectRatio: "16:9", |
| 74 | + }); |
| 75 | + |
| 76 | + expect(postJsonRequestMock).toHaveBeenCalledWith( |
| 77 | + expect.objectContaining({ |
| 78 | + url: "https://api.dev.runwayml.com/v1/text_to_video", |
| 79 | + body: { |
| 80 | + model: "gen4.5", |
| 81 | + promptText: "a tiny lobster DJ under neon lights", |
| 82 | + ratio: "1280:720", |
| 83 | + duration: 4, |
| 84 | + }, |
| 85 | + }), |
| 86 | + ); |
| 87 | + expect(fetchWithTimeoutMock).toHaveBeenNthCalledWith( |
| 88 | + 1, |
| 89 | + "https://api.dev.runwayml.com/v1/tasks/task-1", |
| 90 | + expect.objectContaining({ method: "GET" }), |
| 91 | + 120000, |
| 92 | + fetch, |
| 93 | + ); |
| 94 | + expect(result.videos).toHaveLength(1); |
| 95 | + expect(result.metadata).toEqual( |
| 96 | + expect.objectContaining({ |
| 97 | + taskId: "task-1", |
| 98 | + status: "SUCCEEDED", |
| 99 | + endpoint: "/v1/text_to_video", |
| 100 | + }), |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it("accepts local image buffers by converting them into data URIs", async () => { |
| 105 | + postJsonRequestMock.mockResolvedValue({ |
| 106 | + response: { |
| 107 | + json: async () => ({ id: "task-2" }), |
| 108 | + }, |
| 109 | + release: vi.fn(async () => {}), |
| 110 | + }); |
| 111 | + fetchWithTimeoutMock |
| 112 | + .mockResolvedValueOnce({ |
| 113 | + json: async () => ({ |
| 114 | + id: "task-2", |
| 115 | + status: "SUCCEEDED", |
| 116 | + output: ["https://example.com/out.mp4"], |
| 117 | + }), |
| 118 | + headers: new Headers(), |
| 119 | + }) |
| 120 | + .mockResolvedValueOnce({ |
| 121 | + arrayBuffer: async () => Buffer.from("mp4-bytes"), |
| 122 | + headers: new Headers({ "content-type": "video/mp4" }), |
| 123 | + }); |
| 124 | + |
| 125 | + const provider = buildRunwayVideoGenerationProvider(); |
| 126 | + await provider.generateVideo({ |
| 127 | + provider: "runway", |
| 128 | + model: "gen4_turbo", |
| 129 | + prompt: "animate this frame", |
| 130 | + cfg: {}, |
| 131 | + inputImages: [{ buffer: Buffer.from("png-bytes"), mimeType: "image/png" }], |
| 132 | + aspectRatio: "1:1", |
| 133 | + durationSeconds: 6, |
| 134 | + }); |
| 135 | + |
| 136 | + expect(postJsonRequestMock).toHaveBeenCalledWith( |
| 137 | + expect.objectContaining({ |
| 138 | + url: "https://api.dev.runwayml.com/v1/image_to_video", |
| 139 | + body: expect.objectContaining({ |
| 140 | + promptImage: expect.stringMatching(/^data:image\/png;base64,/u), |
| 141 | + ratio: "960:960", |
| 142 | + duration: 6, |
| 143 | + }), |
| 144 | + }), |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + it("requires gen4_aleph for video-to-video", async () => { |
| 149 | + const provider = buildRunwayVideoGenerationProvider(); |
| 150 | + |
| 151 | + await expect( |
| 152 | + provider.generateVideo({ |
| 153 | + provider: "runway", |
| 154 | + model: "gen4.5", |
| 155 | + prompt: "restyle this clip", |
| 156 | + cfg: {}, |
| 157 | + inputVideos: [{ url: "https://example.com/input.mp4" }], |
| 158 | + }), |
| 159 | + ).rejects.toThrow("Runway video-to-video currently requires model gen4_aleph."); |
| 160 | + expect(postJsonRequestMock).not.toHaveBeenCalled(); |
| 161 | + }); |
| 162 | +}); |
0 commit comments