|
| 1 | +// @vitest-environment node |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { handleChatGatewayEvent, type ChatEventPayload } from "./chat-gateway.ts"; |
| 4 | +import type { ChatState } from "./chat-history.ts"; |
| 5 | + |
| 6 | +type AbortDiagnosticState = ChatState & { |
| 7 | + chatRunStatus?: { phase: string; runId: string | null; sessionKey: string } | null; |
| 8 | + lastLocalTerminalReconcile?: { sessionStatus: string } | null; |
| 9 | + sessionsResult?: { |
| 10 | + ts: number; |
| 11 | + path: string; |
| 12 | + count: number; |
| 13 | + defaults: Record<string, unknown>; |
| 14 | + sessions: Array<Record<string, unknown>>; |
| 15 | + }; |
| 16 | +}; |
| 17 | + |
| 18 | +function createAbortDiagnosticState(runId = "run-validation-abort"): AbortDiagnosticState { |
| 19 | + return { |
| 20 | + chatAttachments: [], |
| 21 | + chatLoading: false, |
| 22 | + chatMessage: "", |
| 23 | + chatMessages: [], |
| 24 | + chatQueue: [], |
| 25 | + chatRunError: null, |
| 26 | + chatRunId: runId, |
| 27 | + chatSending: false, |
| 28 | + chatStream: "Partial assistant reply", |
| 29 | + chatStreamStartedAt: 100, |
| 30 | + chatRunStartup: null, |
| 31 | + chatThinkingLevel: null, |
| 32 | + chatVerboseLevel: null, |
| 33 | + client: null, |
| 34 | + connected: true, |
| 35 | + connectionEpoch: 0, |
| 36 | + hello: null, |
| 37 | + lastError: null, |
| 38 | + sessionKey: "main", |
| 39 | + sessionsResult: { |
| 40 | + ts: 0, |
| 41 | + path: "", |
| 42 | + count: 1, |
| 43 | + defaults: {}, |
| 44 | + sessions: [ |
| 45 | + { |
| 46 | + key: "main", |
| 47 | + kind: "direct", |
| 48 | + updatedAt: 1, |
| 49 | + hasActiveRun: true, |
| 50 | + activeRunIds: [runId], |
| 51 | + status: "running", |
| 52 | + startedAt: 100, |
| 53 | + }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +describe("aborted chat diagnostics", () => { |
| 60 | + beforeEach(() => vi.useFakeTimers()); |
| 61 | + afterEach(() => { |
| 62 | + vi.clearAllTimers(); |
| 63 | + vi.useRealTimers(); |
| 64 | + }); |
| 65 | + |
| 66 | + it.each([ |
| 67 | + { name: "plain cancellation", errorMessage: undefined, expectedError: null }, |
| 68 | + { |
| 69 | + name: "validation self-abort", |
| 70 | + errorMessage: "edit tool validation failed: edits: must be an array", |
| 71 | + expectedError: "Error: edit tool validation failed: edits: must be an array", |
| 72 | + }, |
| 73 | + ])("keeps first-terminal $name killed and interrupted", ({ errorMessage, expectedError }) => { |
| 74 | + const state = createAbortDiagnosticState(); |
| 75 | + const payload: ChatEventPayload = { |
| 76 | + runId: "run-validation-abort", |
| 77 | + sessionKey: "main", |
| 78 | + state: "aborted", |
| 79 | + ...(errorMessage ? { errorMessage } : {}), |
| 80 | + }; |
| 81 | + |
| 82 | + expect(handleChatGatewayEvent(state, payload)).toBe("aborted"); |
| 83 | + |
| 84 | + expect(state.chatRunError?.summary ?? null).toBe(expectedError); |
| 85 | + expect(state.chatRunStatus).toMatchObject({ |
| 86 | + phase: "interrupted", |
| 87 | + runId: "run-validation-abort", |
| 88 | + sessionKey: "main", |
| 89 | + }); |
| 90 | + expect(state.lastLocalTerminalReconcile?.sessionStatus).toBe("killed"); |
| 91 | + expect(state.sessionsResult?.sessions[0]).toMatchObject({ |
| 92 | + activeRunIds: [], |
| 93 | + hasActiveRun: false, |
| 94 | + status: "killed", |
| 95 | + }); |
| 96 | + expect(state.chatRunId).toBeNull(); |
| 97 | + }); |
| 98 | + |
| 99 | + it("surfaces one late aborted diagnostic and ignores its replay", () => { |
| 100 | + const state = createAbortDiagnosticState(); |
| 101 | + const aborted = { |
| 102 | + runId: "run-validation-abort", |
| 103 | + sessionKey: "main", |
| 104 | + state: "aborted" as const, |
| 105 | + }; |
| 106 | + const diagnostic = { |
| 107 | + ...aborted, |
| 108 | + errorMessage: "edit tool validation failed: edits: must be an array", |
| 109 | + }; |
| 110 | + |
| 111 | + expect(handleChatGatewayEvent(state, aborted)).toBe("aborted"); |
| 112 | + expect(state.chatRunError).toBeNull(); |
| 113 | + expect(handleChatGatewayEvent(state, diagnostic)).toBe("aborted"); |
| 114 | + const displayedDiagnostic = state.chatRunError; |
| 115 | + expect(handleChatGatewayEvent(state, diagnostic)).toBe("aborted"); |
| 116 | + |
| 117 | + expect(state.chatRunError).toBe(displayedDiagnostic); |
| 118 | + expect(state.chatRunError).toEqual({ |
| 119 | + summary: "Error: edit tool validation failed: edits: must be an array", |
| 120 | + }); |
| 121 | + expect(state.chatRunId).toBeNull(); |
| 122 | + }); |
| 123 | + |
| 124 | + it("does not publish a late aborted diagnostic over a newer active run", () => { |
| 125 | + const state = createAbortDiagnosticState("run-old"); |
| 126 | + expect( |
| 127 | + handleChatGatewayEvent(state, { |
| 128 | + runId: "run-old", |
| 129 | + sessionKey: "main", |
| 130 | + state: "aborted", |
| 131 | + }), |
| 132 | + ).toBe("aborted"); |
| 133 | + state.chatRunId = "run-new"; |
| 134 | + state.chatStream = "New run output"; |
| 135 | + |
| 136 | + expect( |
| 137 | + handleChatGatewayEvent(state, { |
| 138 | + runId: "run-old", |
| 139 | + sessionKey: "main", |
| 140 | + state: "aborted", |
| 141 | + errorMessage: "edit tool validation failed: invalid arguments", |
| 142 | + }), |
| 143 | + ).toBe("aborted"); |
| 144 | + |
| 145 | + expect(state.chatRunError).toBeNull(); |
| 146 | + expect(state.chatRunId).toBe("run-new"); |
| 147 | + expect(state.chatStream).toBe("New run output"); |
| 148 | + }); |
| 149 | + |
| 150 | + it("does not restore an old diagnostic while a newer send awaits its ACK", () => { |
| 151 | + const state = createAbortDiagnosticState("run-old"); |
| 152 | + expect( |
| 153 | + handleChatGatewayEvent(state, { |
| 154 | + runId: "run-old", |
| 155 | + sessionKey: "main", |
| 156 | + state: "aborted", |
| 157 | + }), |
| 158 | + ).toBe("aborted"); |
| 159 | + state.chatQueue = [ |
| 160 | + { |
| 161 | + id: "pending-new-send", |
| 162 | + text: "New request", |
| 163 | + createdAt: 200, |
| 164 | + sendRunId: "run-new", |
| 165 | + sendState: "sending", |
| 166 | + }, |
| 167 | + ]; |
| 168 | + |
| 169 | + expect( |
| 170 | + handleChatGatewayEvent(state, { |
| 171 | + runId: "run-old", |
| 172 | + sessionKey: "main", |
| 173 | + state: "aborted", |
| 174 | + errorMessage: "edit tool validation failed: invalid arguments", |
| 175 | + }), |
| 176 | + ).toBe("aborted"); |
| 177 | + |
| 178 | + expect(state.chatRunError).toBeNull(); |
| 179 | + expect(state.chatRunId).toBeNull(); |
| 180 | + expect(state.lastLocalTerminalReconcile?.runId).toBe("run-old"); |
| 181 | + }); |
| 182 | + |
| 183 | + it("does not publish an old aborted diagnostic after a newer run completes", () => { |
| 184 | + const state = createAbortDiagnosticState("run-old"); |
| 185 | + expect( |
| 186 | + handleChatGatewayEvent(state, { |
| 187 | + runId: "run-old", |
| 188 | + sessionKey: "main", |
| 189 | + state: "aborted", |
| 190 | + }), |
| 191 | + ).toBe("aborted"); |
| 192 | + state.chatRunId = "run-new"; |
| 193 | + state.chatStream = "New final answer"; |
| 194 | + expect( |
| 195 | + handleChatGatewayEvent(state, { |
| 196 | + runId: "run-new", |
| 197 | + sessionKey: "main", |
| 198 | + state: "final", |
| 199 | + message: { |
| 200 | + role: "assistant", |
| 201 | + content: [{ type: "text", text: "New final answer" }], |
| 202 | + }, |
| 203 | + }), |
| 204 | + ).toBe("final"); |
| 205 | + |
| 206 | + expect( |
| 207 | + handleChatGatewayEvent(state, { |
| 208 | + runId: "run-old", |
| 209 | + sessionKey: "main", |
| 210 | + state: "aborted", |
| 211 | + errorMessage: "edit tool validation failed: invalid arguments", |
| 212 | + }), |
| 213 | + ).toBe("aborted"); |
| 214 | + |
| 215 | + expect(state.chatRunError).toBeNull(); |
| 216 | + expect(state.lastLocalTerminalReconcile?.runId).toBe("run-new"); |
| 217 | + expect(state.chatRunId).toBeNull(); |
| 218 | + expect(state.chatMessages.at(-1)).toMatchObject({ |
| 219 | + role: "assistant", |
| 220 | + content: [{ type: "text", text: "New final answer" }], |
| 221 | + }); |
| 222 | + }); |
| 223 | +}); |
0 commit comments