1- import { Readable } from 'node:stream' ;
1+ import { PassThrough , Readable } from 'node:stream' ;
22import { beforeEach , describe , expect , it , vi } from 'vitest' ;
33import proxyDownloadHandler from '@/pages/api/system/file/download/[token]' ;
44import proxyUploadHandler from '@/pages/api/system/file/upload/[token]' ;
@@ -29,37 +29,36 @@ vi.mock('@fastgpt/service/common/s3/sources/chat', () => ({
2929} ) ) ;
3030
3131const makeMockStream = ( ) => {
32- const stream = {
33- pipe : vi . fn ( ) ,
34- on : vi . fn ( ( ) => stream )
35- } ;
32+ const stream = Readable . from ( [ Buffer . from ( 'mock file content' ) ] ) ;
33+ vi . spyOn ( stream , 'pipe' ) ;
3634 return stream ;
3735} ;
3836
37+ const makeMockReq = ( overrides : Record < string , unknown > ) => ( {
38+ aborted : false ,
39+ once : vi . fn ( ) ,
40+ off : vi . fn ( ) ,
41+ ...overrides
42+ } ) ;
43+
3944const makeMockRes = ( ) => {
4045 const headers : Record < string , string | number > = { } ;
41- const res = {
46+ const res = Object . assign ( new PassThrough ( ) , {
4247 headers,
4348 statusCode : 200 ,
4449 headersSent : false ,
45- writableFinished : false ,
4650 setHeader : vi . fn ( ( key : string , value : string | number ) => {
4751 headers [ key ] = value ;
4852 } ) ,
4953 getHeader : vi . fn ( ( key : string ) => headers [ key ] ) ,
50- once : vi . fn ( ( ) => res ) ,
5154 status : vi . fn ( ( statusCode : number ) => {
5255 res . statusCode = statusCode ;
5356 return res ;
5457 } ) ,
55- end : vi . fn ( ( ) => {
56- res . writableFinished = true ;
57- } ) ,
5858 json : vi . fn ( ( ) => {
59- res . writableFinished = true ;
60- } ) ,
61- destroy : vi . fn ( )
62- } ;
59+ res . end ( ) ;
60+ } )
61+ } ) ;
6362 return res ;
6463} ;
6564
@@ -88,19 +87,19 @@ describe('system file response content type', () => {
8887 type : 'download'
8988 } ) ;
9089
91- const req = {
90+ const req = makeMockReq ( {
9291 method : 'GET' ,
9392 url : '/api/system/file/download/token' ,
9493 headers : { } ,
9594 query : { token : 'token' }
96- } as any ;
95+ } ) as any ;
9796 const res = makeMockRes ( ) as any ;
9897
9998 await proxyDownloadHandler ( req , res ) ;
10099
101100 expect ( res . headers [ 'Content-Type' ] ) . toBe ( 'text/markdown; charset=utf-8' ) ;
102101 expect ( res . headers [ 'Content-Length' ] ) . toBe ( 32 ) ;
103- expect ( stream . pipe ) . toHaveBeenCalledWith ( res ) ;
102+ expect ( stream . pipe ) . toHaveBeenCalledWith ( res , expect . any ( Object ) ) ;
104103 } ) ;
105104
106105 it ( 'keeps binary content type unchanged in proxy download mode' , async ( ) => {
@@ -122,12 +121,12 @@ describe('system file response content type', () => {
122121 type : 'download'
123122 } ) ;
124123
125- const req = {
124+ const req = makeMockReq ( {
126125 method : 'GET' ,
127126 url : '/api/system/file/download/token' ,
128127 headers : { } ,
129128 query : { token : 'token' }
130- } as any ;
129+ } ) as any ;
131130 const res = makeMockRes ( ) as any ;
132131
133132 await proxyDownloadHandler ( req , res ) ;
@@ -193,25 +192,29 @@ describe('system file response content type', () => {
193192 it ( 'adds utf-8 charset for text files in legacy file entry' , async ( ) => {
194193 const stream = makeMockStream ( ) ;
195194 const datasetSource = {
195+ bucketName : 'fastgpt-private' ,
196196 getFileStream : vi . fn ( ) . mockResolvedValue ( stream ) ,
197197 getFileMetadata : vi . fn ( ) . mockResolvedValue ( {
198198 filename : 'page.html' ,
199199 contentType : 'text/html' ,
200200 contentLength : 128
201201 } )
202202 } ;
203+ ( global as any ) . s3BucketMap = {
204+ 'fastgpt-private' : datasetSource
205+ } ;
203206 vi . mocked ( getS3DatasetSource ) . mockReturnValue ( datasetSource as any ) ;
204207 vi . mocked ( getS3ChatSource ) . mockReturnValue ( { } as any ) ;
205208 vi . mocked ( verifyToken ) . mockResolvedValue ( {
206209 objectKey : 'dataset/team/page.html'
207210 } ) ;
208211
209- const req = { query : { jwt : 'jwt' } } as any ;
212+ const req = makeMockReq ( { query : { jwt : 'jwt' } } ) as any ;
210213 const res = makeMockRes ( ) as any ;
211214
212215 await legacyFileHandler ( req , res ) ;
213216
214217 expect ( res . headers [ 'Content-Type' ] ) . toBe ( 'text/html; charset=utf-8' ) ;
215- expect ( stream . pipe ) . toHaveBeenCalledWith ( res ) ;
218+ expect ( stream . pipe ) . toHaveBeenCalledWith ( res , expect . any ( Object ) ) ;
216219 } ) ;
217220} ) ;
0 commit comments