@ftrdotdev/pi-inquiry 0.8.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 +8 -1
- package/package.json +18 -16
- package/src/core/domain.test.ts +35 -11
- package/src/core/domain.ts +31 -26
- package/src/core/editor.test.ts +87 -42
- package/src/core/flow.test.ts +7 -4
- 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 +4 -4
- package/src/core/machine.ts +25 -21
- package/src/core/purity.test.ts +118 -0
- package/src/core/scene.test.ts +12 -6
- package/src/core/scene.ts +37 -20
- 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 +46 -14
- package/src/sdk/pi-ui.ts +118 -93
- package/src/sdk/schema.test.ts +2 -2
- package/src/tool/question.test.ts +14 -20
- package/src/tool/question.ts +5 -6
package/src/core/editor.test.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
1
|
+
import { assert, describe, expect, it } from "@effect/vitest";
|
|
2
|
+
import { PROPERTY_RUNS } from "@ftrdotdev/pi-test-kit";
|
|
3
|
+
import {
|
|
4
|
+
cursorSeedArbitrary,
|
|
5
|
+
editorInputArbitrary,
|
|
6
|
+
pasteTextArbitrary,
|
|
7
|
+
permittedTextArbitrary,
|
|
8
|
+
singleCodePointArbitrary,
|
|
9
|
+
stepsArbitrary,
|
|
10
|
+
} from "../../test-support/generators.ts";
|
|
2
11
|
import {
|
|
3
12
|
editorBackspace,
|
|
4
13
|
editorDelete,
|
|
@@ -29,21 +38,6 @@ describe("editorInsert", () => {
|
|
|
29
38
|
expect(s.text).toBe("ab");
|
|
30
39
|
expect(s.cursor).toBe(2);
|
|
31
40
|
});
|
|
32
|
-
|
|
33
|
-
it("inserts at the cursor position", () => {
|
|
34
|
-
const s = editorInsert(editorInsert(emptyEditor(), "ab"), "X");
|
|
35
|
-
expect(s.text).toBe("abX");
|
|
36
|
-
const moved = editorMoveLeft(editorInsert(emptyEditor(), "ab"));
|
|
37
|
-
const inserted = editorInsert(moved, "X");
|
|
38
|
-
expect(inserted.text).toBe("aXb");
|
|
39
|
-
expect(inserted.cursor).toBe(2);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("treats surrogate pairs as single characters", () => {
|
|
43
|
-
const s = editorInsert(emptyEditor(), "😀");
|
|
44
|
-
expect(s.cursor).toBe(1);
|
|
45
|
-
expect(s.text).toBe("😀");
|
|
46
|
-
});
|
|
47
41
|
});
|
|
48
42
|
|
|
49
43
|
describe("editorBackspace", () => {
|
|
@@ -52,19 +46,6 @@ describe("editorBackspace", () => {
|
|
|
52
46
|
expect(s.text).toBe("ab");
|
|
53
47
|
expect(s.cursor).toBe(2);
|
|
54
48
|
});
|
|
55
|
-
|
|
56
|
-
it("is a no-op at the start", () => {
|
|
57
|
-
const s = editorBackspace(emptyEditor());
|
|
58
|
-
expect(s).toEqual(emptyEditor());
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it("deletes whole code points", () => {
|
|
62
|
-
// insert "😀a", move left of the trailing "a", then backspace deletes the emoji
|
|
63
|
-
const base = editorInsert(emptyEditor(), "😀a");
|
|
64
|
-
const s = editorBackspace(editorMoveLeft(base));
|
|
65
|
-
expect(s.text).toBe("a");
|
|
66
|
-
expect(s.cursor).toBe(0);
|
|
67
|
-
});
|
|
68
49
|
});
|
|
69
50
|
|
|
70
51
|
describe("editorDelete", () => {
|
|
@@ -87,11 +68,6 @@ describe("editor movement", () => {
|
|
|
87
68
|
expect(editorMoveRight(emptyEditor()).cursor).toBe(0);
|
|
88
69
|
});
|
|
89
70
|
|
|
90
|
-
it("clamps at the boundaries", () => {
|
|
91
|
-
expect(editorMoveLeft(emptyEditor()).cursor).toBe(0);
|
|
92
|
-
expect(editorMoveRight({ text: "ab", cursor: 2 }).cursor).toBe(2);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
71
|
it("moves by code points", () => {
|
|
96
72
|
const base = editorInsert(emptyEditor(), "😀a");
|
|
97
73
|
expect(editorMoveLeft(base).cursor).toBe(1);
|
|
@@ -106,10 +82,6 @@ describe("editor movement", () => {
|
|
|
106
82
|
});
|
|
107
83
|
|
|
108
84
|
describe("sanitizePaste", () => {
|
|
109
|
-
it("normalizes line endings to newlines", () => {
|
|
110
|
-
expect(sanitizePaste("a\r\nb\rc")).toBe("a\nb\nc");
|
|
111
|
-
});
|
|
112
|
-
|
|
113
85
|
it("expands tabs to four spaces", () => {
|
|
114
86
|
expect(sanitizePaste("a\tb")).toBe("a b");
|
|
115
87
|
});
|
|
@@ -117,8 +89,81 @@ describe("sanitizePaste", () => {
|
|
|
117
89
|
it("keeps newlines and printable characters", () => {
|
|
118
90
|
expect(sanitizePaste("line1\nline2 😀")).toBe("line1\nline2 😀");
|
|
119
91
|
});
|
|
120
|
-
|
|
121
|
-
it("drops other control characters", () => {
|
|
122
|
-
expect(sanitizePaste("\x03a\x07\x1bb")).toBe("ab");
|
|
123
|
-
});
|
|
124
92
|
});
|
|
93
|
+
|
|
94
|
+
it.prop(
|
|
95
|
+
"the cursor stays within the code point count after any edit",
|
|
96
|
+
{ text: permittedTextArbitrary, input: editorInputArbitrary },
|
|
97
|
+
({ text, input }) => {
|
|
98
|
+
const edits = [
|
|
99
|
+
editorInsert(editorFromText(text), input),
|
|
100
|
+
editorDelete(editorFromText(text)),
|
|
101
|
+
editorBackspace(editorFromText(text)),
|
|
102
|
+
];
|
|
103
|
+
for (const state of edits) {
|
|
104
|
+
assert.isAtLeast(state.cursor, 0);
|
|
105
|
+
assert.isAtMost(state.cursor, Array.from(state.text).length);
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
it.prop(
|
|
112
|
+
"backspace after inserting at the end returns the original text",
|
|
113
|
+
{ text: permittedTextArbitrary, input: singleCodePointArbitrary },
|
|
114
|
+
({ text, input }) => {
|
|
115
|
+
assert.strictEqual(editorBackspace(editorInsert(editorFromText(text), input)).text, text);
|
|
116
|
+
},
|
|
117
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
it.prop(
|
|
121
|
+
"insert splices the input at the cursor and advances the cursor by its code point count",
|
|
122
|
+
{ text: permittedTextArbitrary, input: editorInputArbitrary, cursorSeed: cursorSeedArbitrary },
|
|
123
|
+
({ text, input, cursorSeed }) => {
|
|
124
|
+
const chars = Array.from(text);
|
|
125
|
+
const at = cursorSeed % (chars.length + 1);
|
|
126
|
+
const next = editorInsert({ text, cursor: at }, input);
|
|
127
|
+
const inserted = Array.from(input).length;
|
|
128
|
+
assert.strictEqual(Array.from(next.text).length, chars.length + inserted);
|
|
129
|
+
assert.strictEqual(next.cursor, at + inserted);
|
|
130
|
+
assert.strictEqual(next.text, chars.slice(0, at).join("") + input + chars.slice(at).join(""));
|
|
131
|
+
},
|
|
132
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
it.prop(
|
|
136
|
+
"movement clamps at both ends",
|
|
137
|
+
{ text: permittedTextArbitrary, steps: stepsArbitrary },
|
|
138
|
+
({ text, steps }) => {
|
|
139
|
+
const count = Array.from(text).length;
|
|
140
|
+
let left = editorFromText(text);
|
|
141
|
+
let right = { text, cursor: 0 };
|
|
142
|
+
for (let step = 0; step < steps; step += 1) {
|
|
143
|
+
const movedLeft = editorMoveLeft(left);
|
|
144
|
+
assert.strictEqual(movedLeft.cursor, left.cursor === 0 ? 0 : left.cursor - 1);
|
|
145
|
+
left = movedLeft;
|
|
146
|
+
const movedRight = editorMoveRight(right);
|
|
147
|
+
assert.strictEqual(movedRight.cursor, right.cursor === count ? count : right.cursor + 1);
|
|
148
|
+
right = movedRight;
|
|
149
|
+
}
|
|
150
|
+
assert.isAtLeast(left.cursor, 0);
|
|
151
|
+
assert.isAtMost(right.cursor, count);
|
|
152
|
+
},
|
|
153
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
it.prop(
|
|
157
|
+
"sanitizing a paste leaves no carriage return or control character and is idempotent",
|
|
158
|
+
{ text: pasteTextArbitrary },
|
|
159
|
+
({ text }) => {
|
|
160
|
+
const once = sanitizePaste(text);
|
|
161
|
+
assert.strictEqual(sanitizePaste(once), once);
|
|
162
|
+
assert(!once.includes("\r"));
|
|
163
|
+
for (const character of once) {
|
|
164
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
165
|
+
assert(codePoint === 0x0a || codePoint >= 0x20);
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
169
|
+
);
|
package/src/core/flow.test.ts
CHANGED
|
@@ -7,11 +7,11 @@ import { KeyEvent } from "./keyboard.ts";
|
|
|
7
7
|
import type { MachineState } from "./machine.ts";
|
|
8
8
|
|
|
9
9
|
const question = (over: Partial<QuestionType> = {}): QuestionType =>
|
|
10
|
-
|
|
10
|
+
Question.make({
|
|
11
11
|
id: "q1",
|
|
12
12
|
label: "Q1",
|
|
13
13
|
prompt: "Pick one?",
|
|
14
|
-
options: [
|
|
14
|
+
options: [Option.make({ label: "Yes" }), Option.make({ label: "No" })],
|
|
15
15
|
allowOther: true,
|
|
16
16
|
multiple: false,
|
|
17
17
|
...over,
|
|
@@ -21,7 +21,7 @@ const question = (over: Partial<QuestionType> = {}): QuestionType =>
|
|
|
21
21
|
const scriptedKeyboard = (keys: KeyEvent[]): Keyboard["Service"] => {
|
|
22
22
|
const remaining = [...keys];
|
|
23
23
|
return {
|
|
24
|
-
nextKey:
|
|
24
|
+
nextKey: Effect.sync(() => remaining.shift() ?? KeyEvent.escape),
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
27
|
|
|
@@ -124,7 +124,10 @@ describe("runQuestionnaire", () => {
|
|
|
124
124
|
const harness = yield* run(
|
|
125
125
|
[
|
|
126
126
|
question({
|
|
127
|
-
options: [
|
|
127
|
+
options: [
|
|
128
|
+
Option.make({ label: "Yes", recommended: true }),
|
|
129
|
+
Option.make({ label: "No" }),
|
|
130
|
+
],
|
|
128
131
|
}),
|
|
129
132
|
],
|
|
130
133
|
[KeyEvent.enter],
|
package/src/core/flow.ts
CHANGED
|
@@ -32,7 +32,7 @@ export const runQuestionnaire = Effect.fn("inquiry.core.runQuestionnaire")(funct
|
|
|
32
32
|
yield* renderer.present(state);
|
|
33
33
|
|
|
34
34
|
while (true) {
|
|
35
|
-
const key = yield* keyboard.nextKey
|
|
35
|
+
const key = yield* keyboard.nextKey;
|
|
36
36
|
const previousEditor = state.editor;
|
|
37
37
|
const stepped = step(state, key);
|
|
38
38
|
state = stepped.state;
|
|
@@ -47,7 +47,7 @@ export const runQuestionnaire = Effect.fn("inquiry.core.runQuestionnaire")(funct
|
|
|
47
47
|
const suggestion = state.suggestions;
|
|
48
48
|
const item =
|
|
49
49
|
suggestion?.items[Math.min(suggestion.selectedIndex, suggestion.items.length - 1)];
|
|
50
|
-
if (suggestion && item) {
|
|
50
|
+
if (suggestion !== null && item !== undefined) {
|
|
51
51
|
const applied = yield* fileSuggestions.apply(
|
|
52
52
|
state.editor.text,
|
|
53
53
|
state.editor.cursor,
|
|
@@ -87,7 +87,11 @@ const refreshSuggestions = Effect.fn("inquiry.core.refreshSuggestions")(function
|
|
|
87
87
|
fileSuggestions: FileSuggestions["Service"],
|
|
88
88
|
force: boolean,
|
|
89
89
|
): Effect.fn.Return<MachineState> {
|
|
90
|
-
if (
|
|
90
|
+
if (
|
|
91
|
+
!force &&
|
|
92
|
+
state.suggestions === null &&
|
|
93
|
+
!isAtCompletionContext(textBeforeCursor(state.editor))
|
|
94
|
+
) {
|
|
91
95
|
return state;
|
|
92
96
|
}
|
|
93
97
|
const result = yield* fileSuggestions.query(state.editor.text, state.editor.cursor, force);
|
|
@@ -23,7 +23,7 @@ describe("KeyEvent", () => {
|
|
|
23
23
|
expect(new Set(events).size).toBe(13); // distinct instances
|
|
24
24
|
// each singleton survives a decode round-trip
|
|
25
25
|
for (const event of events) {
|
|
26
|
-
expect(Schema.
|
|
26
|
+
expect(Schema.decodeSync(KeyEventSchema)(event)).toEqual(event);
|
|
27
27
|
}
|
|
28
28
|
});
|
|
29
29
|
|
|
@@ -32,8 +32,8 @@ describe("KeyEvent", () => {
|
|
|
32
32
|
const pasted = KeyEvent.paste("hello\nworld");
|
|
33
33
|
expect(typed).toEqual({ _tag: "char", char: "a" });
|
|
34
34
|
expect(pasted).toEqual({ _tag: "paste", text: "hello\nworld" });
|
|
35
|
-
expect(Schema.
|
|
36
|
-
expect(Schema.
|
|
35
|
+
expect(Schema.decodeSync(KeyEventSchema)(typed)).toEqual(typed);
|
|
36
|
+
expect(Schema.decodeSync(KeyEventSchema)(pasted)).toEqual(pasted);
|
|
37
37
|
});
|
|
38
38
|
|
|
39
39
|
it("rejects unknown tags and missing payloads", () => {
|
package/src/core/keyboard.ts
CHANGED
|
@@ -30,7 +30,7 @@ export const KeyEventSchema = Schema.TaggedUnion({
|
|
|
30
30
|
paste: { text: Schema.String },
|
|
31
31
|
});
|
|
32
32
|
|
|
33
|
-
export type KeyEvent =
|
|
33
|
+
export type KeyEvent = typeof KeyEventSchema.Type;
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* Canonical event instances, plus factories for the data-carrying variants.
|
package/src/core/machine.test.ts
CHANGED
|
@@ -15,11 +15,11 @@ import {
|
|
|
15
15
|
} from "./machine.ts";
|
|
16
16
|
|
|
17
17
|
const question = (over: Partial<QuestionType> = {}): QuestionType =>
|
|
18
|
-
|
|
18
|
+
Question.make({
|
|
19
19
|
id: "q1",
|
|
20
20
|
label: "Q1",
|
|
21
21
|
prompt: "Pick one?",
|
|
22
|
-
options: [
|
|
22
|
+
options: [Option.make({ label: "Yes" }), Option.make({ label: "No" })],
|
|
23
23
|
allowOther: true,
|
|
24
24
|
multiple: false,
|
|
25
25
|
...over,
|
|
@@ -261,7 +261,7 @@ describe("multi question", () => {
|
|
|
261
261
|
it("carries the recommendation on every option row and false on 'Type something.'", () => {
|
|
262
262
|
const state = initialMachineState([
|
|
263
263
|
question({
|
|
264
|
-
options: [
|
|
264
|
+
options: [Option.make({ label: "Yes", recommended: true }), Option.make({ label: "No" })],
|
|
265
265
|
}),
|
|
266
266
|
]);
|
|
267
267
|
expect(currentOptions(state).map((o) => o.recommended)).toEqual([true, false, false]);
|
|
@@ -361,7 +361,7 @@ describe("multi-select question", () => {
|
|
|
361
361
|
const state = initialMachineState([
|
|
362
362
|
question({
|
|
363
363
|
multiple: true,
|
|
364
|
-
options: [
|
|
364
|
+
options: [Option.make({ label: "Yes", recommended: true }), Option.make({ label: "No" })],
|
|
365
365
|
}),
|
|
366
366
|
]);
|
|
367
367
|
const entries = multiEntries(state);
|
package/src/core/machine.ts
CHANGED
|
@@ -63,7 +63,7 @@ export type MultiEntry =
|
|
|
63
63
|
*/
|
|
64
64
|
export const multiEntries = (state: MachineState): readonly MultiEntry[] => {
|
|
65
65
|
const question = state.questions[state.currentTab];
|
|
66
|
-
if (
|
|
66
|
+
if (question === undefined || question.multiple !== true) return [];
|
|
67
67
|
const answers = state.answers.get(question.id) ?? [];
|
|
68
68
|
const entries: MultiEntry[] = question.options.map((o, i) => ({
|
|
69
69
|
kind: "option",
|
|
@@ -82,7 +82,7 @@ export const multiEntries = (state: MachineState): readonly MultiEntry[] => {
|
|
|
82
82
|
/** Whether a multi-select checkbox row is currently selected. */
|
|
83
83
|
export const isMultiEntrySelected = (state: MachineState, entry: MultiEntry): boolean => {
|
|
84
84
|
const question = state.questions[state.currentTab];
|
|
85
|
-
if (
|
|
85
|
+
if (question === undefined) return false;
|
|
86
86
|
const answers = state.answers.get(question.id) ?? [];
|
|
87
87
|
if (entry.kind === "option") return isOptionSelected(answers, entry.index);
|
|
88
88
|
return false;
|
|
@@ -146,10 +146,10 @@ export const optionsForTab = (
|
|
|
146
146
|
tab: number,
|
|
147
147
|
): readonly OptionChoice[] => {
|
|
148
148
|
const question = questions[tab];
|
|
149
|
-
if (
|
|
149
|
+
if (question === undefined) return [];
|
|
150
150
|
const options: OptionChoice[] = question.options.map((o) => ({
|
|
151
151
|
label: o.label,
|
|
152
|
-
description: o.description,
|
|
152
|
+
...(o.description === undefined ? {} : { description: o.description }),
|
|
153
153
|
recommended: o.recommended,
|
|
154
154
|
isOther: false,
|
|
155
155
|
}));
|
|
@@ -230,11 +230,13 @@ const stepInOptions = (state: MachineState, event: KeyEvent): StepResult => {
|
|
|
230
230
|
up: () => (onQuestion ? moveSelection(state, -1) : ignore(state)),
|
|
231
231
|
down: () => (onQuestion ? moveSelection(state, 1) : ignore(state)),
|
|
232
232
|
// Enter confirms the choice: a single-select question records and advances;
|
|
233
|
-
// 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.
|
|
234
236
|
enter: () =>
|
|
235
237
|
!onQuestion
|
|
236
238
|
? maybeSubmit(state)
|
|
237
|
-
: question?.multiple
|
|
239
|
+
: question?.multiple === true
|
|
238
240
|
? confirmMultiple(state, question)
|
|
239
241
|
: chooseOption(state),
|
|
240
242
|
escape: () => complete(state, true),
|
|
@@ -243,7 +245,7 @@ const stepInOptions = (state: MachineState, event: KeyEvent): StepResult => {
|
|
|
243
245
|
// ignored while navigating options.
|
|
244
246
|
char: (event) =>
|
|
245
247
|
onQuestion && event.char === " "
|
|
246
|
-
? question?.multiple
|
|
248
|
+
? question?.multiple === true
|
|
247
249
|
? toggleMultiEntry(state, question)
|
|
248
250
|
: chooseOption(state)
|
|
249
251
|
: ignore(state),
|
|
@@ -278,10 +280,10 @@ const enterTab = (state: MachineState): MachineState => ({
|
|
|
278
280
|
/** Option index that reflects the existing answer on the current tab, if any. */
|
|
279
281
|
const answeredOptionIndex = (state: MachineState): number | undefined => {
|
|
280
282
|
const question = state.questions[state.currentTab];
|
|
281
|
-
if (
|
|
283
|
+
if (question === undefined) return undefined;
|
|
282
284
|
if (question.multiple) return undefined; // no single "current answer" to highlight
|
|
283
285
|
const answer = state.answers.get(question.id)?.[0];
|
|
284
|
-
if (
|
|
286
|
+
if (answer === undefined) return undefined;
|
|
285
287
|
const options = optionsForTab(state.questions, state.currentTab);
|
|
286
288
|
if (answer.wasCustom) return options.length - 1; // the "Type something" entry
|
|
287
289
|
if (answer.index === undefined) return undefined;
|
|
@@ -291,7 +293,7 @@ const answeredOptionIndex = (state: MachineState): number | undefined => {
|
|
|
291
293
|
/** Number of rows the user can navigate on the active tab. */
|
|
292
294
|
const navigableCount = (state: MachineState): number => {
|
|
293
295
|
const question = state.questions[state.currentTab];
|
|
294
|
-
return question?.multiple ? multiEntries(state).length : currentOptions(state).length;
|
|
296
|
+
return (question?.multiple ?? false) ? multiEntries(state).length : currentOptions(state).length;
|
|
295
297
|
};
|
|
296
298
|
|
|
297
299
|
const moveSelection = (state: MachineState, dir: -1 | 1): StepResult => {
|
|
@@ -307,16 +309,16 @@ const maybeSubmit = (state: MachineState): StepResult =>
|
|
|
307
309
|
/** Enter on a question tab: pick the highlighted option, or open the editor. */
|
|
308
310
|
const chooseOption = (state: MachineState): StepResult => {
|
|
309
311
|
const question = state.questions[state.currentTab];
|
|
310
|
-
if (
|
|
312
|
+
if (question === undefined) return ignore(state);
|
|
311
313
|
const option = currentOptions(state)[state.optionIndex];
|
|
312
|
-
if (
|
|
314
|
+
if (option === undefined) return ignore(state);
|
|
313
315
|
if (option.isOther) {
|
|
314
316
|
const existing = state.answers.get(question.id)?.[0];
|
|
315
317
|
return {
|
|
316
318
|
state: {
|
|
317
319
|
...state,
|
|
318
320
|
inputMode: true,
|
|
319
|
-
editor: existing?.wasCustom ? editorFromText(existing.label) : emptyEditor(),
|
|
321
|
+
editor: existing?.wasCustom === true ? editorFromText(existing.label) : emptyEditor(),
|
|
320
322
|
suggestions: null,
|
|
321
323
|
},
|
|
322
324
|
action: { type: "none" },
|
|
@@ -330,7 +332,7 @@ const chooseOption = (state: MachineState): StepResult => {
|
|
|
330
332
|
/** Space on a multi-select row: toggle an option, remove a chip, or open the editor. */
|
|
331
333
|
const toggleMultiEntry = (state: MachineState, question: Question): StepResult => {
|
|
332
334
|
const entry = multiEntries(state)[state.optionIndex];
|
|
333
|
-
if (
|
|
335
|
+
if (entry === undefined) return ignore(state);
|
|
334
336
|
switch (entry.kind) {
|
|
335
337
|
case "option":
|
|
336
338
|
return { state: toggleOption(state, question, entry.index), action: { type: "none" } };
|
|
@@ -358,7 +360,9 @@ const confirmMultiple = (state: MachineState, question: Question): StepResult =>
|
|
|
358
360
|
};
|
|
359
361
|
|
|
360
362
|
const stepInEditor = (state: MachineState, event: KeyEvent): StepResult =>
|
|
361
|
-
state.suggestions
|
|
363
|
+
state.suggestions !== null
|
|
364
|
+
? stepInEditorWithSuggestions(state, event)
|
|
365
|
+
: stepInEditorPlain(state, event);
|
|
362
366
|
|
|
363
367
|
/**
|
|
364
368
|
* Key handling while the completion popup is open. Navigation and selection
|
|
@@ -433,7 +437,7 @@ const exitEditor = (state: MachineState): StepResult => ({
|
|
|
433
437
|
/** Enter in the editor: submit the typed answer. */
|
|
434
438
|
const submitEditor = (state: MachineState): StepResult => {
|
|
435
439
|
const question = state.questions[state.currentTab];
|
|
436
|
-
if (
|
|
440
|
+
if (question === undefined) return ignore(state);
|
|
437
441
|
const label = state.editor.text.trim() || "(no response)";
|
|
438
442
|
const closed = { inputMode: false, editor: emptyEditor(), suggestions: null } as const;
|
|
439
443
|
if (question.multiple) {
|
|
@@ -464,7 +468,7 @@ const toggleOption = (
|
|
|
464
468
|
if (next.length === 0) answers.delete(question.id);
|
|
465
469
|
else answers.set(question.id, next);
|
|
466
470
|
} else {
|
|
467
|
-
const answer =
|
|
471
|
+
const answer = Answer.make({
|
|
468
472
|
id: question.id,
|
|
469
473
|
label: question.options[optionIndex]!.label,
|
|
470
474
|
wasCustom: false,
|
|
@@ -487,7 +491,7 @@ const removeCustom = (state: MachineState, question: Question, label: string): M
|
|
|
487
491
|
/** Append a newly typed custom alternative to the question's answers. */
|
|
488
492
|
const addCustom = (state: MachineState, question: Question, label: string): MachineState => {
|
|
489
493
|
const answers = new Map(state.answers);
|
|
490
|
-
const answer =
|
|
494
|
+
const answer = Answer.make({ id: question.id, label, wasCustom: true });
|
|
491
495
|
answers.set(question.id, [...answersFor(state, question), answer]);
|
|
492
496
|
return { ...state, answers };
|
|
493
497
|
};
|
|
@@ -503,8 +507,8 @@ const withAnswer = (
|
|
|
503
507
|
answers.set(
|
|
504
508
|
question.id,
|
|
505
509
|
index === undefined
|
|
506
|
-
? [
|
|
507
|
-
: [
|
|
510
|
+
? [Answer.make({ id: question.id, label, wasCustom })]
|
|
511
|
+
: [Answer.make({ id: question.id, label, wasCustom, index })],
|
|
508
512
|
);
|
|
509
513
|
return { ...state, answers };
|
|
510
514
|
};
|
|
@@ -520,7 +524,7 @@ const complete = (state: MachineState, cancelled: boolean): StepResult => ({
|
|
|
520
524
|
state,
|
|
521
525
|
action: {
|
|
522
526
|
type: "complete",
|
|
523
|
-
result:
|
|
527
|
+
result: QuestionResult.make({
|
|
524
528
|
questions: [...state.questions],
|
|
525
529
|
answers: [...state.answers.values()].flatMap((group) => group),
|
|
526
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,
|
|
@@ -88,7 +88,10 @@ describe("buildScene: single question", () => {
|
|
|
88
88
|
|
|
89
89
|
it("shows descriptions indented under options", () => {
|
|
90
90
|
const q = question({
|
|
91
|
-
options: [
|
|
91
|
+
options: [
|
|
92
|
+
Option.make({ label: "Yes", description: "Go ahead" }),
|
|
93
|
+
Option.make({ label: "No" }),
|
|
94
|
+
],
|
|
92
95
|
});
|
|
93
96
|
const scene = buildScene(initialMachineState([q]), 40);
|
|
94
97
|
const lines = plainLines(scene);
|
|
@@ -108,7 +111,7 @@ describe("buildScene: single question", () => {
|
|
|
108
111
|
describe("buildScene: recommended options", () => {
|
|
109
112
|
const recommended = (over: Partial<QuestionType> = {}): QuestionType =>
|
|
110
113
|
question({
|
|
111
|
-
options: [
|
|
114
|
+
options: [Option.make({ label: "Yes", recommended: true }), Option.make({ label: "No" })],
|
|
112
115
|
...over,
|
|
113
116
|
});
|
|
114
117
|
|
|
@@ -139,7 +142,7 @@ describe("buildScene: recommended options", () => {
|
|
|
139
142
|
|
|
140
143
|
it("wraps a marked long label past the marker", () => {
|
|
141
144
|
const q = question({
|
|
142
|
-
options: [
|
|
145
|
+
options: [Option.make({ label: "Document the deployment guard", recommended: true })],
|
|
143
146
|
});
|
|
144
147
|
const lines = plainLines(buildScene(initialMachineState([q]), 26));
|
|
145
148
|
expect(lines[3]).toBe(boxed("→ ✓ 1. Document the", 26));
|
|
@@ -209,7 +212,10 @@ describe("buildScene: multi question", () => {
|
|
|
209
212
|
|
|
210
213
|
it("indents the custom answer subtitle like option descriptions", () => {
|
|
211
214
|
const q = question({
|
|
212
|
-
options: [
|
|
215
|
+
options: [
|
|
216
|
+
Option.make({ label: "Yes", description: "Go ahead" }),
|
|
217
|
+
Option.make({ label: "No" }),
|
|
218
|
+
],
|
|
213
219
|
});
|
|
214
220
|
let state = initialMachineState([q, question({ id: "q2", label: "Q2", prompt: "Second?" })]);
|
|
215
221
|
state = step(state, KeyEvent.down).state;
|