Open History to see the last commit
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
43describe("resolvePendingUserInputAnswer", () => {
44 it("prefers a custom answer over selected options", () => {
45 expect(
46 resolvePendingUserInputAnswer(singleSelectQuestion, {
47 selectedOptionLabels: ["Orchestration-first"],
48 customAnswer: "Keep the existing envelope for one release",
49 }),
50 ).toBe("Keep the existing envelope for one release");
51 });
52
53 it("falls back to the selected option for single-select questions", () => {
54 expect(
55 resolvePendingUserInputAnswer(singleSelectQuestion, {
56 selectedOptionLabels: ["Orchestration-first"],
57 }),
58 ).toBe("Orchestration-first");
59 });
60
61 it("returns all selected labels for multi-select questions", () => {
62 expect(
63 resolvePendingUserInputAnswer(multiSelectQuestion, {
64 selectedOptionLabels: ["Server", "Web"],
65 }),
66 ).toEqual(["Server", "Web"]);
67 });
68
69 it("clears the preset selection when a custom answer is entered", () => {
70 expect(
71 setPendingUserInputCustomAnswer(
72 {
73 selectedOptionLabels: ["Server", "Web"],
74 },
75 "doesn't matter",
76 ),
77 ).toEqual({
78 customAnswer: "doesn't matter",
79 });
80 });
81});
82
83describe("togglePendingUserInputOptionSelection", () => {
84 it("toggles options for multi-select questions", () => {
85 expect(togglePendingUserInputOptionSelection(multiSelectQuestion, undefined, "Server")).toEqual(
86 {
87 customAnswer: "",
88 selectedOptionLabels: ["Server"],
89 },
90 );
91
92 expect(
93 togglePendingUserInputOptionSelection(
94 multiSelectQuestion,
95 {
96 selectedOptionLabels: ["Server", "Web"],
97 },
98 "Server",
99 ),
100 ).toEqual({
101 customAnswer: "",
102 selectedOptionLabels: ["Web"],
103 });
104 });
105});
106
107describe("buildPendingUserInputAnswers", () => {
108 it("returns a canonical answer map for complete prompts", () => {
109 expect(
110 buildPendingUserInputAnswers(
111 [
112 singleSelectQuestion,
113 {
114 id: "compat",
115 header: "Compat",
116 question: "How strict should compatibility be?",
117 options: [
118 {
119 label: "Keep current envelope",
120 description: "Preserve current wire format",
121 },
122 ],
123 multiSelect: false,
124 },
125 ],
126 {
127 scope: {
128 selectedOptionLabels: ["Orchestration-first"],
129 },
130 compat: {
131 customAnswer: "Keep the current envelope for one release window",
132 },
133 },
134 ),
135 ).toEqual({
136 scope: "Orchestration-first",
137 compat: "Keep the current envelope for one release window",
138 });
139 });
140
141 it("returns arrays for answered multi-select prompts", () => {
142 expect(
143 buildPendingUserInputAnswers([multiSelectQuestion], {
144 areas: {
145 selectedOptionLabels: ["Server", "Web"],
146 },
147 }),
148 ).toEqual({
149 areas: ["Server", "Web"],
150 });
151 });
152
153 it("returns null when any question is unanswered", () => {
154 expect(buildPendingUserInputAnswers([singleSelectQuestion], {})).toBeNull();
155 });
156});
157
158describe("pending user input question progress", () => {
159 const questions = [
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 ] as const;
174
175 it("counts only answered questions", () => {
176 expect(
177 countAnsweredPendingUserInputQuestions(questions, {
178 scope: {
179 selectedOptionLabels: ["Orchestration-first"],
180 },
181 }),
182 ).toBe(1);
183 });
184
185 it("finds the first unanswered question", () => {
186 expect(
187 findFirstUnansweredPendingUserInputQuestionIndex(questions, {
188 scope: {
189 selectedOptionLabels: ["Orchestration-first"],
190 },
191 }),
192 ).toBe(1);
193 });
194
195 it("returns the last question index when all answers are complete", () => {
196 expect(
197 findFirstUnansweredPendingUserInputQuestionIndex(questions, {
198 scope: {
199 selectedOptionLabels: ["Orchestration-first"],
200 },
201 compat: {
202 customAnswer: "Keep it for one release window",
203 },
204 }),
205 ).toBe(1);
206 });
207
208 it("derives the active question and advancement state", () => {
209 expect(
210 derivePendingUserInputProgress(
211 questions,
212 {
213 scope: {
214 selectedOptionLabels: ["Orchestration-first"],
215 },
216 },
217 0,
218 ),
219 ).toMatchObject({
220 questionIndex: 0,
221 activeQuestion: questions[0],
222 selectedOptionLabels: ["Orchestration-first"],
223 customAnswer: "",
224 resolvedAnswer: "Orchestration-first",
225 answeredQuestionCount: 1,
226 isLastQuestion: false,
227 isComplete: false,
228 canAdvance: true,
229 });
230 });
231
232 it("treats multi-select questions as answered when they have selected options", () => {
233 expect(
234 derivePendingUserInputProgress(
235 [multiSelectQuestion],
236 {
237 areas: {
238 selectedOptionLabels: ["Server", "Web"],
239 },
240 },
241 0,
242 ),
243 ).toMatchObject({
244 selectedOptionLabels: ["Server", "Web"],
245 resolvedAnswer: ["Server", "Web"],
246 canAdvance: true,
247 isComplete: true,
248 });
249 });
250});