@ryan_nookpi/pi-extension-ask-user-question 0.3.4 → 0.3.5
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/controller.test.ts +12 -0
- package/controller.ts +5 -1
- package/form-ui.test.ts +26 -0
- package/package.json +1 -1
package/controller.test.ts
CHANGED
|
@@ -66,6 +66,18 @@ describe("ask-user-question/controller", () => {
|
|
|
66
66
|
expect(empty.done).not.toHaveBeenCalled();
|
|
67
67
|
});
|
|
68
68
|
|
|
69
|
+
it("invalidates cached lines when render width changes (terminal resize)", () => {
|
|
70
|
+
const setup = createController([{ id: "text", type: "text", prompt: "Explain" }]);
|
|
71
|
+
const wide = setup.controller.render(70);
|
|
72
|
+
const narrow = setup.controller.render(34);
|
|
73
|
+
expect(narrow).not.toBe(wide);
|
|
74
|
+
for (const line of narrow) {
|
|
75
|
+
expect(line.length).toBeLessThanOrEqual(34);
|
|
76
|
+
}
|
|
77
|
+
const narrowAgain = setup.controller.render(34);
|
|
78
|
+
expect(narrowAgain).toBe(narrow);
|
|
79
|
+
});
|
|
80
|
+
|
|
69
81
|
it("handles text questions, submission, cancellation, and editor fallback submit", () => {
|
|
70
82
|
const first = createController([{ id: "text", type: "text", prompt: "Explain", default: "seed" }]);
|
|
71
83
|
expect(first.editor.getText()).toBe("seed");
|
package/controller.ts
CHANGED
|
@@ -37,6 +37,7 @@ export function createFormController(input: CreateFormControllerInput): FormCont
|
|
|
37
37
|
let otherMode = false;
|
|
38
38
|
let otherQuestionId: string | null = null;
|
|
39
39
|
let cachedLines: string[] | undefined;
|
|
40
|
+
let cachedWidth: number | undefined;
|
|
40
41
|
|
|
41
42
|
function getCurrentQuestion(): NormalizedQuestion | undefined {
|
|
42
43
|
return questions[currentTab];
|
|
@@ -44,6 +45,7 @@ export function createFormController(input: CreateFormControllerInput): FormCont
|
|
|
44
45
|
|
|
45
46
|
function refresh(): void {
|
|
46
47
|
cachedLines = undefined;
|
|
48
|
+
cachedWidth = undefined;
|
|
47
49
|
input.requestRender();
|
|
48
50
|
}
|
|
49
51
|
|
|
@@ -116,7 +118,8 @@ export function createFormController(input: CreateFormControllerInput): FormCont
|
|
|
116
118
|
|
|
117
119
|
return {
|
|
118
120
|
render(width) {
|
|
119
|
-
if (cachedLines) return cachedLines;
|
|
121
|
+
if (cachedLines && cachedWidth === width) return cachedLines;
|
|
122
|
+
cachedWidth = width;
|
|
120
123
|
cachedLines = renderForm({
|
|
121
124
|
title: input.title,
|
|
122
125
|
description: input.description,
|
|
@@ -134,6 +137,7 @@ export function createFormController(input: CreateFormControllerInput): FormCont
|
|
|
134
137
|
},
|
|
135
138
|
invalidate() {
|
|
136
139
|
cachedLines = undefined;
|
|
140
|
+
cachedWidth = undefined;
|
|
137
141
|
},
|
|
138
142
|
handleInput(data) {
|
|
139
143
|
if (otherMode) {
|
package/form-ui.test.ts
CHANGED
|
@@ -66,6 +66,32 @@ describe("ask-user-question/form-ui", () => {
|
|
|
66
66
|
expect(result).toEqual({ title: "Title", questions, answers: [], cancelled: true });
|
|
67
67
|
});
|
|
68
68
|
|
|
69
|
+
it("coerces unexpected native bridge answers into safe form results", async () => {
|
|
70
|
+
const questions = normalizeQuestions([
|
|
71
|
+
{ id: "choice", type: "radio", prompt: "Pick", options: [{ value: "a", label: "A" }] },
|
|
72
|
+
{ id: "single", type: "checkbox", prompt: "Single", options: [{ value: "solo", label: "Solo" }] },
|
|
73
|
+
{ id: "missing", type: "checkbox", prompt: "Missing", options: [{ value: "x", label: "X" }] },
|
|
74
|
+
]);
|
|
75
|
+
const askUserQuestion = vi.fn(async () => ({ choice: 123, single: "solo" }));
|
|
76
|
+
const ctx = {
|
|
77
|
+
hasUI: true,
|
|
78
|
+
ui: { askUserQuestion },
|
|
79
|
+
} as unknown as ExtensionContext;
|
|
80
|
+
|
|
81
|
+
const result = await runAskUserQuestionForm(ctx, {}, questions);
|
|
82
|
+
|
|
83
|
+
expect(result).toEqual({
|
|
84
|
+
title: undefined,
|
|
85
|
+
questions,
|
|
86
|
+
answers: [
|
|
87
|
+
{ id: "choice", type: "radio", value: "", wasCustom: false },
|
|
88
|
+
{ id: "single", type: "checkbox", value: ["solo"], wasCustom: false },
|
|
89
|
+
{ id: "missing", type: "checkbox", value: [], wasCustom: false },
|
|
90
|
+
],
|
|
91
|
+
cancelled: false,
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
69
95
|
it("wires the custom UI factory to the controller", async () => {
|
|
70
96
|
const questions = normalizeQuestions([{ id: "text", type: "text", prompt: "Explain", default: "seed" }]);
|
|
71
97
|
let captured: FormResult | undefined;
|