|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { afterEach, describe, expect, it } from "vitest"; |
| 4 | +import { createOpenClawTools } from "../../../../src/agents/openclaw-tools.js"; |
| 5 | +import type { PreparedModelRuntimeSnapshot } from "../../../../src/agents/prepared-model-runtime.js"; |
| 6 | +import { createPluginMetadataSnapshot } from "../../../../src/config/plugin-auto-enable.test-helpers.js"; |
| 7 | +import type { OpenClawConfig } from "../../../../src/config/types.openclaw.js"; |
| 8 | +import type { |
| 9 | + MusicGenerationProvider, |
| 10 | + MusicGenerationRequest, |
| 11 | +} from "../../../../src/music-generation/types.js"; |
| 12 | +import { |
| 13 | + withBundledPluginEnablementCompat, |
| 14 | + withBundledPluginVitestCompat, |
| 15 | +} from "../../../../src/plugins/bundled-compat.js"; |
| 16 | +import { prepareMediaCapabilityProviders } from "../../../../src/plugins/capability-provider-runtime.js"; |
| 17 | +import { |
| 18 | + captureCurrentPluginMetadataSnapshotState, |
| 19 | + restoreCurrentPluginMetadataSnapshotState, |
| 20 | + setCurrentPluginMetadataSnapshot, |
| 21 | +} from "../../../../src/plugins/current-plugin-metadata-snapshot.js"; |
| 22 | +import { resolvePluginRegistryLoadCacheKey } from "../../../../src/plugins/loader.js"; |
| 23 | +import type { PluginManifestRecord } from "../../../../src/plugins/manifest-registry.js"; |
| 24 | +import { createEmptyPluginRegistry } from "../../../../src/plugins/registry.js"; |
| 25 | +import { |
| 26 | + captureActivePluginRegistrySnapshot, |
| 27 | + restoreActivePluginRegistrySnapshot, |
| 28 | + setActivePluginRegistry, |
| 29 | +} from "../../../../src/plugins/runtime.js"; |
| 30 | +import { withEnvAsync } from "../../../../src/test-utils/env.js"; |
| 31 | +import { useAutoCleanupTempDirTracker } from "../../../helpers/temp-dir.js"; |
| 32 | + |
| 33 | +type ToolResult = { |
| 34 | + content?: Array<{ type: string; text?: string }>; |
| 35 | + details?: unknown; |
| 36 | +}; |
| 37 | + |
| 38 | +type ProviderCall = { |
| 39 | + provider: string; |
| 40 | + request: MusicGenerationRequest; |
| 41 | +}; |
| 42 | + |
| 43 | +const PLUGIN_ID = "qa-music-controls"; |
| 44 | + |
| 45 | +function detailsOf(result: ToolResult): Record<string, unknown> { |
| 46 | + if (!result.details || typeof result.details !== "object") { |
| 47 | + throw new Error("expected music_generate result details"); |
| 48 | + } |
| 49 | + return result.details as Record<string, unknown>; |
| 50 | +} |
| 51 | + |
| 52 | +function textOf(result: ToolResult): string { |
| 53 | + return result.content?.find((entry) => entry.type === "text")?.text ?? ""; |
| 54 | +} |
| 55 | + |
| 56 | +function createMusicFixture() { |
| 57 | + const calls: ProviderCall[] = []; |
| 58 | + const primary: MusicGenerationProvider = { |
| 59 | + id: "qa-primary", |
| 60 | + label: "QA primary music", |
| 61 | + defaultModel: "song-v1", |
| 62 | + models: ["song-v1"], |
| 63 | + capabilities: { |
| 64 | + generate: { |
| 65 | + maxDurationSeconds: 60, |
| 66 | + supportsLyrics: true, |
| 67 | + supportsInstrumental: true, |
| 68 | + supportsDuration: true, |
| 69 | + supportsFormat: true, |
| 70 | + supportedFormats: ["mp3", "wav"], |
| 71 | + }, |
| 72 | + }, |
| 73 | + isConfigured: () => true, |
| 74 | + generateMusic: async (request) => { |
| 75 | + calls.push({ provider: "qa-primary", request }); |
| 76 | + throw new Error("intentional primary provider outage"); |
| 77 | + }, |
| 78 | + }; |
| 79 | + const fallback: MusicGenerationProvider = { |
| 80 | + id: "qa-fallback", |
| 81 | + label: "QA fallback music", |
| 82 | + defaultModel: "beat-v2", |
| 83 | + models: ["beat-v2"], |
| 84 | + capabilities: { |
| 85 | + generate: { |
| 86 | + maxDurationSeconds: 30, |
| 87 | + supportsLyrics: false, |
| 88 | + supportsInstrumental: true, |
| 89 | + supportsDuration: true, |
| 90 | + supportsFormat: true, |
| 91 | + supportedFormats: ["mp3"], |
| 92 | + }, |
| 93 | + }, |
| 94 | + isConfigured: () => true, |
| 95 | + generateMusic: async (request) => { |
| 96 | + calls.push({ provider: "qa-fallback", request }); |
| 97 | + const kind = request.instrumental ? "instrumental" : "vocal-fallback"; |
| 98 | + return { |
| 99 | + tracks: [ |
| 100 | + { |
| 101 | + buffer: Buffer.from(`ID3-qa-${kind}-audio`), |
| 102 | + mimeType: "audio/mpeg", |
| 103 | + fileName: `${kind}.mp3`, |
| 104 | + }, |
| 105 | + ], |
| 106 | + model: request.model, |
| 107 | + }; |
| 108 | + }, |
| 109 | + }; |
| 110 | + const config: OpenClawConfig = { |
| 111 | + agents: { |
| 112 | + defaults: { |
| 113 | + mediaModels: { |
| 114 | + music: { |
| 115 | + primary: "qa-primary/song-v1", |
| 116 | + fallbacks: ["qa-fallback/beat-v2"], |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }; |
| 122 | + const registry = createEmptyPluginRegistry(); |
| 123 | + for (const provider of [primary, fallback]) { |
| 124 | + registry.musicGenerationProviders.push({ |
| 125 | + pluginId: PLUGIN_ID, |
| 126 | + pluginName: "QA music controls", |
| 127 | + source: "test", |
| 128 | + provider, |
| 129 | + }); |
| 130 | + } |
| 131 | + const manifest = { |
| 132 | + id: PLUGIN_ID, |
| 133 | + channels: [], |
| 134 | + providers: [], |
| 135 | + cliBackends: [], |
| 136 | + skills: [], |
| 137 | + hooks: [], |
| 138 | + origin: "bundled", |
| 139 | + rootDir: `/fake/${PLUGIN_ID}`, |
| 140 | + source: `/fake/${PLUGIN_ID}/index.js`, |
| 141 | + manifestPath: `/fake/${PLUGIN_ID}/openclaw.plugin.json`, |
| 142 | + contracts: { |
| 143 | + musicGenerationProviders: [primary.id, fallback.id], |
| 144 | + }, |
| 145 | + } satisfies PluginManifestRecord; |
| 146 | + const pluginMetadataSnapshot = createPluginMetadataSnapshot({ |
| 147 | + config, |
| 148 | + manifestRegistry: { |
| 149 | + plugins: [manifest], |
| 150 | + diagnostics: [], |
| 151 | + }, |
| 152 | + }); |
| 153 | + const mediaCapabilityProviders = prepareMediaCapabilityProviders({ |
| 154 | + cfg: config, |
| 155 | + pluginMetadataSnapshot, |
| 156 | + registry, |
| 157 | + }); |
| 158 | + expect(mediaCapabilityProviders.musicGenerationProviders?.map((provider) => provider.id)).toEqual( |
| 159 | + ["qa-primary", "qa-fallback"], |
| 160 | + ); |
| 161 | + |
| 162 | + const preparedModelRuntime = { |
| 163 | + metadataSnapshot: pluginMetadataSnapshot, |
| 164 | + mediaCapabilityProviders, |
| 165 | + } as unknown as PreparedModelRuntimeSnapshot; |
| 166 | + const musicTools = createOpenClawTools({ |
| 167 | + config, |
| 168 | + preparedModelRuntime, |
| 169 | + disableMessageTool: true, |
| 170 | + disablePluginTools: true, |
| 171 | + wrapBeforeToolCallHook: false, |
| 172 | + }).filter((candidate) => candidate.name === "music_generate"); |
| 173 | + expect(musicTools).toHaveLength(1); |
| 174 | + const [tool] = musicTools; |
| 175 | + if (!tool) { |
| 176 | + throw new Error( |
| 177 | + "expected production tool inventory to include music_generate for the prepared provider runtime", |
| 178 | + ); |
| 179 | + } |
| 180 | + return { calls, config, fallback, pluginMetadataSnapshot, primary, registry, tool }; |
| 181 | +} |
| 182 | + |
| 183 | +describe("music generation controls QA product proof", () => { |
| 184 | + const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 185 | + |
| 186 | + it("lists capabilities, falls back in config order, normalizes controls, and persists audio", async () => { |
| 187 | + const fixture = createMusicFixture(); |
| 188 | + const activeRegistry = captureActivePluginRegistrySnapshot(); |
| 189 | + const metadataSnapshot = captureCurrentPluginMetadataSnapshotState(); |
| 190 | + try { |
| 191 | + setCurrentPluginMetadataSnapshot(fixture.pluginMetadataSnapshot, { |
| 192 | + config: fixture.config, |
| 193 | + }); |
| 194 | + const pluginIds = [PLUGIN_ID]; |
| 195 | + const enabledConfig = withBundledPluginEnablementCompat({ |
| 196 | + config: fixture.config, |
| 197 | + pluginIds, |
| 198 | + }); |
| 199 | + const loadConfig = withBundledPluginVitestCompat({ |
| 200 | + config: enabledConfig, |
| 201 | + pluginIds, |
| 202 | + env: process.env, |
| 203 | + }); |
| 204 | + const cacheKey = resolvePluginRegistryLoadCacheKey({ |
| 205 | + ...(loadConfig ? { config: loadConfig } : {}), |
| 206 | + onlyPluginIds: pluginIds, |
| 207 | + activate: false, |
| 208 | + }); |
| 209 | + setActivePluginRegistry(fixture.registry, cacheKey); |
| 210 | + const listed = (await fixture.tool.execute("music-list", { |
| 211 | + action: "list", |
| 212 | + })) as ToolResult; |
| 213 | + const listedProviders = detailsOf(listed).providers as Array<Record<string, unknown>>; |
| 214 | + const primaryListing = listedProviders.find((entry) => entry.id === fixture.primary.id); |
| 215 | + const fallbackListing = listedProviders.find((entry) => entry.id === fixture.fallback.id); |
| 216 | + |
| 217 | + expect(primaryListing).toMatchObject({ |
| 218 | + configured: true, |
| 219 | + defaultModel: "song-v1", |
| 220 | + models: ["song-v1"], |
| 221 | + modes: ["generate"], |
| 222 | + }); |
| 223 | + expect(fallbackListing).toMatchObject({ |
| 224 | + configured: true, |
| 225 | + defaultModel: "beat-v2", |
| 226 | + models: ["beat-v2"], |
| 227 | + modes: ["generate"], |
| 228 | + }); |
| 229 | + expect(textOf(listed)).toContain( |
| 230 | + "capabilities: modes=generate, maxDurationSeconds=60, lyrics, instrumental, duration, format, supportedFormats=mp3/wav", |
| 231 | + ); |
| 232 | + } finally { |
| 233 | + restoreCurrentPluginMetadataSnapshotState(metadataSnapshot); |
| 234 | + restoreActivePluginRegistrySnapshot(activeRegistry); |
| 235 | + } |
| 236 | + |
| 237 | + const stateDir = await fs.realpath(tempDirs.make("openclaw-qa-music-controls-")); |
| 238 | + await withEnvAsync({ OPENCLAW_STATE_DIR: stateDir }, async () => { |
| 239 | + const vocalResult = (await fixture.tool.execute("music-vocal-fallback", { |
| 240 | + prompt: "bright QA chorus", |
| 241 | + lyrics: "OpenClaw keeps the signal clear", |
| 242 | + instrumental: false, |
| 243 | + durationSeconds: 45, |
| 244 | + format: "wav", |
| 245 | + })) as ToolResult; |
| 246 | + |
| 247 | + expect(fixture.calls.map((call) => `${call.provider}/${call.request.model}`)).toEqual([ |
| 248 | + "qa-primary/song-v1", |
| 249 | + "qa-fallback/beat-v2", |
| 250 | + ]); |
| 251 | + expect(fixture.calls[0]?.request).toMatchObject({ |
| 252 | + lyrics: "OpenClaw keeps the signal clear", |
| 253 | + instrumental: false, |
| 254 | + durationSeconds: 45, |
| 255 | + format: "wav", |
| 256 | + }); |
| 257 | + expect(fixture.calls[1]?.request).toMatchObject({ |
| 258 | + instrumental: false, |
| 259 | + durationSeconds: 30, |
| 260 | + }); |
| 261 | + expect(fixture.calls[1]?.request.lyrics).toBeUndefined(); |
| 262 | + expect(fixture.calls[1]?.request.format).toBeUndefined(); |
| 263 | + |
| 264 | + const vocalDetails = detailsOf(vocalResult); |
| 265 | + expect(vocalDetails).toMatchObject({ |
| 266 | + provider: "qa-fallback", |
| 267 | + model: "beat-v2", |
| 268 | + count: 1, |
| 269 | + instrumental: false, |
| 270 | + durationSeconds: 30, |
| 271 | + requestedDurationSeconds: 45, |
| 272 | + attempts: [ |
| 273 | + { |
| 274 | + provider: "qa-primary", |
| 275 | + model: "song-v1", |
| 276 | + error: "intentional primary provider outage", |
| 277 | + }, |
| 278 | + ], |
| 279 | + ignoredOverrides: [ |
| 280 | + { key: "lyrics", value: "OpenClaw keeps the signal clear" }, |
| 281 | + { key: "format", value: "wav" }, |
| 282 | + ], |
| 283 | + normalization: { |
| 284 | + durationSeconds: { |
| 285 | + requested: 45, |
| 286 | + applied: 30, |
| 287 | + }, |
| 288 | + }, |
| 289 | + }); |
| 290 | + expect(vocalDetails).not.toHaveProperty("requestedLyrics"); |
| 291 | + expect(vocalDetails).not.toHaveProperty("format"); |
| 292 | + expect(textOf(vocalResult)).toContain( |
| 293 | + "Warning: Ignored unsupported overrides for qa-fallback/beat-v2: lyrics=OpenClaw keeps the signal clear, format=wav.", |
| 294 | + ); |
| 295 | + expect(textOf(vocalResult)).toContain("Duration normalized: requested 45s; used 30s."); |
| 296 | + |
| 297 | + const instrumentalResult = (await fixture.tool.execute("music-instrumental", { |
| 298 | + prompt: "tight QA percussion loop", |
| 299 | + model: "qa-fallback/beat-v2", |
| 300 | + instrumental: true, |
| 301 | + durationSeconds: 12, |
| 302 | + format: "mp3", |
| 303 | + })) as ToolResult; |
| 304 | + expect(fixture.calls.map((call) => `${call.provider}/${call.request.model}`)).toEqual([ |
| 305 | + "qa-primary/song-v1", |
| 306 | + "qa-fallback/beat-v2", |
| 307 | + "qa-fallback/beat-v2", |
| 308 | + ]); |
| 309 | + expect(fixture.calls[2]?.request).toMatchObject({ |
| 310 | + instrumental: true, |
| 311 | + durationSeconds: 12, |
| 312 | + format: "mp3", |
| 313 | + }); |
| 314 | + expect(detailsOf(instrumentalResult)).toMatchObject({ |
| 315 | + provider: "qa-fallback", |
| 316 | + model: "beat-v2", |
| 317 | + instrumental: true, |
| 318 | + durationSeconds: 12, |
| 319 | + format: "mp3", |
| 320 | + attempts: [], |
| 321 | + }); |
| 322 | + |
| 323 | + const persisted = [ |
| 324 | + ...(vocalDetails.paths as string[]), |
| 325 | + ...(detailsOf(instrumentalResult).paths as string[]), |
| 326 | + ]; |
| 327 | + expect(persisted).toHaveLength(2); |
| 328 | + for (const filePath of persisted) { |
| 329 | + const canonicalPath = await fs.realpath(filePath); |
| 330 | + expect( |
| 331 | + canonicalPath.startsWith(path.join(stateDir, "media", "tool-music-generation")), |
| 332 | + ).toBe(true); |
| 333 | + expect((await fs.readFile(canonicalPath)).byteLength).toBeGreaterThan(0); |
| 334 | + } |
| 335 | + }); |
| 336 | + }); |
| 337 | + |
| 338 | + it("rejects invalid output formats before invoking a prepared provider", async () => { |
| 339 | + const fixture = createMusicFixture(); |
| 340 | + |
| 341 | + await expect( |
| 342 | + fixture.tool.execute("music-invalid-format", { |
| 343 | + prompt: "QA invalid format proof", |
| 344 | + format: "flac", |
| 345 | + }), |
| 346 | + ).rejects.toThrow('format must be one of "mp3" or "wav"'); |
| 347 | + expect(fixture.calls).toEqual([]); |
| 348 | + }); |
| 349 | +}); |
0 commit comments