refactor: share webchat media audio fixture

This commit is contained in:
Vincent Koc
2026-06-01 05:20:49 +02:00
parent e2c9c06de1
commit 2dcee8ac2b

View File

@@ -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);