Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pendingUserInput.test.ts

347 lines9.5 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 buildPendingUserInputAnswers,
5 countAnsweredPendingUserInputQuestions,
6 derivePendingUserInputProgress,
7 findFirstUnansweredPendingUserInputQuestionIndex,
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("finds the first unanswered question", () => {
250 expect(
251 findFirstUnansweredPendingUserInputQuestionIndex(questions, {
252 scope: {
253 selectedOptionValues: ["Orchestration-first"],
254 },
255 }),
256 ).toBe(1);
257 });
258​
259 it("returns the last question index when all answers are complete", () => {
260 expect(
261 findFirstUnansweredPendingUserInputQuestionIndex(questions, {
262 scope: {
263 selectedOptionValues: ["Orchestration-first"],
264 },
265 compat: {
266 customAnswer: "Keep it for one release window",
267 },
268 }),
269 ).toBe(1);
270 });
271​
272 it("derives the active question and advancement state", () => {
273 expect(
274 derivePendingUserInputProgress(
275 questions,
276 {
277 scope: {
278 selectedOptionValues: ["Orchestration-first"],
279 },
280 },
281 0,
282 ),
283 ).toMatchObject({
284 questionIndex: 0,
285 activeQuestion: questions[0],
286 selectedOptionValues: ["Orchestration-first"],
287 customAnswer: "",
288 resolvedAnswer: "Orchestration-first",
289 answeredQuestionCount: 1,
290 isLastQuestion: false,
291 isComplete: false,
292 canAdvance: true,
293 });
294 });
295​
296 it("treats multi-select questions as answered when they have selected options", () => {
297 expect(
298 derivePendingUserInputProgress(
299 [multiSelectQuestion],
300 {
301 areas: {
302 selectedOptionValues: ["Server", "Web"],
303 },
304 },
305 0,
306 ),
307 ).toMatchObject({
308 selectedOptionValues: ["Server", "Web"],
309 resolvedAnswer: ["Server", "Web"],
310 canAdvance: true,
311 isComplete: true,
312 });
313 });
314​
315 it("requires an option when custom answers are disabled", () => {
316 const drafts = { result: { customAnswer: "Use another result" } };
317​
318 expect(buildPendingUserInputAnswers([nativeChoiceQuestion], drafts)).toBeNull();
319 expect(derivePendingUserInputProgress([nativeChoiceQuestion], drafts, 0)).toMatchObject({
320 customAnswer: "",
321 usingCustomAnswer: false,
322 resolvedAnswer: null,
323 answeredQuestionCount: 0,
324 canAdvance: false,
325 isComplete: false,
326 });
327 });
328});
329​
330it("accepts attachment-only answers after every upload finishes", () => {
331 const questions = [
332 { id: "spec", header: "Spec", question: "Provide a spec", options: [], multiSelect: false },
333 ];
334 expect(buildPendingUserInputAnswers(questions, { spec: { attachmentCount: 1 } })).toEqual({
335 spec: "",
336 });
337 expect(
338 buildPendingUserInputAnswers(questions, {
339 spec: { attachmentCount: 1, attachmentsBlocked: true },
340 }),
341 ).toBeNull();
342 expect(
343 buildPendingUserInputAnswers([{ ...questions[0]!, allowCustomAnswer: false }], {
344 spec: { attachmentCount: 1 },
345 }),
346 ).toBeNull();
347});