fix: cache tmp dir resolution, add test for TTS temp path

Address review feedback:
- Cache resolvePreferredOpenClawTmpDir() result to avoid sync I/O
  on every media path check and prevent mkdir side effects
- Add test for TTS voice output from /tmp/openclaw/ path
This commit is contained in:
jet
2026-04-09 11:13:17 +08:00
committed by George Pickett
parent f6424aa484
commit 62284b58bc
2 changed files with 24 additions and 2 deletions

View File

@@ -176,4 +176,24 @@ describe("createReplyMediaPathNormalizer", () => {
mediaUrls: ["/Users/peter/.openclaw/media/outbound/persisted.png"],
});
});
it("keeps TTS voice output from the OpenClaw temp directory", async () => {
// resolvePreferredOpenClawTmpDir() returns /tmp/openclaw on POSIX when it exists.
// We rely on the real function (no mock) since the test environment has /tmp/openclaw.
const normalize = createReplyMediaPathNormalizer({
cfg: {},
sessionKey: "session-key",
workspaceDir: "/tmp/agent-workspace",
});
const result = await normalize({
mediaUrls: ["/tmp/openclaw/tts-abc123/voice-1234567890.opus"],
});
expect(result).toMatchObject({
mediaUrl: "/tmp/openclaw/tts-abc123/voice-1234567890.opus",
mediaUrls: ["/tmp/openclaw/tts-abc123/voice-1234567890.opus"],
});
expect(saveMediaSource).not.toHaveBeenCalled();
});
});

View File

@@ -60,10 +60,12 @@ function isAllowedAbsoluteReplyMediaPath(params: {
* These are trusted paths written by OpenClaw's own tooling
* (TTS, media processing, etc.) and should be deliverable as reply media.
*/
let cachedTmpRoot: string | undefined;
function isOpenClawTmpPath(candidate: string): boolean {
try {
const tmpRoot = resolvePreferredOpenClawTmpDir();
return isPathInside(tmpRoot, candidate);
cachedTmpRoot ??= resolvePreferredOpenClawTmpDir();
return isPathInside(cachedTmpRoot, candidate);
} catch {
return false;
}