|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { afterEach, describe, expect, it } from "vitest"; |
| 4 | +import type { PreparedModelRuntimeSnapshot } from "../../../../src/agents/prepared-model-runtime.js"; |
| 5 | +import { createVideoGenerateTool } from "../../../../src/agents/tools/video-generate-tool.js"; |
| 6 | +import type { OpenClawConfig } from "../../../../src/config/types.js"; |
| 7 | +import { withEnvAsync } from "../../../../src/test-utils/env.js"; |
| 8 | +import { resolveVideoGenerationMode } from "../../../../src/video-generation/capabilities.js"; |
| 9 | +import type { |
| 10 | + VideoGenerationProvider, |
| 11 | + VideoGenerationRequest, |
| 12 | +} from "../../../../src/video-generation/types.js"; |
| 13 | +import { createSolidPngBuffer } from "../../../helpers/image-fixtures.js"; |
| 14 | +import { useAutoCleanupTempDirTracker } from "../../../helpers/temp-dir.js"; |
| 15 | + |
| 16 | +function createMp4Fixture(): Buffer { |
| 17 | + return Buffer.from([ |
| 18 | + 0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6f, 0x6d, 0x00, 0x00, 0x02, 0x00, |
| 19 | + 0x69, 0x73, 0x6f, 0x6d, 0x6d, 0x70, 0x34, 0x31, |
| 20 | + ]); |
| 21 | +} |
| 22 | + |
| 23 | +function createPreparedRuntime(providers: VideoGenerationProvider[]): PreparedModelRuntimeSnapshot { |
| 24 | + return { |
| 25 | + mediaCapabilityProviders: { |
| 26 | + videoGenerationProviders: providers, |
| 27 | + }, |
| 28 | + } as unknown as PreparedModelRuntimeSnapshot; |
| 29 | +} |
| 30 | + |
| 31 | +function createConfig(primary: string, fallbacks: string[]): OpenClawConfig { |
| 32 | + return { |
| 33 | + agents: { |
| 34 | + defaults: { |
| 35 | + mediaModels: { |
| 36 | + video: { primary, fallbacks }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +function requireVideoTool(tool: ReturnType<typeof createVideoGenerateTool>) { |
| 44 | + if (!tool) { |
| 45 | + throw new Error("expected video_generate tool"); |
| 46 | + } |
| 47 | + return tool; |
| 48 | +} |
| 49 | + |
| 50 | +function requireDetails(result: { details?: unknown }): Record<string, unknown> { |
| 51 | + if (!result.details || typeof result.details !== "object") { |
| 52 | + throw new Error("expected video generation result details"); |
| 53 | + } |
| 54 | + return result.details as Record<string, unknown>; |
| 55 | +} |
| 56 | + |
| 57 | +describe("video generation invocation QA", () => { |
| 58 | + const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 59 | + |
| 60 | + it("selects image-to-video fallback, forwards declared options, and persists video bytes", async () => { |
| 61 | + const root = await fs.realpath(tempDirs.make("openclaw-qa-video-invocation-")); |
| 62 | + const referenceBytes = createSolidPngBuffer(2, 2, { r: 32, g: 112, b: 224 }); |
| 63 | + const referencePath = path.join(root, "reference.png"); |
| 64 | + await fs.writeFile(referencePath, referenceBytes); |
| 65 | + |
| 66 | + const generatedVideo = createMp4Fixture(); |
| 67 | + let primaryGenerateCalls = 0; |
| 68 | + let fallbackRequest: VideoGenerationRequest | undefined; |
| 69 | + const primaryProvider: VideoGenerationProvider = { |
| 70 | + id: "qa-limited-video", |
| 71 | + defaultModel: "limited-v1", |
| 72 | + models: ["limited-v1"], |
| 73 | + isConfigured: () => true, |
| 74 | + capabilities: { |
| 75 | + imageToVideo: { |
| 76 | + enabled: true, |
| 77 | + maxInputImages: 1, |
| 78 | + providerOptions: { seed: "number", draft: "boolean" }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + resolveModelCapabilities: async () => ({ |
| 82 | + imageToVideo: { |
| 83 | + enabled: false, |
| 84 | + }, |
| 85 | + }), |
| 86 | + generateVideo: async () => { |
| 87 | + primaryGenerateCalls += 1; |
| 88 | + throw new Error("unsupported provider must be skipped before forwarding"); |
| 89 | + }, |
| 90 | + }; |
| 91 | + const fallbackProvider: VideoGenerationProvider = { |
| 92 | + id: "qa-capable-video", |
| 93 | + defaultModel: "capable-v1", |
| 94 | + models: ["capable-v1"], |
| 95 | + isConfigured: () => true, |
| 96 | + capabilities: { |
| 97 | + imageToVideo: { |
| 98 | + enabled: true, |
| 99 | + maxInputImages: 1, |
| 100 | + providerOptions: { seed: "number", draft: "boolean" }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + generateVideo: async (request) => { |
| 104 | + fallbackRequest = request; |
| 105 | + return { |
| 106 | + model: request.model, |
| 107 | + videos: [ |
| 108 | + { |
| 109 | + buffer: generatedVideo, |
| 110 | + mimeType: "video/mp4", |
| 111 | + fileName: "provider-result.mp4", |
| 112 | + }, |
| 113 | + ], |
| 114 | + }; |
| 115 | + }, |
| 116 | + }; |
| 117 | + const providers = [primaryProvider, fallbackProvider]; |
| 118 | + const config = createConfig("qa-limited-video/limited-v1", ["qa-capable-video/capable-v1"]); |
| 119 | + const providerOptions = { seed: 17, draft: true }; |
| 120 | + |
| 121 | + await withEnvAsync({ OPENCLAW_STATE_DIR: path.join(root, "state") }, async () => { |
| 122 | + const tool = requireVideoTool( |
| 123 | + createVideoGenerateTool({ |
| 124 | + config, |
| 125 | + agentDir: path.join(root, "agent"), |
| 126 | + workspaceDir: root, |
| 127 | + preparedModelRuntime: createPreparedRuntime(providers), |
| 128 | + }), |
| 129 | + ); |
| 130 | + const result = await tool.execute("qa-video-invocation", { |
| 131 | + prompt: "Animate the reference image into a short QA clip.", |
| 132 | + image: referencePath, |
| 133 | + imageRoles: ["first_frame"], |
| 134 | + providerOptions, |
| 135 | + filename: "qa-selected-video.mp4", |
| 136 | + }); |
| 137 | + const details = requireDetails(result); |
| 138 | + |
| 139 | + expect(primaryGenerateCalls).toBe(0); |
| 140 | + expect(fallbackRequest).toMatchObject({ |
| 141 | + provider: "qa-capable-video", |
| 142 | + model: "capable-v1", |
| 143 | + providerOptions, |
| 144 | + }); |
| 145 | + expect( |
| 146 | + resolveVideoGenerationMode({ |
| 147 | + inputImageCount: fallbackRequest?.inputImages?.length, |
| 148 | + inputVideoCount: fallbackRequest?.inputVideos?.length, |
| 149 | + }), |
| 150 | + ).toBe("imageToVideo"); |
| 151 | + expect(fallbackRequest?.inputImages).toEqual([ |
| 152 | + { |
| 153 | + buffer: referenceBytes, |
| 154 | + mimeType: "image/png", |
| 155 | + fileName: "reference.png", |
| 156 | + role: "first_frame", |
| 157 | + }, |
| 158 | + ]); |
| 159 | + expect(details.provider).toBe("qa-capable-video"); |
| 160 | + expect(details.model).toBe("capable-v1"); |
| 161 | + expect(details.count).toBe(1); |
| 162 | + expect(details.attempts).toEqual([ |
| 163 | + { |
| 164 | + provider: "qa-limited-video", |
| 165 | + model: "limited-v1", |
| 166 | + error: expect.stringContaining( |
| 167 | + "does not support reference image inputs; skipping to avoid silent reference drop", |
| 168 | + ), |
| 169 | + }, |
| 170 | + ]); |
| 171 | + |
| 172 | + const savedPaths = details.paths as string[]; |
| 173 | + expect(savedPaths).toHaveLength(1); |
| 174 | + const savedPath = savedPaths[0]; |
| 175 | + if (!savedPath) { |
| 176 | + throw new Error("expected saved video path"); |
| 177 | + } |
| 178 | + const savedStat = await fs.stat(savedPath); |
| 179 | + expect(savedStat.isFile()).toBe(true); |
| 180 | + expect(savedStat.size).toBe(generatedVideo.byteLength); |
| 181 | + await expect(fs.readFile(savedPath)).resolves.toEqual(generatedVideo); |
| 182 | + expect(details.attachments).toEqual([ |
| 183 | + expect.objectContaining({ |
| 184 | + type: "video", |
| 185 | + path: savedPath, |
| 186 | + mimeType: "video/mp4", |
| 187 | + sizeBytes: generatedVideo.byteLength, |
| 188 | + }), |
| 189 | + ]); |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + it("rejects unknown and wrong-typed provider options before provider invocation", async () => { |
| 194 | + let providerCalls = 0; |
| 195 | + const createProvider = (id: string, model: string): VideoGenerationProvider => ({ |
| 196 | + id, |
| 197 | + defaultModel: model, |
| 198 | + models: [model], |
| 199 | + isConfigured: () => true, |
| 200 | + capabilities: { |
| 201 | + providerOptions: { seed: "number", draft: "boolean" }, |
| 202 | + }, |
| 203 | + generateVideo: async () => { |
| 204 | + providerCalls += 1; |
| 205 | + return { |
| 206 | + videos: [{ buffer: createMp4Fixture(), mimeType: "video/mp4" }], |
| 207 | + }; |
| 208 | + }, |
| 209 | + }); |
| 210 | + const providers = [ |
| 211 | + createProvider("qa-options-primary", "primary-v1"), |
| 212 | + createProvider("qa-options-fallback", "fallback-v1"), |
| 213 | + ]; |
| 214 | + const tool = requireVideoTool( |
| 215 | + createVideoGenerateTool({ |
| 216 | + config: createConfig("qa-options-primary/primary-v1", ["qa-options-fallback/fallback-v1"]), |
| 217 | + preparedModelRuntime: createPreparedRuntime(providers), |
| 218 | + }), |
| 219 | + ); |
| 220 | + |
| 221 | + await expect( |
| 222 | + tool.execute("qa-video-options-unknown", { |
| 223 | + prompt: "Generate a QA clip.", |
| 224 | + providerOptions: { seed: 21, unknown_option: true }, |
| 225 | + }), |
| 226 | + ).rejects.toThrow(/does not accept providerOptions keys: unknown_option/); |
| 227 | + await expect( |
| 228 | + tool.execute("qa-video-options-type", { |
| 229 | + prompt: "Generate another QA clip.", |
| 230 | + providerOptions: { seed: "twenty-one" }, |
| 231 | + }), |
| 232 | + ).rejects.toThrow(/expects providerOptions\.seed to be a finite number, got string/); |
| 233 | + expect(providerCalls).toBe(0); |
| 234 | + }); |
| 235 | +}); |
0 commit comments