11// Tests media path handling and sandbox staging inside agent runner inputs.
2- import { mkdtemp , rm , writeFile } from "node:fs/promises" ;
3- import os from "node:os" ;
2+ import { writeFile } from "node:fs/promises" ;
43import path from "node:path" ;
54import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
5+ import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js" ;
66import type { EmbeddedAgentQueueMessageOutcome } from "../../agents/embedded-agent-runner/runs.js" ;
77import {
88 runInitialModelFallbackAttempt ,
@@ -22,6 +22,9 @@ import {
2222 createMockTypingController ,
2323} from "./test-helpers.js" ;
2424
25+ const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
26+ let testWorkspaceDir : string ;
27+
2528const runEmbeddedAgentMock = vi . fn ( ) ;
2629const runWithModelFallbackMock = vi . fn ( ) ;
2730const abortEmbeddedAgentRunMock = vi . fn ( ) ;
@@ -304,16 +307,15 @@ function makeRunReplyAgentParams(
304307) : Parameters < typeof runReplyAgent > [ 0 ] {
305308 const provider = overrides . provider ?? "whatsapp" ;
306309 const prompt = overrides . prompt ?? "generate chart" ;
307- const workspaceDir = overrides . workspaceDir ?? "/tmp/workspace" ;
310+ const runWorkspaceDir = overrides . workspaceDir ?? testWorkspaceDir ;
308311 const followupRun =
309312 overrides . followupRun ??
310313 createMockFollowupRun ( {
311314 prompt,
312315 run : {
313316 agentId : "main" ,
314- agentDir : "/tmp/agent" ,
315317 messageProvider : provider ,
316- workspaceDir,
318+ workspaceDir : runWorkspaceDir ,
317319 } ,
318320 } ) ;
319321 const replyOperation =
@@ -394,9 +396,8 @@ function makeRunReplyAgentParams(
394396}
395397
396398describe ( "runReplyAgent media path normalization" , ( ) => {
397- const cleanupPaths : string [ ] = [ ] ;
398-
399399 beforeEach ( ( ) => {
400+ testWorkspaceDir = tempDirs . make ( "openclaw-agent-media-workspace-" ) ;
400401 runEmbeddedAgentMock . mockReset ( ) ;
401402 runWithModelFallbackMock . mockReset ( ) ;
402403 abortEmbeddedAgentRunMock . mockReset ( ) ;
@@ -455,8 +456,6 @@ describe("runReplyAgent media path normalization", () => {
455456 operation . complete ( ) ;
456457 }
457458 vi . useRealTimers ( ) ;
458- const paths = cleanupPaths . splice ( 0 ) ;
459- return Promise . all ( paths . map ( ( entry ) => rm ( entry , { recursive : true , force : true } ) ) ) ;
460459 } ) ;
461460
462461 it ( "normalizes final MEDIA replies against the run workspace" , async ( ) => {
@@ -483,9 +482,9 @@ describe("runReplyAgent media path normalization", () => {
483482 mediaUrls : [ "/tmp/outbound-media/generated.png" ] ,
484483 } ) ;
485484 expect ( resolveOutboundAttachmentFromUrlMock ) . toHaveBeenCalledWith (
486- path . join ( "/tmp/workspace" , "out" , "generated.png" ) ,
485+ path . join ( testWorkspaceDir , "out" , "generated.png" ) ,
487486 5 * 1024 * 1024 ,
488- { mediaAccess : expect . objectContaining ( { workspaceDir : "/tmp/workspace" } ) } ,
487+ { mediaAccess : expect . objectContaining ( { workspaceDir : testWorkspaceDir } ) } ,
489488 ) ;
490489 expect ( runEmbeddedAgentMock ) . toHaveBeenCalledOnce ( ) ;
491490 expect ( createReplyMediaContextRuntimeMock ) . not . toHaveBeenCalled ( ) ;
@@ -716,7 +715,7 @@ describe("runReplyAgent media path normalization", () => {
716715 const mediaContext = createReplyMediaContext ( {
717716 cfg : { } ,
718717 sessionKey : "main" ,
719- workspaceDir : "/tmp/workspace" ,
718+ workspaceDir : testWorkspaceDir ,
720719 messageProvider : "telegram" ,
721720 accountId : "default" ,
722721 } ) ;
@@ -762,7 +761,7 @@ describe("runReplyAgent media path normalization", () => {
762761 run : {
763762 provider : "ollama" ,
764763 model : "gemma4:latest" ,
765- workspaceDir : "/tmp/workspace" ,
764+ workspaceDir : testWorkspaceDir ,
766765 config : { } ,
767766 } ,
768767 } ) ,
@@ -818,7 +817,7 @@ describe("runReplyAgent media path normalization", () => {
818817 run : {
819818 provider : "anthropic" ,
820819 model : "claude" ,
821- workspaceDir : "/tmp/workspace" ,
820+ workspaceDir : testWorkspaceDir ,
822821 config : { } ,
823822 } ,
824823 } ) ;
@@ -868,8 +867,7 @@ describe("runReplyAgent media path normalization", () => {
868867 } ) ;
869868
870869 it ( "passes current inbound media paths as native OpenClaw images" , async ( ) => {
871- const tmpDir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-native-agent-media-" ) ) ;
872- cleanupPaths . push ( tmpDir ) ;
870+ const tmpDir = tempDirs . make ( "openclaw-native-agent-media-" ) ;
873871 const imagePath = path . join ( tmpDir , "photo.png" ) ;
874872 await writeFile (
875873 imagePath ,
@@ -918,8 +916,7 @@ describe("runReplyAgent media path normalization", () => {
918916 } ) ;
919917
920918 it ( "does not pass recent history images as unlabeled native OpenClaw images" , async ( ) => {
921- const tmpDir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-native-agent-history-" ) ) ;
922- cleanupPaths . push ( tmpDir ) ;
919+ const tmpDir = tempDirs . make ( "openclaw-native-agent-history-" ) ;
923920 const imagePath = path . join ( tmpDir , "recent.png" ) ;
924921 await writeFile (
925922 imagePath ,
@@ -972,8 +969,7 @@ describe("runReplyAgent media path normalization", () => {
972969 } ) ;
973970
974971 it ( "retains resolved current images and skips unresolved attachments" , async ( ) => {
975- const tmpDir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-native-agent-partial-" ) ) ;
976- cleanupPaths . push ( tmpDir ) ;
972+ const tmpDir = tempDirs . make ( "openclaw-native-agent-partial-" ) ;
977973 const imagePath = path . join ( tmpDir , "present.png" ) ;
978974 await writeFile (
979975 imagePath ,
0 commit comments