Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import {
4 buildPendingUserInputAnswers,
5 countAnsweredPendingUserInputQuestions,
6 derivePendingUserInputProgress,
7 findFirstUnansweredPendingUserInputQuestionIndex,
8 resolvePendingUserInputAnswer,
9 setPendingUserInputCustomAnswer,
10} from "./pendingUserInput";
11
12describe("resolvePendingUserInputAnswer", () => {
13 it("prefers a custom answer over a selected option", () => {
14 expect(
15 resolvePendingUserInputAnswer({
16 selectedOptionLabel: "Keep current envelope",
17 customAnswer: "Keep the existing envelope for one release",
18 }),
19 ).toBe("Keep the existing envelope for one release");
20 });
21
22 it("falls back to the selected option", () => {
23 expect(
24 resolvePendingUserInputAnswer({
25 selectedOptionLabel: "Scaffold only",
26 }),
27 ).toBe("Scaffold only");
28 });
29
30 it("clears the preset selection when a custom answer is entered", () => {
31 expect(
32 setPendingUserInputCustomAnswer(
33 {
34 selectedOptionLabel: "Preserve existing tags",
35 },
36 "doesn't matter",
37 ),
38 ).toEqual({
39 selectedOptionLabel: undefined,
40 customAnswer: "doesn't matter",
41 });
42 });
43});
44
45describe("buildPendingUserInputAnswers", () => {
46 it("returns a canonical answer map for complete prompts", () => {
47 expect(
48 buildPendingUserInputAnswers(
49 [
50 {
51 id: "scope",
52 header: "Scope",
53 question: "What should the plan target first?",
54 options: [
55 {
56 label: "Orchestration-first",
57 description: "Focus on orchestration first",
58 },
59 ],
60 },
61 {
62 id: "compat",
63 header: "Compat",
64 question: "How strict should compatibility be?",
65 options: [
66 {
67 label: "Keep current envelope",
68 description: "Preserve current wire format",
69 },
70 ],
71 },
72 ],
73 {
74 scope: {
75 selectedOptionLabel: "Orchestration-first",
76 },
77 compat: {
78 customAnswer: "Keep the current envelope for one release window",
79 },
80 },
81 ),
82 ).toEqual({
83 scope: "Orchestration-first",
84 compat: "Keep the current envelope for one release window",
85 });
86 });
87
88 it("returns null when any question is unanswered", () => {
89 expect(
90 buildPendingUserInputAnswers(
91 [
92 {
93 id: "scope",
94 header: "Scope",
95 question: "What should the plan target first?",
96 options: [
97 {
98 label: "Orchestration-first",
99 description: "Focus on orchestration first",
100 },
101 ],
102 },
103 ],
104 {},
105 ),
106 ).toBeNull();
107 });
108});
109
110describe("pending user input question progress", () => {
111 const questions = [
112 {
113 id: "scope",
114 header: "Scope",
115 question: "What should the plan target first?",
116 options: [
117 {
118 label: "Orchestration-first",
119 description: "Focus on orchestration first",
120 },
121 ],
122 },
123 {
124 id: "compat",
125 header: "Compat",
126 question: "How strict should compatibility be?",
127 options: [
128 {
129 label: "Keep current envelope",
130 description: "Preserve current wire format",
131 },
132 ],
133 },
134 ] as const;
135
136 it("counts only answered questions", () => {
137 expect(
138 countAnsweredPendingUserInputQuestions(questions, {
139 scope: {
140 selectedOptionLabel: "Orchestration-first",
141 },
142 }),
143 ).toBe(1);
144 });
145
146 it("finds the first unanswered question", () => {
147 expect(
148 findFirstUnansweredPendingUserInputQuestionIndex(questions, {
149 scope: {
150 selectedOptionLabel: "Orchestration-first",
151 },
152 }),
153 ).toBe(1);
154 });
155
156 it("returns the last question index when all answers are complete", () => {
157 expect(
158 findFirstUnansweredPendingUserInputQuestionIndex(questions, {
159 scope: {
160 selectedOptionLabel: "Orchestration-first",
161 },
162 compat: {
163 customAnswer: "Keep it for one release window",
164 },
165 }),
166 ).toBe(1);
167 });
168
169 it("derives the active question and advancement state", () => {
170 expect(
171 derivePendingUserInputProgress(
172 questions,
173 {
174 scope: {
175 selectedOptionLabel: "Orchestration-first",
176 },
177 },
178 0,
179 ),
180 ).toMatchObject({
181 questionIndex: 0,
182 activeQuestion: questions[0],
183 selectedOptionLabel: "Orchestration-first",
184 customAnswer: "",
185 resolvedAnswer: "Orchestration-first",
186 answeredQuestionCount: 1,
187 isLastQuestion: false,
188 isComplete: false,
189 canAdvance: true,
190 });
191 });
192});