|
| 1 | +import { test, type TestContext } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import os from "node:os"; |
| 5 | +import path from "node:path"; |
| 6 | +import { syncBuiltinESMExports } from "node:module"; |
| 7 | +import React from "react"; |
| 8 | +import { SessionManager } from "@vegamo/deepcode-core"; |
| 9 | +import App from "../ui/views/App"; |
| 10 | +import { PromptInput, type PromptSubmission } from "../ui/views/PromptInput"; |
| 11 | +import { UndoSelector, type QueueUndoTarget } from "../ui/views/UndoSelector"; |
| 12 | +import { isBusyCommandAllowed, BUILTIN_SLASH_COMMANDS } from "../ui/core/slash-commands"; |
| 13 | +import { terminal } from "./terminal-helper"; |
| 14 | + |
| 15 | +test("submitted prompts display during preparation and appear once after consumption", async (t) => { |
| 16 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), "deepcode-prompt-display-")); |
| 17 | + const originalEnv = process.env; |
| 18 | + process.env = Object.fromEntries(Object.entries(originalEnv).filter(([key]) => !key.startsWith("DEEPCODE_"))); |
| 19 | + t.mock.method(os, "homedir", () => root); |
| 20 | + syncBuiltinESMExports(); |
| 21 | + fs.mkdirSync(path.join(root, ".deepcode")); |
| 22 | + fs.writeFileSync(path.join(root, ".deepcode", "settings.json"), JSON.stringify({ statusline: { enabled: false } })); |
| 23 | + let releasePreparation!: () => void; |
| 24 | + const preparation = new Promise<void>((resolve) => (releasePreparation = resolve)); |
| 25 | + let releaseMatching!: () => void; |
| 26 | + const matching = new Promise<void>((resolve) => (releaseMatching = resolve)); |
| 27 | + const managerRef: { current?: SessionManager } = {}; |
| 28 | + t.mock.method(SessionManager.prototype, "initMcpServers", async function (this: SessionManager) { |
| 29 | + managerRef.current = this; |
| 30 | + Object.assign(this, { |
| 31 | + createOpenAIClient: Object.assign(() => ({ client: {}, model: "test", thinkingEnabled: false }), { |
| 32 | + prepare: () => preparation, |
| 33 | + }), |
| 34 | + getProjectStorage: () => ({ |
| 35 | + projectCode: "test", |
| 36 | + projectDir: path.join(root, "storage"), |
| 37 | + sessionsIndexPath: path.join(root, "storage", "sessions-index.json"), |
| 38 | + }), |
| 39 | + maybeGenerateSessionTitle: () => {}, |
| 40 | + reportNewPrompt: () => {}, |
| 41 | + requestSessionCompletion: async () => ({ choices: [{ message: { content: "3" } }] }), |
| 42 | + }); |
| 43 | + }); |
| 44 | + t.mock.method(SessionManager.prototype, "listSkills", async () => []); |
| 45 | + t.mock.method(SessionManager.prototype, "identifyMatchingSkillNames", async () => { |
| 46 | + await matching; |
| 47 | + return []; |
| 48 | + }); |
| 49 | + const h = await terminal(t, React.createElement(App, { projectRoot: root }), 100); |
| 50 | + const manager = managerRef.current!; |
| 51 | + t.after(async () => { |
| 52 | + manager?.interruptActiveSession(); |
| 53 | + releasePreparation(); |
| 54 | + releaseMatching(); |
| 55 | + const id = manager?.getActiveSessionId(); |
| 56 | + if (id) await manager.waitForSessionIdle(id).catch(() => {}); |
| 57 | + manager?.dispose(); |
| 58 | + await new Promise((resolve) => setImmediate(resolve)); |
| 59 | + t.mock.restoreAll(); |
| 60 | + syncBuiltinESMExports(); |
| 61 | + process.env = originalEnv; |
| 62 | + fs.rmSync(root, { recursive: true, force: true }); |
| 63 | + }); |
| 64 | + await h.send("1+2="); |
| 65 | + await h.send("\r"); |
| 66 | + const id = manager.getActiveSessionId()!; |
| 67 | + assert.equal(manager.getSession(id), null); |
| 68 | + assert.match(h.frame(), /> 1\+2=/); |
| 69 | + assert.match(h.frame(), /Type your message/); |
| 70 | + assert.doesNotMatch(h.frame(), /pending messages?/); |
| 71 | + |
| 72 | + releasePreparation(); |
| 73 | + await h.send(""); |
| 74 | + assert.equal(manager.getSession(id)?.status, "pending"); |
| 75 | + assert.match(h.frame(), /> 1\+2=/); |
| 76 | + assert.doesNotMatch(h.frame(), /pending messages?/); |
| 77 | + assert.equal(manager.listSessionMessages(id).filter((message) => message.role === "user").length, 0); |
| 78 | + |
| 79 | + await h.send("later"); |
| 80 | + await h.send("\r"); |
| 81 | + assert.match(h.frame(), /> 1\+2=[\s\S]*> later/); |
| 82 | + assert.match(h.frame(), /1 pending message \(use \/undo to edit\)/); |
| 83 | + const queued = manager.listQueuedMessages(id)[1]; |
| 84 | + manager.beginQueuedMessageEdit(id, queued.id); |
| 85 | + manager.finishQueuedMessageEdit(id, queued.id, { text: "edited" }); |
| 86 | + await h.send(""); |
| 87 | + assert.match(h.frame(), /> edited/); |
| 88 | + assert.doesNotMatch(h.frame(), /> later/); |
| 89 | + manager.removeQueuedMessage(id, queued.id); |
| 90 | + await h.send(""); |
| 91 | + assert.doesNotMatch(h.frame(), /> edited/); |
| 92 | + assert.doesNotMatch(h.frame(), /pending messages?/); |
| 93 | + |
| 94 | + releaseMatching(); |
| 95 | + await manager.waitForSessionIdle(id); |
| 96 | + await h.send(""); |
| 97 | + assert.equal(h.frame().match(/> 1\+2=/g)?.length, 1); |
| 98 | + assert.deepEqual(manager.listQueuedMessages(id), []); |
| 99 | + assert.doesNotMatch(h.frame(), /pending messages?/); |
| 100 | +}); |
| 101 | + |
| 102 | +async function input(t: TestContext, editing = false) { |
| 103 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), "deepcode-input-queue-")); |
| 104 | + t.after(() => fs.rmSync(root, { recursive: true, force: true })); |
| 105 | + const submissions: PromptSubmission[] = []; |
| 106 | + const events: string[] = []; |
| 107 | + const h = await terminal( |
| 108 | + t, |
| 109 | + React.createElement(PromptInput, { |
| 110 | + projectRoot: root, |
| 111 | + skills: [], |
| 112 | + modelConfig: { model: "test", thinkingEnabled: false, reasoningEffort: "max" }, |
| 113 | + reasoningSummary: "off", |
| 114 | + screenWidth: 100, |
| 115 | + promptHistory: [], |
| 116 | + busy: true, |
| 117 | + planMode: false, |
| 118 | + onSubmit: (submission) => { |
| 119 | + submissions.push(submission); |
| 120 | + }, |
| 121 | + onPromptDraftApplied: () => {}, |
| 122 | + onReasoningSummaryChange: () => "", |
| 123 | + onModelConfigChange: () => "", |
| 124 | + onPermissionSettingsChange: () => "", |
| 125 | + onPlanModeChange: () => {}, |
| 126 | + onInterrupt: () => { |
| 127 | + events.push("interrupt"); |
| 128 | + }, |
| 129 | + onExitShortcut: () => { |
| 130 | + events.push("exit"); |
| 131 | + }, |
| 132 | + editingQueuedMessage: editing, |
| 133 | + onCancelQueueEdit: () => { |
| 134 | + events.push("cancel edit"); |
| 135 | + }, |
| 136 | + }), |
| 137 | + 100 |
| 138 | + ); |
| 139 | + return { ...h, submissions, events }; |
| 140 | +} |
| 141 | + |
| 142 | +test("busy input submits ordinary prompts and clears the input for another message", async (t) => { |
| 143 | + const h = await input(t); |
| 144 | + await h.send("first"); |
| 145 | + await h.send("\r"); |
| 146 | + await h.send("second"); |
| 147 | + await h.send("\r"); |
| 148 | + assert.deepEqual( |
| 149 | + h.submissions.map((s) => s.text), |
| 150 | + ["first", "second"] |
| 151 | + ); |
| 152 | +}); |
| 153 | + |
| 154 | +test("busy commands are exactly the supported whitelist; forbidden direct input never submits", async (t) => { |
| 155 | + assert.deepEqual( |
| 156 | + BUILTIN_SLASH_COMMANDS.filter((command) => isBusyCommandAllowed(command.kind)) |
| 157 | + .map((c) => c.kind) |
| 158 | + .sort(), |
| 159 | + ["copy", "exit", "experimental", "mcp", "model", "permissions", "raw", "skills", "undo"] |
| 160 | + ); |
| 161 | + const h = await input(t); |
| 162 | + for (const command of ["/new", "/continue", "/plan do something", "/compact"]) { |
| 163 | + await h.send("\x15" + command); |
| 164 | + await h.send("\r"); |
| 165 | + } |
| 166 | + assert.equal(h.submissions.length, 0); |
| 167 | + await h.send("\x15/copy"); |
| 168 | + await h.send("\r"); |
| 169 | + assert.equal(h.submissions[0]?.command, "copy"); |
| 170 | + await h.send("/undo"); |
| 171 | + await h.send("\r"); |
| 172 | + assert.equal(h.submissions[1]?.command, "undo"); |
| 173 | + await h.send("/exit"); |
| 174 | + await h.send("\r"); |
| 175 | + assert.equal(h.submissions[2]?.command, "exit"); |
| 176 | +}); |
| 177 | + |
| 178 | +test("double Ctrl+D requests exit only on the second press", async (t) => { |
| 179 | + const h = await input(t); |
| 180 | + await h.send("\x04"); |
| 181 | + assert.deepEqual(h.events, []); |
| 182 | + await h.send("\x04"); |
| 183 | + assert.deepEqual(h.events, ["exit"]); |
| 184 | +}); |
| 185 | + |
| 186 | +test("Esc cancels a queue edit without interrupting the model", async (t) => { |
| 187 | + const h = await input(t, true); |
| 188 | + await h.send("\x1b"); |
| 189 | + assert.deepEqual(h.events, ["cancel edit"]); |
| 190 | +}); |
| 191 | + |
| 192 | +test("pending undo targets expose only remove and edit", async (t) => { |
| 193 | + const target: QueueUndoTarget = { |
| 194 | + index: 0, |
| 195 | + canRestoreCode: false, |
| 196 | + queued: { id: "queued", sessionId: "session", submittedAt: new Date().toISOString(), content: { text: "later" } }, |
| 197 | + message: { |
| 198 | + id: "queued", |
| 199 | + sessionId: "session", |
| 200 | + role: "user", |
| 201 | + content: "later", |
| 202 | + contentParams: null, |
| 203 | + messageParams: null, |
| 204 | + compacted: false, |
| 205 | + visible: true, |
| 206 | + createTime: new Date().toISOString(), |
| 207 | + updateTime: new Date().toISOString(), |
| 208 | + }, |
| 209 | + }; |
| 210 | + const selected: string[] = []; |
| 211 | + const onSelect = (_target: QueueUndoTarget, mode: string) => { |
| 212 | + selected.push(mode); |
| 213 | + }; |
| 214 | + const h = await terminal( |
| 215 | + t, |
| 216 | + React.createElement(UndoSelector, { |
| 217 | + targets: [target], |
| 218 | + busy: true, |
| 219 | + onCancel: () => {}, |
| 220 | + onSelect, |
| 221 | + }) |
| 222 | + ); |
| 223 | + assert.match(h.frame(), /pending/); |
| 224 | + await h.send("\r"); |
| 225 | + assert.match(h.frame(), /Remove/); |
| 226 | + assert.match(h.frame(), /Edit/); |
| 227 | + assert.doesNotMatch(h.frame(), /Restore code|Restore conversation/); |
| 228 | + await h.send("\x1b[B"); |
| 229 | + await h.send("\r"); |
| 230 | + assert.deepEqual(selected, ["edit"]); |
| 231 | + h.rerender( |
| 232 | + React.createElement(UndoSelector, { |
| 233 | + targets: [{ ...target, queued: undefined }], |
| 234 | + busy: false, |
| 235 | + onCancel: () => {}, |
| 236 | + onSelect, |
| 237 | + }) |
| 238 | + ); |
| 239 | + await h.send(""); |
| 240 | + assert.match(h.frame(), /no longer available/); |
| 241 | + await h.send("\r"); |
| 242 | + assert.deepEqual(selected, ["edit"]); |
| 243 | +}); |
0 commit comments