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