|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { Readable, Writable } from "node:stream"; |
| 3 | +import { test } from "node:test"; |
| 4 | +import { setTimeout as delay } from "node:timers/promises"; |
| 5 | +import { stripVTControlCharacters } from "node:util"; |
| 6 | +import React from "react"; |
| 7 | +import { render } from "ink"; |
| 8 | +import { PromptInput } from "../ui"; |
| 9 | +import type { PromptSubmission } from "../ui"; |
| 10 | + |
| 11 | +type Harness = ReturnType<typeof createHarness>; |
| 12 | + |
| 13 | +function createHarness() { |
| 14 | + const frames: string[] = []; |
| 15 | + const stdout = Object.assign( |
| 16 | + new Writable({ |
| 17 | + write(chunk, _encoding, callback) { |
| 18 | + const frame = stripVTControlCharacters(chunk.toString()); |
| 19 | + if (frame.trim()) { |
| 20 | + frames.push(frame); |
| 21 | + } |
| 22 | + callback(); |
| 23 | + }, |
| 24 | + }), |
| 25 | + { columns: 100, rows: 24, isTTY: true } |
| 26 | + ); |
| 27 | + const stdin = Object.assign(new Readable({ read() {} }), { |
| 28 | + isTTY: true, |
| 29 | + setRawMode() {}, |
| 30 | + ref() {}, |
| 31 | + unref() {}, |
| 32 | + }); |
| 33 | + return { frames, stdout, stdin }; |
| 34 | +} |
| 35 | + |
| 36 | +function renderPromptInput( |
| 37 | + harness: Harness, |
| 38 | + props: { |
| 39 | + busy: boolean; |
| 40 | + queuedPrompts?: string[]; |
| 41 | + onSubmit: (submission: PromptSubmission) => void; |
| 42 | + onRemoveQueuedPrompt?: () => void; |
| 43 | + } |
| 44 | +) { |
| 45 | + const element = React.createElement(PromptInput, { |
| 46 | + projectRoot: process.cwd(), |
| 47 | + skills: [], |
| 48 | + modelConfig: { model: "deepseek-v4-flash", thinkingEnabled: true, reasoningEffort: "max" }, |
| 49 | + screenWidth: 100, |
| 50 | + promptHistory: [], |
| 51 | + busy: props.busy, |
| 52 | + queuedPrompts: props.queuedPrompts, |
| 53 | + planMode: false, |
| 54 | + onSubmit: props.onSubmit, |
| 55 | + onModelConfigChange: () => "", |
| 56 | + onPlanModeChange: () => {}, |
| 57 | + onInterrupt: () => {}, |
| 58 | + onRemoveQueuedPrompt: props.onRemoveQueuedPrompt, |
| 59 | + }); |
| 60 | + |
| 61 | + return render(element, { |
| 62 | + stdout: harness.stdout as unknown as NodeJS.WriteStream, |
| 63 | + stdin: harness.stdin as unknown as NodeJS.ReadStream, |
| 64 | + debug: true, |
| 65 | + patchConsole: false, |
| 66 | + exitOnCtrlC: false, |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +async function press(harness: Harness, app: { waitUntilRenderFlush: () => Promise<void> }, data: string) { |
| 71 | + harness.stdin.push(data); |
| 72 | + await delay(0); |
| 73 | + await app.waitUntilRenderFlush(); |
| 74 | +} |
| 75 | + |
| 76 | +test("PromptInput submits a plain prompt on enter while idle", async () => { |
| 77 | + const harness = createHarness(); |
| 78 | + const submissions: PromptSubmission[] = []; |
| 79 | + const app = renderPromptInput(harness, { busy: false, onSubmit: (submission) => submissions.push(submission) }); |
| 80 | + try { |
| 81 | + await press(harness, app, "first prompt"); |
| 82 | + await press(harness, app, "\r"); |
| 83 | + assert.deepEqual( |
| 84 | + submissions.map((submission) => submission.text), |
| 85 | + ["first prompt"] |
| 86 | + ); |
| 87 | + } finally { |
| 88 | + app.unmount(); |
| 89 | + } |
| 90 | +}); |
| 91 | + |
| 92 | +test("PromptInput still submits a plain prompt while busy so App can queue it", async () => { |
| 93 | + const harness = createHarness(); |
| 94 | + const submissions: PromptSubmission[] = []; |
| 95 | + const app = renderPromptInput(harness, { busy: true, onSubmit: (submission) => submissions.push(submission) }); |
| 96 | + try { |
| 97 | + await press(harness, app, "queued prompt"); |
| 98 | + await press(harness, app, "\r"); |
| 99 | + assert.deepEqual( |
| 100 | + submissions.map((submission) => submission.text), |
| 101 | + ["queued prompt"] |
| 102 | + ); |
| 103 | + } finally { |
| 104 | + app.unmount(); |
| 105 | + } |
| 106 | +}); |
| 107 | + |
| 108 | +test("PromptInput keeps blocking slash commands while busy", async () => { |
| 109 | + const harness = createHarness(); |
| 110 | + const submissions: PromptSubmission[] = []; |
| 111 | + const app = renderPromptInput(harness, { busy: true, onSubmit: (submission) => submissions.push(submission) }); |
| 112 | + try { |
| 113 | + await press(harness, app, "/model"); |
| 114 | + await press(harness, app, "\r"); |
| 115 | + assert.deepEqual(submissions, []); |
| 116 | + // The buffer is kept so the user can run the command once the turn finishes. |
| 117 | + assert.match(harness.frames.at(-1) ?? "", /\/model/); |
| 118 | + } finally { |
| 119 | + app.unmount(); |
| 120 | + } |
| 121 | +}); |
| 122 | + |
| 123 | +test("PromptInput renders pending guidance and removes the last one on backspace", async () => { |
| 124 | + const harness = createHarness(); |
| 125 | + let removed = 0; |
| 126 | + const app = renderPromptInput(harness, { |
| 127 | + busy: true, |
| 128 | + queuedPrompts: ["older prompt", "newer prompt"], |
| 129 | + onSubmit: () => {}, |
| 130 | + onRemoveQueuedPrompt: () => { |
| 131 | + removed += 1; |
| 132 | + }, |
| 133 | + }); |
| 134 | + try { |
| 135 | + await delay(0); |
| 136 | + await app.waitUntilRenderFlush(); |
| 137 | + const frame = harness.frames.at(-1) ?? ""; |
| 138 | + assert.match(frame, /guidance 1\. older prompt/); |
| 139 | + assert.match(frame, /guidance 2\. newer prompt/); |
| 140 | + assert.match(frame, /2 guidance queued/); |
| 141 | + |
| 142 | + await press(harness, app, "\u007F"); |
| 143 | + assert.equal(removed, 1); |
| 144 | + } finally { |
| 145 | + app.unmount(); |
| 146 | + } |
| 147 | +}); |
0 commit comments