|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { stripAnsi } from "../../packages/terminal-core/src/ansi.js"; |
| 3 | +import { ChatLog } from "./components/chat-log.js"; |
| 4 | +import { createEventHandlers } from "./tui-event-handlers.js"; |
| 5 | +import { extractTextFromMessage, extractTuiAbortedText } from "./tui-formatters.js"; |
| 6 | +import { getTuiSessionProjection, reduceTuiSessionProjection } from "./tui-session-projection.js"; |
| 7 | +import { TuiStreamAssembler } from "./tui-stream-assembler.js"; |
| 8 | +import type { TuiStateAccess } from "./tui-types.js"; |
| 9 | + |
| 10 | +const audioFailure = { |
| 11 | + type: "attachment_error", |
| 12 | + attachment: { code: "delivery-failed", kind: "audio", label: "Generated audio 1" }, |
| 13 | +}; |
| 14 | +const audioReceipt = "⚠️ audio attachment: Delivery failed. Try sending this file again."; |
| 15 | +const caption = { type: "text", text: "Your recording" }; |
| 16 | +const image = { type: "image", url: "file:///private/image.png" }; |
| 17 | +const assistant = (content: unknown[]) => ({ role: "assistant", content }); |
| 18 | + |
| 19 | +function createDisplayHarness() { |
| 20 | + const state: TuiStateAccess = { |
| 21 | + agentDefaultId: "main", |
| 22 | + sessionMainKey: "main", |
| 23 | + sessionScope: "per-sender", |
| 24 | + agents: [], |
| 25 | + currentAgentId: "main", |
| 26 | + currentSessionKey: "agent:main:main", |
| 27 | + currentSessionId: "session-1", |
| 28 | + activeChatRunId: null, |
| 29 | + pendingSubmit: null, |
| 30 | + historyLoaded: true, |
| 31 | + sessionInfo: {}, |
| 32 | + initialSessionApplied: true, |
| 33 | + isConnected: true, |
| 34 | + autoMessageSent: false, |
| 35 | + toolsExpanded: false, |
| 36 | + showThinking: false, |
| 37 | + connectionStatus: "connected", |
| 38 | + activityStatus: "idle", |
| 39 | + statusTimeout: null, |
| 40 | + lastCtrlCAt: 0, |
| 41 | + }; |
| 42 | + const chatLog = new ChatLog(); |
| 43 | + const handlers = createEventHandlers({ |
| 44 | + state, |
| 45 | + chatLog, |
| 46 | + tui: { requestRender: vi.fn() }, |
| 47 | + btw: { clear: vi.fn(), showResult: vi.fn() }, |
| 48 | + setActivityStatus: (value) => { |
| 49 | + state.activityStatus = value; |
| 50 | + }, |
| 51 | + streamingWatchdogMs: 0, |
| 52 | + }); |
| 53 | + return { |
| 54 | + state, |
| 55 | + chatLog, |
| 56 | + ...handlers, |
| 57 | + visible: () => |
| 58 | + chatLog |
| 59 | + .render(160) |
| 60 | + .map((line) => stripAnsi(line).trimEnd()) |
| 61 | + .join("\n") |
| 62 | + .trim(), |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +describe("TUI attachment failure presentation", () => { |
| 67 | + it.each([ |
| 68 | + { name: "failure only", content: [audioFailure], expected: audioReceipt }, |
| 69 | + { |
| 70 | + name: "caption and failure", |
| 71 | + content: [caption, audioFailure], |
| 72 | + expected: `Your recording\n${audioReceipt}`, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "success and failure", |
| 76 | + content: [image, audioFailure], |
| 77 | + expected: `Attached image\n${audioReceipt}`, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "caption, success and failure", |
| 81 | + content: [caption, image, audioFailure], |
| 82 | + expected: `Your recording\n${audioReceipt}`, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "multiple failures", |
| 86 | + content: [audioFailure, audioFailure], |
| 87 | + expected: `${audioReceipt}\n${audioReceipt}`, |
| 88 | + }, |
| 89 | + ])("keeps $name in history and final output", ({ content, expected }) => { |
| 90 | + const message = assistant(content); |
| 91 | + expect(extractTextFromMessage(message)).toBe(expected); |
| 92 | + expect(new TuiStreamAssembler().finalize("run-1", message, false)).toBe(expected); |
| 93 | + }); |
| 94 | + |
| 95 | + it.each([ |
| 96 | + { |
| 97 | + kind: "image", |
| 98 | + code: "file-not-found", |
| 99 | + expected: "⚠️ image attachment: File not found. Check the path and try again.", |
| 100 | + }, |
| 101 | + { |
| 102 | + kind: "video", |
| 103 | + code: "unsupported-format", |
| 104 | + expected: |
| 105 | + "⚠️ video attachment: Rejected by the local attachment allowlist. Send a supported file type.", |
| 106 | + }, |
| 107 | + { |
| 108 | + kind: "document", |
| 109 | + code: "delivery-failed", |
| 110 | + expected: "⚠️ file attachment: Delivery failed. Try sending this file again.", |
| 111 | + }, |
| 112 | + ])( |
| 113 | + "renders actionable $kind/$code receipts without source metadata", |
| 114 | + ({ kind, code, expected }) => { |
| 115 | + const message = assistant([ |
| 116 | + { |
| 117 | + type: "attachment_error", |
| 118 | + attachment: { |
| 119 | + kind, |
| 120 | + code, |
| 121 | + label: "\x1b]52;c;private-clipboard\x07file:///private/name", |
| 122 | + url: "https://private.invalid/file?ticket=private", |
| 123 | + mimeType: "private/type", |
| 124 | + }, |
| 125 | + }, |
| 126 | + ]); |
| 127 | + expect(extractTextFromMessage(message)).toBe(expected); |
| 128 | + expect(new TuiStreamAssembler().finalize("run-1", message, false)).toBe(expected); |
| 129 | + }, |
| 130 | + ); |
| 131 | + |
| 132 | + it.each([false, true])( |
| 133 | + "retains streamed captions and thinking visibility (%s) before a failure-only final", |
| 134 | + (showThinking) => { |
| 135 | + const assembler = new TuiStreamAssembler(); |
| 136 | + assembler.ingestDelta( |
| 137 | + "run-1", |
| 138 | + assistant([caption, { type: "thinking", thinking: "Preparing" }]), |
| 139 | + showThinking, |
| 140 | + ); |
| 141 | + expect(assembler.finalize("run-1", assistant([audioFailure]), showThinking)).toBe( |
| 142 | + `${showThinking ? "[thinking]\nPreparing\n\n" : ""}Your recording\n${audioReceipt}`, |
| 143 | + ); |
| 144 | + }, |
| 145 | + ); |
| 146 | + |
| 147 | + it("keeps genuine errors ahead of success fallback while appending the failure receipt", () => { |
| 148 | + const message = { |
| 149 | + ...assistant([image, audioFailure]), |
| 150 | + stopReason: "error", |
| 151 | + errorMessage: "generation stopped", |
| 152 | + }; |
| 153 | + expect(extractTextFromMessage(message)).toBe(`generation stopped\n${audioReceipt}`); |
| 154 | + expect( |
| 155 | + new TuiStreamAssembler().finalize( |
| 156 | + "run-1", |
| 157 | + assistant([image, audioFailure]), |
| 158 | + false, |
| 159 | + "generation stopped", |
| 160 | + ), |
| 161 | + ).toBe(`generation stopped\n${audioReceipt}`); |
| 162 | + }); |
| 163 | + |
| 164 | + it.each([ |
| 165 | + { name: "failure only", content: [audioFailure], streamed: false, expected: audioReceipt }, |
| 166 | + { |
| 167 | + name: "mixed attachments", |
| 168 | + content: [image, audioFailure], |
| 169 | + streamed: false, |
| 170 | + expected: `Attached image\n${audioReceipt}`, |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "streamed caption", |
| 174 | + content: [image, audioFailure], |
| 175 | + streamed: true, |
| 176 | + expected: `Your recording\n${audioReceipt}`, |
| 177 | + }, |
| 178 | + ])( |
| 179 | + "renders one receipt through optimistic and authoritative history: $name", |
| 180 | + ({ content, streamed, expected }) => { |
| 181 | + const harness = createDisplayHarness(); |
| 182 | + const event = { runId: "run-1", sessionKey: harness.state.currentSessionKey }; |
| 183 | + try { |
| 184 | + if (streamed) { |
| 185 | + harness.handleChatEvent({ ...event, state: "delta", message: assistant([caption]) }); |
| 186 | + expect(harness.visible()).toBe("Your recording"); |
| 187 | + } |
| 188 | + const message = assistant(content); |
| 189 | + harness.handleChatEvent({ ...event, state: "final", message }); |
| 190 | + expect(harness.visible()).toBe(expected); |
| 191 | + expect(harness.state.activityStatus).toBe("idle"); |
| 192 | + |
| 193 | + for (const messages of [ |
| 194 | + [], |
| 195 | + [ |
| 196 | + { |
| 197 | + ...assistant(streamed ? [caption, ...content] : content), |
| 198 | + __openclaw: { id: "assistant-1", seq: 1, runId: event.runId }, |
| 199 | + }, |
| 200 | + ], |
| 201 | + ]) { |
| 202 | + const projection = reduceTuiSessionProjection(harness.state, { |
| 203 | + type: "snapshotLoaded", |
| 204 | + messages, |
| 205 | + }); |
| 206 | + expect(projection.messages).toHaveLength(1); |
| 207 | + harness.chatLog.clearAll(); |
| 208 | + for (const row of projection.messages) { |
| 209 | + harness.chatLog.finalizeAssistant(extractTextFromMessage(row)); |
| 210 | + } |
| 211 | + expect(harness.visible()).toBe(expected); |
| 212 | + } |
| 213 | + } finally { |
| 214 | + harness.dispose(); |
| 215 | + } |
| 216 | + }, |
| 217 | + ); |
| 218 | + |
| 219 | + it("keeps failure-only aborts diagnostic and ignores a late attachment final", () => { |
| 220 | + const harness = createDisplayHarness(); |
| 221 | + const event = { |
| 222 | + runId: "run-1", |
| 223 | + sessionKey: harness.state.currentSessionKey, |
| 224 | + message: assistant([audioFailure]), |
| 225 | + }; |
| 226 | + try { |
| 227 | + expect(extractTuiAbortedText(event.message, false)).toBe(""); |
| 228 | + harness.handleChatEvent({ ...event, state: "aborted" }); |
| 229 | + harness.handleChatEvent({ ...event, state: "final" }); |
| 230 | + expect(harness.visible()).toBe("run aborted"); |
| 231 | + expect(getTuiSessionProjection(harness.state).messages).toHaveLength(0); |
| 232 | + } finally { |
| 233 | + harness.dispose(); |
| 234 | + } |
| 235 | + }); |
| 236 | + |
| 237 | + it("does not invent failure output for empty or unsupported blocks", () => { |
| 238 | + for (const content of [ |
| 239 | + [], |
| 240 | + [{ type: "tool_use", name: "search" }], |
| 241 | + [{ type: "attachment_error", attachment: { code: "unknown", kind: "audio" } }], |
| 242 | + ]) { |
| 243 | + const message = assistant(content); |
| 244 | + expect(extractTextFromMessage(message)).toBe(""); |
| 245 | + expect(new TuiStreamAssembler().finalize("run-1", message, false)).toBe("(no output)"); |
| 246 | + } |
| 247 | + }); |
| 248 | +}); |
0 commit comments