|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { PassThrough, Writable } from "node:stream"; |
| 3 | +import { test, type TestContext } from "node:test"; |
| 4 | +import { setTimeout } from "node:timers/promises"; |
| 5 | +import { stripVTControlCharacters } from "node:util"; |
| 6 | +import React from "react"; |
| 7 | +import { render } from "ink"; |
| 8 | +import type { SessionMessage } from "@vegamo/deepcode-core"; |
| 9 | +import { buildProcessOutputItems, type ProcessOutputItem } from "../ui/core/process-output"; |
| 10 | +import { ProcessStdoutView } from "../ui/views/ProcessStdoutView"; |
| 11 | +import { PromptInput } from "../ui/views/PromptInput"; |
| 12 | + |
| 13 | +function toolMessage(id: string, command: string, output: string, exitCode: number): SessionMessage { |
| 14 | + return { |
| 15 | + id, |
| 16 | + sessionId: "session", |
| 17 | + role: "tool", |
| 18 | + visible: exitCode === 0, |
| 19 | + compacted: false, |
| 20 | + createTime: "2026-01-01T00:00:00Z", |
| 21 | + updateTime: "2026-01-01T00:00:00Z", |
| 22 | + contentParams: null, |
| 23 | + messageParams: { tool_call_id: id }, |
| 24 | + meta: { function: { name: "bash", arguments: JSON.stringify({ command }) } }, |
| 25 | + content: JSON.stringify({ output, metadata: { exitCode } }), |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +test("history retains repeated commands and failed outputs, and merges a running background call", () => { |
| 30 | + const first = toolMessage("first", "npm test", "passed", 0); |
| 31 | + const failed = toolMessage("second", "npm test", "failed", 1); |
| 32 | + const background = toolMessage("background", "npm start", "started", 0); |
| 33 | + const items = buildProcessOutputItems( |
| 34 | + [first, failed, background], |
| 35 | + new Map([ |
| 36 | + [ |
| 37 | + "123", |
| 38 | + { |
| 39 | + command: "npm start", |
| 40 | + startTime: first.createTime, |
| 41 | + toolCallId: "background", |
| 42 | + runInBackground: true, |
| 43 | + }, |
| 44 | + ], |
| 45 | + ]) |
| 46 | + ); |
| 47 | + assert.deepEqual( |
| 48 | + items.map((item) => [item.id, item.output, item.exitCode, item.running]), |
| 49 | + [ |
| 50 | + ["first", "passed", 0, false], |
| 51 | + ["second", "failed", 1, false], |
| 52 | + ["background", "", null, true], |
| 53 | + ] |
| 54 | + ); |
| 55 | + assert.equal(items[2]?.background, true); |
| 56 | + assert.deepEqual(buildProcessOutputItems([{ ...first, content: "invalid json" }], null), []); |
| 57 | +}); |
| 58 | + |
| 59 | +async function terminal(t: TestContext, element: React.ReactElement) { |
| 60 | + let frame = ""; |
| 61 | + const stdin = Object.assign(new PassThrough(), { isTTY: true, setRawMode() {}, ref() {}, unref() {} }); |
| 62 | + const stdout = Object.assign( |
| 63 | + new Writable({ |
| 64 | + write(chunk, _encoding, done) { |
| 65 | + const text = stripVTControlCharacters(chunk.toString()); |
| 66 | + if (text.trim()) frame = text; |
| 67 | + done(); |
| 68 | + }, |
| 69 | + }), |
| 70 | + { isTTY: true, columns: 120, rows: 24 } |
| 71 | + ); |
| 72 | + const app = render(element, { |
| 73 | + stdin: stdin as unknown as NodeJS.ReadStream, |
| 74 | + stdout: stdout as unknown as NodeJS.WriteStream, |
| 75 | + stderr: stdout as unknown as NodeJS.WriteStream, |
| 76 | + debug: true, |
| 77 | + patchConsole: false, |
| 78 | + exitOnCtrlC: false, |
| 79 | + }); |
| 80 | + t.after(() => { |
| 81 | + app.unmount(); |
| 82 | + app.cleanup(); |
| 83 | + }); |
| 84 | + async function flush() { |
| 85 | + await setTimeout(30); |
| 86 | + await app.waitUntilRenderFlush(); |
| 87 | + } |
| 88 | + await flush(); |
| 89 | + return { |
| 90 | + frame: () => frame, |
| 91 | + async send(input: string) { |
| 92 | + stdin.emit("data", input); |
| 93 | + await flush(); |
| 94 | + }, |
| 95 | + async rerender(next: React.ReactElement) { |
| 96 | + app.rerender(next); |
| 97 | + await flush(); |
| 98 | + }, |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +test("panel retains the selected result, navigates history and targets timeout adjustments", async (t) => { |
| 103 | + const old = buildProcessOutputItems([toolMessage("old", "old command", "old output", 2)], null)[0]!; |
| 104 | + const running: ProcessOutputItem = { |
| 105 | + id: "live", |
| 106 | + command: "npm test", |
| 107 | + pid: "123", |
| 108 | + background: false, |
| 109 | + running: true, |
| 110 | + output: "", |
| 111 | + exitCode: null, |
| 112 | + timeoutMs: 60000, |
| 113 | + }; |
| 114 | + const adjustments: string[] = []; |
| 115 | + let dismissed = 0; |
| 116 | + const props = { |
| 117 | + processStdoutRef: { current: new Map([[123, Array.from({ length: 40 }, (_, i) => `line ${i}`).join("\n")]]) }, |
| 118 | + onDismiss: () => { |
| 119 | + dismissed++; |
| 120 | + }, |
| 121 | + onAdjustTimeout: (_delta: number, pid: string) => { |
| 122 | + adjustments.push(pid); |
| 123 | + return null; |
| 124 | + }, |
| 125 | + screenWidth: 120, |
| 126 | + screenHeight: 15, |
| 127 | + }; |
| 128 | + const view = (items: ProcessOutputItem[]) => React.createElement(ProcessStdoutView, { ...props, items }); |
| 129 | + const h = await terminal(t, view([old, running])); |
| 130 | + assert.match(h.frame(), /── Process 123 \[npm test\] ──/); |
| 131 | + assert.match(h.frame(), /line 39/); |
| 132 | + await h.send("+"); |
| 133 | + assert.deepEqual(adjustments, ["123"]); |
| 134 | + await h.send("\u001b[A"); |
| 135 | + assert.doesNotMatch(h.frame(), /line 39/); |
| 136 | + await h.send("\u001b[D"); |
| 137 | + assert.match(h.frame(), /── \(return 2\) \[old command\] ──/); |
| 138 | + await h.send("+"); |
| 139 | + assert.deepEqual(adjustments, ["123"]); |
| 140 | + await h.send("\u001b[D"); |
| 141 | + assert.match(h.frame(), /old output/); |
| 142 | + await h.send("\u001b[C"); |
| 143 | + assert.match(h.frame(), /line 39/); |
| 144 | + |
| 145 | + const completed = { ...running, running: false, output: "finished", exitCode: 0 }; |
| 146 | + const next = { ...running, id: "next", pid: "456", command: "next command", background: true }; |
| 147 | + await h.rerender(view([old, completed, next])); |
| 148 | + assert.match(h.frame(), /── \(return 0\) \[npm test\] ──/); |
| 149 | + assert.match(h.frame(), /finished/); |
| 150 | + await h.send("\u001b[C"); |
| 151 | + assert.match(h.frame(), /── Process 456 \(background\) \[next command\] ──/); |
| 152 | + await h.send("+"); |
| 153 | + assert.deepEqual(adjustments, ["123"]); |
| 154 | + await h.send("\u000f"); |
| 155 | + assert.equal(dismissed, 1); |
| 156 | +}); |
| 157 | + |
| 158 | +test("Ctrl+O opens output for an empty prompt and expands pasted text in a nonempty prompt", async (t) => { |
| 159 | + let opened = 0; |
| 160 | + const h = await terminal( |
| 161 | + t, |
| 162 | + React.createElement(PromptInput, { |
| 163 | + projectRoot: process.cwd(), |
| 164 | + skills: [], |
| 165 | + modelConfig: { model: "test", thinkingEnabled: false, reasoningEffort: "high" }, |
| 166 | + screenWidth: 120, |
| 167 | + promptHistory: [], |
| 168 | + busy: false, |
| 169 | + planMode: false, |
| 170 | + onSubmit() {}, |
| 171 | + onPromptDraftApplied() {}, |
| 172 | + onModelConfigChange: () => "", |
| 173 | + onPlanModeChange() {}, |
| 174 | + onInterrupt() {}, |
| 175 | + onToggleProcessStdout: () => { |
| 176 | + opened++; |
| 177 | + }, |
| 178 | + }) |
| 179 | + ); |
| 180 | + await h.send("\u000f"); |
| 181 | + assert.equal(opened, 1); |
| 182 | + await h.send(" "); |
| 183 | + await h.send("\u000f"); |
| 184 | + assert.equal(opened, 1); |
| 185 | + await h.send("\u007f"); |
| 186 | + await h.send("\u001b[200~" + Array.from({ length: 20 }, (_, i) => `pasted line ${i}`).join("\n") + "\u001b[201~"); |
| 187 | + assert.match(h.frame(), /\[paste #1 \+20 lines\]/); |
| 188 | + await h.send("\u000f"); |
| 189 | + assert.equal(opened, 1); |
| 190 | + assert.match(h.frame(), /pasted line 0/); |
| 191 | + assert.doesNotMatch(h.frame(), /\[paste #1/); |
| 192 | + await h.send("\u000f"); |
| 193 | + assert.match(h.frame(), /\[paste #1 \+20 lines\]/); |
| 194 | +}); |
0 commit comments