@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.
- package/LICENSE +21 -0
- package/README.md +161 -0
- package/package.json +68 -0
- package/src/core/domain.test.ts +154 -0
- package/src/core/domain.ts +149 -0
- package/src/core/editor.test.ts +124 -0
- package/src/core/editor.ts +78 -0
- package/src/core/flow.test.ts +445 -0
- package/src/core/flow.ts +98 -0
- package/src/core/keyboard.test.ts +44 -0
- package/src/core/keyboard.ts +59 -0
- package/src/core/machine.test.ts +438 -0
- package/src/core/machine.ts +378 -0
- package/src/core/scene.test.ts +385 -0
- package/src/core/scene.ts +555 -0
- package/src/core/services.ts +81 -0
- package/src/core/text.test.ts +77 -0
- package/src/core/text.ts +207 -0
- package/src/sdk/box-parity.test.ts +76 -0
- package/src/sdk/keys.test.ts +59 -0
- package/src/sdk/keys.ts +77 -0
- package/src/sdk/painter.ts +33 -0
- package/src/sdk/pi-ui.test.ts +185 -0
- package/src/sdk/pi-ui.ts +210 -0
- package/src/sdk/schema.test.ts +67 -0
- package/src/tool/question.test.ts +70 -0
- package/src/tool/question.ts +176 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keyboard events understood by the core interaction machine.
|
|
3
|
+
*
|
|
4
|
+
* The fringe maps pi-tui key data into these events. The core never sees raw
|
|
5
|
+
* terminal escape sequences.
|
|
6
|
+
*
|
|
7
|
+
* The union is defined as an Effect Schema so the type has a single source of
|
|
8
|
+
* truth, is decodable at runtime (tests, serialization), and can be matched
|
|
9
|
+
* exhaustively: every reducer must handle every tag, so adding a new event is
|
|
10
|
+
* a compile-time prompt to decide what each mode does with it.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { Schema } from "effect";
|
|
14
|
+
|
|
15
|
+
export const KeyEventSchema = Schema.TaggedUnion({
|
|
16
|
+
up: {},
|
|
17
|
+
down: {},
|
|
18
|
+
left: {},
|
|
19
|
+
right: {},
|
|
20
|
+
tab: {},
|
|
21
|
+
shiftTab: {},
|
|
22
|
+
enter: {},
|
|
23
|
+
shiftEnter: {},
|
|
24
|
+
escape: {},
|
|
25
|
+
backspace: {},
|
|
26
|
+
delete: {},
|
|
27
|
+
home: {},
|
|
28
|
+
end: {},
|
|
29
|
+
char: { char: Schema.String },
|
|
30
|
+
paste: { text: Schema.String },
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export type KeyEvent = Schema.Schema.Type<typeof KeyEventSchema>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Canonical event instances, plus factories for the data-carrying variants.
|
|
37
|
+
*
|
|
38
|
+
* The tag-only events are singletons: the fringe and tests use `KeyEvent.up`,
|
|
39
|
+
* `KeyEvent.escape`, … instead of re-typing `_tag` literals, so the event
|
|
40
|
+
* vocabulary lives in exactly one place. `char` and `paste` carry data and are
|
|
41
|
+
* built with `KeyEvent.char(text)` / `KeyEvent.paste(text)`.
|
|
42
|
+
*/
|
|
43
|
+
export const KeyEvent = {
|
|
44
|
+
up: KeyEventSchema.make({ _tag: "up" }),
|
|
45
|
+
down: KeyEventSchema.make({ _tag: "down" }),
|
|
46
|
+
left: KeyEventSchema.make({ _tag: "left" }),
|
|
47
|
+
right: KeyEventSchema.make({ _tag: "right" }),
|
|
48
|
+
tab: KeyEventSchema.make({ _tag: "tab" }),
|
|
49
|
+
shiftTab: KeyEventSchema.make({ _tag: "shiftTab" }),
|
|
50
|
+
enter: KeyEventSchema.make({ _tag: "enter" }),
|
|
51
|
+
shiftEnter: KeyEventSchema.make({ _tag: "shiftEnter" }),
|
|
52
|
+
escape: KeyEventSchema.make({ _tag: "escape" }),
|
|
53
|
+
backspace: KeyEventSchema.make({ _tag: "backspace" }),
|
|
54
|
+
delete: KeyEventSchema.make({ _tag: "delete" }),
|
|
55
|
+
home: KeyEventSchema.make({ _tag: "home" }),
|
|
56
|
+
end: KeyEventSchema.make({ _tag: "end" }),
|
|
57
|
+
char: (char: string): KeyEvent => KeyEventSchema.make({ _tag: "char", char }),
|
|
58
|
+
paste: (text: string): KeyEvent => KeyEventSchema.make({ _tag: "paste", text }),
|
|
59
|
+
} as const;
|
|
@@ -0,0 +1,438 @@
|
|
|
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
|
+
currentOptions,
|
|
6
|
+
initialMachineState,
|
|
7
|
+
isAtCompletionContext,
|
|
8
|
+
openSuggestions,
|
|
9
|
+
step,
|
|
10
|
+
textBeforeCursor,
|
|
11
|
+
type MachineState,
|
|
12
|
+
type SuggestionState,
|
|
13
|
+
} from "./machine.ts";
|
|
14
|
+
|
|
15
|
+
const question = (over: Partial<QuestionType> = {}): QuestionType =>
|
|
16
|
+
new Question({
|
|
17
|
+
id: "q1",
|
|
18
|
+
label: "Q1",
|
|
19
|
+
prompt: "Pick one?",
|
|
20
|
+
options: [new Option({ label: "Yes" }), new Option({ label: "No" })],
|
|
21
|
+
allowOther: true,
|
|
22
|
+
...over,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const twoQuestions = (): readonly Question[] => [
|
|
26
|
+
question(),
|
|
27
|
+
question({ id: "q2", label: "Q2", prompt: "Second?" }),
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
const down = (n: number) => Array.from({ length: n }, () => KeyEvent.down);
|
|
31
|
+
|
|
32
|
+
describe("single question", () => {
|
|
33
|
+
it("starts in single mode on the first tab", () => {
|
|
34
|
+
const state = initialMachineState([question()]);
|
|
35
|
+
expect(state.mode).toBe("single");
|
|
36
|
+
expect(state.currentTab).toBe(0);
|
|
37
|
+
expect(state.optionIndex).toBe(0);
|
|
38
|
+
expect(state.inputMode).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("navigates options with up and down, clamped", () => {
|
|
42
|
+
let state = initialMachineState([question()]);
|
|
43
|
+
for (const event of down(2)) state = step(state, event).state;
|
|
44
|
+
expect(state.optionIndex).toBe(2); // clamps at "Type something."
|
|
45
|
+
state = step(state, KeyEvent.up).state;
|
|
46
|
+
expect(state.optionIndex).toBe(1);
|
|
47
|
+
state = step(state, KeyEvent.up).state;
|
|
48
|
+
state = step(state, KeyEvent.up).state;
|
|
49
|
+
expect(state.optionIndex).toBe(0); // clamps at 0
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("completes with the selected option on enter", () => {
|
|
53
|
+
const result = step(
|
|
54
|
+
step(initialMachineState([question()]), KeyEvent.down).state,
|
|
55
|
+
KeyEvent.enter,
|
|
56
|
+
);
|
|
57
|
+
expect(result.action.type).toBe("complete");
|
|
58
|
+
if (result.action.type === "complete") {
|
|
59
|
+
const answer = result.action.result.answers[0];
|
|
60
|
+
expect(result.action.result.cancelled).toBe(false);
|
|
61
|
+
expect(answer?.id).toBe("q1");
|
|
62
|
+
expect(answer?.label).toBe("No");
|
|
63
|
+
expect(answer?.wasCustom).toBe(false);
|
|
64
|
+
expect(answer?.index).toBe(2);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("cancels with escape", () => {
|
|
69
|
+
const result = step(initialMachineState([question()]), KeyEvent.escape);
|
|
70
|
+
expect(result.action.type).toBe("complete");
|
|
71
|
+
if (result.action.type === "complete") {
|
|
72
|
+
expect(result.action.result.cancelled).toBe(true);
|
|
73
|
+
expect(result.action.result.answers).toHaveLength(0);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("opens the editor on the 'Type something' option", () => {
|
|
78
|
+
let state = initialMachineState([question()]);
|
|
79
|
+
for (const event of down(2)) state = step(state, event).state;
|
|
80
|
+
const next = step(state, KeyEvent.enter);
|
|
81
|
+
expect(next.state.inputMode).toBe(true);
|
|
82
|
+
expect(next.state.editor.text).toBe("");
|
|
83
|
+
expect(next.action.type).toBe("none");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("submits a custom answer from the editor", () => {
|
|
87
|
+
let state = initialMachineState([question()]);
|
|
88
|
+
for (const event of down(2)) state = step(state, event).state;
|
|
89
|
+
state = step(state, KeyEvent.enter).state;
|
|
90
|
+
for (const ch of ["c", "u", "s"]) {
|
|
91
|
+
state = step(state, KeyEvent.char(ch)).state;
|
|
92
|
+
}
|
|
93
|
+
const result = step(state, KeyEvent.enter);
|
|
94
|
+
expect(result.action.type).toBe("complete");
|
|
95
|
+
if (result.action.type === "complete") {
|
|
96
|
+
const answer = result.action.result.answers[0];
|
|
97
|
+
expect(answer?.label).toBe("cus");
|
|
98
|
+
expect(answer?.wasCustom).toBe(true);
|
|
99
|
+
expect(answer?.index).toBeUndefined();
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("returns to the options on escape from the editor", () => {
|
|
104
|
+
let state = initialMachineState([question()]);
|
|
105
|
+
for (const event of down(2)) state = step(state, event).state;
|
|
106
|
+
state = step(state, KeyEvent.enter).state;
|
|
107
|
+
state = step(state, KeyEvent.char("x")).state;
|
|
108
|
+
const next = step(state, KeyEvent.escape);
|
|
109
|
+
expect(next.state.inputMode).toBe(false);
|
|
110
|
+
expect(next.state.editor.text).toBe("");
|
|
111
|
+
expect(next.action.type).toBe("none");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("submits an empty editor as '(no response)'", () => {
|
|
115
|
+
let state = initialMachineState([question()]);
|
|
116
|
+
for (const event of down(2)) state = step(state, event).state;
|
|
117
|
+
state = step(state, KeyEvent.enter).state;
|
|
118
|
+
const result = step(state, KeyEvent.enter);
|
|
119
|
+
expect(result.action.type).toBe("complete");
|
|
120
|
+
if (result.action.type === "complete") {
|
|
121
|
+
expect(result.action.result.answers[0]?.label).toBe("(no response)");
|
|
122
|
+
expect(result.action.result.answers[0]?.wasCustom).toBe(true);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("inserts a newline on shift+enter while editing", () => {
|
|
127
|
+
let state = initialMachineState([question()]);
|
|
128
|
+
for (const event of down(2)) state = step(state, event).state;
|
|
129
|
+
state = step(state, KeyEvent.enter).state;
|
|
130
|
+
const events = [KeyEvent.char("a"), KeyEvent.shiftEnter, KeyEvent.char("b")];
|
|
131
|
+
for (const event of events) state = step(state, event).state;
|
|
132
|
+
expect(state.editor.text).toBe("a\nb");
|
|
133
|
+
expect(state.editor.cursor).toBe(3);
|
|
134
|
+
expect(state.inputMode).toBe(true); // not submitted
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("inserts sanitized paste text while editing", () => {
|
|
138
|
+
let state = initialMachineState([question()]);
|
|
139
|
+
for (const event of down(2)) state = step(state, event).state;
|
|
140
|
+
state = step(state, KeyEvent.enter).state;
|
|
141
|
+
state = step(state, KeyEvent.paste("line1\r\nline2")).state;
|
|
142
|
+
expect(state.editor.text).toBe("line1\nline2");
|
|
143
|
+
state = step(state, KeyEvent.paste("\x03a\x07b\t c")).state;
|
|
144
|
+
expect(state.editor.text).toBe("line1\nline2ab c");
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("ignores paste and shift+enter while navigating options", () => {
|
|
148
|
+
let state = initialMachineState([question()]);
|
|
149
|
+
expect(step(state, KeyEvent.paste("x")).state).toBe(state);
|
|
150
|
+
expect(step(state, KeyEvent.shiftEnter).state).toBe(state);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("ignores tab navigation in single mode", () => {
|
|
154
|
+
const result = step(initialMachineState([question()]), KeyEvent.tab);
|
|
155
|
+
expect(result.state.currentTab).toBe(0);
|
|
156
|
+
expect(result.action.type).toBe("none");
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe("multi question", () => {
|
|
161
|
+
it("starts in multi mode", () => {
|
|
162
|
+
expect(initialMachineState(twoQuestions()).mode).toBe("multi");
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("cycles tabs with tab and shift-tab, including the submit tab", () => {
|
|
166
|
+
let state = initialMachineState(twoQuestions());
|
|
167
|
+
state = step(state, KeyEvent.tab).state;
|
|
168
|
+
expect(state.currentTab).toBe(1);
|
|
169
|
+
state = step(state, KeyEvent.tab).state;
|
|
170
|
+
expect(state.currentTab).toBe(2); // submit tab
|
|
171
|
+
state = step(state, KeyEvent.tab).state;
|
|
172
|
+
expect(state.currentTab).toBe(0); // wraps
|
|
173
|
+
state = step(state, KeyEvent.shiftTab).state;
|
|
174
|
+
expect(state.currentTab).toBe(2);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("treats left and right as tab navigation", () => {
|
|
178
|
+
let state = initialMachineState(twoQuestions());
|
|
179
|
+
state = step(state, KeyEvent.right).state;
|
|
180
|
+
expect(state.currentTab).toBe(1);
|
|
181
|
+
state = step(state, KeyEvent.left).state;
|
|
182
|
+
expect(state.currentTab).toBe(0);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("resets the option index when changing tabs", () => {
|
|
186
|
+
let state = initialMachineState(twoQuestions());
|
|
187
|
+
state = step(state, KeyEvent.down).state;
|
|
188
|
+
expect(state.optionIndex).toBe(1);
|
|
189
|
+
state = step(state, KeyEvent.tab).state;
|
|
190
|
+
expect(state.optionIndex).toBe(0);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("answers and advances to the next tab", () => {
|
|
194
|
+
const result = step(initialMachineState(twoQuestions()), KeyEvent.enter);
|
|
195
|
+
expect(result.state.currentTab).toBe(1);
|
|
196
|
+
expect(result.state.answers.get("q1")?.label).toBe("Yes");
|
|
197
|
+
expect(result.action.type).toBe("none");
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("advances to the submit tab after the last question", () => {
|
|
201
|
+
let state = initialMachineState(twoQuestions());
|
|
202
|
+
state = step(state, KeyEvent.tab).state;
|
|
203
|
+
state = step(state, KeyEvent.enter).state;
|
|
204
|
+
expect(state.currentTab).toBe(2);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("gates submit on all questions being answered", () => {
|
|
208
|
+
let state = initialMachineState(twoQuestions());
|
|
209
|
+
state = step(state, KeyEvent.enter).state; // q1 answered, now q2
|
|
210
|
+
state = step(state, KeyEvent.tab).state; // submit tab, q2 unanswered
|
|
211
|
+
const blocked = step(state, KeyEvent.enter);
|
|
212
|
+
expect(blocked.action.type).toBe("none");
|
|
213
|
+
expect(blocked.state.currentTab).toBe(2);
|
|
214
|
+
const cancelled = step(state, KeyEvent.escape);
|
|
215
|
+
expect(cancelled.action.type).toBe("complete");
|
|
216
|
+
if (cancelled.action.type === "complete") {
|
|
217
|
+
expect(cancelled.action.result.cancelled).toBe(true);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("completes when all answered and submit is confirmed", () => {
|
|
222
|
+
let state = initialMachineState(twoQuestions());
|
|
223
|
+
state = step(state, KeyEvent.enter).state;
|
|
224
|
+
state = step(state, KeyEvent.enter).state; // answers q2, advances to submit tab
|
|
225
|
+
const result = step(state, KeyEvent.enter);
|
|
226
|
+
expect(result.action.type).toBe("complete");
|
|
227
|
+
if (result.action.type === "complete") {
|
|
228
|
+
expect(result.action.result.cancelled).toBe(false);
|
|
229
|
+
expect(result.action.result.answers).toHaveLength(2);
|
|
230
|
+
expect(result.action.result.questions.map((q) => q.id)).toEqual(["q1", "q2"]);
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("keeps answers in answering order", () => {
|
|
235
|
+
let state = initialMachineState(twoQuestions());
|
|
236
|
+
state = step(state, KeyEvent.tab).state; // q2
|
|
237
|
+
state = step(state, KeyEvent.enter).state; // answer q2, advances to submit tab
|
|
238
|
+
state = step(state, KeyEvent.shiftTab).state; // q2
|
|
239
|
+
state = step(state, KeyEvent.shiftTab).state; // q1
|
|
240
|
+
state = step(state, KeyEvent.enter).state; // answer q1, advances to q2
|
|
241
|
+
state = step(state, KeyEvent.tab).state; // submit tab
|
|
242
|
+
const result = step(state, KeyEvent.enter);
|
|
243
|
+
expect(result.action.type).toBe("complete");
|
|
244
|
+
if (result.action.type === "complete") {
|
|
245
|
+
expect(result.action.result.answers.map((a) => a.id)).toEqual(["q2", "q1"]);
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("excludes the 'Type something' option when allowOther is false", () => {
|
|
250
|
+
const state = initialMachineState([
|
|
251
|
+
question({ id: "x", allowOther: false }),
|
|
252
|
+
question({ id: "y", allowOther: false }),
|
|
253
|
+
]);
|
|
254
|
+
expect(currentOptions(state)).toHaveLength(2);
|
|
255
|
+
expect(currentOptions(state).every((o) => !o.isOther)).toBe(true);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("answers with an index relative to the visible options", () => {
|
|
259
|
+
let state = initialMachineState([question({ allowOther: false })]);
|
|
260
|
+
state = step(state, KeyEvent.down).state; // "No" is index 1 of 2
|
|
261
|
+
const result = step(state, KeyEvent.enter);
|
|
262
|
+
expect(result.action.type).toBe("complete");
|
|
263
|
+
if (result.action.type === "complete") {
|
|
264
|
+
expect(result.action.result.answers[0]?.index).toBe(2);
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("highlights the answered option when returning to a tab", () => {
|
|
269
|
+
let state = initialMachineState(twoQuestions());
|
|
270
|
+
state = step(state, KeyEvent.down).state; // "No"
|
|
271
|
+
state = step(state, KeyEvent.enter).state; // answer q1, advance to q2
|
|
272
|
+
state = step(state, KeyEvent.shiftTab).state; // back to q1
|
|
273
|
+
expect(state.currentTab).toBe(0);
|
|
274
|
+
expect(state.optionIndex).toBe(1); // "No" is the second option
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("highlights 'Type something' when returning to a custom-answered tab", () => {
|
|
278
|
+
let state = initialMachineState(twoQuestions());
|
|
279
|
+
for (const event of down(2)) state = step(state, event).state;
|
|
280
|
+
state = step(state, KeyEvent.enter).state; // open editor
|
|
281
|
+
state = step(state, KeyEvent.char("hi")).state;
|
|
282
|
+
state = step(state, KeyEvent.enter).state; // submit, advance to q2
|
|
283
|
+
state = step(state, KeyEvent.shiftTab).state; // back to q1
|
|
284
|
+
expect(state.currentTab).toBe(0);
|
|
285
|
+
expect(state.optionIndex).toBe(2); // the "Type something" entry
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("pre-fills the editor with the existing custom answer when reopened", () => {
|
|
289
|
+
let state = initialMachineState(twoQuestions());
|
|
290
|
+
for (const event of down(2)) state = step(state, event).state;
|
|
291
|
+
state = step(state, KeyEvent.enter).state;
|
|
292
|
+
state = step(state, KeyEvent.char("hey")).state;
|
|
293
|
+
state = step(state, KeyEvent.enter).state; // submit, advance to q2
|
|
294
|
+
state = step(state, KeyEvent.shiftTab).state; // back to q1
|
|
295
|
+
state = step(state, KeyEvent.enter).state; // reopen the editor
|
|
296
|
+
expect(state.inputMode).toBe(true);
|
|
297
|
+
expect(state.editor).toEqual({ text: "hey", cursor: 3 });
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("opens an empty editor on a tab answered with an option", () => {
|
|
301
|
+
let state = initialMachineState(twoQuestions());
|
|
302
|
+
state = step(state, KeyEvent.enter).state; // answer q1 with "Yes"
|
|
303
|
+
state = step(state, KeyEvent.shiftTab).state; // back to q1
|
|
304
|
+
state = step(state, KeyEvent.down).state;
|
|
305
|
+
state = step(state, KeyEvent.down).state; // "Type something"
|
|
306
|
+
state = step(state, KeyEvent.enter).state;
|
|
307
|
+
expect(state.inputMode).toBe(true);
|
|
308
|
+
expect(state.editor).toEqual({ text: "", cursor: 0 });
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("updates an existing custom answer when edited and re-submitted", () => {
|
|
312
|
+
let state = initialMachineState(twoQuestions());
|
|
313
|
+
for (const event of down(2)) state = step(state, event).state;
|
|
314
|
+
state = step(state, KeyEvent.enter).state;
|
|
315
|
+
state = step(state, KeyEvent.char("hey")).state;
|
|
316
|
+
state = step(state, KeyEvent.enter).state; // submit "hey"
|
|
317
|
+
state = step(state, KeyEvent.shiftTab).state; // back to q1
|
|
318
|
+
state = step(state, KeyEvent.enter).state; // reopen, pre-filled
|
|
319
|
+
state = step(state, KeyEvent.char("!")).state;
|
|
320
|
+
state = step(state, KeyEvent.enter).state; // re-submit "hey!"
|
|
321
|
+
expect(state.answers.get("q1")?.label).toBe("hey!");
|
|
322
|
+
expect(state.answers.get("q1")?.wasCustom).toBe(true);
|
|
323
|
+
expect(state.currentTab).toBe(1);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("highlights an existing answer when advancing to an answered tab", () => {
|
|
327
|
+
let state = initialMachineState(twoQuestions());
|
|
328
|
+
state = step(state, KeyEvent.tab).state; // q2
|
|
329
|
+
state = step(state, KeyEvent.down).state; // "No"
|
|
330
|
+
state = step(state, KeyEvent.enter).state; // answer q2, advance to submit
|
|
331
|
+
state = step(state, KeyEvent.shiftTab).state; // q2
|
|
332
|
+
state = step(state, KeyEvent.shiftTab).state; // q1
|
|
333
|
+
state = step(state, KeyEvent.enter).state; // answer q1 "Yes", advance to q2
|
|
334
|
+
expect(state.currentTab).toBe(1);
|
|
335
|
+
expect(state.optionIndex).toBe(1); // q2's answer "No" is highlighted
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
describe("file suggestions", () => {
|
|
340
|
+
const suggestions = (over: Partial<SuggestionState> = {}): SuggestionState => ({
|
|
341
|
+
prefix: "@src",
|
|
342
|
+
items: [
|
|
343
|
+
{ value: "src/main.ts", label: "main.ts", description: "src/main.ts" },
|
|
344
|
+
{ value: "src/utils/", label: "utils/", description: "src/utils/" },
|
|
345
|
+
],
|
|
346
|
+
selectedIndex: 0,
|
|
347
|
+
...over,
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
const openEditorWithSuggestions = (): MachineState => {
|
|
351
|
+
let state = initialMachineState([question()]);
|
|
352
|
+
for (const event of down(2)) state = step(state, event).state;
|
|
353
|
+
state = step(state, KeyEvent.enter).state;
|
|
354
|
+
return openSuggestions(state, "@src", suggestions().items);
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
it("preselects the best match on open", () => {
|
|
358
|
+
const state = openEditorWithSuggestions();
|
|
359
|
+
expect(state.suggestions?.selectedIndex).toBe(0);
|
|
360
|
+
// Exact value match wins over prefix match
|
|
361
|
+
const state2 = openSuggestions(openEditorWithSuggestions(), "src/main", [
|
|
362
|
+
{ value: "src/main.ts", label: "main.ts" },
|
|
363
|
+
{ value: "src/main", label: "main", description: "src/main" },
|
|
364
|
+
]);
|
|
365
|
+
expect(state2.suggestions?.selectedIndex).toBe(1);
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it("navigates the highlight with up and down, wrapping at both ends", () => {
|
|
369
|
+
let state = openEditorWithSuggestions();
|
|
370
|
+
state = step(state, KeyEvent.up).state;
|
|
371
|
+
expect(state.suggestions?.selectedIndex).toBe(1); // wrapped from top
|
|
372
|
+
state = step(state, KeyEvent.down).state;
|
|
373
|
+
expect(state.suggestions?.selectedIndex).toBe(0);
|
|
374
|
+
state = step(state, KeyEvent.down).state;
|
|
375
|
+
expect(state.suggestions?.selectedIndex).toBe(1);
|
|
376
|
+
expect(state.editor.text).toBe(""); // navigation does not edit
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it("tab and enter ask to apply the highlighted suggestion", () => {
|
|
380
|
+
const state = openEditorWithSuggestions();
|
|
381
|
+
const tab = step(state, KeyEvent.tab);
|
|
382
|
+
expect(tab.action.type).toBe("applySuggestion");
|
|
383
|
+
const enter = step(state, KeyEvent.enter);
|
|
384
|
+
expect(enter.action.type).toBe("applySuggestion");
|
|
385
|
+
// The editor text is untouched: applying is the flow's job
|
|
386
|
+
expect(tab.state.editor).toEqual(enter.state.editor);
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
it("escape closes the popup without leaving the editor", () => {
|
|
390
|
+
let state = openEditorWithSuggestions();
|
|
391
|
+
state = step(state, KeyEvent.char("x")).state;
|
|
392
|
+
const next = step(state, KeyEvent.escape);
|
|
393
|
+
expect(next.state.inputMode).toBe(true);
|
|
394
|
+
expect(next.state.suggestions).toBeNull();
|
|
395
|
+
expect(next.state.editor.text).toBe("x");
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it("typing and editing still work while the popup is open", () => {
|
|
399
|
+
let state = openEditorWithSuggestions();
|
|
400
|
+
state = step(state, KeyEvent.char("m")).state;
|
|
401
|
+
state = step(state, KeyEvent.backspace).state;
|
|
402
|
+
expect(state.editor.text).toBe("");
|
|
403
|
+
expect(state.suggestions).not.toBeNull(); // popup stays; the flow refreshes it
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("tab with no popup asks for a forced completion", () => {
|
|
407
|
+
let state = initialMachineState([question()]);
|
|
408
|
+
for (const event of down(2)) state = step(state, event).state;
|
|
409
|
+
state = step(state, KeyEvent.enter).state;
|
|
410
|
+
expect(step(state, KeyEvent.tab).action.type).toBe("forceSuggestion");
|
|
411
|
+
expect(step(state, KeyEvent.shiftTab).action.type).toBe("none");
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it("closes the popup when leaving the editor", () => {
|
|
415
|
+
let state = openEditorWithSuggestions();
|
|
416
|
+
state = step(state, KeyEvent.escape).state; // popup closed, editor stays
|
|
417
|
+
state = step(state, KeyEvent.escape).state; // editor closed
|
|
418
|
+
expect(state.inputMode).toBe(false);
|
|
419
|
+
expect(state.suggestions).toBeNull();
|
|
420
|
+
state = step(state, KeyEvent.enter).state; // reopen the editor
|
|
421
|
+
expect(state.inputMode).toBe(true);
|
|
422
|
+
expect(state.suggestions).toBeNull(); // starts without a popup
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it("detects @ completion contexts at token boundaries", () => {
|
|
426
|
+
expect(isAtCompletionContext("@")).toBe(true);
|
|
427
|
+
expect(isAtCompletionContext("see @src")).toBe(true);
|
|
428
|
+
expect(isAtCompletionContext("see @src/utils/")).toBe(true);
|
|
429
|
+
expect(isAtCompletionContext("src/")).toBe(false); // plain paths need Tab
|
|
430
|
+
expect(isAtCompletionContext("@src ")).toBe(false); // broken by a space
|
|
431
|
+
expect(isAtCompletionContext("a@src")).toBe(false); // mid-token
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
it("textBeforeCursor slices at the cursor", () => {
|
|
435
|
+
const editor = { text: "ab@cd", cursor: 3 };
|
|
436
|
+
expect(textBeforeCursor(editor)).toBe("ab@");
|
|
437
|
+
});
|
|
438
|
+
});
|