|
14 | 14 |
|
15 | 15 | import path from "path" |
16 | 16 |
|
| 17 | +import type { ToolUse } from "../../../shared/tools" |
| 18 | +import type { Task } from "../../task/Task" |
| 19 | + |
17 | 20 | import { isBinaryFile } from "isbinaryfile" |
18 | 21 |
|
19 | 22 | import { readFileTool, ReadFileTool } from "../ReadFileTool" |
@@ -987,19 +990,82 @@ describe("ReadFileTool", () => { |
987 | 990 | }) |
988 | 991 |
|
989 | 992 | describe("handlePartial", () => { |
990 | | - it("should handle partial display for new format", async () => { |
| 993 | + it.each([{}, { path: "" }, undefined])( |
| 994 | + "renders streamed native arguments before their path arrives: %j", |
| 995 | + async (nativeArgs) => { |
| 996 | + const mockTask = createMockTask() |
| 997 | + mockTask.cwd = path.resolve(mockTask.cwd) |
| 998 | + const streamedBlock = { |
| 999 | + type: "tool_use", |
| 1000 | + name: "read_file", |
| 1001 | + params: {}, |
| 1002 | + nativeArgs, |
| 1003 | + partial: true, |
| 1004 | + } |
| 1005 | + |
| 1006 | + // Streaming JSON is incomplete even though finalized native arguments require a path. |
| 1007 | + await Reflect.apply(readFileTool.handlePartial, readFileTool, [mockTask, streamedBlock]) |
| 1008 | + |
| 1009 | + expect(mockTask.ask).toHaveBeenCalledExactlyOnceWith( |
| 1010 | + "tool", |
| 1011 | + JSON.stringify({ tool: "readFile", path: "", isOutsideWorkspace: false }), |
| 1012 | + true, |
| 1013 | + ) |
| 1014 | + expect(mockedFsReadFile).not.toHaveBeenCalled() |
| 1015 | + expect(mockedFsStat).not.toHaveBeenCalled() |
| 1016 | + expect(mockedExtractTextFromFile).not.toHaveBeenCalled() |
| 1017 | + }, |
| 1018 | + ) |
| 1019 | + |
| 1020 | + it("renders incomplete legacy arguments without a file entry or outside-workspace warning", async () => { |
991 | 1021 | const mockTask = createMockTask() |
992 | | - mockTask.ask.mockResolvedValue({ response: "yesButtonClicked", text: undefined, images: undefined }) |
993 | | - |
994 | | - const block = { |
995 | | - nativeArgs: { path: "src/app.ts" }, |
| 1022 | + mockTask.cwd = path.resolve(mockTask.cwd) |
| 1023 | + const block: ToolUse<"read_file"> = { |
| 1024 | + type: "tool_use", |
| 1025 | + name: "read_file", |
| 1026 | + params: {}, |
| 1027 | + nativeArgs: { files: [], _legacyFormat: true }, |
996 | 1028 | partial: true, |
997 | 1029 | } |
998 | 1030 |
|
999 | | - await readFileTool.handlePartial(mockTask as any, block as any) |
| 1031 | + // This reader-boundary double omits Task fields unrelated to file reading. |
| 1032 | + await readFileTool.handlePartial(mockTask as unknown as Task, block) |
1000 | 1033 |
|
1001 | | - expect(mockTask.ask).toHaveBeenCalledWith("tool", expect.stringContaining("readFile"), true) |
1002 | | - }) |
| 1034 | + expect(mockTask.ask).toHaveBeenCalledExactlyOnceWith( |
| 1035 | + "tool", |
| 1036 | + JSON.stringify({ tool: "readFile", path: "", isOutsideWorkspace: false }), |
| 1037 | + true, |
| 1038 | + ) |
| 1039 | + expect(mockedFsReadFile).not.toHaveBeenCalled() |
| 1040 | + expect(mockedFsStat).not.toHaveBeenCalled() |
| 1041 | + expect(mockedExtractTextFromFile).not.toHaveBeenCalled() |
| 1042 | + }) |
| 1043 | + |
| 1044 | + it.each(["src/app.ts", "src/файл.ts", "src/e\u0301-📚.ts"])( |
| 1045 | + "should preserve partial display for %s", |
| 1046 | + async (filePath) => { |
| 1047 | + const mockTask = createMockTask() |
| 1048 | + mockTask.cwd = path.resolve(mockTask.cwd) |
| 1049 | + mockTask.ask.mockResolvedValue({ response: "yesButtonClicked", text: undefined, images: undefined }) |
| 1050 | + |
| 1051 | + const block: ToolUse<"read_file"> = { |
| 1052 | + type: "tool_use", |
| 1053 | + name: "read_file", |
| 1054 | + params: {}, |
| 1055 | + nativeArgs: { path: filePath }, |
| 1056 | + partial: true, |
| 1057 | + } |
| 1058 | + |
| 1059 | + // This reader-boundary double omits Task fields unrelated to file reading. |
| 1060 | + await readFileTool.handlePartial(mockTask as unknown as Task, block) |
| 1061 | + |
| 1062 | + expect(mockTask.ask).toHaveBeenCalledExactlyOnceWith( |
| 1063 | + "tool", |
| 1064 | + JSON.stringify({ tool: "readFile", path: filePath, isOutsideWorkspace: true }), |
| 1065 | + true, |
| 1066 | + ) |
| 1067 | + }, |
| 1068 | + ) |
1003 | 1069 |
|
1004 | 1070 | it("should handle partial display for legacy format", async () => { |
1005 | 1071 | const mockTask = createMockTask() |
@@ -1043,9 +1109,9 @@ describe("ReadFileTool", () => { |
1043 | 1109 | expect(mockTask.ask).toHaveBeenCalled() |
1044 | 1110 | }) |
1045 | 1111 |
|
1046 | | - it("should gracefully handle ask rejection in partial", async () => { |
| 1112 | + it.each(["Cancelled", "superseded"])("should gracefully handle %s ask rejection in partial", async (reason) => { |
1047 | 1113 | const mockTask = createMockTask() |
1048 | | - mockTask.ask.mockRejectedValue(new Error("Cancelled")) |
| 1114 | + mockTask.ask.mockRejectedValue(new Error(reason)) |
1049 | 1115 |
|
1050 | 1116 | const block = { |
1051 | 1117 | nativeArgs: { path: "test.ts" }, |
|
0 commit comments