|
13 | 13 | */ |
14 | 14 |
|
15 | 15 | import path from "path" |
| 16 | +import type { Stats } from "fs" |
16 | 17 |
|
17 | 18 | import { isBinaryFile } from "isbinaryfile" |
18 | 19 |
|
19 | 20 | import { readFileTool, ReadFileTool } from "../ReadFileTool" |
| 21 | +import type { Task } from "../../task/Task" |
| 22 | +import { ObservationRegistry } from "../../task/observationRegistry" |
| 23 | +import { computeVersionToken } from "../../../utils/versionToken" |
20 | 24 | import { formatResponse } from "../../prompts/responses" |
21 | 25 | import { |
22 | 26 | validateImageForProcessing, |
@@ -136,13 +140,15 @@ interface MockTaskOptions { |
136 | 140 | rooIgnoreAllowed?: boolean |
137 | 141 | maxImageFileSize?: number |
138 | 142 | maxTotalImageSize?: number |
| 143 | + observationRegistry?: import("../../task/observationRegistry").ObservationRegistry |
139 | 144 | } |
140 | 145 |
|
141 | 146 | function createMockTask(options: MockTaskOptions = {}) { |
142 | 147 | const { supportsImages = false, rooIgnoreAllowed = true, maxImageFileSize = 5, maxTotalImageSize = 20 } = options |
143 | 148 |
|
144 | 149 | return { |
145 | 150 | cwd: "/test/workspace", |
| 151 | + observationRegistry: options.observationRegistry, |
146 | 152 | api: { |
147 | 153 | getModel: vi.fn().mockReturnValue({ |
148 | 154 | info: { supportsImages }, |
@@ -1489,5 +1495,72 @@ describe("ReadFileTool", () => { |
1489 | 1495 |
|
1490 | 1496 | expect(mockTask.didToolFailInCurrentTurn).toBe(true) |
1491 | 1497 | }) |
| 1498 | + |
| 1499 | + describe("observation registry", () => { |
| 1500 | + it("records an observation on successful read of an existing file", async () => { |
| 1501 | + const mockTask = createMockTask({ |
| 1502 | + observationRegistry: new ObservationRegistry(), |
| 1503 | + }) |
| 1504 | + const callbacks = createMockCallbacks() |
| 1505 | + |
| 1506 | + // Override the beforeEach default stat mock with proper BigIntStats. |
| 1507 | + mockedFsStat.mockResolvedValue({ |
| 1508 | + isDirectory: () => false, |
| 1509 | + dev: BigInt(1), |
| 1510 | + ino: BigInt(2), |
| 1511 | + size: BigInt(300), |
| 1512 | + mtimeNs: BigInt(4_000_000_000n), |
| 1513 | + ctimeNs: BigInt(5_000_000_000n), |
| 1514 | + // Cast: the mock only implements the members the tool and versionToken read. |
| 1515 | + } as unknown as Stats) |
| 1516 | + mockedIsBinaryFile.mockResolvedValue(false) |
| 1517 | + |
| 1518 | + // Spy on observe to capture the exact key used (Windows path.resolve may use backslashes). |
| 1519 | + const reg = mockTask.observationRegistry! |
| 1520 | + const observeSpy = vi.spyOn(reg, "observe") |
| 1521 | + |
| 1522 | + // Cast: the mock task only implements the members ReadFileTool.execute touches. |
| 1523 | + await readFileTool.execute({ path: "existing.ts" }, mockTask as unknown as Task, callbacks) |
| 1524 | + |
| 1525 | + // Verify the tool called observe exactly once with a valid token. |
| 1526 | + expect(observeSpy).toHaveBeenCalledTimes(1) |
| 1527 | + const [calledPath, calledVersion] = observeSpy.mock.calls[0] |
| 1528 | + expect(calledPath).toContain("existing.ts") |
| 1529 | + expect(calledVersion).toMatch(/^\d+:\d+:\d+:\d+:\d+$/) |
| 1530 | + |
| 1531 | + // Verify get() returns the same data using the spy-captured key. |
| 1532 | + const obs = reg.get(calledPath) |
| 1533 | + expect(obs).toBeDefined() |
| 1534 | + expect(obs!.version).toBe(calledVersion) |
| 1535 | + }) |
| 1536 | + |
| 1537 | + it("a failed read (absent path) leaves the registry size 0 and does not throw", async () => { |
| 1538 | + const mockTask = createMockTask({ |
| 1539 | + observationRegistry: new ObservationRegistry(), |
| 1540 | + }) |
| 1541 | + const callbacks = createMockCallbacks() |
| 1542 | + |
| 1543 | + mockedFsReadFile.mockRejectedValue(new Error("ENOENT")) |
| 1544 | + |
| 1545 | + // Cast: the mock task only implements the members ReadFileTool.execute touches. |
| 1546 | + await readFileTool.execute({ path: "missing.ts" }, mockTask as unknown as Task, callbacks) |
| 1547 | + |
| 1548 | + // observationRegistry is guaranteed present because we passed it in createMockTask. |
| 1549 | + const reg = mockTask.observationRegistry |
| 1550 | + expect(reg).toBeDefined() |
| 1551 | + expect(reg!.size).toBe(0) |
| 1552 | + }) |
| 1553 | + |
| 1554 | + it("two separate Task-owned registries are independent", async () => { |
| 1555 | + const regA = new ObservationRegistry() |
| 1556 | + const regB = new ObservationRegistry() |
| 1557 | + regA.observe("/shared.ts", "v1") |
| 1558 | + expect(regA.get("/shared.ts")!.version).toBe("v1") |
| 1559 | + expect(regB.get("/shared.ts")).toBeUndefined() |
| 1560 | + regB.observe("/shared.ts", "v2") |
| 1561 | + expect(regA.get("/shared.ts")!.version).toBe("v1") |
| 1562 | + expect(regB.get("/shared.ts")!.version).toBe("v2") |
| 1563 | + }) |
| 1564 | + }) |
1492 | 1565 | }) |
1493 | 1566 | }) |
0 commit comments