@@ -69,6 +69,7 @@ vi.mock('@/app/api/files/authorization', () => ({
6969vi . mock ( '@/lib/uploads' , ( ) => ( {
7070 getStorageProvider : mockGetStorageProvider ,
7171 isUsingCloudStorage : mockIsUsingCloudStorage ,
72+ StorageService : storageServiceMock ,
7273} ) )
7374
7475vi . mock ( '@/lib/file-parsers' , ( ) => ( {
@@ -172,6 +173,7 @@ describe('File Parse API Route', () => {
172173
173174 permissionsMockFns . mockGetUserEntityPermissions . mockResolvedValue ( { canView : true } )
174175 storageServiceMockFns . mockHasCloudStorage . mockReturnValue ( true )
176+ storageServiceMockFns . mockDownloadFile . mockResolvedValue ( Buffer . from ( 'test file content' ) )
175177 mockIsSupportedFileType . mockReturnValue ( true )
176178 mockParseFile . mockResolvedValue ( {
177179 content : 'parsed content' ,
@@ -245,6 +247,48 @@ describe('File Parse API Route', () => {
245247 }
246248 } )
247249
250+ it ( 'should keep known binary extensions as binary even when the bytes are valid UTF-8' , async ( ) => {
251+ setupFileApiMocks ( {
252+ cloudEnabled : true ,
253+ storageProvider : 's3' ,
254+ authenticated : true ,
255+ } )
256+ mockIsSupportedFileType . mockReturnValue ( false )
257+ storageServiceMockFns . mockDownloadFile . mockResolvedValue ( Buffer . from ( 'valid utf8 bytes' ) )
258+
259+ const req = createMockRequest ( 'POST' , {
260+ filePath : '/api/files/serve/execution/workspace-1/workflow-1/execution-1/image.png' ,
261+ } )
262+
263+ const response = await POST ( req )
264+ const data = await response . json ( )
265+
266+ expect ( response . status ) . toBe ( 200 )
267+ expect ( data . success ) . toBe ( true )
268+ expect ( data . output . content ) . toBe ( '[Binary PNG file - 16 bytes]' )
269+ } )
270+
271+ it ( 'should parse unknown extensions as text when the bytes look like UTF-8 text' , async ( ) => {
272+ setupFileApiMocks ( {
273+ cloudEnabled : true ,
274+ storageProvider : 's3' ,
275+ authenticated : true ,
276+ } )
277+ mockIsSupportedFileType . mockReturnValue ( false )
278+ storageServiceMockFns . mockDownloadFile . mockResolvedValue ( Buffer . from ( 'plain text content' ) )
279+
280+ const req = createMockRequest ( 'POST' , {
281+ filePath : '/api/files/serve/execution/workspace-1/workflow-1/execution-1/readme.customtext' ,
282+ } )
283+
284+ const response = await POST ( req )
285+ const data = await response . json ( )
286+
287+ expect ( response . status ) . toBe ( 200 )
288+ expect ( data . success ) . toBe ( true )
289+ expect ( data . output . content ) . toBe ( 'plain text content' )
290+ } )
291+
248292 it ( 'should handle multiple files' , async ( ) => {
249293 setupFileApiMocks ( {
250294 cloudEnabled : false ,
0 commit comments