Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 buildPendingUserInputAnswers,
5 carryDisplacedCustomAnswerIntoPrompt,
6 countAnsweredPendingUserInputQuestions,
7 derivePendingUserInputProgress,
8 findFirstUnansweredPendingUserInputQuestionIndex,
9 resolvePendingUserInputAnswer,
10 setPendingUserInputCustomAnswer,
11 togglePendingUserInputOptionSelection,
12} from "./pendingUserInput";
13
14const singleSelectQuestion = {
15 id: "scope",
16 header: "Scope",
17 question: "What should the plan target first?",
18 options: [
19 {
20 label: "Orchestration-first",
21 description: "Focus on orchestration first",
22 },
23 ],
24 multiSelect: false,
25} as const;
26
27const multiSelectQuestion = {
28 id: "areas",
29 header: "Areas",
30 question: "Which areas should this change cover?",
31 options: [
32 {
33 label: "Server",
34 description: "Server",
35 },
36 {
37 label: "Web",
38 description: "Web",
39 },
40 ],
41 multiSelect: true,
42} as const;
43
44const nativeChoiceQuestion = {
45 id: "result",
46 header: "Result",
47 question: "Which result should be used?",
48 options: [
49 { value: " first\t", label: "Result", description: "First result" },
50 { value: "second", label: "Result", description: "Second result" },
51 ],
52 allowCustomAnswer: false,
53 multiSelect: false,
54} as const;
55
56describe("resolvePendingUserInputAnswer", () => {
57 it("preserves exact editor text, including a deliberately cleared answer", () => {
58 const question = { ...singleSelectQuestion, initialAnswer: " Proposed message\n" };
59 expect(resolvePendingUserInputAnswer(question, { customAnswer: question.initialAnswer })).toBe(
60 question.initialAnswer,
61 );
62 expect(resolvePendingUserInputAnswer(question, { customAnswer: " Edited message\n\n" })).toBe(
63 " Edited message\n\n",
64 );
65 expect(resolvePendingUserInputAnswer(question, { customAnswer: "" })).toBe("");
66 expect(resolvePendingUserInputAnswer(question, { customAnswer: " \n" })).toBe(" \n");
67 expect(
68 setPendingUserInputCustomAnswer(
69 { selectedOptionValues: ["Orchestration-first"] },
70 "",
71 question,
72 ),
73 ).toEqual({ customAnswer: "" });
74 const questions = [question, { ...singleSelectQuestion, id: "follow-up" }];
75 const drafts = { scope: { customAnswer: "" } };
76 expect(findFirstUnansweredPendingUserInputQuestionIndex(questions, drafts)).toBe(1);
77 expect(derivePendingUserInputProgress([question], drafts, 0)).toMatchObject({
78 customAnswer: "",
79 resolvedAnswer: "",
80 canAdvance: true,
81 isComplete: true,
82 });
83 });
84
85 it("prefers a custom answer over selected options", () => {
86 expect(
87 resolvePendingUserInputAnswer(singleSelectQuestion, {
88 selectedOptionValues: ["Orchestration-first"],
89 customAnswer: "Keep the existing envelope for one release",
90 }),
91 ).toBe("Keep the existing envelope for one release");
92 });
93
94 it("falls back to the selected option for single-select questions", () => {
95 expect(
96 resolvePendingUserInputAnswer(singleSelectQuestion, {
97 selectedOptionValues: ["Orchestration-first"],
98 }),
99 ).toBe("Orchestration-first");
100 });
101
102 it("returns all selected labels for multi-select questions", () => {
103 expect(
104 resolvePendingUserInputAnswer(multiSelectQuestion, {
105 selectedOptionValues: ["Server", "Web"],
106 }),
107 ).toEqual(["Server", "Web"]);
108 });
109
110 it("clears the preset selection when a custom answer is entered", () => {
111 expect(
112 setPendingUserInputCustomAnswer(
113 {
114 selectedOptionValues: ["Server", "Web"],
115 },
116 "doesn't matter",
117 ),
118 ).toEqual({
119 customAnswer: "doesn't matter",
120 });
121 });
122
123 it("does not replace a required choice with a custom answer", () => {
124 expect(
125 resolvePendingUserInputAnswer(nativeChoiceQuestion, {
126 selectedOptionValues: ["second"],
127 customAnswer: "Use another result",
128 }),
129 ).toBe("second");
130 });
131
132 it("does not submit labels or unknown values when an option has a value", () => {
133 expect(
134 resolvePendingUserInputAnswer(nativeChoiceQuestion, {
135 selectedOptionValues: ["Result", "unknown"],
136 }),
137 ).toBeNull();
138 });
139});
140
141describe("togglePendingUserInputOptionSelection", () => {
142 it("toggles options for multi-select questions", () => {
143 expect(togglePendingUserInputOptionSelection(multiSelectQuestion, undefined, "Server")).toEqual(
144 {
145 customAnswer: "",
146 selectedOptionValues: ["Server"],
147 },
148 );
149
150 expect(
151 togglePendingUserInputOptionSelection(
152 multiSelectQuestion,
153 {
154 selectedOptionValues: ["Server", "Web"],
155 },
156 "Server",
157 ),
158 ).toEqual({
159 customAnswer: "",
160 selectedOptionValues: ["Web"],
161 });
162 });
163
164 it("selects and removes options with the same label independently", () => {
165 const question = { ...nativeChoiceQuestion, multiSelect: true };
166 const firstSelected = togglePendingUserInputOptionSelection(question, undefined, " first\t");
167 const bothSelected = togglePendingUserInputOptionSelection(question, firstSelected, "second");
168
169 expect(buildPendingUserInputAnswers([question], { result: bothSelected })).toEqual({
170 result: [" first\t", "second"],
171 });
172
173 const secondSelected = togglePendingUserInputOptionSelection(
174 question,
175 bothSelected,
176 " first\t",
177 );
178 expect(buildPendingUserInputAnswers([question], { result: secondSelected })).toEqual({
179 result: ["second"],
180 });
181 });
182});
183
184describe("buildPendingUserInputAnswers", () => {
185 it("returns a canonical answer map for complete prompts", () => {
186 expect(
187 buildPendingUserInputAnswers(
188 [
189 singleSelectQuestion,
190 {
191 id: "compat",
192 header: "Compat",
193 question: "How strict should compatibility be?",
194 options: [
195 {
196 label: "Keep current envelope",
197 description: "Preserve current wire format",
198 },
199 ],
200 multiSelect: false,
201 },
202 ],
203 {
204 scope: {
205 selectedOptionValues: ["Orchestration-first"],
206 },
207 compat: {
208 customAnswer: "Keep the current envelope for one release window",
209 },
210 },
211 ),
212 ).toEqual({
213 scope: "Orchestration-first",
214 compat: "Keep the current envelope for one release window",
215 });
216 });
217
218 it("returns arrays for answered multi-select prompts", () => {
219 expect(
220 buildPendingUserInputAnswers([multiSelectQuestion], {
221 areas: {
222 selectedOptionValues: ["Server", "Web"],
223 },
224 }),
225 ).toEqual({
226 areas: ["Server", "Web"],
227 });
228 });
229
230 it("returns null when any question is unanswered", () => {
231 expect(buildPendingUserInputAnswers([singleSelectQuestion], {})).toBeNull();
232 });
233
234 it.each([" first\t", ""])("preserves the exact selected option value %j", (value) => {
235 const question = {
236 ...nativeChoiceQuestion,
237 options: [{ ...nativeChoiceQuestion.options[0], value }, nativeChoiceQuestion.options[1]],
238 };
239 const draft = togglePendingUserInputOptionSelection(question, undefined, value);
240
241 expect(buildPendingUserInputAnswers([question], { result: draft })).toEqual({ result: value });
242 expect(derivePendingUserInputProgress([question], { result: draft }, 0)).toMatchObject({
243 selectedOptionValues: [value],
244 answeredQuestionCount: 1,
245 canAdvance: true,
246 isComplete: true,
247 });
248 });
249});
250
251describe("pending user input question progress", () => {
252 const questions = [
253 singleSelectQuestion,
254 {
255 id: "compat",
256 header: "Compat",
257 question: "How strict should compatibility be?",
258 options: [
259 {
260 label: "Keep current envelope",
261 description: "Preserve current wire format",
262 },
263 ],
264 multiSelect: false,
265 },
266 ] as const;
267
268 it("counts only answered questions", () => {
269 expect(
270 countAnsweredPendingUserInputQuestions(questions, {
271 scope: {
272 selectedOptionValues: ["Orchestration-first"],
273 },
274 }),
275 ).toBe(1);
276 });
277
278 it("finds the first unanswered question", () => {
279 expect(
280 findFirstUnansweredPendingUserInputQuestionIndex(questions, {
281 scope: {
282 selectedOptionValues: ["Orchestration-first"],
283 },
284 }),
285 ).toBe(1);
286 });
287
288 it("returns the last question index when all answers are complete", () => {
289 expect(
290 findFirstUnansweredPendingUserInputQuestionIndex(questions, {
291 scope: {
292 selectedOptionValues: ["Orchestration-first"],
293 },
294 compat: {
295 customAnswer: "Keep it for one release window",
296 },
297 }),
298 ).toBe(1);
299 });
300
301 it("derives the active question and advancement state", () => {
302 expect(
303 derivePendingUserInputProgress(
304 questions,
305 {
306 scope: {
307 selectedOptionValues: ["Orchestration-first"],
308 },
309 },
310 0,
311 ),
312 ).toMatchObject({
313 questionIndex: 0,
314 activeQuestion: questions[0],
315 selectedOptionValues: ["Orchestration-first"],
316 customAnswer: "",
317 resolvedAnswer: "Orchestration-first",
318 answeredQuestionCount: 1,
319 isLastQuestion: false,
320 isComplete: false,
321 canAdvance: true,
322 });
323 });
324
325 it("treats multi-select questions as answered when they have selected options", () => {
326 expect(
327 derivePendingUserInputProgress(
328 [multiSelectQuestion],
329 {
330 areas: {
331 selectedOptionValues: ["Server", "Web"],
332 },
333 },
334 0,
335 ),
336 ).toMatchObject({
337 selectedOptionValues: ["Server", "Web"],
338 resolvedAnswer: ["Server", "Web"],
339 canAdvance: true,
340 isComplete: true,
341 });
342 });
343
344 it("requires an option when custom answers are disabled", () => {
345 const drafts = { result: { customAnswer: "Use another result" } };
346
347 expect(buildPendingUserInputAnswers([nativeChoiceQuestion], drafts)).toBeNull();
348 expect(derivePendingUserInputProgress([nativeChoiceQuestion], drafts, 0)).toMatchObject({
349 customAnswer: "",
350 usingCustomAnswer: false,
351 resolvedAnswer: null,
352 answeredQuestionCount: 0,
353 canAdvance: false,
354 isComplete: false,
355 });
356 });
357});
358
359it("accepts attachment-only answers after every upload finishes", () => {
360 const questions = [
361 { id: "spec", header: "Spec", question: "Provide a spec", options: [], multiSelect: false },
362 ];
363 expect(buildPendingUserInputAnswers(questions, { spec: { attachmentCount: 1 } })).toEqual({
364 spec: "",
365 });
366 expect(
367 buildPendingUserInputAnswers(questions, {
368 spec: { attachmentCount: 1, attachmentsBlocked: true },
369 }),
370 ).toBeNull();
371 expect(
372 buildPendingUserInputAnswers([{ ...questions[0]!, allowCustomAnswer: false }], {
373 spec: { attachmentCount: 1 },
374 }),
375 ).toBeNull();
376});
377
378describe("carryDisplacedCustomAnswerIntoPrompt", () => {
379 it("keeps the thread draft when nothing was typed into the answer", () => {
380 expect(carryDisplacedCustomAnswerIntoPrompt("draft", undefined)).toBe("draft");
381 expect(carryDisplacedCustomAnswerIntoPrompt("draft", " ")).toBe("draft");
382 });
383
384 it("moves the typed answer into an empty thread draft", () => {
385 expect(carryDisplacedCustomAnswerIntoPrompt("", "also rename the flag ")).toBe(
386 "also rename the flag",
387 );
388 });
389
390 it("appends the typed answer after an existing thread draft", () => {
391 expect(carryDisplacedCustomAnswerIntoPrompt("first half\n", "second half")).toBe(
392 "first half\n\nsecond half",
393 );
394 });
395});