11// Outbound attachment tests cover media loading rules for outgoing messages.
2- import { describe , expect , it , vi } from "vitest" ;
2+ import { afterEach , describe , expect , it , vi } from "vitest" ;
33
44const loadWebMedia = vi . hoisted ( ( ) => vi . fn ( ) ) ;
55const markTrustedGeneratedHtmlPath = vi . hoisted ( ( ) => vi . fn ( ) ) ;
66const saveMediaBuffer = vi . hoisted ( ( ) => vi . fn ( ) ) ;
7+ const rm = vi . hoisted ( ( ) => vi . fn ( async ( ) => { } ) ) ;
8+
9+ vi . mock ( "node:fs/promises" , ( ) => ( {
10+ rm,
11+ } ) ) ;
712
813vi . mock ( "./web-media.js" , ( ) => ( {
914 loadWebMedia,
@@ -17,6 +22,10 @@ vi.mock("./store.js", () => ({
1722const { resolveOutboundAttachmentFromBuffer, resolveOutboundAttachmentFromUrl } =
1823 await import ( "./outbound-attachment.js" ) ;
1924
25+ afterEach ( ( ) => {
26+ vi . clearAllMocks ( ) ;
27+ } ) ;
28+
2029describe ( "resolveOutboundAttachmentFromUrl" , ( ) => {
2130 it ( "preserves the loaded file name when staging outbound media" , async ( ) => {
2231 const buffer = Buffer . from ( "pdf" ) ;
@@ -59,9 +68,38 @@ describe("resolveOutboundAttachmentFromUrl", () => {
5968
6069 expect ( markTrustedGeneratedHtmlPath ) . toHaveBeenCalledWith (
6170 "/tmp/media/outbound/report---uuid.html" ,
71+ buffer ,
6272 ) ;
6373 } ) ;
6474
75+ it ( "propagates a marker write failure and best-effort unlinks the staged file" , async ( ) => {
76+ const buffer = Buffer . from ( "<!doctype html><title>x</title>" ) ;
77+ loadWebMedia . mockResolvedValueOnce ( {
78+ buffer,
79+ contentType : "text/html" ,
80+ fileName : "report.html" ,
81+ trustedGeneratedHtmlSource : true ,
82+ } ) ;
83+ saveMediaBuffer . mockResolvedValueOnce ( {
84+ path : "/tmp/media/outbound/report---uuid.html" ,
85+ contentType : "text/html" ,
86+ } ) ;
87+
88+ markTrustedGeneratedHtmlPath . mockReset ( ) ;
89+ rm . mockReset ( ) ;
90+ rm . mockResolvedValueOnce ( undefined ) ;
91+ const markerError = new Error ( "marker write failed" ) ;
92+ markTrustedGeneratedHtmlPath . mockRejectedValueOnce ( markerError ) ;
93+
94+ await expect (
95+ resolveOutboundAttachmentFromUrl ( "/tmp/openclaw/report.html" , 1024 ) ,
96+ ) . rejects . toThrow ( markerError ) ;
97+
98+ expect ( rm ) . toHaveBeenCalledWith ( "/tmp/media/outbound/report---uuid.html" , {
99+ force : true ,
100+ } ) ;
101+ } ) ;
102+
65103 it ( "does not mark untrusted outbound HTML staging" , async ( ) => {
66104 const buffer = Buffer . from ( "<!doctype html><title>x</title>" ) ;
67105 loadWebMedia . mockResolvedValueOnce ( {
0 commit comments