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

pendingUserInput.test.ts

367 lines10.3 KB

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