|
| 1 | +/* eslint-disable @typescript-eslint/unbound-method */ |
| 2 | +import { describe, test, expect, beforeEach, mock } from "bun:test"; |
| 3 | +import { PartialService } from "./partialService"; |
| 4 | +import type { HistoryService } from "./historyService"; |
| 5 | +import type { Config } from "@/config"; |
| 6 | +import type { CmuxMessage } from "@/types/message"; |
| 7 | +import { Ok } from "@/types/result"; |
| 8 | + |
| 9 | +// Mock Config |
| 10 | +const createMockConfig = (): Config => { |
| 11 | + return { |
| 12 | + getSessionDir: mock((workspaceId: string) => `/tmp/test-sessions/${workspaceId}`), |
| 13 | + } as unknown as Config; |
| 14 | +}; |
| 15 | + |
| 16 | +// Mock HistoryService |
| 17 | +const createMockHistoryService = (): HistoryService => { |
| 18 | + return { |
| 19 | + appendToHistory: mock(() => Promise.resolve(Ok(undefined))), |
| 20 | + getHistory: mock(() => Promise.resolve(Ok([]))), |
| 21 | + updateHistory: mock(() => Promise.resolve(Ok(undefined))), |
| 22 | + truncateAfterMessage: mock(() => Promise.resolve(Ok(undefined))), |
| 23 | + clearHistory: mock(() => Promise.resolve(Ok(undefined))), |
| 24 | + } as unknown as HistoryService; |
| 25 | +}; |
| 26 | + |
| 27 | +describe("PartialService - Error Recovery", () => { |
| 28 | + let partialService: PartialService; |
| 29 | + let mockConfig: Config; |
| 30 | + let mockHistoryService: HistoryService; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + mockConfig = createMockConfig(); |
| 34 | + mockHistoryService = createMockHistoryService(); |
| 35 | + partialService = new PartialService(mockConfig, mockHistoryService); |
| 36 | + }); |
| 37 | + |
| 38 | + test("commitToHistory should strip error metadata and commit parts from errored partial", async () => { |
| 39 | + const workspaceId = "test-workspace"; |
| 40 | + const erroredPartial: CmuxMessage = { |
| 41 | + id: "msg-1", |
| 42 | + role: "assistant", |
| 43 | + metadata: { |
| 44 | + historySequence: 1, |
| 45 | + timestamp: Date.now(), |
| 46 | + model: "test-model", |
| 47 | + partial: true, |
| 48 | + error: "Stream error occurred", |
| 49 | + errorType: "network", |
| 50 | + }, |
| 51 | + parts: [ |
| 52 | + { type: "text", text: "Hello, I was processing when" }, |
| 53 | + { type: "text", text: " the error occurred" }, |
| 54 | + ], |
| 55 | + }; |
| 56 | + |
| 57 | + // Mock readPartial to return errored partial |
| 58 | + partialService.readPartial = mock(() => Promise.resolve(erroredPartial)); |
| 59 | + |
| 60 | + // Mock deletePartial |
| 61 | + partialService.deletePartial = mock(() => Promise.resolve(Ok(undefined))); |
| 62 | + |
| 63 | + // Mock getHistory to return no existing messages |
| 64 | + mockHistoryService.getHistory = mock(() => Promise.resolve(Ok([]))); |
| 65 | + |
| 66 | + // Call commitToHistory |
| 67 | + const result = await partialService.commitToHistory(workspaceId); |
| 68 | + |
| 69 | + // Should succeed |
| 70 | + expect(result.success).toBe(true); |
| 71 | + |
| 72 | + // Should have called appendToHistory with cleaned metadata (no error/errorType) |
| 73 | + const appendToHistory = mockHistoryService.appendToHistory as ReturnType<typeof mock>; |
| 74 | + expect(appendToHistory).toHaveBeenCalledTimes(1); |
| 75 | + const appendedMessage = appendToHistory.mock.calls[0][1] as CmuxMessage; |
| 76 | + |
| 77 | + expect(appendedMessage.id).toBe("msg-1"); |
| 78 | + expect(appendedMessage.parts).toEqual(erroredPartial.parts); |
| 79 | + expect(appendedMessage.metadata?.error).toBeUndefined(); |
| 80 | + expect(appendedMessage.metadata?.errorType).toBeUndefined(); |
| 81 | + expect(appendedMessage.metadata?.historySequence).toBe(1); |
| 82 | + |
| 83 | + // Should have deleted the partial after committing |
| 84 | + const deletePartial = partialService.deletePartial as ReturnType<typeof mock>; |
| 85 | + expect(deletePartial).toHaveBeenCalledWith(workspaceId); |
| 86 | + }); |
| 87 | + |
| 88 | + test("commitToHistory should update existing placeholder when errored partial has more parts", async () => { |
| 89 | + const workspaceId = "test-workspace"; |
| 90 | + const erroredPartial: CmuxMessage = { |
| 91 | + id: "msg-1", |
| 92 | + role: "assistant", |
| 93 | + metadata: { |
| 94 | + historySequence: 1, |
| 95 | + timestamp: Date.now(), |
| 96 | + model: "test-model", |
| 97 | + partial: true, |
| 98 | + error: "Stream error occurred", |
| 99 | + errorType: "network", |
| 100 | + }, |
| 101 | + parts: [ |
| 102 | + { type: "text", text: "Accumulated content before error" }, |
| 103 | + { |
| 104 | + type: "dynamic-tool", |
| 105 | + toolCallId: "call-1", |
| 106 | + toolName: "bash", |
| 107 | + state: "input-available", |
| 108 | + input: { script: "echo test" }, |
| 109 | + }, |
| 110 | + ], |
| 111 | + }; |
| 112 | + |
| 113 | + const existingPlaceholder: CmuxMessage = { |
| 114 | + id: "msg-1", |
| 115 | + role: "assistant", |
| 116 | + metadata: { |
| 117 | + historySequence: 1, |
| 118 | + timestamp: Date.now(), |
| 119 | + model: "test-model", |
| 120 | + partial: true, |
| 121 | + }, |
| 122 | + parts: [], // Empty placeholder |
| 123 | + }; |
| 124 | + |
| 125 | + // Mock readPartial to return errored partial |
| 126 | + partialService.readPartial = mock(() => Promise.resolve(erroredPartial)); |
| 127 | + |
| 128 | + // Mock deletePartial |
| 129 | + partialService.deletePartial = mock(() => Promise.resolve(Ok(undefined))); |
| 130 | + |
| 131 | + // Mock getHistory to return existing placeholder |
| 132 | + mockHistoryService.getHistory = mock(() => Promise.resolve(Ok([existingPlaceholder]))); |
| 133 | + |
| 134 | + // Call commitToHistory |
| 135 | + const result = await partialService.commitToHistory(workspaceId); |
| 136 | + |
| 137 | + // Should succeed |
| 138 | + expect(result.success).toBe(true); |
| 139 | + |
| 140 | + // Should have called updateHistory (not append) with cleaned metadata |
| 141 | + const updateHistory = mockHistoryService.updateHistory as ReturnType<typeof mock>; |
| 142 | + const appendToHistory = mockHistoryService.appendToHistory as ReturnType<typeof mock>; |
| 143 | + expect(updateHistory).toHaveBeenCalledTimes(1); |
| 144 | + expect(appendToHistory).not.toHaveBeenCalled(); |
| 145 | + |
| 146 | + const updatedMessage = updateHistory.mock.calls[0][1] as CmuxMessage; |
| 147 | + |
| 148 | + expect(updatedMessage.parts).toEqual(erroredPartial.parts); |
| 149 | + expect(updatedMessage.metadata?.error).toBeUndefined(); |
| 150 | + expect(updatedMessage.metadata?.errorType).toBeUndefined(); |
| 151 | + |
| 152 | + // Should have deleted the partial after updating |
| 153 | + const deletePartial = partialService.deletePartial as ReturnType<typeof mock>; |
| 154 | + expect(deletePartial).toHaveBeenCalledWith(workspaceId); |
| 155 | + }); |
| 156 | + |
| 157 | + test("commitToHistory should skip empty errored partial", async () => { |
| 158 | + const workspaceId = "test-workspace"; |
| 159 | + const emptyErrorPartial: CmuxMessage = { |
| 160 | + id: "msg-1", |
| 161 | + role: "assistant", |
| 162 | + metadata: { |
| 163 | + historySequence: 1, |
| 164 | + timestamp: Date.now(), |
| 165 | + model: "test-model", |
| 166 | + partial: true, |
| 167 | + error: "Network error", |
| 168 | + errorType: "network", |
| 169 | + }, |
| 170 | + parts: [], // Empty - no content accumulated before error |
| 171 | + }; |
| 172 | + |
| 173 | + // Mock readPartial to return empty errored partial |
| 174 | + partialService.readPartial = mock(() => Promise.resolve(emptyErrorPartial)); |
| 175 | + |
| 176 | + // Mock deletePartial |
| 177 | + partialService.deletePartial = mock(() => Promise.resolve(Ok(undefined))); |
| 178 | + |
| 179 | + // Mock getHistory to return no existing messages |
| 180 | + mockHistoryService.getHistory = mock(() => Promise.resolve(Ok([]))); |
| 181 | + |
| 182 | + // Call commitToHistory |
| 183 | + const result = await partialService.commitToHistory(workspaceId); |
| 184 | + |
| 185 | + // Should succeed |
| 186 | + expect(result.success).toBe(true); |
| 187 | + |
| 188 | + // Should NOT call appendToHistory for empty message (no value to preserve) |
| 189 | + const appendToHistory = mockHistoryService.appendToHistory as ReturnType<typeof mock>; |
| 190 | + expect(appendToHistory).not.toHaveBeenCalled(); |
| 191 | + |
| 192 | + // Should still delete the partial (cleanup) |
| 193 | + const deletePartial = partialService.deletePartial as ReturnType<typeof mock>; |
| 194 | + expect(deletePartial).toHaveBeenCalledWith(workspaceId); |
| 195 | + }); |
| 196 | +}); |
0 commit comments