|
1 | 1 | // Video runner tests cover provider request wiring, auth/config precedence, and |
2 | 2 | // provider output handling for video attachments. |
3 | 3 | import { describe, expect, it, vi } from "vitest"; |
| 4 | +import { |
| 5 | + formatAudioTranscripts, |
| 6 | + formatMediaUnderstandingBody, |
| 7 | +} from "../../packages/media-understanding-common/src/format.js"; |
4 | 8 | import type { OpenClawConfig } from "../config/types.js"; |
5 | 9 | import { withTempDir } from "../test-helpers/temp-dir.js"; |
6 | 10 | import { withEnvAsync } from "../test-utils/env.js"; |
@@ -465,3 +469,124 @@ describe("runCapability video provider wiring", () => { |
465 | 469 | expect(firstCall?.modelApi).toBeUndefined(); |
466 | 470 | }); |
467 | 471 | }); |
| 472 | + |
| 473 | +describe("runCapability provider output decisions", () => { |
| 474 | + const outputs = [ |
| 475 | + { label: "empty", text: "" }, |
| 476 | + { label: "whitespace", text: " \t\n" }, |
| 477 | + { label: "usable", text: " usable primary output " }, |
| 478 | + ] as const; |
| 479 | + const cases = (["audio", "video", "image"] as const).flatMap((capability) => |
| 480 | + outputs.flatMap((output) => |
| 481 | + (output.text.trim() ? [true] : [true, false]).map((configureFallback) => ({ |
| 482 | + capability, |
| 483 | + configureFallback, |
| 484 | + label: output.label, |
| 485 | + text: output.text, |
| 486 | + })), |
| 487 | + ), |
| 488 | + ); |
| 489 | + |
| 490 | + it.each(cases)( |
| 491 | + "handles $label $capability provider output with fallback=$configureFallback", |
| 492 | + async ({ capability, configureFallback, text }) => { |
| 493 | + const extension = capability === "image" ? "png" : capability === "video" ? "mp4" : "wav"; |
| 494 | + const mime = `${capability}/${extension}`; |
| 495 | + const buffer = |
| 496 | + capability === "image" |
| 497 | + ? Buffer.from( |
| 498 | + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8Xw8AAuMBg4n8tLwAAAAASUVORK5CYII=", |
| 499 | + "base64", |
| 500 | + ) |
| 501 | + : Buffer.alloc(2048, 1); |
| 502 | + const primary = vi.fn(async () => ({ text, model: "primary-model" })); |
| 503 | + const fallback = vi.fn(async () => ({ |
| 504 | + text: "usable fallback output", |
| 505 | + model: "fallback-model", |
| 506 | + })); |
| 507 | + const createProvider = ( |
| 508 | + id: string, |
| 509 | + run: () => Promise<{ text: string; model: string }>, |
| 510 | + ): MediaUnderstandingProvider => ({ |
| 511 | + id, |
| 512 | + capabilities: [capability], |
| 513 | + ...(capability === "audio" |
| 514 | + ? { transcribeAudio: run } |
| 515 | + : capability === "video" |
| 516 | + ? { describeVideo: run } |
| 517 | + : { describeImage: run }), |
| 518 | + }); |
| 519 | + const providerIds = ["qa-primary", ...(configureFallback ? ["qa-fallback"] : [])]; |
| 520 | + const cfg = { |
| 521 | + models: { |
| 522 | + providers: Object.fromEntries( |
| 523 | + providerIds.map((provider) => [provider, { apiKey: "test-key", models: [] }]), |
| 524 | + ), |
| 525 | + }, |
| 526 | + tools: { |
| 527 | + media: { |
| 528 | + models: providerIds.map((provider) => ({ |
| 529 | + provider, |
| 530 | + model: provider === "qa-primary" ? "primary-model" : "fallback-model", |
| 531 | + capabilities: [capability], |
| 532 | + })), |
| 533 | + [capability]: { enabled: true }, |
| 534 | + }, |
| 535 | + }, |
| 536 | + } as unknown as OpenClawConfig; |
| 537 | + |
| 538 | + const result = await runCapability({ |
| 539 | + capability, |
| 540 | + cfg, |
| 541 | + ctx: { Body: "" }, |
| 542 | + attachments: { |
| 543 | + getBuffer: async () => ({ |
| 544 | + buffer, |
| 545 | + mime, |
| 546 | + fileName: `fixture.${extension}`, |
| 547 | + size: buffer.length, |
| 548 | + }), |
| 549 | + } as unknown as Parameters<typeof runCapability>[0]["attachments"], |
| 550 | + media: [{ index: 0, kind: capability, mime }], |
| 551 | + agentDir: "/tmp/openclaw-media-provider-output-test", |
| 552 | + providerRegistry: new Map<string, MediaUnderstandingProvider>([ |
| 553 | + ["qa-primary", createProvider("qa-primary", primary)], |
| 554 | + ["qa-fallback", createProvider("qa-fallback", fallback)], |
| 555 | + ]), |
| 556 | + }); |
| 557 | + |
| 558 | + const usablePrimary = text.trim(); |
| 559 | + const expectedText = usablePrimary || (configureFallback ? "usable fallback output" : ""); |
| 560 | + const expectedFallbackCalls = !usablePrimary && configureFallback ? 1 : 0; |
| 561 | + expect(primary).toHaveBeenCalledOnce(); |
| 562 | + expect(fallback).toHaveBeenCalledTimes(expectedFallbackCalls); |
| 563 | + expect(result.outputs.map((output) => output.text)).toEqual( |
| 564 | + expectedText ? [expectedText] : [], |
| 565 | + ); |
| 566 | + expect(result.decision.outcome).toBe(expectedText ? "success" : "skipped"); |
| 567 | + |
| 568 | + const attempts = result.decision.attachments[0]?.attempts.map( |
| 569 | + ({ provider, outcome, reason }) => ({ |
| 570 | + provider, |
| 571 | + outcome, |
| 572 | + ...(reason ? { reason } : {}), |
| 573 | + }), |
| 574 | + ); |
| 575 | + expect(attempts).toEqual([ |
| 576 | + usablePrimary |
| 577 | + ? { provider: "qa-primary", outcome: "success" } |
| 578 | + : { provider: "qa-primary", outcome: "skipped", reason: "empty output" }, |
| 579 | + ...(expectedFallbackCalls ? [{ provider: "qa-fallback", outcome: "success" }] : []), |
| 580 | + ]); |
| 581 | + |
| 582 | + if (capability === "audio") { |
| 583 | + expect(formatMediaUnderstandingBody({ outputs: result.outputs })).toBe( |
| 584 | + expectedText ? `[Audio]\nTranscript:\n${expectedText}` : "", |
| 585 | + ); |
| 586 | + if (expectedText) { |
| 587 | + expect(formatAudioTranscripts(result.outputs)).toBe(expectedText); |
| 588 | + } |
| 589 | + } |
| 590 | + }, |
| 591 | + ); |
| 592 | +}); |
0 commit comments