|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { buildAlibabaVideoGenerationProvider } 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: "alibaba-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("alibaba 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 async Wan generation, polls task status, and downloads the resulting video", async () => { |
| 44 | + postJsonRequestMock.mockResolvedValue({ |
| 45 | + response: { |
| 46 | + json: async () => ({ |
| 47 | + request_id: "req-1", |
| 48 | + output: { |
| 49 | + task_id: "task-1", |
| 50 | + }, |
| 51 | + }), |
| 52 | + }, |
| 53 | + release: vi.fn(async () => {}), |
| 54 | + }); |
| 55 | + fetchWithTimeoutMock |
| 56 | + .mockResolvedValueOnce({ |
| 57 | + json: async () => ({ |
| 58 | + output: { |
| 59 | + task_status: "SUCCEEDED", |
| 60 | + results: [{ video_url: "https://example.com/out.mp4" }], |
| 61 | + }, |
| 62 | + }), |
| 63 | + headers: new Headers(), |
| 64 | + }) |
| 65 | + .mockResolvedValueOnce({ |
| 66 | + arrayBuffer: async () => Buffer.from("mp4-bytes"), |
| 67 | + headers: new Headers({ "content-type": "video/mp4" }), |
| 68 | + }); |
| 69 | + |
| 70 | + const provider = buildAlibabaVideoGenerationProvider(); |
| 71 | + const result = await provider.generateVideo({ |
| 72 | + provider: "alibaba", |
| 73 | + model: "wan2.6-r2v-flash", |
| 74 | + prompt: "animate this shot", |
| 75 | + cfg: {}, |
| 76 | + inputImages: [{ url: "https://example.com/ref.png" }], |
| 77 | + durationSeconds: 6, |
| 78 | + audio: true, |
| 79 | + watermark: false, |
| 80 | + }); |
| 81 | + |
| 82 | + expect(postJsonRequestMock).toHaveBeenCalledWith( |
| 83 | + expect.objectContaining({ |
| 84 | + url: "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/video-generation/video-synthesis", |
| 85 | + body: expect.objectContaining({ |
| 86 | + model: "wan2.6-r2v-flash", |
| 87 | + input: expect.objectContaining({ |
| 88 | + prompt: "animate this shot", |
| 89 | + img_url: "https://example.com/ref.png", |
| 90 | + }), |
| 91 | + parameters: expect.objectContaining({ |
| 92 | + duration: 6, |
| 93 | + enable_audio: true, |
| 94 | + watermark: false, |
| 95 | + }), |
| 96 | + }), |
| 97 | + }), |
| 98 | + ); |
| 99 | + expect(fetchWithTimeoutMock).toHaveBeenNthCalledWith( |
| 100 | + 1, |
| 101 | + "https://dashscope-intl.aliyuncs.com/api/v1/tasks/task-1", |
| 102 | + expect.objectContaining({ method: "GET" }), |
| 103 | + 120000, |
| 104 | + fetch, |
| 105 | + ); |
| 106 | + expect(result.videos).toHaveLength(1); |
| 107 | + expect(result.videos[0]?.mimeType).toBe("video/mp4"); |
| 108 | + expect(result.metadata).toEqual( |
| 109 | + expect.objectContaining({ |
| 110 | + requestId: "req-1", |
| 111 | + taskId: "task-1", |
| 112 | + taskStatus: "SUCCEEDED", |
| 113 | + }), |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it("fails fast when reference inputs are local buffers instead of remote URLs", async () => { |
| 118 | + const provider = buildAlibabaVideoGenerationProvider(); |
| 119 | + |
| 120 | + await expect( |
| 121 | + provider.generateVideo({ |
| 122 | + provider: "alibaba", |
| 123 | + model: "wan2.6-i2v", |
| 124 | + prompt: "animate this local frame", |
| 125 | + cfg: {}, |
| 126 | + inputImages: [{ buffer: Buffer.from("png-bytes"), mimeType: "image/png" }], |
| 127 | + }), |
| 128 | + ).rejects.toThrow( |
| 129 | + "Alibaba Wan video generation currently requires remote http(s) URLs for reference images/videos.", |
| 130 | + ); |
| 131 | + expect(postJsonRequestMock).not.toHaveBeenCalled(); |
| 132 | + }); |
| 133 | +}); |
0 commit comments