@ftrdotdev/pi-inquiry 0.7.0 → 0.9.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.
- package/CHANGELOG.md +268 -0
- package/README.md +24 -4
- package/package.json +18 -16
- package/src/core/domain.test.ts +198 -9
- package/src/core/domain.ts +75 -24
- package/src/core/editor.test.ts +87 -42
- package/src/core/flow.test.ts +22 -3
- package/src/core/flow.ts +7 -3
- package/src/core/keyboard.test.ts +3 -3
- package/src/core/keyboard.ts +1 -1
- package/src/core/machine.test.ts +25 -2
- package/src/core/machine.ts +30 -22
- package/src/core/purity.test.ts +118 -0
- package/src/core/scene.test.ts +75 -20
- package/src/core/scene.ts +78 -42
- package/src/core/services.ts +6 -5
- package/src/core/text.test.ts +44 -15
- package/src/core/text.ts +7 -0
- package/src/harness-policy.test.ts +27 -0
- package/src/sdk/box-parity.test.ts +2 -7
- package/src/sdk/keys.test.ts +24 -1
- package/src/sdk/painter.ts +3 -3
- package/src/sdk/pi-ui.test.ts +56 -12
- package/src/sdk/pi-ui.ts +118 -93
- package/src/sdk/schema.test.ts +31 -1
- package/src/tool/question.test.ts +136 -6
- package/src/tool/question.ts +11 -12
package/src/core/machine.ts
CHANGED
|
@@ -36,6 +36,7 @@ import type { SuggestionItem } from "./services.ts";
|
|
|
36
36
|
export interface OptionChoice {
|
|
37
37
|
readonly label: string;
|
|
38
38
|
readonly description?: string;
|
|
39
|
+
readonly recommended: boolean;
|
|
39
40
|
readonly isOther: boolean;
|
|
40
41
|
}
|
|
41
42
|
|
|
@@ -50,6 +51,7 @@ export type MultiEntry =
|
|
|
50
51
|
readonly index: number;
|
|
51
52
|
readonly label: string;
|
|
52
53
|
readonly description?: string;
|
|
54
|
+
readonly recommended: boolean;
|
|
53
55
|
}
|
|
54
56
|
| { readonly kind: "custom"; readonly label: string }
|
|
55
57
|
| { readonly kind: "add" };
|
|
@@ -61,13 +63,14 @@ export type MultiEntry =
|
|
|
61
63
|
*/
|
|
62
64
|
export const multiEntries = (state: MachineState): readonly MultiEntry[] => {
|
|
63
65
|
const question = state.questions[state.currentTab];
|
|
64
|
-
if (
|
|
66
|
+
if (question === undefined || question.multiple !== true) return [];
|
|
65
67
|
const answers = state.answers.get(question.id) ?? [];
|
|
66
68
|
const entries: MultiEntry[] = question.options.map((o, i) => ({
|
|
67
69
|
kind: "option",
|
|
68
70
|
index: i,
|
|
69
71
|
label: o.label,
|
|
70
72
|
...(o.description === undefined ? {} : { description: o.description }),
|
|
73
|
+
recommended: o.recommended,
|
|
71
74
|
}));
|
|
72
75
|
for (const a of answers) {
|
|
73
76
|
if (a.wasCustom) entries.push({ kind: "custom", label: a.label });
|
|
@@ -79,7 +82,7 @@ export const multiEntries = (state: MachineState): readonly MultiEntry[] => {
|
|
|
79
82
|
/** Whether a multi-select checkbox row is currently selected. */
|
|
80
83
|
export const isMultiEntrySelected = (state: MachineState, entry: MultiEntry): boolean => {
|
|
81
84
|
const question = state.questions[state.currentTab];
|
|
82
|
-
if (
|
|
85
|
+
if (question === undefined) return false;
|
|
83
86
|
const answers = state.answers.get(question.id) ?? [];
|
|
84
87
|
if (entry.kind === "option") return isOptionSelected(answers, entry.index);
|
|
85
88
|
return false;
|
|
@@ -143,14 +146,15 @@ export const optionsForTab = (
|
|
|
143
146
|
tab: number,
|
|
144
147
|
): readonly OptionChoice[] => {
|
|
145
148
|
const question = questions[tab];
|
|
146
|
-
if (
|
|
149
|
+
if (question === undefined) return [];
|
|
147
150
|
const options: OptionChoice[] = question.options.map((o) => ({
|
|
148
151
|
label: o.label,
|
|
149
|
-
description: o.description,
|
|
152
|
+
...(o.description === undefined ? {} : { description: o.description }),
|
|
153
|
+
recommended: o.recommended,
|
|
150
154
|
isOther: false,
|
|
151
155
|
}));
|
|
152
156
|
if (question.allowOther) {
|
|
153
|
-
options.push({ label: "Type something.", isOther: true });
|
|
157
|
+
options.push({ label: "Type something.", isOther: true, recommended: false });
|
|
154
158
|
}
|
|
155
159
|
return options;
|
|
156
160
|
};
|
|
@@ -226,11 +230,13 @@ const stepInOptions = (state: MachineState, event: KeyEvent): StepResult => {
|
|
|
226
230
|
up: () => (onQuestion ? moveSelection(state, -1) : ignore(state)),
|
|
227
231
|
down: () => (onQuestion ? moveSelection(state, 1) : ignore(state)),
|
|
228
232
|
// Enter confirms the choice: a single-select question records and advances;
|
|
229
|
-
// a multi-select question confirms the selection and moves on.
|
|
233
|
+
// a multi-select question confirms the selection and moves on. The strict
|
|
234
|
+
// comparison, rather than `?? false`, is what narrows `question` for the
|
|
235
|
+
// handler arguments here and below.
|
|
230
236
|
enter: () =>
|
|
231
237
|
!onQuestion
|
|
232
238
|
? maybeSubmit(state)
|
|
233
|
-
: question?.multiple
|
|
239
|
+
: question?.multiple === true
|
|
234
240
|
? confirmMultiple(state, question)
|
|
235
241
|
: chooseOption(state),
|
|
236
242
|
escape: () => complete(state, true),
|
|
@@ -239,7 +245,7 @@ const stepInOptions = (state: MachineState, event: KeyEvent): StepResult => {
|
|
|
239
245
|
// ignored while navigating options.
|
|
240
246
|
char: (event) =>
|
|
241
247
|
onQuestion && event.char === " "
|
|
242
|
-
? question?.multiple
|
|
248
|
+
? question?.multiple === true
|
|
243
249
|
? toggleMultiEntry(state, question)
|
|
244
250
|
: chooseOption(state)
|
|
245
251
|
: ignore(state),
|
|
@@ -274,10 +280,10 @@ const enterTab = (state: MachineState): MachineState => ({
|
|
|
274
280
|
/** Option index that reflects the existing answer on the current tab, if any. */
|
|
275
281
|
const answeredOptionIndex = (state: MachineState): number | undefined => {
|
|
276
282
|
const question = state.questions[state.currentTab];
|
|
277
|
-
if (
|
|
283
|
+
if (question === undefined) return undefined;
|
|
278
284
|
if (question.multiple) return undefined; // no single "current answer" to highlight
|
|
279
285
|
const answer = state.answers.get(question.id)?.[0];
|
|
280
|
-
if (
|
|
286
|
+
if (answer === undefined) return undefined;
|
|
281
287
|
const options = optionsForTab(state.questions, state.currentTab);
|
|
282
288
|
if (answer.wasCustom) return options.length - 1; // the "Type something" entry
|
|
283
289
|
if (answer.index === undefined) return undefined;
|
|
@@ -287,7 +293,7 @@ const answeredOptionIndex = (state: MachineState): number | undefined => {
|
|
|
287
293
|
/** Number of rows the user can navigate on the active tab. */
|
|
288
294
|
const navigableCount = (state: MachineState): number => {
|
|
289
295
|
const question = state.questions[state.currentTab];
|
|
290
|
-
return question?.multiple ? multiEntries(state).length : currentOptions(state).length;
|
|
296
|
+
return (question?.multiple ?? false) ? multiEntries(state).length : currentOptions(state).length;
|
|
291
297
|
};
|
|
292
298
|
|
|
293
299
|
const moveSelection = (state: MachineState, dir: -1 | 1): StepResult => {
|
|
@@ -303,16 +309,16 @@ const maybeSubmit = (state: MachineState): StepResult =>
|
|
|
303
309
|
/** Enter on a question tab: pick the highlighted option, or open the editor. */
|
|
304
310
|
const chooseOption = (state: MachineState): StepResult => {
|
|
305
311
|
const question = state.questions[state.currentTab];
|
|
306
|
-
if (
|
|
312
|
+
if (question === undefined) return ignore(state);
|
|
307
313
|
const option = currentOptions(state)[state.optionIndex];
|
|
308
|
-
if (
|
|
314
|
+
if (option === undefined) return ignore(state);
|
|
309
315
|
if (option.isOther) {
|
|
310
316
|
const existing = state.answers.get(question.id)?.[0];
|
|
311
317
|
return {
|
|
312
318
|
state: {
|
|
313
319
|
...state,
|
|
314
320
|
inputMode: true,
|
|
315
|
-
editor: existing?.wasCustom ? editorFromText(existing.label) : emptyEditor(),
|
|
321
|
+
editor: existing?.wasCustom === true ? editorFromText(existing.label) : emptyEditor(),
|
|
316
322
|
suggestions: null,
|
|
317
323
|
},
|
|
318
324
|
action: { type: "none" },
|
|
@@ -326,7 +332,7 @@ const chooseOption = (state: MachineState): StepResult => {
|
|
|
326
332
|
/** Space on a multi-select row: toggle an option, remove a chip, or open the editor. */
|
|
327
333
|
const toggleMultiEntry = (state: MachineState, question: Question): StepResult => {
|
|
328
334
|
const entry = multiEntries(state)[state.optionIndex];
|
|
329
|
-
if (
|
|
335
|
+
if (entry === undefined) return ignore(state);
|
|
330
336
|
switch (entry.kind) {
|
|
331
337
|
case "option":
|
|
332
338
|
return { state: toggleOption(state, question, entry.index), action: { type: "none" } };
|
|
@@ -354,7 +360,9 @@ const confirmMultiple = (state: MachineState, question: Question): StepResult =>
|
|
|
354
360
|
};
|
|
355
361
|
|
|
356
362
|
const stepInEditor = (state: MachineState, event: KeyEvent): StepResult =>
|
|
357
|
-
state.suggestions
|
|
363
|
+
state.suggestions !== null
|
|
364
|
+
? stepInEditorWithSuggestions(state, event)
|
|
365
|
+
: stepInEditorPlain(state, event);
|
|
358
366
|
|
|
359
367
|
/**
|
|
360
368
|
* Key handling while the completion popup is open. Navigation and selection
|
|
@@ -429,7 +437,7 @@ const exitEditor = (state: MachineState): StepResult => ({
|
|
|
429
437
|
/** Enter in the editor: submit the typed answer. */
|
|
430
438
|
const submitEditor = (state: MachineState): StepResult => {
|
|
431
439
|
const question = state.questions[state.currentTab];
|
|
432
|
-
if (
|
|
440
|
+
if (question === undefined) return ignore(state);
|
|
433
441
|
const label = state.editor.text.trim() || "(no response)";
|
|
434
442
|
const closed = { inputMode: false, editor: emptyEditor(), suggestions: null } as const;
|
|
435
443
|
if (question.multiple) {
|
|
@@ -460,7 +468,7 @@ const toggleOption = (
|
|
|
460
468
|
if (next.length === 0) answers.delete(question.id);
|
|
461
469
|
else answers.set(question.id, next);
|
|
462
470
|
} else {
|
|
463
|
-
const answer =
|
|
471
|
+
const answer = Answer.make({
|
|
464
472
|
id: question.id,
|
|
465
473
|
label: question.options[optionIndex]!.label,
|
|
466
474
|
wasCustom: false,
|
|
@@ -483,7 +491,7 @@ const removeCustom = (state: MachineState, question: Question, label: string): M
|
|
|
483
491
|
/** Append a newly typed custom alternative to the question's answers. */
|
|
484
492
|
const addCustom = (state: MachineState, question: Question, label: string): MachineState => {
|
|
485
493
|
const answers = new Map(state.answers);
|
|
486
|
-
const answer =
|
|
494
|
+
const answer = Answer.make({ id: question.id, label, wasCustom: true });
|
|
487
495
|
answers.set(question.id, [...answersFor(state, question), answer]);
|
|
488
496
|
return { ...state, answers };
|
|
489
497
|
};
|
|
@@ -499,8 +507,8 @@ const withAnswer = (
|
|
|
499
507
|
answers.set(
|
|
500
508
|
question.id,
|
|
501
509
|
index === undefined
|
|
502
|
-
? [
|
|
503
|
-
: [
|
|
510
|
+
? [Answer.make({ id: question.id, label, wasCustom })]
|
|
511
|
+
: [Answer.make({ id: question.id, label, wasCustom, index })],
|
|
504
512
|
);
|
|
505
513
|
return { ...state, answers };
|
|
506
514
|
};
|
|
@@ -516,7 +524,7 @@ const complete = (state: MachineState, cancelled: boolean): StepResult => ({
|
|
|
516
524
|
state,
|
|
517
525
|
action: {
|
|
518
526
|
type: "complete",
|
|
519
|
-
result:
|
|
527
|
+
result: QuestionResult.make({
|
|
520
528
|
questions: [...state.questions],
|
|
521
529
|
answers: [...state.answers.values()].flatMap((group) => group),
|
|
522
530
|
cancelled,
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { findViolations } from "../../test-support/purity.ts";
|
|
6
|
+
|
|
7
|
+
const CORE_DIR = fileURLToPath(new URL(".", import.meta.url));
|
|
8
|
+
|
|
9
|
+
/** Every non-test module of the real core, read as text (house technique). */
|
|
10
|
+
const realCore = (): Record<string, string> =>
|
|
11
|
+
Object.fromEntries(
|
|
12
|
+
readdirSync(CORE_DIR)
|
|
13
|
+
.filter((file) => file.endsWith(".ts") && !file.endsWith(".test.ts"))
|
|
14
|
+
.map((file) => [file, readFileSync(join(CORE_DIR, file), "utf8")]),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// The detector is developed red-first: a guard that cannot fail proves nothing.
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
it("the detector reports a pi import outside the carve-out", () => {
|
|
22
|
+
// Arrange: the same specifier the sdk fringe may import, in core code.
|
|
23
|
+
const sources = { "hot.ts": 'import { TUI } from "@earendil-works/pi-tui";' };
|
|
24
|
+
|
|
25
|
+
// Act
|
|
26
|
+
const violations = findViolations(sources);
|
|
27
|
+
|
|
28
|
+
// Assert: one violation, naming the file and the specifier.
|
|
29
|
+
assert.strictEqual(violations.length, 1);
|
|
30
|
+
assert.ok(violations[0]!.includes("hot.ts"));
|
|
31
|
+
assert.ok(violations[0]!.includes("@earendil-works/pi-tui"));
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("the detector reports a platform import and a node: builtin", () => {
|
|
35
|
+
// Arrange
|
|
36
|
+
const platform = { "a.ts": 'import { NodeFileSystem } from "@effect/platform-node";' };
|
|
37
|
+
const builtin = { "b.ts": 'import { readFileSync } from "node:fs";' };
|
|
38
|
+
|
|
39
|
+
// Act
|
|
40
|
+
const platformViolations = findViolations(platform);
|
|
41
|
+
const builtinViolations = findViolations(builtin);
|
|
42
|
+
|
|
43
|
+
// Assert: one violation each, with the offending specifier in the message.
|
|
44
|
+
assert.strictEqual(platformViolations.length, 1);
|
|
45
|
+
assert.ok(platformViolations[0]!.includes("@effect/platform-node"));
|
|
46
|
+
assert.strictEqual(builtinViolations.length, 1);
|
|
47
|
+
assert.ok(builtinViolations[0]!.includes("node:fs"));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("the detector reports bare imports, re-exports and dynamic imports", () => {
|
|
51
|
+
// Arrange: the three forms the `import … from` clause does not cover.
|
|
52
|
+
const sources = {
|
|
53
|
+
"side-effect.ts": 'import "node:fs";',
|
|
54
|
+
"reexport.ts": 'export * from "@effect/platform-node";',
|
|
55
|
+
"dynamic.ts": 'const load = () => import("node:path");',
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// Act
|
|
59
|
+
const violations = findViolations(sources);
|
|
60
|
+
|
|
61
|
+
// Assert: one per file, each naming its specifier.
|
|
62
|
+
assert.deepStrictEqual(
|
|
63
|
+
violations.map((violation) => violation.split(":")[0]).sort((a, b) => (a! < b! ? -1 : 1)),
|
|
64
|
+
["dynamic.ts", "reexport.ts", "side-effect.ts"],
|
|
65
|
+
);
|
|
66
|
+
assert.ok(violations.some((violation) => violation.includes("node:fs")));
|
|
67
|
+
assert.ok(violations.some((violation) => violation.includes("@effect/platform-node")));
|
|
68
|
+
assert.ok(violations.some((violation) => violation.includes("node:path")));
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("the detector reports a Layer in the core, including a static layer on a service", () => {
|
|
72
|
+
// Arrange
|
|
73
|
+
const sources = {
|
|
74
|
+
"svc.ts": [
|
|
75
|
+
'export class K extends Context.Service<K, {}>()("k") {',
|
|
76
|
+
" static readonly layer = Layer.succeed(K, K.of({}));",
|
|
77
|
+
"}",
|
|
78
|
+
].join("\n"),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// Act / Assert
|
|
82
|
+
const violations = findViolations(sources);
|
|
83
|
+
assert.strictEqual(violations.length, 1);
|
|
84
|
+
assert.ok(violations[0]!.includes("Layer"));
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("the real core is clean, and the box-frame carve-out is exercised", () => {
|
|
88
|
+
// Arrange
|
|
89
|
+
const sources = realCore();
|
|
90
|
+
|
|
91
|
+
// Act / Assert: no violation in the real core.
|
|
92
|
+
assert.deepStrictEqual(findViolations(sources), []);
|
|
93
|
+
|
|
94
|
+
// The carve-out is real: the scene imports the allow-listed specifier...
|
|
95
|
+
assert.ok(sources["scene.ts"]?.includes("@ftrdotdev/pi-tui/box-frame"));
|
|
96
|
+
|
|
97
|
+
// ...and the detector reading the same sources plus one injected violation
|
|
98
|
+
// reports exactly that file, so the empty result above is proof, not a stub.
|
|
99
|
+
const poisoned = { ...sources, "hot.ts": 'import { TUI } from "@earendil-works/pi-tui";' };
|
|
100
|
+
assert.deepStrictEqual(
|
|
101
|
+
findViolations(poisoned).map((violation) => violation.split(":")[0]),
|
|
102
|
+
["hot.ts"],
|
|
103
|
+
);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("the three abstract services are still supplied per call, not layered", () => {
|
|
107
|
+
// Arrange
|
|
108
|
+
const services = readFileSync(join(CORE_DIR, "services.ts"), "utf8");
|
|
109
|
+
const piUi = readFileSync(new URL("../sdk/pi-ui.ts", import.meta.url), "utf8");
|
|
110
|
+
|
|
111
|
+
// Act / Assert: three tags, no layer machinery, and the fringe supplies them.
|
|
112
|
+
assert.strictEqual((services.match(/Context\.Service/g) ?? []).length, 3);
|
|
113
|
+
assert.ok(!services.includes(".layer"));
|
|
114
|
+
assert.ok(!/\bLayer\b/.test(services));
|
|
115
|
+
assert.ok(piUi.includes("Effect.provideService(Keyboard"));
|
|
116
|
+
assert.ok(piUi.includes("Effect.provideService(Renderer"));
|
|
117
|
+
assert.ok(piUi.includes("Effect.provideService(FileSuggestions"));
|
|
118
|
+
});
|
package/src/core/scene.test.ts
CHANGED
|
@@ -24,11 +24,11 @@ import {
|
|
|
24
24
|
import { emptyEditor } from "./editor.ts";
|
|
25
25
|
|
|
26
26
|
const question = (over: Partial<QuestionType> = {}): QuestionType =>
|
|
27
|
-
|
|
27
|
+
Question.make({
|
|
28
28
|
id: "q1",
|
|
29
29
|
label: "Q1",
|
|
30
30
|
prompt: "Pick one?",
|
|
31
|
-
options: [
|
|
31
|
+
options: [Option.make({ label: "Yes" }), Option.make({ label: "No" })],
|
|
32
32
|
allowOther: true,
|
|
33
33
|
multiple: false,
|
|
34
34
|
...over,
|
|
@@ -60,9 +60,9 @@ describe("buildScene: single question", () => {
|
|
|
60
60
|
expect(lines[0]).toBe(boxTop(60));
|
|
61
61
|
expect(lines[1]).toBe(boxed(" Pick one?", 60));
|
|
62
62
|
expect(lines[2]).toBe(boxed("", 60));
|
|
63
|
-
expect(lines[3]).toBe(boxed("→
|
|
64
|
-
expect(lines[4]).toBe(boxed("
|
|
65
|
-
expect(lines[5]).toBe(boxed("
|
|
63
|
+
expect(lines[3]).toBe(boxed("→ 1. Yes", 60));
|
|
64
|
+
expect(lines[4]).toBe(boxed(" 2. No", 60));
|
|
65
|
+
expect(lines[5]).toBe(boxed(" 3. Type something.", 60));
|
|
66
66
|
expect(lines[6]).toBe(boxed("", 60));
|
|
67
67
|
expect(lines[7]).toBe(boxed(" ↑↓ navigate • Enter to select • Esc to cancel", 60));
|
|
68
68
|
expect(lines[8]).toBe(boxBottom(60));
|
|
@@ -74,18 +74,28 @@ describe("buildScene: single question", () => {
|
|
|
74
74
|
// framed: [rail, marker, label, pad..., rail]
|
|
75
75
|
const selectedLine = scene.lines[3]!;
|
|
76
76
|
expect(selectedLine[1]).toEqual({ text: "→ ", style: "accent" });
|
|
77
|
-
expect(selectedLine
|
|
77
|
+
expect(selectedLine.find((s) => s.text === "1. Yes")).toEqual({
|
|
78
|
+
text: "1. Yes",
|
|
79
|
+
style: "accent",
|
|
80
|
+
});
|
|
78
81
|
const unselectedLine = scene.lines[4]!;
|
|
79
|
-
expect(unselectedLine
|
|
82
|
+
expect(unselectedLine.find((s) => s.text === "2. No")).toEqual({
|
|
83
|
+
text: "2. No",
|
|
84
|
+
style: "text",
|
|
85
|
+
});
|
|
86
|
+
// The marker column's width is pinned once, in the recommendation slice.
|
|
80
87
|
});
|
|
81
88
|
|
|
82
89
|
it("shows descriptions indented under options", () => {
|
|
83
90
|
const q = question({
|
|
84
|
-
options: [
|
|
91
|
+
options: [
|
|
92
|
+
Option.make({ label: "Yes", description: "Go ahead" }),
|
|
93
|
+
Option.make({ label: "No" }),
|
|
94
|
+
],
|
|
85
95
|
});
|
|
86
96
|
const scene = buildScene(initialMachineState([q]), 40);
|
|
87
97
|
const lines = plainLines(scene);
|
|
88
|
-
expect(lines.
|
|
98
|
+
expect(lines[4]).toBe(boxed(" Go ahead", 40));
|
|
89
99
|
});
|
|
90
100
|
|
|
91
101
|
it("wraps long prompts with continuation indentation", () => {
|
|
@@ -98,6 +108,48 @@ describe("buildScene: single question", () => {
|
|
|
98
108
|
});
|
|
99
109
|
});
|
|
100
110
|
|
|
111
|
+
describe("buildScene: recommended options", () => {
|
|
112
|
+
const recommended = (over: Partial<QuestionType> = {}): QuestionType =>
|
|
113
|
+
question({
|
|
114
|
+
options: [Option.make({ label: "Yes", recommended: true }), Option.make({ label: "No" })],
|
|
115
|
+
...over,
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("renders a ✓ in the marker column for a recommended single-choice option", () => {
|
|
119
|
+
const scene = buildScene(initialMachineState([recommended()]), 60);
|
|
120
|
+
const lines = plainLines(scene);
|
|
121
|
+
expect(lines[3]).toBe(boxed("→ ✓ 1. Yes", 60));
|
|
122
|
+
expect(scene.lines[3]![1]).toEqual({ text: "→ ", style: "accent" });
|
|
123
|
+
expect(scene.lines[3]![2]).toEqual({ text: "✓ ", style: "success" });
|
|
124
|
+
expect(scene.lines[3]![3]).toEqual({ text: "1. Yes", style: "accent" });
|
|
125
|
+
expect(lines[4]).toBe(boxed(" 2. No", 60));
|
|
126
|
+
expect(scene.lines[4]![2]).toEqual({ text: " " });
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("renders a ✓ beside the checkbox for a recommended multi-select option", () => {
|
|
130
|
+
const multi = recommended({ multiple: true });
|
|
131
|
+
const unchecked = buildScene(initialMachineState([multi]), 60);
|
|
132
|
+
expect(plainLines(unchecked)[3]).toBe(boxed("→ ✓ □ 1. Yes", 60));
|
|
133
|
+
let state = initialMachineState([multi]);
|
|
134
|
+
state = step(state, KeyEvent.char(" ")).state; // check option 0
|
|
135
|
+
expect(plainLines(buildScene(state, 60))[3]).toBe(boxed("→ ✓ ■ 1. Yes", 60));
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("leaves the reserved column blank on non-option rows", () => {
|
|
139
|
+
const scene = buildScene(initialMachineState([recommended({ multiple: true })]), 60);
|
|
140
|
+
expect(plainLines(scene)[5]).toBe(boxed(" + Add your own answer", 60));
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("wraps a marked long label past the marker", () => {
|
|
144
|
+
const q = question({
|
|
145
|
+
options: [Option.make({ label: "Document the deployment guard", recommended: true })],
|
|
146
|
+
});
|
|
147
|
+
const lines = plainLines(buildScene(initialMachineState([q]), 26));
|
|
148
|
+
expect(lines[3]).toBe(boxed("→ ✓ 1. Document the", 26));
|
|
149
|
+
expect(lines[4]).toBe(boxed(" deployment guard", 26));
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
101
153
|
describe("buildScene: multi question", () => {
|
|
102
154
|
it("renders a tab bar with the active tab marked by a bordered square", () => {
|
|
103
155
|
const scene = buildScene(initialMachineState(twoQuestions()), 60);
|
|
@@ -144,8 +196,8 @@ describe("buildScene: multi question", () => {
|
|
|
144
196
|
state = step(state, KeyEvent.enter).state; // submit, advance to q2
|
|
145
197
|
state = step(state, KeyEvent.shiftTab).state; // back to q1
|
|
146
198
|
const lines = plainLines(buildScene(state, 60));
|
|
147
|
-
expect(lines.some((l) => l.includes("→
|
|
148
|
-
expect(lines.some((l) => l.includes("
|
|
199
|
+
expect(lines.some((l) => l.includes("→ 3. Type something."))).toBe(true);
|
|
200
|
+
expect(lines.some((l) => l.includes(" Current answer: hi"))).toBe(true);
|
|
149
201
|
});
|
|
150
202
|
|
|
151
203
|
it("shows the chosen option when returning to its tab", () => {
|
|
@@ -154,13 +206,16 @@ describe("buildScene: multi question", () => {
|
|
|
154
206
|
state = step(state, KeyEvent.enter).state; // answer q1, advance to q2
|
|
155
207
|
state = step(state, KeyEvent.shiftTab).state; // back to q1
|
|
156
208
|
const lines = plainLines(buildScene(state, 60));
|
|
157
|
-
expect(lines.some((l) => l.includes("→
|
|
209
|
+
expect(lines.some((l) => l.includes("→ 2. No"))).toBe(true);
|
|
158
210
|
expect(lines.some((l) => l.includes("Current answer"))).toBe(false);
|
|
159
211
|
});
|
|
160
212
|
|
|
161
213
|
it("indents the custom answer subtitle like option descriptions", () => {
|
|
162
214
|
const q = question({
|
|
163
|
-
options: [
|
|
215
|
+
options: [
|
|
216
|
+
Option.make({ label: "Yes", description: "Go ahead" }),
|
|
217
|
+
Option.make({ label: "No" }),
|
|
218
|
+
],
|
|
164
219
|
});
|
|
165
220
|
let state = initialMachineState([q, question({ id: "q2", label: "Q2", prompt: "Second?" })]);
|
|
166
221
|
state = step(state, KeyEvent.down).state;
|
|
@@ -197,9 +252,9 @@ describe("buildScene: multi-select question", () => {
|
|
|
197
252
|
it("renders checkbox options and an add entry", () => {
|
|
198
253
|
const scene = buildScene(initialMachineState([multiQuestion()]), 60);
|
|
199
254
|
const lines = plainLines(scene);
|
|
200
|
-
expect(lines[3]).toBe(boxed("→
|
|
201
|
-
expect(lines[4]).toBe(boxed("
|
|
202
|
-
expect(lines[5]).toBe(boxed("
|
|
255
|
+
expect(lines[3]).toBe(boxed("→ □ 1. Yes", 60));
|
|
256
|
+
expect(lines[4]).toBe(boxed(" □ 2. No", 60));
|
|
257
|
+
expect(lines[5]).toBe(boxed(" + Add your own answer", 60));
|
|
203
258
|
});
|
|
204
259
|
|
|
205
260
|
it("shows multi-select controls in the help line", () => {
|
|
@@ -211,8 +266,8 @@ describe("buildScene: multi-select question", () => {
|
|
|
211
266
|
let state = initialMachineState([multiQuestion()]);
|
|
212
267
|
state = step(state, KeyEvent.char(" ")).state; // select option 0
|
|
213
268
|
const lines = plainLines(buildScene(state, 60));
|
|
214
|
-
expect(lines[3]).toBe(boxed("→
|
|
215
|
-
expect(lines[4]).toBe(boxed("
|
|
269
|
+
expect(lines[3]).toBe(boxed("→ ■ 1. Yes", 60));
|
|
270
|
+
expect(lines[4]).toBe(boxed(" □ 2. No", 60));
|
|
216
271
|
});
|
|
217
272
|
|
|
218
273
|
it("renders an added custom answer as a chip", () => {
|
|
@@ -250,8 +305,8 @@ describe("buildScene: editor mode", () => {
|
|
|
250
305
|
expect(lines).toContain(boxed(" Your answer:", 40));
|
|
251
306
|
expect(lines.some((l) => l.includes("Shift+Enter"))).toBe(true);
|
|
252
307
|
expect(lines.some((l) => l.includes("Esc to go back"))).toBe(true);
|
|
253
|
-
expect(lines[3]).toBe(boxed("
|
|
254
|
-
expect(lines[5]).toBe(boxed("→
|
|
308
|
+
expect(lines[3]).toBe(boxed(" 1. Yes", 40));
|
|
309
|
+
expect(lines[5]).toBe(boxed("→ 3. Type something. ✎", 40));
|
|
255
310
|
// editor box frame, prefixed with the gutter space, inside the outer rails
|
|
256
311
|
expect(lines[8]).toBe("│ ╭" + "─".repeat(35) + "╮│");
|
|
257
312
|
});
|