|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import type { MsgContext } from "../auto-reply/templating.js"; |
| 6 | +import type { OpenClawConfig } from "../config/config.js"; |
| 7 | +import { MIN_AUDIO_FILE_BYTES } from "./defaults.js"; |
| 8 | +import { |
| 9 | + buildProviderRegistry, |
| 10 | + createMediaAttachmentCache, |
| 11 | + normalizeMediaAttachments, |
| 12 | + runCapability, |
| 13 | +} from "./runner.js"; |
| 14 | + |
| 15 | +describe("runCapability skips tiny audio files", () => { |
| 16 | + it("skips audio transcription when file is smaller than MIN_AUDIO_FILE_BYTES", async () => { |
| 17 | + const originalPath = process.env.PATH; |
| 18 | + process.env.PATH = "/usr/bin:/bin"; |
| 19 | + |
| 20 | + // Create a tiny audio file (well below the 1KB threshold) |
| 21 | + const tmpPath = path.join(os.tmpdir(), `openclaw-tiny-audio-${Date.now()}.wav`); |
| 22 | + const tinyBuffer = Buffer.alloc(100); // 100 bytes, way below 1024 |
| 23 | + await fs.writeFile(tmpPath, tinyBuffer); |
| 24 | + |
| 25 | + const ctx: MsgContext = { MediaPath: tmpPath, MediaType: "audio/wav" }; |
| 26 | + const media = normalizeMediaAttachments(ctx); |
| 27 | + const cache = createMediaAttachmentCache(media); |
| 28 | + |
| 29 | + let transcribeCalled = false; |
| 30 | + const providerRegistry = buildProviderRegistry({ |
| 31 | + openai: { |
| 32 | + id: "openai", |
| 33 | + capabilities: ["audio"], |
| 34 | + transcribeAudio: async (req) => { |
| 35 | + transcribeCalled = true; |
| 36 | + return { text: "should not happen", model: req.model }; |
| 37 | + }, |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + const cfg = { |
| 42 | + models: { |
| 43 | + providers: { |
| 44 | + openai: { |
| 45 | + apiKey: "test-key", |
| 46 | + models: [], |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + } as unknown as OpenClawConfig; |
| 51 | + |
| 52 | + try { |
| 53 | + const result = await runCapability({ |
| 54 | + capability: "audio", |
| 55 | + cfg, |
| 56 | + ctx, |
| 57 | + attachments: cache, |
| 58 | + media, |
| 59 | + providerRegistry, |
| 60 | + }); |
| 61 | + |
| 62 | + // The provider should never be called |
| 63 | + expect(transcribeCalled).toBe(false); |
| 64 | + |
| 65 | + // The result should indicate the attachment was skipped |
| 66 | + expect(result.outputs).toHaveLength(0); |
| 67 | + expect(result.decision.outcome).toBe("skipped"); |
| 68 | + expect(result.decision.attachments[0]?.attempts[0]?.outcome).toBe("skipped"); |
| 69 | + expect(result.decision.attachments[0]?.attempts[0]?.reason).toContain("tooSmall"); |
| 70 | + } finally { |
| 71 | + process.env.PATH = originalPath; |
| 72 | + await cache.cleanup(); |
| 73 | + await fs.unlink(tmpPath).catch(() => {}); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it("skips audio transcription for empty (0-byte) files", async () => { |
| 78 | + const originalPath = process.env.PATH; |
| 79 | + process.env.PATH = "/usr/bin:/bin"; |
| 80 | + |
| 81 | + const tmpPath = path.join(os.tmpdir(), `openclaw-empty-audio-${Date.now()}.ogg`); |
| 82 | + await fs.writeFile(tmpPath, Buffer.alloc(0)); |
| 83 | + |
| 84 | + const ctx: MsgContext = { MediaPath: tmpPath, MediaType: "audio/ogg" }; |
| 85 | + const media = normalizeMediaAttachments(ctx); |
| 86 | + const cache = createMediaAttachmentCache(media); |
| 87 | + |
| 88 | + let transcribeCalled = false; |
| 89 | + const providerRegistry = buildProviderRegistry({ |
| 90 | + openai: { |
| 91 | + id: "openai", |
| 92 | + capabilities: ["audio"], |
| 93 | + transcribeAudio: async () => { |
| 94 | + transcribeCalled = true; |
| 95 | + return { text: "nope", model: "whisper-1" }; |
| 96 | + }, |
| 97 | + }, |
| 98 | + }); |
| 99 | + |
| 100 | + const cfg = { |
| 101 | + models: { |
| 102 | + providers: { |
| 103 | + openai: { |
| 104 | + apiKey: "test-key", |
| 105 | + models: [], |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } as unknown as OpenClawConfig; |
| 110 | + |
| 111 | + try { |
| 112 | + const result = await runCapability({ |
| 113 | + capability: "audio", |
| 114 | + cfg, |
| 115 | + ctx, |
| 116 | + attachments: cache, |
| 117 | + media, |
| 118 | + providerRegistry, |
| 119 | + }); |
| 120 | + |
| 121 | + expect(transcribeCalled).toBe(false); |
| 122 | + expect(result.outputs).toHaveLength(0); |
| 123 | + } finally { |
| 124 | + process.env.PATH = originalPath; |
| 125 | + await cache.cleanup(); |
| 126 | + await fs.unlink(tmpPath).catch(() => {}); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + it("proceeds with transcription when file meets minimum size", async () => { |
| 131 | + const originalPath = process.env.PATH; |
| 132 | + process.env.PATH = "/usr/bin:/bin"; |
| 133 | + |
| 134 | + const tmpPath = path.join(os.tmpdir(), `openclaw-ok-audio-${Date.now()}.wav`); |
| 135 | + const okBuffer = Buffer.alloc(MIN_AUDIO_FILE_BYTES + 100); |
| 136 | + await fs.writeFile(tmpPath, okBuffer); |
| 137 | + |
| 138 | + const ctx: MsgContext = { MediaPath: tmpPath, MediaType: "audio/wav" }; |
| 139 | + const media = normalizeMediaAttachments(ctx); |
| 140 | + const cache = createMediaAttachmentCache(media); |
| 141 | + |
| 142 | + let transcribeCalled = false; |
| 143 | + const providerRegistry = buildProviderRegistry({ |
| 144 | + openai: { |
| 145 | + id: "openai", |
| 146 | + capabilities: ["audio"], |
| 147 | + transcribeAudio: async (req) => { |
| 148 | + transcribeCalled = true; |
| 149 | + return { text: "hello world", model: req.model }; |
| 150 | + }, |
| 151 | + }, |
| 152 | + }); |
| 153 | + |
| 154 | + const cfg = { |
| 155 | + models: { |
| 156 | + providers: { |
| 157 | + openai: { |
| 158 | + apiKey: "test-key", |
| 159 | + models: [], |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + } as unknown as OpenClawConfig; |
| 164 | + |
| 165 | + try { |
| 166 | + const result = await runCapability({ |
| 167 | + capability: "audio", |
| 168 | + cfg, |
| 169 | + ctx, |
| 170 | + attachments: cache, |
| 171 | + media, |
| 172 | + providerRegistry, |
| 173 | + }); |
| 174 | + |
| 175 | + expect(transcribeCalled).toBe(true); |
| 176 | + expect(result.outputs).toHaveLength(1); |
| 177 | + expect(result.outputs[0]?.text).toBe("hello world"); |
| 178 | + expect(result.decision.outcome).toBe("success"); |
| 179 | + } finally { |
| 180 | + process.env.PATH = originalPath; |
| 181 | + await cache.cleanup(); |
| 182 | + await fs.unlink(tmpPath).catch(() => {}); |
| 183 | + } |
| 184 | + }); |
| 185 | +}); |
0 commit comments