1- // Attachment cache tests cover MIME detection after local and remote bytes are available .
1+ // Attachment cache tests cover bounded reads, MIME detection, and temporary-file ownership .
22import fs from "node:fs/promises" ;
33import path from "node:path" ;
44import JSZip from "jszip" ;
5- import { afterEach , describe , expect , it , vi } from "vitest" ;
5+ import { afterEach , describe , expect , expectTypeOf , it , vi } from "vitest" ;
66import { withTestDir } from "../test-helpers/temp-dir.js" ;
77import { MediaAttachmentCache } from "./attachments.js" ;
88
@@ -35,7 +35,7 @@ const PNG_1X1 = Buffer.from(
3535) ;
3636const AMBIGUOUS_WEBM = Buffer . from ( "1a45dfa3874282847765626d" , "hex" ) ;
3737
38- describe ( "media understanding attachment MIME detection " , ( ) => {
38+ describe ( "media understanding attachment cache " , ( ) => {
3939 afterEach ( ( ) => {
4040 vi . restoreAllMocks ( ) ;
4141 buildRandomTempFilePathMock . mockReset ( ) ;
@@ -79,6 +79,111 @@ describe("media understanding attachment MIME detection", () => {
7979 expect ( result . mime ) . toBe ( "image/png" ) ;
8080 } ) ;
8181
82+ it ( "bounds bytes consumed when a local file grows after stat" , async ( ) => {
83+ await withTestDir ( { prefix : "openclaw-media-cache-growth-" } , async ( base ) => {
84+ const attachmentPath = path . join ( base , "growing.png" ) ;
85+ await fs . writeFile ( attachmentPath , PNG_1X1 ) ;
86+ const maxBytes = PNG_1X1 . length ;
87+ const open = fs . open . bind ( fs ) ;
88+ let consumed = 0 ;
89+ let grew = false ;
90+ const growBeforeRead = async ( ) => {
91+ if ( ! grew ) {
92+ grew = true ;
93+ await fs . appendFile ( attachmentPath , Buffer . alloc ( 4096 ) ) ;
94+ }
95+ } ;
96+ vi . spyOn ( fs , "open" ) . mockImplementation ( async ( ...args ) => {
97+ const handle = await open ( ...args ) ;
98+ const read = handle . read . bind ( handle ) ;
99+ vi . spyOn ( handle , "read" ) . mockImplementation ( async ( ...readArgs ) => {
100+ await growBeforeRead ( ) ;
101+ const result = await read ( ...readArgs ) ;
102+ consumed += result . bytesRead ;
103+ return result ;
104+ } ) ;
105+ const readFile = handle . readFile . bind ( handle ) ;
106+ vi . spyOn ( handle , "readFile" ) . mockImplementation ( async ( ...readArgs ) => {
107+ await growBeforeRead ( ) ;
108+ const result = await readFile ( ...readArgs ) ;
109+ consumed += result . length ;
110+ return result ;
111+ } ) ;
112+ return handle ;
113+ } ) ;
114+ const cache = new MediaAttachmentCache ( [ { index : 0 , path : attachmentPath } ] , {
115+ localPathRoots : [ base ] ,
116+ includeDefaultLocalPathRoots : false ,
117+ } ) ;
118+
119+ await expect (
120+ cache . getBuffer ( { attachmentIndex : 0 , maxBytes, timeoutMs : 1000 } ) ,
121+ ) . rejects . toMatchObject ( { reason : "maxBytes" } ) ;
122+ expect ( consumed ) . toBeGreaterThan ( 0 ) ;
123+ expect ( consumed ) . toBeLessThanOrEqual ( maxBytes + 1 ) ;
124+ } ) ;
125+ } ) ;
126+
127+ it . each ( [ "local" , "staged" ] as const ) (
128+ "enforces a zero-byte path limit for %s files" ,
129+ async ( source ) => {
130+ expectTypeOf < Parameters < MediaAttachmentCache [ "getPath" ] > [ 0 ] > ( ) . toEqualTypeOf < {
131+ attachmentIndex : number ;
132+ maxBytes : number ;
133+ timeoutMs : number ;
134+ } > ( ) ;
135+ await withTestDir ( { prefix : "openclaw-media-cache-path-limit-" } , async ( base ) => {
136+ const attachmentPath = path . join ( base , "photo.png" ) ;
137+ await fs . writeFile ( attachmentPath , PNG_1X1 ) ;
138+ buildRandomTempFilePathMock . mockReturnValue ( path . join ( base , "staged.png" ) ) ;
139+ readRemoteMediaBufferMock . mockResolvedValue ( { buffer : PNG_1X1 , fileName : "photo.png" } ) ;
140+ const attachment =
141+ source === "local"
142+ ? { index : 0 , path : attachmentPath }
143+ : { index : 0 , url : "https://example.com/photo.png" } ;
144+ const cache = new MediaAttachmentCache ( [ attachment ] , { localPathRoots : [ base ] } ) ;
145+ const request = { attachmentIndex : 0 , maxBytes : PNG_1X1 . length , timeoutMs : 1000 } ;
146+ try {
147+ await expect ( cache . getPath ( request ) ) . resolves . toHaveProperty ( "path" ) ;
148+ await expect ( cache . getPath ( { ...request , maxBytes : 0 } ) ) . rejects . toMatchObject ( {
149+ reason : "maxBytes" ,
150+ } ) ;
151+ } finally {
152+ await cache . cleanup ( ) ;
153+ }
154+ } ) ;
155+ } ,
156+ ) ;
157+
158+ it . each ( [ "cache" , "returned" ] as const ) (
159+ "restages files after %s cleanup" ,
160+ async ( cleanupOwner ) => {
161+ await withTestDir ( { prefix : "openclaw-media-cache-restage-" } , async ( base ) => {
162+ buildRandomTempFilePathMock
163+ . mockReturnValueOnce ( path . join ( base , "first.png" ) )
164+ . mockReturnValueOnce ( path . join ( base , "second.png" ) ) ;
165+ readRemoteMediaBufferMock . mockResolvedValue ( { buffer : PNG_1X1 , fileName : "photo.png" } ) ;
166+ const cache = new MediaAttachmentCache ( [
167+ { index : 0 , url : "https://example.com/photo.png" } ,
168+ ] ) ;
169+ const request = { attachmentIndex : 0 , maxBytes : PNG_1X1 . length , timeoutMs : 1000 } ;
170+ try {
171+ const first = await cache . getPath ( request ) ;
172+ await ( cleanupOwner === "cache" ? cache . cleanup ( ) : first . cleanup ?.( ) ) ;
173+ await expect ( fs . stat ( first . path ) ) . rejects . toMatchObject ( { code : "ENOENT" } ) ;
174+ const second = await cache . getPath ( request ) ;
175+ await expect ( fs . readFile ( second . path ) ) . resolves . toEqual ( PNG_1X1 ) ;
176+ await first . cleanup ?.( ) ;
177+ expect ( ( await cache . getPath ( request ) ) . path ) . toBe ( second . path ) ;
178+ expect ( readRemoteMediaBufferMock ) . toHaveBeenCalledTimes ( 1 ) ;
179+ } finally {
180+ await cache . cleanup ( ) ;
181+ }
182+ expect ( await fs . readdir ( base ) ) . toEqual ( [ ] ) ;
183+ } ) ;
184+ } ,
185+ ) ;
186+
82187 it ( "uses fetched audio metadata when declared MIME is stale for ambiguous WebM" , async ( ) => {
83188 const url = "https://example.com/voice.webm" ;
84189 readRemoteMediaBufferMock . mockResolvedValue ( {
@@ -132,9 +237,9 @@ describe("media understanding attachment MIME detection", () => {
132237 } ) ;
133238 const cache = new MediaAttachmentCache ( [ { index : 0 , url : "https://example.com/photo.png" } ] ) ;
134239
135- await expect ( cache . getPath ( { attachmentIndex : 0 , timeoutMs : 1_000 } ) ) . rejects . toBe (
136- writeError ,
137- ) ;
240+ await expect (
241+ cache . getPath ( { attachmentIndex : 0 , maxBytes : 1024 , timeoutMs : 1_000 } ) ,
242+ ) . rejects . toBe ( writeError ) ;
138243 await cache . cleanup ( ) ;
139244
140245 expect ( await fs . readdir ( base ) ) . toEqual ( [ ] ) ;
@@ -156,7 +261,7 @@ describe("media understanding attachment MIME detection", () => {
156261 } ) ;
157262 vi . spyOn ( fs , "unlink" ) . mockRejectedValueOnce ( cleanupError ) ;
158263 const cache = new MediaAttachmentCache ( [ { index : 0 , url : "https://example.com/photo.png" } ] ) ;
159- const request = { attachmentIndex : 0 , timeoutMs : 1_000 } ;
264+ const request = { attachmentIndex : 0 , maxBytes : 1024 , timeoutMs : 1_000 } ;
160265
161266 await expect ( cache . getPath ( request ) ) . rejects . toBe ( writeError ) ;
162267 expect ( await fs . readdir ( base ) ) . toEqual ( [ "failed.png" ] ) ;
0 commit comments