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