|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import { FileContextTracker } from "../FileContextTracker" |
| 3 | + |
| 4 | +vi.mock("vscode", () => ({ |
| 5 | + workspace: { |
| 6 | + workspaceFolders: [{ uri: { fsPath: "/workspace" } }], |
| 7 | + createFileSystemWatcher: vi.fn().mockReturnValue({ |
| 8 | + onDidChange: vi.fn(), |
| 9 | + onDidCreate: vi.fn(), |
| 10 | + onDidDelete: vi.fn(), |
| 11 | + dispose: vi.fn(), |
| 12 | + }), |
| 13 | + }, |
| 14 | + Uri: { file: vi.fn((p: string) => ({ fsPath: p })) }, |
| 15 | + RelativePattern: vi.fn(), |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock("../../../utils/storage", () => ({ |
| 19 | + getTaskDirectoryPath: vi.fn().mockResolvedValue("/storage/task-1"), |
| 20 | +})) |
| 21 | + |
| 22 | +vi.mock("../../../utils/fs", () => ({ |
| 23 | + fileExistsAtPath: vi.fn().mockResolvedValue(false), |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock("../../../utils/safeWriteJson", () => ({ |
| 27 | + safeWriteJson: vi.fn().mockResolvedValue(undefined), |
| 28 | +})) |
| 29 | + |
| 30 | +vi.mock("fs/promises", () => ({ |
| 31 | + default: { readFile: vi.fn() }, |
| 32 | +})) |
| 33 | + |
| 34 | +vi.mock("path", async () => { |
| 35 | + const actual = await vi.importActual<typeof import("path")>("path") |
| 36 | + return { ...actual, default: actual } |
| 37 | +}) |
| 38 | + |
| 39 | +describe("FileContextTracker.addFileToFileContextTracker", () => { |
| 40 | + let tracker: FileContextTracker |
| 41 | + const mockProvider = { |
| 42 | + contextProxy: { |
| 43 | + globalStorageUri: { fsPath: "/storage" }, |
| 44 | + }, |
| 45 | + } as any |
| 46 | + |
| 47 | + beforeEach(() => { |
| 48 | + vi.clearAllMocks() |
| 49 | + tracker = new FileContextTracker(mockProvider, "task-1") |
| 50 | + }) |
| 51 | + |
| 52 | + it("creates a new active entry with record_source set to the given source", async () => { |
| 53 | + const { safeWriteJson } = await import("../../../utils/safeWriteJson") |
| 54 | + const mockWrite = vi.mocked(safeWriteJson) |
| 55 | + |
| 56 | + await tracker.addFileToFileContextTracker("task-1", "/workspace/foo.ts", "read_tool") |
| 57 | + |
| 58 | + expect(mockWrite).toHaveBeenCalledOnce() |
| 59 | + const written = mockWrite.mock.calls[0][1] as any |
| 60 | + const entry = written.files_in_context[0] |
| 61 | + expect(entry.path).toBe("/workspace/foo.ts") |
| 62 | + expect(entry.record_state).toBe("active") |
| 63 | + expect(entry.record_source).toBe("read_tool") |
| 64 | + expect(entry.roo_read_date).toBeTypeOf("number") |
| 65 | + }) |
| 66 | + |
| 67 | + it("marks existing active entries as stale before adding the new entry", async () => { |
| 68 | + const { fileExistsAtPath } = await import("../../../utils/fs") |
| 69 | + const fs = await import("fs/promises") |
| 70 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 71 | + vi.mocked(fs.default.readFile).mockResolvedValue( |
| 72 | + JSON.stringify({ |
| 73 | + files_in_context: [{ path: "/workspace/foo.ts", record_state: "active", record_source: "read_tool" }], |
| 74 | + }) as any, |
| 75 | + ) |
| 76 | + |
| 77 | + const { safeWriteJson } = await import("../../../utils/safeWriteJson") |
| 78 | + const mockWrite = vi.mocked(safeWriteJson) |
| 79 | + |
| 80 | + await tracker.addFileToFileContextTracker("task-1", "/workspace/foo.ts", "roo_edited") |
| 81 | + |
| 82 | + const written = mockWrite.mock.calls[0][1] as any |
| 83 | + expect(written.files_in_context[0].record_state).toBe("stale") |
| 84 | + expect(written.files_in_context[1].record_state).toBe("active") |
| 85 | + }) |
| 86 | +}) |
0 commit comments