|
1 | 1 | import { describe, expect, it, vi, beforeEach } from "vitest"; |
2 | | -import { fileGeneratorTool, FileGeneratorToolResult } from "./index"; |
3 | 2 | import type { FileStorage } from "lib/file-storage/file-storage.interface"; |
4 | 3 |
|
5 | 4 | // Mock the server file storage |
| 5 | +const mockUpload = vi.fn(); |
| 6 | +const mockGetDownloadUrl = vi.fn(); |
| 7 | + |
6 | 8 | vi.mock("lib/file-storage", () => ({ |
7 | 9 | serverFileStorage: { |
8 | | - upload: vi.fn(), |
9 | | - getDownloadUrl: vi.fn(), |
| 10 | + upload: mockUpload, |
| 11 | + getDownloadUrl: mockGetDownloadUrl, |
10 | 12 | } as Partial<FileStorage>, |
11 | 13 | })); |
12 | 14 |
|
13 | 15 | // Mock logger |
14 | 16 | vi.mock("logger", () => ({ |
15 | 17 | default: { |
16 | 18 | error: vi.fn(), |
| 19 | + info: vi.fn(), |
17 | 20 | }, |
18 | 21 | })); |
19 | 22 |
|
20 | 23 | // Import after mocking |
21 | | -const { serverFileStorage } = await import("lib/file-storage"); |
| 24 | +const { fileGeneratorTool } = await import("./index"); |
| 25 | + |
| 26 | +type FileGeneratorToolResult = { |
| 27 | + files: { |
| 28 | + url: string; |
| 29 | + filename: string; |
| 30 | + mimeType: string; |
| 31 | + size: number; |
| 32 | + }[]; |
| 33 | + description?: string; |
| 34 | + guide?: string; |
| 35 | +}; |
22 | 36 |
|
23 | 37 | describe("fileGeneratorTool.execute", () => { |
24 | 38 | beforeEach(() => { |
@@ -48,7 +62,6 @@ describe("fileGeneratorTool.execute", () => { |
48 | 62 |
|
49 | 63 | testCases.forEach(({ ext, expected }) => { |
50 | 64 | it(`should infer ${expected} for .${ext} files`, async () => { |
51 | | - const mockUpload = vi.mocked(serverFileStorage.upload!); |
52 | 65 | mockUpload.mockResolvedValue({ |
53 | 66 | key: "test-key", |
54 | 67 | sourceUrl: "https://example.com/file", |
@@ -86,7 +99,6 @@ describe("fileGeneratorTool.execute", () => { |
86 | 99 | }); |
87 | 100 |
|
88 | 101 | it("should use provided mimeType over inferred one", async () => { |
89 | | - const mockUpload = vi.mocked(serverFileStorage.upload!); |
90 | 102 | mockUpload.mockResolvedValue({ |
91 | 103 | key: "test-key", |
92 | 104 | sourceUrl: "https://example.com/file", |
@@ -125,7 +137,6 @@ describe("fileGeneratorTool.execute", () => { |
125 | 137 |
|
126 | 138 | describe("file upload", () => { |
127 | 139 | it("should upload file content as Buffer", async () => { |
128 | | - const mockUpload = vi.mocked(serverFileStorage.upload!); |
129 | 140 | mockUpload.mockResolvedValue({ |
130 | 141 | key: "test-key", |
131 | 142 | sourceUrl: "https://example.com/file", |
@@ -159,9 +170,6 @@ describe("fileGeneratorTool.execute", () => { |
159 | 170 | }); |
160 | 171 |
|
161 | 172 | it("should use presigned URL when available", async () => { |
162 | | - const mockUpload = vi.mocked(serverFileStorage.upload!); |
163 | | - const mockGetDownloadUrl = vi.mocked(serverFileStorage.getDownloadUrl!); |
164 | | - |
165 | 173 | mockUpload.mockResolvedValue({ |
166 | 174 | key: "test-key", |
167 | 175 | sourceUrl: "https://s3.example.com/file", |
@@ -205,9 +213,8 @@ describe("fileGeneratorTool.execute", () => { |
205 | 213 | }); |
206 | 214 |
|
207 | 215 | 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; |
| 216 | + // Temporarily set getDownloadUrl to undefined |
| 217 | + mockGetDownloadUrl.mockImplementation(() => undefined as any); |
211 | 218 |
|
212 | 219 | mockUpload.mockResolvedValue({ |
213 | 220 | key: "test-key", |
@@ -242,13 +249,9 @@ describe("fileGeneratorTool.execute", () => { |
242 | 249 | if (result && !(Symbol.asyncIterator in result)) { |
243 | 250 | expect(result.files[0].url).toBe("https://example.com/file"); |
244 | 251 | } |
245 | | - |
246 | | - // Restore for other tests |
247 | | - (serverFileStorage as any).getDownloadUrl = vi.fn(); |
248 | 252 | }); |
249 | 253 |
|
250 | 254 | it("should upload multiple files in parallel", async () => { |
251 | | - const mockUpload = vi.mocked(serverFileStorage.upload!); |
252 | 255 | mockUpload.mockImplementation(async (buffer, options) => ({ |
253 | 256 | key: `key-${options?.filename}`, |
254 | 257 | sourceUrl: `https://example.com/${options?.filename}`, |
@@ -290,7 +293,6 @@ describe("fileGeneratorTool.execute", () => { |
290 | 293 |
|
291 | 294 | describe("result format", () => { |
292 | 295 | it("should return correct result structure", async () => { |
293 | | - const mockUpload = vi.mocked(serverFileStorage.upload!); |
294 | 296 | mockUpload.mockResolvedValue({ |
295 | 297 | key: "test-key", |
296 | 298 | sourceUrl: "https://example.com/file.csv", |
@@ -334,12 +336,13 @@ describe("fileGeneratorTool.execute", () => { |
334 | 336 | }, |
335 | 337 | ], |
336 | 338 | description: "Test CSV file", |
| 339 | + guide: |
| 340 | + "Your file has been generated successfully and is ready for download above. You can click the download button to save it to your device.", |
337 | 341 | }); |
338 | 342 | } |
339 | 343 | }); |
340 | 344 |
|
341 | 345 | it("should include file size in result", async () => { |
342 | | - const mockUpload = vi.mocked(serverFileStorage.upload!); |
343 | 346 | const content = "test content with some length"; |
344 | 347 |
|
345 | 348 | mockUpload.mockResolvedValue({ |
@@ -380,7 +383,6 @@ describe("fileGeneratorTool.execute", () => { |
380 | 383 |
|
381 | 384 | describe("error handling", () => { |
382 | 385 | it("should throw error when upload fails", async () => { |
383 | | - const mockUpload = vi.mocked(serverFileStorage.upload!); |
384 | 386 | mockUpload.mockRejectedValue(new Error("Upload failed")); |
385 | 387 |
|
386 | 388 | if (fileGeneratorTool.execute) { |
|
0 commit comments