11import { describe , expect , it , vi , beforeEach } from "vitest" ;
2- import { fileGeneratorTool , FileGeneratorToolResult } from "./index" ;
32import type { FileStorage } from "lib/file-storage/file-storage.interface" ;
43
54// Mock the server file storage
5+ const mockUpload = vi . fn ( ) ;
6+ const mockGetDownloadUrl = vi . fn ( ) ;
7+
68vi . mock ( "lib/file-storage" , ( ) => ( {
79 serverFileStorage : {
8- upload : vi . fn ( ) ,
9- getDownloadUrl : vi . fn ( ) ,
10+ upload : mockUpload ,
11+ getDownloadUrl : mockGetDownloadUrl ,
1012 } as Partial < FileStorage > ,
1113} ) ) ;
1214
1315// Mock logger
1416vi . mock ( "logger" , ( ) => ( {
1517 default : {
1618 error : vi . fn ( ) ,
19+ info : vi . fn ( ) ,
1720 } ,
1821} ) ) ;
1922
2023// Import after mocking
21- const { serverFileStorage } = await import ( "lib/file-storage" ) ;
24+ const { fileGeneratorTool } = await import ( "./index" ) ;
25+ type FileGeneratorToolResult = Awaited <
26+ ReturnType < typeof fileGeneratorTool . execute >
27+ > ;
2228
2329describe ( "fileGeneratorTool.execute" , ( ) => {
2430 beforeEach ( ( ) => {
@@ -48,7 +54,6 @@ describe("fileGeneratorTool.execute", () => {
4854
4955 testCases . forEach ( ( { ext, expected } ) => {
5056 it ( `should infer ${ expected } for .${ ext } files` , async ( ) => {
51- const mockUpload = vi . mocked ( serverFileStorage . upload ! ) ;
5257 mockUpload . mockResolvedValue ( {
5358 key : "test-key" ,
5459 sourceUrl : "https://example.com/file" ,
@@ -86,7 +91,6 @@ describe("fileGeneratorTool.execute", () => {
8691 } ) ;
8792
8893 it ( "should use provided mimeType over inferred one" , async ( ) => {
89- const mockUpload = vi . mocked ( serverFileStorage . upload ! ) ;
9094 mockUpload . mockResolvedValue ( {
9195 key : "test-key" ,
9296 sourceUrl : "https://example.com/file" ,
@@ -125,7 +129,6 @@ describe("fileGeneratorTool.execute", () => {
125129
126130 describe ( "file upload" , ( ) => {
127131 it ( "should upload file content as Buffer" , async ( ) => {
128- const mockUpload = vi . mocked ( serverFileStorage . upload ! ) ;
129132 mockUpload . mockResolvedValue ( {
130133 key : "test-key" ,
131134 sourceUrl : "https://example.com/file" ,
@@ -159,9 +162,6 @@ describe("fileGeneratorTool.execute", () => {
159162 } ) ;
160163
161164 it ( "should use presigned URL when available" , async ( ) => {
162- const mockUpload = vi . mocked ( serverFileStorage . upload ! ) ;
163- const mockGetDownloadUrl = vi . mocked ( serverFileStorage . getDownloadUrl ! ) ;
164-
165165 mockUpload . mockResolvedValue ( {
166166 key : "test-key" ,
167167 sourceUrl : "https://s3.example.com/file" ,
@@ -204,11 +204,7 @@ describe("fileGeneratorTool.execute", () => {
204204 }
205205 } ) ;
206206
207- it ( "should fall back to sourceUrl when getDownloadUrl is not available" , async ( ) => {
208- const mockUpload = vi . mocked ( serverFileStorage . upload ! ) ;
209- // Remove getDownloadUrl
210- ( serverFileStorage as any ) . getDownloadUrl = undefined ;
211-
207+ it ( "should fall back to sourceUrl when getDownloadUrl returns null" , async ( ) => {
212208 mockUpload . mockResolvedValue ( {
213209 key : "test-key" ,
214210 sourceUrl : "https://example.com/file" ,
@@ -221,6 +217,8 @@ describe("fileGeneratorTool.execute", () => {
221217 } ,
222218 } ) ;
223219
220+ mockGetDownloadUrl . mockResolvedValue ( null ) ;
221+
224222 let result :
225223 | FileGeneratorToolResult
226224 | AsyncIterable < FileGeneratorToolResult >
@@ -242,13 +240,9 @@ describe("fileGeneratorTool.execute", () => {
242240 if ( result && ! ( Symbol . asyncIterator in result ) ) {
243241 expect ( result . files [ 0 ] . url ) . toBe ( "https://example.com/file" ) ;
244242 }
245-
246- // Restore for other tests
247- ( serverFileStorage as any ) . getDownloadUrl = vi . fn ( ) ;
248243 } ) ;
249244
250245 it ( "should upload multiple files in parallel" , async ( ) => {
251- const mockUpload = vi . mocked ( serverFileStorage . upload ! ) ;
252246 mockUpload . mockImplementation ( async ( buffer , options ) => ( {
253247 key : `key-${ options ?. filename } ` ,
254248 sourceUrl : `https://example.com/${ options ?. filename } ` ,
@@ -290,7 +284,6 @@ describe("fileGeneratorTool.execute", () => {
290284
291285 describe ( "result format" , ( ) => {
292286 it ( "should return correct result structure" , async ( ) => {
293- const mockUpload = vi . mocked ( serverFileStorage . upload ! ) ;
294287 mockUpload . mockResolvedValue ( {
295288 key : "test-key" ,
296289 sourceUrl : "https://example.com/file.csv" ,
@@ -324,7 +317,7 @@ describe("fileGeneratorTool.execute", () => {
324317 }
325318
326319 if ( result && ! ( Symbol . asyncIterator in result ) ) {
327- expect ( result ) . toEqual ( {
320+ expect ( result ) . toMatchObject ( {
328321 files : [
329322 {
330323 url : "https://example.com/file.csv" ,
@@ -335,11 +328,11 @@ describe("fileGeneratorTool.execute", () => {
335328 ] ,
336329 description : "Test CSV file" ,
337330 } ) ;
331+ expect ( result . guide ) . toBeDefined ( ) ;
338332 }
339333 } ) ;
340334
341335 it ( "should include file size in result" , async ( ) => {
342- const mockUpload = vi . mocked ( serverFileStorage . upload ! ) ;
343336 const content = "test content with some length" ;
344337
345338 mockUpload . mockResolvedValue ( {
@@ -380,7 +373,6 @@ describe("fileGeneratorTool.execute", () => {
380373
381374 describe ( "error handling" , ( ) => {
382375 it ( "should throw error when upload fails" , async ( ) => {
383- const mockUpload = vi . mocked ( serverFileStorage . upload ! ) ;
384376 mockUpload . mockRejectedValue ( new Error ( "Upload failed" ) ) ;
385377
386378 if ( fileGeneratorTool . execute ) {
0 commit comments