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