From 2dcee8ac2b348fcc6e16551ec662eb0d7dfcb96f Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Mon, 1 Jun 2026 05:20:49 +0200 Subject: [PATCH] refactor: share webchat media audio fixture --- .../server-methods/chat-webchat-media.test.ts | 47 ++++++++----------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/src/gateway/server-methods/chat-webchat-media.test.ts b/src/gateway/server-methods/chat-webchat-media.test.ts index 180a8cfdc94..2d499702e7c 100644 --- a/src/gateway/server-methods/chat-webchat-media.test.ts +++ b/src/gateway/server-methods/chat-webchat-media.test.ts @@ -20,14 +20,19 @@ describe("buildWebchatAudioContentBlocksFromReplyPayloads", () => { tmpDir = undefined; }); - it("exposes a local audio file as a media-ticketed attachment when it is under localRoots", async () => { + function writeAudioFixture(bytes = [0xff, 0xfb, 0x90, 0x00]) { tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-webchat-audio-")); const audioPath = path.join(tmpDir, "clip.mp3"); - fs.writeFileSync(audioPath, Buffer.from([0xff, 0xfb, 0x90, 0x00])); + fs.writeFileSync(audioPath, Buffer.from(bytes)); + return { audioPath, localRoot: tmpDir }; + } + + it("exposes a local audio file as a media-ticketed attachment when it is under localRoots", async () => { + const { audioPath, localRoot } = writeAudioFixture(); const blocks = await buildWebchatAudioContentBlocksFromReplyPayloads( [{ mediaUrl: audioPath, trustedLocalMedia: true }], - { localRoots: [tmpDir] }, + { localRoots: [localRoot] }, ); expect(blocks).toHaveLength(1); @@ -45,13 +50,11 @@ describe("buildWebchatAudioContentBlocksFromReplyPayloads", () => { }); it("preserves voice-note metadata on local audio attachments", async () => { - tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-webchat-audio-")); - const audioPath = path.join(tmpDir, "clip.mp3"); - fs.writeFileSync(audioPath, Buffer.from([0xff, 0xfb, 0x90, 0x00])); + const { audioPath, localRoot } = writeAudioFixture(); const blocks = await buildWebchatAudioContentBlocksFromReplyPayloads( [{ mediaUrl: audioPath, trustedLocalMedia: true, audioAsVoice: true }], - { localRoots: [tmpDir] }, + { localRoots: [localRoot] }, ); expect(blocks).toHaveLength(1); @@ -64,9 +67,7 @@ describe("buildWebchatAudioContentBlocksFromReplyPayloads", () => { }); it("suppresses reasoning payload audio", async () => { - tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-webchat-audio-")); - const audioPath = path.join(tmpDir, "clip.mp3"); - fs.writeFileSync(audioPath, Buffer.from([0xff, 0xfb, 0x90, 0x00])); + const { audioPath, localRoot } = writeAudioFixture(); const blocks = await buildWebchatAudioContentBlocksFromReplyPayloads( [ @@ -77,7 +78,7 @@ describe("buildWebchatAudioContentBlocksFromReplyPayloads", () => { isReasoning: true, }, ], - { localRoots: [tmpDir] }, + { localRoots: [localRoot] }, ); expect(blocks).toHaveLength(0); @@ -104,30 +105,26 @@ describe("buildWebchatAudioContentBlocksFromReplyPayloads", () => { }); it("dedupes repeated paths", async () => { - tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-webchat-audio-")); - const audioPath = path.join(tmpDir, "clip.mp3"); - fs.writeFileSync(audioPath, Buffer.from([0x00])); + const { audioPath, localRoot } = writeAudioFixture([0x00]); const blocks = await buildWebchatAudioContentBlocksFromReplyPayloads( [ { mediaUrl: audioPath, trustedLocalMedia: true }, { mediaUrl: audioPath, trustedLocalMedia: true }, ], - { localRoots: [tmpDir] }, + { localRoots: [localRoot] }, ); expect(blocks).toHaveLength(1); }); it("embeds file:// URLs pointing at a local file within localRoots", async () => { - tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-webchat-audio-")); - const audioPath = path.join(tmpDir, "clip.mp3"); - fs.writeFileSync(audioPath, Buffer.from([0x01])); + const { audioPath, localRoot } = writeAudioFixture([0x01]); const fileUrl = pathToFileURL(audioPath).href; const blocks = await buildWebchatAudioContentBlocksFromReplyPayloads( [{ mediaUrl: fileUrl, trustedLocalMedia: true }], { - localRoots: [tmpDir], + localRoots: [localRoot], }, ); @@ -194,27 +191,23 @@ describe("buildWebchatAudioContentBlocksFromReplyPayloads", () => { }); it("skips local audio when the opened file stat is over the cap", async () => { - tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-webchat-audio-")); - const audioPath = path.join(tmpDir, "huge.mp3"); - fs.writeFileSync(audioPath, Buffer.from([0x02])); + const { audioPath, localRoot } = writeAudioFixture([0x02]); fs.truncateSync(audioPath, 16 * 1024 * 1024); const blocks = await buildWebchatAudioContentBlocksFromReplyPayloads( [{ mediaUrl: audioPath, trustedLocalMedia: true }], - { localRoots: [tmpDir] }, + { localRoots: [localRoot] }, ); expect(blocks).toHaveLength(0); }); it("rejects untrusted local audio paths", async () => { - tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-webchat-audio-")); - const audioPath = path.join(tmpDir, "clip.mp3"); - fs.writeFileSync(audioPath, Buffer.from([0xff, 0xfb, 0x90, 0x00])); + const { audioPath, localRoot } = writeAudioFixture(); const blocks = await buildWebchatAudioContentBlocksFromReplyPayloads( [{ mediaUrl: audioPath }], - { localRoots: [tmpDir] }, + { localRoots: [localRoot] }, ); expect(blocks).toHaveLength(0);