Skip to content

Commit ea60c4f

Browse files
committed
Refactor test files: update type assertions for mocked functions to improve type safety and clarity
1 parent 027d679 commit ea60c4f

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

src/agent/tools/definitions/search.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ async function implementation(args: z.infer<typeof searchSchema>["arguments"]):
5656
child.on("error", (err) => {
5757
reject(new ToolError(`Command failed to start: ${err.message}`));
5858
});
59-
} catch (err: any) {
60-
reject(new ToolError(`Command failed to start: ${err.message}`));
59+
} catch (err: unknown) {
60+
const message = err instanceof Error ? err.message : String(err);
61+
reject(new ToolError(`Command failed to start: ${message}`));
6162
}
6263
});
6364
}

tests/agent/tools/definitions/edit.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ vi.mock("../../../../src/agent/file-tracker");
88
vi.mock("../../../../src/agent/autofix");
99

1010
describe("edit tool", () => {
11-
const mockAssertCanEdit = vi.mocked(fileTracker.assertCanEdit);
1211
const mockRead = vi.mocked(fileTracker.read);
1312
const mockWrite = vi.mocked(fileTracker.write);
1413
const mockAutofixEdit = vi.mocked(autofixEdit);

tests/agent/tools/definitions/list.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("list tool", () => {
1414
const mockStat = vi.mocked(fs.stat);
1515
const mockReaddir = vi.mocked(fs.readdir);
1616

17-
mockStat.mockResolvedValue({ isDirectory: () => true } as any);
17+
mockStat.mockResolvedValue({ isDirectory: () => true } as fs.Stats);
1818
mockReaddir.mockResolvedValue(["file1.ts", "file2.ts"]);
1919

2020
const { implementation } = listTool;
@@ -29,7 +29,7 @@ describe("list tool", () => {
2929
const mockStat = vi.mocked(fs.stat);
3030
const mockReaddir = vi.mocked(fs.readdir);
3131

32-
mockStat.mockResolvedValue({ isDirectory: () => true } as any);
32+
mockStat.mockResolvedValue({ isDirectory: () => true } as fs.Stats);
3333
mockReaddir.mockResolvedValue([]);
3434

3535
const { implementation } = listTool;
@@ -40,7 +40,7 @@ describe("list tool", () => {
4040

4141
it("should throw a ToolError when the path is not a directory", async () => {
4242
const mockStat = vi.mocked(fs.stat);
43-
mockStat.mockResolvedValue({ isDirectory: () => false } as any);
43+
mockStat.mockResolvedValue({ isDirectory: () => false } as fs.Stats);
4444

4545
const { implementation } = listTool;
4646
await expect(implementation({ path: "/test" })).rejects.toThrow(
@@ -75,7 +75,7 @@ describe("list tool", () => {
7575
const mockReaddir = vi.mocked(fs.readdir);
7676
const cwd = process.cwd();
7777

78-
mockStat.mockResolvedValue({ isDirectory: () => true } as any);
78+
mockStat.mockResolvedValue({ isDirectory: () => true } as fs.Stats);
7979
mockReaddir.mockResolvedValue(["file1.ts", "file2.ts"]);
8080

8181
const { implementation } = listTool;

tests/agent/tools/definitions/mcp.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { describe, it, expect, vi, beforeEach } from "vitest";
22
import mcpTool from "../../../../src/agent/tools/definitions/mcp";
33
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
44
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
5-
import { ToolError } from "../../../../src/agent/errors";
65
import type { Config } from "../../../../src/config";
76

87
vi.mock("@modelcontextprotocol/sdk/client/index.js");
@@ -12,9 +11,9 @@ describe("mcp tool", () => {
1211
const mockClient = vi.mocked(Client);
1312
const mockTransport = vi.mocked(StdioClientTransport);
1413

15-
let mockConnect: any;
16-
let mockCallTool: any;
17-
let mockClose: any;
14+
let mockConnect: vi.Mock;
15+
let mockCallTool: vi.Mock;
16+
let mockClose: vi.Mock;
1817

1918
beforeEach(() => {
2019
vi.resetAllMocks();
@@ -29,23 +28,23 @@ describe("mcp tool", () => {
2928
({
3029
connect: mockConnect,
3130
callTool: mockCallTool,
32-
}) as any,
31+
}) as unknown as Client,
3332
);
3433
mockTransport.mockImplementation(
3534
() =>
3635
({
3736
close: mockClose,
38-
}) as any,
37+
}) as unknown as StdioClientTransport,
3938
);
4039
});
4140

42-
const mockConfig: Config = {
41+
const mockConfig = {
4342
mcpServers: {
4443
testServer: {
4544
command: "test-command",
4645
},
4746
},
48-
} as any;
47+
} as unknown as Config;
4948

5049
it("should call an MCP tool and return the text content", async () => {
5150
mockCallTool.mockResolvedValue({
@@ -143,7 +142,7 @@ describe("mcp tool", () => {
143142
args: ["--arg1", "--arg2"],
144143
},
145144
},
146-
} as any;
145+
} as unknown as Config;
147146

148147
const { implementation } = mcpTool;
149148
await implementation({ server: "testServer", tool: "testTool" }, configWithArgs);

tests/agent/tools/definitions/search.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("search tool", () => {
2424
}
2525
}),
2626
};
27-
mockSpawn.mockReturnValue(mockChildProcess as any);
27+
mockSpawn.mockReturnValue(mockChildProcess as unknown as ReturnType<typeof spawn>);
2828

2929
const { implementation } = searchTool;
3030
const result = await implementation({ query: "test" });
@@ -60,7 +60,7 @@ describe("search tool", () => {
6060
}
6161
}),
6262
};
63-
mockSpawn.mockReturnValue(mockChildProcess as any);
63+
mockSpawn.mockReturnValue(mockChildProcess as unknown as ReturnType<typeof spawn>);
6464

6565
const { implementation } = searchTool;
6666
const result = await implementation({ query: "nonexistent" });
@@ -87,7 +87,7 @@ describe("search tool", () => {
8787
}
8888
}),
8989
};
90-
mockSpawn.mockReturnValue(mockChildProcess as any);
90+
mockSpawn.mockReturnValue(mockChildProcess as unknown as ReturnType<typeof spawn>);
9191

9292
const { implementation } = searchTool;
9393
await expect(implementation({ query: "test" })).rejects.toThrow(

0 commit comments

Comments
 (0)