|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" |
| 2 | +import * as fs from "fs/promises" |
| 3 | +import * as path from "path" |
| 4 | +import { ShadowCheckpointService } from "../ShadowCheckpointService" |
| 5 | +import { fileExistsAtPath } from "../../../utils/fs" |
| 6 | +import { safeWriteJson } from "../../../utils/safeWriteJson" |
| 7 | +import type { ApiMessage } from "../../../core/task-persistence" |
| 8 | + |
| 9 | +vi.mock("fs/promises") |
| 10 | +vi.mock("../../../utils/fs") |
| 11 | +vi.mock("../../../utils/safeWriteJson") |
| 12 | + |
| 13 | +describe("API History Snapshots", () => { |
| 14 | + let service: ShadowCheckpointService |
| 15 | + const mockTaskId = "test-task-123" |
| 16 | + const mockCheckpointsDir = "/mock/checkpoints" |
| 17 | + const mockWorkspaceDir = "/mock/workspace" |
| 18 | + const mockLog = vi.fn() |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + vi.clearAllMocks() |
| 22 | + // Create a concrete subclass for testing |
| 23 | + class TestCheckpointService extends ShadowCheckpointService { |
| 24 | + constructor() { |
| 25 | + super(mockTaskId, mockCheckpointsDir, mockWorkspaceDir, mockLog) |
| 26 | + } |
| 27 | + } |
| 28 | + service = new TestCheckpointService() |
| 29 | + }) |
| 30 | + |
| 31 | + describe("saveApiHistorySnapshot", () => { |
| 32 | + it("should save API history snapshot to the correct path", async () => { |
| 33 | + const commitHash = "abc123" |
| 34 | + const apiHistory: ApiMessage[] = [ |
| 35 | + { |
| 36 | + role: "user", |
| 37 | + content: "Test message 1", |
| 38 | + ts: 1000, |
| 39 | + }, |
| 40 | + { |
| 41 | + role: "assistant", |
| 42 | + content: "Test response 1", |
| 43 | + ts: 2000, |
| 44 | + }, |
| 45 | + ] |
| 46 | + |
| 47 | + await service.saveApiHistorySnapshot(commitHash, apiHistory) |
| 48 | + |
| 49 | + const expectedPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) |
| 50 | + expect(safeWriteJson).toHaveBeenCalledWith(expectedPath, apiHistory) |
| 51 | + }) |
| 52 | + |
| 53 | + it("should handle empty API history", async () => { |
| 54 | + const commitHash = "def456" |
| 55 | + const apiHistory: ApiMessage[] = [] |
| 56 | + |
| 57 | + await service.saveApiHistorySnapshot(commitHash, apiHistory) |
| 58 | + |
| 59 | + const expectedPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) |
| 60 | + expect(safeWriteJson).toHaveBeenCalledWith(expectedPath, apiHistory) |
| 61 | + }) |
| 62 | + |
| 63 | + it("should handle API history with context compression markers", async () => { |
| 64 | + const commitHash = "ghi789" |
| 65 | + const apiHistory: ApiMessage[] = [ |
| 66 | + { |
| 67 | + role: "user", |
| 68 | + content: "Initial message", |
| 69 | + ts: 1000, |
| 70 | + }, |
| 71 | + { |
| 72 | + role: "assistant", |
| 73 | + content: "Response", |
| 74 | + ts: 2000, |
| 75 | + }, |
| 76 | + { |
| 77 | + role: "user", |
| 78 | + content: "Compressed context summary", |
| 79 | + ts: 3000, |
| 80 | + isSummary: true, |
| 81 | + }, |
| 82 | + { |
| 83 | + role: "assistant", |
| 84 | + content: "After compression", |
| 85 | + ts: 4000, |
| 86 | + }, |
| 87 | + ] |
| 88 | + |
| 89 | + await service.saveApiHistorySnapshot(commitHash, apiHistory) |
| 90 | + |
| 91 | + const expectedPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) |
| 92 | + expect(safeWriteJson).toHaveBeenCalledWith(expectedPath, apiHistory) |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + describe("restoreApiHistorySnapshot", () => { |
| 97 | + it("should restore API history snapshot when file exists", async () => { |
| 98 | + const commitHash = "abc123" |
| 99 | + const expectedHistory: ApiMessage[] = [ |
| 100 | + { |
| 101 | + role: "user", |
| 102 | + content: "Test message 1", |
| 103 | + ts: 1000, |
| 104 | + }, |
| 105 | + { |
| 106 | + role: "assistant", |
| 107 | + content: "Test response 1", |
| 108 | + ts: 2000, |
| 109 | + }, |
| 110 | + ] |
| 111 | + |
| 112 | + const snapshotPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) |
| 113 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 114 | + vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(expectedHistory)) |
| 115 | + |
| 116 | + const result = await service.restoreApiHistorySnapshot(commitHash) |
| 117 | + |
| 118 | + expect(result).toEqual(expectedHistory) |
| 119 | + expect(fileExistsAtPath).toHaveBeenCalledWith(snapshotPath) |
| 120 | + expect(fs.readFile).toHaveBeenCalledWith(snapshotPath, "utf8") |
| 121 | + }) |
| 122 | + |
| 123 | + it("should return null when snapshot file does not exist", async () => { |
| 124 | + const commitHash = "nonexistent" |
| 125 | + const snapshotPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) |
| 126 | + vi.mocked(fileExistsAtPath).mockResolvedValue(false) |
| 127 | + |
| 128 | + const result = await service.restoreApiHistorySnapshot(commitHash) |
| 129 | + |
| 130 | + expect(result).toBeNull() |
| 131 | + expect(fileExistsAtPath).toHaveBeenCalledWith(snapshotPath) |
| 132 | + expect(fs.readFile).not.toHaveBeenCalled() |
| 133 | + }) |
| 134 | + |
| 135 | + it("should handle invalid JSON gracefully", async () => { |
| 136 | + const commitHash = "invalid" |
| 137 | + const snapshotPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) |
| 138 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 139 | + vi.mocked(fs.readFile).mockResolvedValue("{ invalid json") |
| 140 | + |
| 141 | + const result = await service.restoreApiHistorySnapshot(commitHash) |
| 142 | + |
| 143 | + expect(result).toBeNull() |
| 144 | + expect(mockLog).toHaveBeenCalledWith(expect.stringContaining("Failed to restore snapshot")) |
| 145 | + }) |
| 146 | + |
| 147 | + it("should handle file read errors gracefully", async () => { |
| 148 | + const commitHash = "error" |
| 149 | + const snapshotPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) |
| 150 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 151 | + vi.mocked(fs.readFile).mockRejectedValue(new Error("File read error")) |
| 152 | + |
| 153 | + const result = await service.restoreApiHistorySnapshot(commitHash) |
| 154 | + |
| 155 | + expect(result).toBeNull() |
| 156 | + expect(mockLog).toHaveBeenCalledWith(expect.stringContaining("Failed to restore snapshot")) |
| 157 | + }) |
| 158 | + |
| 159 | + it("should restore API history with preserved context compression state", async () => { |
| 160 | + const commitHash = "with-compression" |
| 161 | + const expectedHistory: ApiMessage[] = [ |
| 162 | + { |
| 163 | + role: "user", |
| 164 | + content: "Original message 1", |
| 165 | + ts: 1000, |
| 166 | + }, |
| 167 | + { |
| 168 | + role: "assistant", |
| 169 | + content: "Original response 1", |
| 170 | + ts: 2000, |
| 171 | + }, |
| 172 | + { |
| 173 | + role: "user", |
| 174 | + content: "Original message 2", |
| 175 | + ts: 3000, |
| 176 | + }, |
| 177 | + { |
| 178 | + role: "assistant", |
| 179 | + content: "Original response 2", |
| 180 | + ts: 4000, |
| 181 | + }, |
| 182 | + // No compression markers - full history preserved |
| 183 | + ] |
| 184 | + |
| 185 | + const snapshotPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) |
| 186 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 187 | + vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(expectedHistory)) |
| 188 | + |
| 189 | + const result = await service.restoreApiHistorySnapshot(commitHash) |
| 190 | + |
| 191 | + expect(result).toEqual(expectedHistory) |
| 192 | + expect(result).toHaveLength(4) |
| 193 | + expect(result?.every((msg) => !msg.isSummary)).toBe(true) |
| 194 | + }) |
| 195 | + }) |
| 196 | +}) |
0 commit comments