@ftrdotdev/pi-inquiry 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,385 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { Option, Question, type Question as QuestionType } from "./domain.ts";
3
+ import { KeyEvent } from "./keyboard.ts";
4
+ import {
5
+ initialMachineState,
6
+ openSuggestions,
7
+ step,
8
+ type MachineState,
9
+ type SuggestionState,
10
+ } from "./machine.ts";
11
+ import {
12
+ buildEditorBox,
13
+ buildScene,
14
+ buildSuggestionPopup,
15
+ line,
16
+ plain,
17
+ span,
18
+ wrapLine,
19
+ type Scene,
20
+ type SceneLine,
21
+ } from "./scene.ts";
22
+ import { emptyEditor } from "./editor.ts";
23
+
24
+ const question = (over: Partial<QuestionType> = {}): QuestionType =>
25
+ new Question({
26
+ id: "q1",
27
+ label: "Q1",
28
+ prompt: "Pick one?",
29
+ options: [new Option({ label: "Yes" }), new Option({ label: "No" })],
30
+ allowOther: true,
31
+ ...over,
32
+ });
33
+
34
+ const twoQuestions = (): readonly Question[] => [
35
+ question(),
36
+ question({ id: "q2", label: "Q2", prompt: "Second?" }),
37
+ ];
38
+
39
+ const plainLines = (scene: Scene): string[] => scene.lines.map(plain);
40
+
41
+ // Helpers for the house rounded border (╭─╮│╰─╯) that frames the scene.
42
+ const boxed = (content: string, width: number): string => "│" + content.padEnd(width - 2) + "│";
43
+ const boxTop = (width: number): string => "╭" + "─".repeat(width - 2) + "╮";
44
+ const boxBottom = (width: number): string => "╰" + "─".repeat(width - 2) + "╯";
45
+
46
+ const openEditor = (state: MachineState): MachineState => {
47
+ let s = state;
48
+ s = step(s, KeyEvent.down).state;
49
+ s = step(s, KeyEvent.down).state;
50
+ return step(s, KeyEvent.enter).state;
51
+ };
52
+
53
+ describe("buildScene: single question", () => {
54
+ it("renders a rounded border, prompt, options and help, without a tab bar", () => {
55
+ const scene = buildScene(initialMachineState([question()]), 60);
56
+ const lines = plainLines(scene);
57
+ expect(lines[0]).toBe(boxTop(60));
58
+ expect(lines[1]).toBe(boxed(" Pick one?", 60));
59
+ expect(lines[2]).toBe(boxed("", 60));
60
+ expect(lines[3]).toBe(boxed("→ 1. Yes", 60));
61
+ expect(lines[4]).toBe(boxed(" 2. No", 60));
62
+ expect(lines[5]).toBe(boxed(" 3. Type something.", 60));
63
+ expect(lines[6]).toBe(boxed("", 60));
64
+ expect(lines[7]).toBe(boxed(" ↑↓ navigate • Enter to select • Esc to cancel", 60));
65
+ expect(lines[8]).toBe(boxBottom(60));
66
+ expect(lines).toHaveLength(9);
67
+ });
68
+
69
+ it("marks the selected option with accent spans", () => {
70
+ const scene = buildScene(initialMachineState([question()]), 40);
71
+ // framed: [rail, marker, label, pad..., rail]
72
+ const selectedLine = scene.lines[3]!;
73
+ expect(selectedLine[1]).toEqual({ text: "→ ", style: "accent" });
74
+ expect(selectedLine[2]).toEqual({ text: "1. Yes", style: "accent" });
75
+ const unselectedLine = scene.lines[4]!;
76
+ expect(unselectedLine[2]).toEqual({ text: "2. No", style: "text" });
77
+ });
78
+
79
+ it("shows descriptions indented under options", () => {
80
+ const q = question({
81
+ options: [new Option({ label: "Yes", description: "Go ahead" }), new Option({ label: "No" })],
82
+ });
83
+ const scene = buildScene(initialMachineState([q]), 40);
84
+ const lines = plainLines(scene);
85
+ expect(lines.some((l) => l.includes(" Go ahead"))).toBe(true);
86
+ });
87
+
88
+ it("wraps long prompts with continuation indentation", () => {
89
+ const q = question({ prompt: "A very long prompt that definitely wraps" });
90
+ const scene = buildScene(initialMachineState([q]), 20);
91
+ const lines = plainLines(scene);
92
+ expect(lines[1]).toBe(boxed(" A very long", 20));
93
+ expect(lines[2]).toBe(boxed(" prompt that", 20));
94
+ expect(lines[3]).toBe(boxed(" definitely wraps", 20));
95
+ });
96
+ });
97
+
98
+ describe("buildScene: multi question", () => {
99
+ it("renders a tab bar with the active tab marked by a bordered square", () => {
100
+ const scene = buildScene(initialMachineState(twoQuestions()), 60);
101
+ const lines = plainLines(scene);
102
+ expect(lines[1]).toBe(boxed(" ← ▣ Q1 □ Q2 ✓ Submit →", 60));
103
+ expect(lines[2]).toBe(boxed("", 60));
104
+ expect(lines[3]).toBe(boxed(" Pick one?", 60));
105
+ });
106
+
107
+ it("marks answered tabs with filled squares", () => {
108
+ let state = initialMachineState(twoQuestions());
109
+ state = step(state, KeyEvent.enter).state; // answer q1
110
+ const scene = buildScene(state, 60);
111
+ expect(plainLines(scene)[1]).toBe(boxed(" ← ■ Q1 ▣ Q2 ✓ Submit →", 60));
112
+ });
113
+
114
+ it("shows the submit tab summary with unanswered warnings", () => {
115
+ let state = initialMachineState(twoQuestions());
116
+ state = step(state, KeyEvent.enter).state; // q1 answered, now q2
117
+ state = step(state, KeyEvent.tab).state; // submit tab
118
+ const scene = buildScene(state, 60);
119
+ const lines = plainLines(scene);
120
+ expect(lines[3]).toBe(boxed(" Ready to submit", 60));
121
+ expect(lines[5]).toBe(boxed(" Q1: Yes", 60));
122
+ expect(lines[7]).toBe(boxed(" Unanswered: Q2", 60));
123
+ });
124
+
125
+ it("offers submit when all questions are answered", () => {
126
+ let state = initialMachineState(twoQuestions());
127
+ state = step(state, KeyEvent.enter).state;
128
+ state = step(state, KeyEvent.enter).state; // q2 answered, now submit tab
129
+ const scene = buildScene(state, 60);
130
+ const lines = plainLines(scene);
131
+ expect(lines).toContain(boxed(" Press Enter to submit", 60));
132
+ expect(lines.some((l) => l.includes(" Unanswered"))).toBe(false);
133
+ });
134
+
135
+ it("shows the custom answer as a subtitle under 'Type something'", () => {
136
+ let state = initialMachineState(twoQuestions());
137
+ state = step(state, KeyEvent.down).state;
138
+ state = step(state, KeyEvent.down).state;
139
+ state = step(state, KeyEvent.enter).state; // open editor
140
+ for (const ch of ["h", "i"]) state = step(state, KeyEvent.char(ch)).state;
141
+ state = step(state, KeyEvent.enter).state; // submit, advance to q2
142
+ state = step(state, KeyEvent.shiftTab).state; // back to q1
143
+ const lines = plainLines(buildScene(state, 60));
144
+ expect(lines.some((l) => l.includes("→ 3. Type something."))).toBe(true);
145
+ expect(lines.some((l) => l.includes(" Current answer: hi"))).toBe(true);
146
+ });
147
+
148
+ it("shows the chosen option when returning to its tab", () => {
149
+ let state = initialMachineState(twoQuestions());
150
+ state = step(state, KeyEvent.down).state; // "No"
151
+ state = step(state, KeyEvent.enter).state; // answer q1, advance to q2
152
+ state = step(state, KeyEvent.shiftTab).state; // back to q1
153
+ const lines = plainLines(buildScene(state, 60));
154
+ expect(lines.some((l) => l.includes("→ 2. No"))).toBe(true);
155
+ expect(lines.some((l) => l.includes("Current answer"))).toBe(false);
156
+ });
157
+
158
+ it("indents the custom answer subtitle like option descriptions", () => {
159
+ const q = question({
160
+ options: [new Option({ label: "Yes", description: "Go ahead" }), new Option({ label: "No" })],
161
+ });
162
+ let state = initialMachineState([q, question({ id: "q2", label: "Q2", prompt: "Second?" })]);
163
+ state = step(state, KeyEvent.down).state;
164
+ state = step(state, KeyEvent.down).state;
165
+ state = step(state, KeyEvent.enter).state; // open editor
166
+ for (const ch of ["h", "i"]) state = step(state, KeyEvent.char(ch)).state;
167
+ state = step(state, KeyEvent.enter).state; // submit, advance to q2
168
+ state = step(state, KeyEvent.shiftTab).state; // back to q1
169
+ const lines = plainLines(buildScene(state, 60));
170
+ const descLine = lines.find((l) => l.includes("Go ahead"));
171
+ const answerLine = lines.find((l) => l.includes("Current answer: hi"));
172
+ // Both subtitles must share the same indentation inside the box.
173
+ const before = (l: string, text: string) => l.slice(0, l.indexOf(text));
174
+ expect(before(descLine!, "Go ahead")).toBe(before(answerLine!, "Current answer: hi"));
175
+ });
176
+
177
+ it("shows a custom answer in the submit summary", () => {
178
+ let state = initialMachineState(twoQuestions());
179
+ state = step(state, KeyEvent.down).state;
180
+ state = step(state, KeyEvent.down).state;
181
+ state = step(state, KeyEvent.enter).state; // open editor
182
+ for (const ch of ["h", "i"]) state = step(state, KeyEvent.char(ch)).state;
183
+ state = step(state, KeyEvent.enter).state; // submit, advance to q2
184
+ state = step(state, KeyEvent.enter).state; // answer q2 "Yes", advance to submit
185
+ const lines = plainLines(buildScene(state, 60));
186
+ expect(lines.some((l) => l.includes(" Q1: (wrote) hi"))).toBe(true);
187
+ expect(lines.some((l) => l.includes(" Q2: Yes"))).toBe(true);
188
+ });
189
+ });
190
+
191
+ describe("buildScene: editor mode", () => {
192
+ it("shows the editor box with prompt and options", () => {
193
+ const scene = buildScene(openEditor(initialMachineState([question()])), 40);
194
+ const lines = plainLines(scene);
195
+ expect(lines[1]).toBe(boxed(" Pick one?", 40));
196
+ expect(lines).toContain(boxed(" Your answer:", 40));
197
+ expect(lines.some((l) => l.includes("Shift+Enter"))).toBe(true);
198
+ expect(lines.some((l) => l.includes("Esc to go back"))).toBe(true);
199
+ expect(lines[3]).toBe(boxed(" 1. Yes", 40));
200
+ expect(lines[5]).toBe(boxed("→ 3. Type something. ✎", 40));
201
+ // editor box frame, prefixed with the gutter space, inside the outer rails
202
+ expect(lines[8]).toBe("│ ╭" + "─".repeat(35) + "╮│");
203
+ });
204
+
205
+ it("does not render the navigation help while editing", () => {
206
+ const scene = buildScene(openEditor(initialMachineState([question()])), 40);
207
+ const lines = plainLines(scene);
208
+ expect(lines.some((l) => l.includes("navigate"))).toBe(false);
209
+ });
210
+
211
+ it("shows the completion popup and its help below the editor", () => {
212
+ let state = openEditor(initialMachineState([question()]));
213
+ state = openSuggestions(state, "@src", [
214
+ { value: "src/main.ts", label: "main.ts", description: "src/main.ts" },
215
+ { value: "src/utils/", label: "utils/", description: "src/utils/" },
216
+ ]);
217
+ const scene = buildScene(state, 40);
218
+ const lines = plainLines(scene);
219
+ const popupStart = lines.findIndex((l) => l.includes("→ main.ts"));
220
+ expect(popupStart).toBeGreaterThan(-1);
221
+ expect(lines[popupStart]).toBe(boxed(" → main.ts", 40));
222
+ expect(lines[popupStart + 1]).toBe(boxed(" utils/", 40));
223
+ expect(lines.some((l) => l.includes("↑↓ pick") && l.includes("Enter/Tab complete"))).toBe(true);
224
+ expect(lines.some((l) => l.includes("Shift+Enter"))).toBe(false);
225
+ });
226
+ });
227
+
228
+ describe("buildSuggestionPopup", () => {
229
+ const suggestions = (): SuggestionState => ({
230
+ prefix: "@src",
231
+ items: [
232
+ { value: "src/main.ts", label: "main.ts", description: "src/main.ts" },
233
+ { value: "src/utils/", label: "utils/", description: "src/utils/" },
234
+ ],
235
+ selectedIndex: 0,
236
+ });
237
+
238
+ it("marks the highlighted item and shows descriptions in a primary column", () => {
239
+ const popup = buildSuggestionPopup(suggestions(), 60);
240
+ // primary column: min(32, 60 - 6) = 32; label padded to 32; then description
241
+ expect(plain(popup[0]!)).toBe("→ main.ts" + " ".repeat(25) + "src/main.ts");
242
+ expect(popup[0]![0]).toEqual({ text: plain(popup[0]!), style: "accent" });
243
+ expect(plain(popup[1]!)).toBe(" utils/" + " ".repeat(26) + "src/utils/");
244
+ // description span is muted, marker and label are plain
245
+ expect(popup[1]![3]).toEqual({ text: "src/utils/", style: "muted" });
246
+ });
247
+
248
+ it("omits descriptions when the terminal is narrow", () => {
249
+ const popup = buildSuggestionPopup(suggestions(), 30);
250
+ expect(plain(popup[0]!)).toBe("→ main.ts");
251
+ expect(plain(popup[1]!)).toBe(" utils/");
252
+ });
253
+
254
+ it("wraps the highlight around the list ends", () => {
255
+ const popup = buildSuggestionPopup({ ...suggestions(), selectedIndex: 1 }, 60);
256
+ expect(plain(popup[0]!)).toBe(" main.ts" + " ".repeat(25) + "src/main.ts");
257
+ expect(plain(popup[1]!).startsWith("→ utils/")).toBe(true);
258
+ });
259
+
260
+ it("centers the window and shows a scroll indicator for long lists", () => {
261
+ const items = Array.from({ length: 20 }, (_, i) => ({
262
+ value: `src/f${i}.ts`,
263
+ label: `f${i}.ts`,
264
+ }));
265
+ const popup = buildSuggestionPopup({ prefix: "@src/", items, selectedIndex: 10 }, 40);
266
+ // window is centered on the selection: f8..f12, selection at position 2
267
+ expect(plain(popup[0]!)).toBe(" f8.ts");
268
+ expect(plain(popup[2]!)).toBe("→ f10.ts");
269
+ expect(plain(popup[4]!)).toBe(" f12.ts");
270
+ expect(plain(popup[5]!)).toBe(" (11/20)");
271
+ expect(popup[5]![0]).toEqual({ text: " (11/20)", style: "muted" });
272
+ });
273
+ });
274
+
275
+ describe("buildEditorBox", () => {
276
+ it("renders an empty rounded box with the cursor at the start", () => {
277
+ const box = buildEditorBox(emptyEditor(), 10);
278
+ expect(plain(box[0]!)).toBe("╭" + "─".repeat(8) + "╮");
279
+ // rail, space, cursor cell + padding, space, rail
280
+ expect(plain(box[1]!)).toBe("│ " + " ".repeat(6) + " │");
281
+ expect(box[1]![0]).toEqual({ text: "│", style: "border" });
282
+ expect(box[1]![1]).toEqual({ text: " " });
283
+ expect(box[1]![2]).toEqual({ text: " ", style: "cursor" });
284
+ expect(plain(box[2]!)).toBe("╰" + "─".repeat(8) + "╯");
285
+ });
286
+
287
+ it("renders the cursor over the character at the cursor position", () => {
288
+ const box = buildEditorBox({ text: "ab", cursor: 1 }, 10);
289
+ const content = box[1]!;
290
+ expect(content[2]).toEqual({ text: "a" });
291
+ expect(content[3]).toEqual({ text: "b", style: "cursor" });
292
+ expect(plain(content).length).toBe(10);
293
+ });
294
+
295
+ it("renders a trailing cursor cell at the end of text", () => {
296
+ const box = buildEditorBox({ text: "ab", cursor: 2 }, 10);
297
+ const content = box[1]!;
298
+ expect(content[2]).toEqual({ text: "a" });
299
+ expect(content[3]).toEqual({ text: "b" });
300
+ expect(content[4]).toEqual({ text: " ", style: "cursor" });
301
+ expect(plain(content).length).toBe(10);
302
+ });
303
+
304
+ it("wraps long text inside the box", () => {
305
+ const box = buildEditorBox({ text: "a".repeat(20), cursor: 20 }, 8);
306
+ expect(plain(box[0]!)).toBe("╭" + "─".repeat(6) + "╮");
307
+ for (let i = 1; i <= 4; i++) {
308
+ expect(plain(box[i]!)).toBe("│ aaaa │");
309
+ }
310
+ // The last logical row is exactly full, so the trailing cursor cell
311
+ // would overflow the rails and is dropped — the row stays flush, matching
312
+ // BorderedBox's row clipping (see sdk/box-parity.test.ts).
313
+ expect(plain(box[5]!)).toBe("│ aaaa │");
314
+ expect(plain(box[6]!)).toBe("╰" + "─".repeat(6) + "╯");
315
+ });
316
+
317
+ it("places the cursor on the correct wrapped line", () => {
318
+ const box = buildEditorBox({ text: "a".repeat(10), cursor: 0 }, 8);
319
+ // cursor at start: first content line's first content span is the cursor
320
+ const first = box[1]!;
321
+ expect(first[2]).toEqual({ text: "a", style: "cursor" });
322
+ const second = box[2]!;
323
+ expect(second.slice(2, -2).every((s) => s.style === undefined || s.style === "cursor")).toBe(
324
+ true,
325
+ );
326
+ expect(second.slice(2, -2).some((s) => s.style === "cursor")).toBe(false);
327
+ });
328
+
329
+ it("renders one row per logical line, preserving newlines", () => {
330
+ const box = buildEditorBox({ text: "one\ntwo", cursor: 7 }, 10);
331
+ expect(plain(box[1]!)).toBe("│ one │");
332
+ expect(plain(box[2]!)).toBe("│ two │");
333
+ // cursor after "two": trailing cursor cell on the last row
334
+ expect(box[2]![5]).toEqual({ text: " ", style: "cursor" });
335
+ expect(plain(box[3]!)).toBe("╰" + "─".repeat(8) + "╯");
336
+ });
337
+
338
+ it("renders empty lines between newlines", () => {
339
+ const box = buildEditorBox({ text: "a\n\nb", cursor: 4 }, 10);
340
+ expect(plain(box[1]!)).toBe("│ a │");
341
+ expect(plain(box[2]!)).toBe("│ │");
342
+ expect(plain(box[3]!)).toBe("│ b │");
343
+ expect(plain(box[4]!)).toBe("╰" + "─".repeat(8) + "╯");
344
+ });
345
+
346
+ it("places the cursor at the end of the line containing the newline", () => {
347
+ // cursor sits on the newline (index 1): end of the first row
348
+ const box = buildEditorBox({ text: "a\nb", cursor: 1 }, 10);
349
+ expect(box[1]![3]).toEqual({ text: " ", style: "cursor" });
350
+ expect(box[2]!.slice(2, -2).some((s) => s.style === "cursor")).toBe(false);
351
+ });
352
+
353
+ it("wraps long logical lines independently", () => {
354
+ const box = buildEditorBox({ text: "a".repeat(6) + "\nb", cursor: 6 }, 8);
355
+ // first logical line wraps to two rows of 4; second stays on its own row
356
+ expect(plain(box[1]!)).toBe("│ aaaa │");
357
+ expect(plain(box[2]!)).toBe("│ aa │");
358
+ expect(plain(box[3]!)).toBe("│ b │");
359
+ });
360
+ });
361
+
362
+ describe("wrapLine", () => {
363
+ it("preserves spans across line breaks", () => {
364
+ const wrapped = wrapLine(line(span("one", "accent"), span(" two three", "muted")), 7);
365
+ expect(plain(wrapped[0]!)).toBe("one two");
366
+ expect(plain(wrapped[1]!)).toBe("three");
367
+ expect(wrapped[0]![0]!.style).toBe("accent");
368
+ expect(wrapped[0]![1]!.style).toBe("muted");
369
+ expect(wrapped[1]![0]!.style).toBe("muted");
370
+ });
371
+
372
+ it("splits on literal newlines inside spans", () => {
373
+ const wrapped = wrapLine(line(span("one\ntwo")), 20);
374
+ expect(wrapped.map(plain)).toEqual(["one", "two"]);
375
+ });
376
+
377
+ it("returns an empty line for empty content", () => {
378
+ expect(wrapLine([], 10)).toEqual([[]]);
379
+ });
380
+
381
+ it("keeps content unchanged when it fits", () => {
382
+ const content: SceneLine = line(span("hello", "accent"));
383
+ expect(wrapLine(content, 20)).toEqual([content]);
384
+ });
385
+ });