|
12 | 12 | * - Output structure formatting |
13 | 13 | */ |
14 | 14 |
|
| 15 | +import os from "os" |
15 | 16 | import path from "path" |
16 | 17 |
|
17 | 18 | import type { ToolUse } from "../../../shared/tools" |
@@ -581,7 +582,72 @@ describe("ReadFileTool", () => { |
581 | 582 |
|
582 | 583 | await readFileTool.execute({ path: "empty.ts" }, mockTask as any, callbacks) |
583 | 584 |
|
584 | | - expect(callbacks.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("empty")) |
| 585 | + expect(callbacks.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("Note: File is empty")) |
| 586 | + }) |
| 587 | + |
| 588 | + it("should propagate beyond-EOF error instead of reporting file as empty", async () => { |
| 589 | + const mockTask = createMockTask() |
| 590 | + const callbacks = createMockCallbacks() |
| 591 | + |
| 592 | + mockedFsReadFile.mockResolvedValue(Buffer.from("line1\nline2\nline3\nline4\nline5")) |
| 593 | + mockedReadWithSlice.mockReturnValue({ |
| 594 | + content: "Error: offset 100 is beyond file end (5 lines)", |
| 595 | + returnedLines: 0, |
| 596 | + totalLines: 5, |
| 597 | + wasTruncated: false, |
| 598 | + includedRanges: [], |
| 599 | + }) |
| 600 | + |
| 601 | + await readFileTool.execute( |
| 602 | + { path: "test.ts", mode: "slice", offset: 100 }, |
| 603 | + mockTask as unknown as Task, |
| 604 | + callbacks, |
| 605 | + ) |
| 606 | + |
| 607 | + expect(mockedReadWithSlice).toHaveBeenCalledWith(expect.any(String), 99, expect.any(Number)) |
| 608 | + expect(callbacks.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("beyond file end")) |
| 609 | + expect(callbacks.pushToolResult).not.toHaveBeenCalledWith(expect.stringContaining("Note: File is empty")) |
| 610 | + }) |
| 611 | + |
| 612 | + // Integration counterpart to the mocked case above: the real readWithSlice |
| 613 | + // producer runs against a real file, so the error text and its 1-based |
| 614 | + // offset are proven end-to-end instead of being asserted against a stub. |
| 615 | + it("should propagate the real beyond-EOF error from the producer for a real file", async () => { |
| 616 | + const realFs = await vi.importActual<typeof import("fs/promises")>("fs/promises") |
| 617 | + const realPath = await vi.importActual<typeof import("path")>("path") |
| 618 | + const tempDir = await realFs.mkdtemp(realPath.join(os.tmpdir(), "read-file-beyond-eof-")) |
| 619 | + const fileName = "sample.txt" |
| 620 | + await realFs.writeFile(realPath.join(tempDir, fileName), "line1\nline2\nline3\nline4\nline5", "utf8") |
| 621 | + |
| 622 | + vi.doUnmock("fs/promises") |
| 623 | + vi.doUnmock("path") |
| 624 | + vi.doUnmock("isbinaryfile") |
| 625 | + vi.doUnmock("../../../integrations/misc/indentation-reader") |
| 626 | + vi.resetModules() |
| 627 | + |
| 628 | + try { |
| 629 | + const { ReadFileTool } = await import("../ReadFileTool") |
| 630 | + const mockTask = { ...createMockTask(), cwd: tempDir } |
| 631 | + const callbacks = createMockCallbacks() |
| 632 | + |
| 633 | + await new ReadFileTool().execute( |
| 634 | + { path: fileName, mode: "slice", offset: 100 }, |
| 635 | + mockTask as unknown as Task, |
| 636 | + callbacks, |
| 637 | + ) |
| 638 | + |
| 639 | + expect(mockedReadWithSlice).not.toHaveBeenCalled() |
| 640 | + expect(callbacks.pushToolResult).toHaveBeenCalledTimes(1) |
| 641 | + expect(callbacks.pushToolResult).toHaveBeenCalledWith( |
| 642 | + expect.stringContaining("Error: offset 100 is beyond file end (5 lines)"), |
| 643 | + ) |
| 644 | + expect(callbacks.pushToolResult).not.toHaveBeenCalledWith( |
| 645 | + expect.stringContaining("Note: File is empty"), |
| 646 | + ) |
| 647 | + } finally { |
| 648 | + vi.resetModules() |
| 649 | + await realFs.rm(tempDir, { recursive: true, force: true }).catch(() => {}) |
| 650 | + } |
585 | 651 | }) |
586 | 652 | }) |
587 | 653 |
|
|
0 commit comments