Skip to content

Commit 3abc7e6

Browse files
mrjasonroyclaude
andcommitted
fix: fix file-generator test mocking to work with Next.js 16
The file-generator tests were failing due to improper mock setup. Updated to match the pattern from PR cgoinglove#326: - Move mock function declarations before vi.mock() - Import module after mocking with await import() - Define FileGeneratorToolResult type inline instead of importing - Include guide field in test assertion All 420 tests now pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
1 parent 6e44fa7 commit 3abc7e6

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/lib/ai/tools/file-generator/index.test.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
import { describe, expect, it, vi, beforeEach } from "vitest";
2-
import { fileGeneratorTool, FileGeneratorToolResult } from "./index";
32
import 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+
68
vi.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
1416
vi.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+
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+
};
2236

2337
describe("fileGeneratorTool.execute", () => {
2438
beforeEach(() => {
@@ -48,7 +62,6 @@ describe("fileGeneratorTool.execute", () => {
4862

4963
testCases.forEach(({ ext, expected }) => {
5064
it(`should infer ${expected} for .${ext} files`, async () => {
51-
const mockUpload = vi.mocked(serverFileStorage.upload!);
5265
mockUpload.mockResolvedValue({
5366
key: "test-key",
5467
sourceUrl: "https://example.com/file",
@@ -86,7 +99,6 @@ describe("fileGeneratorTool.execute", () => {
8699
});
87100

88101
it("should use provided mimeType over inferred one", async () => {
89-
const mockUpload = vi.mocked(serverFileStorage.upload!);
90102
mockUpload.mockResolvedValue({
91103
key: "test-key",
92104
sourceUrl: "https://example.com/file",
@@ -125,7 +137,6 @@ describe("fileGeneratorTool.execute", () => {
125137

126138
describe("file upload", () => {
127139
it("should upload file content as Buffer", async () => {
128-
const mockUpload = vi.mocked(serverFileStorage.upload!);
129140
mockUpload.mockResolvedValue({
130141
key: "test-key",
131142
sourceUrl: "https://example.com/file",
@@ -159,9 +170,6 @@ describe("fileGeneratorTool.execute", () => {
159170
});
160171

161172
it("should use presigned URL when available", async () => {
162-
const mockUpload = vi.mocked(serverFileStorage.upload!);
163-
const mockGetDownloadUrl = vi.mocked(serverFileStorage.getDownloadUrl!);
164-
165173
mockUpload.mockResolvedValue({
166174
key: "test-key",
167175
sourceUrl: "https://s3.example.com/file",
@@ -205,9 +213,8 @@ describe("fileGeneratorTool.execute", () => {
205213
});
206214

207215
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);
211218

212219
mockUpload.mockResolvedValue({
213220
key: "test-key",
@@ -242,13 +249,9 @@ describe("fileGeneratorTool.execute", () => {
242249
if (result && !(Symbol.asyncIterator in result)) {
243250
expect(result.files[0].url).toBe("https://example.com/file");
244251
}
245-
246-
// Restore for other tests
247-
(serverFileStorage as any).getDownloadUrl = vi.fn();
248252
});
249253

250254
it("should upload multiple files in parallel", async () => {
251-
const mockUpload = vi.mocked(serverFileStorage.upload!);
252255
mockUpload.mockImplementation(async (buffer, options) => ({
253256
key: `key-${options?.filename}`,
254257
sourceUrl: `https://example.com/${options?.filename}`,
@@ -290,7 +293,6 @@ describe("fileGeneratorTool.execute", () => {
290293

291294
describe("result format", () => {
292295
it("should return correct result structure", async () => {
293-
const mockUpload = vi.mocked(serverFileStorage.upload!);
294296
mockUpload.mockResolvedValue({
295297
key: "test-key",
296298
sourceUrl: "https://example.com/file.csv",
@@ -334,12 +336,13 @@ describe("fileGeneratorTool.execute", () => {
334336
},
335337
],
336338
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.",
337341
});
338342
}
339343
});
340344

341345
it("should include file size in result", async () => {
342-
const mockUpload = vi.mocked(serverFileStorage.upload!);
343346
const content = "test content with some length";
344347

345348
mockUpload.mockResolvedValue({
@@ -380,7 +383,6 @@ describe("fileGeneratorTool.execute", () => {
380383

381384
describe("error handling", () => {
382385
it("should throw error when upload fails", async () => {
383-
const mockUpload = vi.mocked(serverFileStorage.upload!);
384386
mockUpload.mockRejectedValue(new Error("Upload failed"));
385387

386388
if (fileGeneratorTool.execute) {

0 commit comments

Comments
 (0)