Open History to see the last commit
1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import { formatInlineContextReference } from "./lib/composerContextReferences";
4import { threadContextRecord, threadContextReference } from "./lib/composerContextRecords";
5
6import {
7 buildPendingUserInputAnswers,
8 carryDisplacedCustomAnswerIntoPrompt,
9 countAnsweredPendingUserInputQuestions,
10 derivePendingUserInputProgress,
11 findFirstUnansweredPendingUserInputQuestionIndex,
12 projectPendingUserInputAnswer,
13 resolvePendingUserInputAnswer,
14 setPendingUserInputCustomAnswer,
15 threadContextsReferencedByAnswer,
16 togglePendingUserInputOptionSelection,
17} from "./pendingUserInput";
18
19describe("projectPendingUserInputAnswer", () => {
20 const record = threadContextRecord(
21 {
22 environmentId: EnvironmentId.make("env-answer"),
23 threadId: ThreadId.make("reference-thread"),
24 },
25 "Reference work",
26 );
27
28 it("sends thread chips as readable links with the actual thread ID", () => {
29 const answer = `Compare ${formatInlineContextReference(threadContextReference(record))}`;
30 expect(projectPendingUserInputAnswer(answer, [record])).toBe(
31 "Compare [Reference work](t3-thread://v1/reference-thread)",
32 );
33 });
34
35 it("leaves plain answers unchanged when the draft has unreferenced thread records", () => {
36 expect(projectPendingUserInputAnswer("Plain answer", [record])).toBe("Plain answer");
37 });
38
39 it("preserves option values and exact answer text", () => {
40 const options = [" first\t", "second"];
41 expect(projectPendingUserInputAnswer(options, [record])).toBe(options);
42 expect(projectPendingUserInputAnswer(" Edited answer\n", [record])).toBe(" Edited answer\n");
43 });
44});
45
46describe("threadContextsReferencedByAnswer", () => {
47 const kept = threadContextRecord(
48 { environmentId: EnvironmentId.make("env-answer"), threadId: ThreadId.make("kept-thread") },
49 "Kept work",
50 );
51 const deleted = threadContextRecord(
52 { environmentId: EnvironmentId.make("env-answer"), threadId: ThreadId.make("deleted-thread") },
53 "Deleted work",
54 );
55
56 it("preserves remaining chips without restoring deleted thread records", () => {
57 const answer = `Compare ${formatInlineContextReference(threadContextReference(kept))}`;
58 expect(threadContextsReferencedByAnswer(answer, [deleted, kept])).toEqual([kept]);
59 });
60});
61
62const singleSelectQuestion = {
63 id: "scope",
64 header: "Scope",
65 question: "What should the plan target first?",
66 options: [
67 {
68 label: "Orchestration-first",
69 description: "Focus on orchestration first",
70 },
71 ],
72 multiSelect: false,
73} as const;
74
75const multiSelectQuestion = {
76 id: "areas",
77 header: "Areas",
78 question: "Which areas should this change cover?",
79 options: [
80 {
81 label: "Server",
82 description: "Server",
83 },
84 {
85 label: "Web",
86 description: "Web",
87 },
88 ],
89 multiSelect: true,
90} as const;
91
92const nativeChoiceQuestion = {
93 id: "result",
94 header: "Result",
95 question: "Which result should be used?",
96 options: [
97 { value: " first\t", label: "Result", description: "First result" },
98 { value: "second", label: "Result", description: "Second result" },
99 ],
100 allowCustomAnswer: false,
101 multiSelect: false,
102} as const;
103
104describe("resolvePendingUserInputAnswer", () => {
105 it("preserves exact editor text, including a deliberately cleared answer", () => {
106 const question = { ...singleSelectQuestion, initialAnswer: " Proposed message\n" };
107 expect(resolvePendingUserInputAnswer(question, { customAnswer: question.initialAnswer })).toBe(
108 question.initialAnswer,
109 );
110 expect(resolvePendingUserInputAnswer(question, { customAnswer: " Edited message\n\n" })).toBe(
111 " Edited message\n\n",
112 );
113 expect(resolvePendingUserInputAnswer(question, { customAnswer: "" })).toBe("");
114 expect(resolvePendingUserInputAnswer(question, { customAnswer: " \n" })).toBe(" \n");
115 expect(
116 setPendingUserInputCustomAnswer(
117 { selectedOptionValues: ["Orchestration-first"] },
118 "",
119 question,
120 ),
121 ).toEqual({ customAnswer: "" });
122 const questions = [question, { ...singleSelectQuestion, id: "follow-up" }];
123 const drafts = { scope: { customAnswer: "" } };
124 expect(findFirstUnansweredPendingUserInputQuestionIndex(questions, drafts)).toBe(1);
125 expect(derivePendingUserInputProgress([question], drafts, 0)).toMatchObject({
126 customAnswer: "",
127 resolvedAnswer: "",
128 canAdvance: true,
129 isComplete: true,
130 });
131 });
132
133 it("prefers a custom answer over selected options", () => {
134 expect(
135 resolvePendingUserInputAnswer(singleSelectQuestion, {
136 selectedOptionValues: ["Orchestration-first"],
137 customAnswer: "Keep the existing envelope for one release",
138 }),
139 ).toBe("Keep the existing envelope for one release");
140 });
141
142 it("falls back to the selected option for single-select questions", () => {
143 expect(
144 resolvePendingUserInputAnswer(singleSelectQuestion, {
145 selectedOptionValues: ["Orchestration-first"],
146 }),
147 ).toBe("Orchestration-first");
148 });
149
150 it("returns all selected labels for multi-select questions", () => {
151 expect(
152 resolvePendingUserInputAnswer(multiSelectQuestion, {
153 selectedOptionValues: ["Server", "Web"],
154 }),
155 ).toEqual(["Server", "Web"]);
156 });
157
158 it("clears the preset selection when a custom answer is entered", () => {
159 expect(
160 setPendingUserInputCustomAnswer(
161 {
162 selectedOptionValues: ["Server", "Web"],
163 },
164 "doesn't matter",
165 ),
166 ).toEqual({
167 customAnswer: "doesn't matter",
168 });
169 });
170
171 it("does not replace a required choice with a custom answer", () => {
172 expect(
173 resolvePendingUserInputAnswer(nativeChoiceQuestion, {
174 selectedOptionValues: ["second"],
175 customAnswer: "Use another result",
176 }),
177 ).toBe("second");
178 });
179
180 it("does not submit labels or unknown values when an option has a value", () => {
181 expect(
182 resolvePendingUserInputAnswer(nativeChoiceQuestion, {
183 selectedOptionValues: ["Result", "unknown"],
184 }),
185 ).toBeNull();
186 });
187});
188
189describe("togglePendingUserInputOptionSelection", () => {
190 it("toggles options for multi-select questions", () => {
191 expect(togglePendingUserInputOptionSelection(multiSelectQuestion, undefined, "Server")).toEqual(
192 {
193 customAnswer: "",
194 selectedOptionValues: ["Server"],
195 },
196 );
197
198 expect(
199 togglePendingUserInputOptionSelection(
200 multiSelectQuestion,
201 {
202 selectedOptionValues: ["Server", "Web"],
203 },
204 "Server",
205 ),
206 ).toEqual({
207 customAnswer: "",
208 selectedOptionValues: ["Web"],
209 });
210 });
211
212 it("selects and removes options with the same label independently", () => {
213 const question = { ...nativeChoiceQuestion, multiSelect: true };
214 const firstSelected = togglePendingUserInputOptionSelection(question, undefined, " first\t");
215 const bothSelected = togglePendingUserInputOptionSelection(question, firstSelected, "second");
216
217 expect(buildPendingUserInputAnswers([question], { result: bothSelected })).toEqual({
218 result: [" first\t", "second"],
219 });
220
221 const secondSelected = togglePendingUserInputOptionSelection(
222 question,
223 bothSelected,
224 " first\t",
225 );
226 expect(buildPendingUserInputAnswers([question], { result: secondSelected })).toEqual({
227 result: ["second"],
228 });
229 });
230});
231
232describe("buildPendingUserInputAnswers", () => {
233 it("returns a canonical answer map for complete prompts", () => {
234 expect(
235 buildPendingUserInputAnswers(
236 [
237 singleSelectQuestion,
238 {
239 id: "compat",
240 header: "Compat",
241 question: "How strict should compatibility be?",
242 options: [
243 {
244 label: "Keep current envelope",
245 description: "Preserve current wire format",
246 },
247 ],
248 multiSelect: false,
249 },
250 ],
251 {
252 scope: {
253 selectedOptionValues: ["Orchestration-first"],
254 },
255 compat: {
256 customAnswer: "Keep the current envelope for one release window",
257 },
258 },
259 ),
260 ).toEqual({
261 scope: "Orchestration-first",
262 compat: "Keep the current envelope for one release window",
263 });
264 });
265
266 it("returns arrays for answered multi-select prompts", () => {
267 expect(
268 buildPendingUserInputAnswers([multiSelectQuestion], {
269 areas: {
270 selectedOptionValues: ["Server", "Web"],
271 },
272 }),
273 ).toEqual({
274 areas: ["Server", "Web"],
275 });
276 });
277
278 it("returns null when any question is unanswered", () => {
279 expect(buildPendingUserInputAnswers([singleSelectQuestion], {})).toBeNull();
280 });
281
282 it.each([" first\t", ""])("preserves the exact selected option value %j", (value) => {
283 const question = {
284 ...nativeChoiceQuestion,
285 options: [{ ...nativeChoiceQuestion.options[0], value }, nativeChoiceQuestion.options[1]],
286 };
287 const draft = togglePendingUserInputOptionSelection(question, undefined, value);
288
289 expect(buildPendingUserInputAnswers([question], { result: draft })).toEqual({ result: value });
290 expect(derivePendingUserInputProgress([question], { result: draft }, 0)).toMatchObject({
291 selectedOptionValues: [value],
292 answeredQuestionCount: 1,
293 canAdvance: true,
294 isComplete: true,
295 });
296 });
297});
298
299describe("pending user input question progress", () => {
300 const questions = [
301 singleSelectQuestion,
302 {
303 id: "compat",
304 header: "Compat",
305 question: "How strict should compatibility be?",
306 options: [
307 {
308 label: "Keep current envelope",
309 description: "Preserve current wire format",
310 },
311 ],
312 multiSelect: false,
313 },
314 ] as const;
315
316 it("counts only answered questions", () => {
317 expect(
318 countAnsweredPendingUserInputQuestions(questions, {
319 scope: {
320 selectedOptionValues: ["Orchestration-first"],
321 },
322 }),
323 ).toBe(1);
324 });
325
326 it("finds the first unanswered question", () => {
327 expect(
328 findFirstUnansweredPendingUserInputQuestionIndex(questions, {
329 scope: {
330 selectedOptionValues: ["Orchestration-first"],
331 },
332 }),
333 ).toBe(1);
334 });
335
336 it("returns the last question index when all answers are complete", () => {
337 expect(
338 findFirstUnansweredPendingUserInputQuestionIndex(questions, {
339 scope: {
340 selectedOptionValues: ["Orchestration-first"],
341 },
342 compat: {
343 customAnswer: "Keep it for one release window",
344 },
345 }),
346 ).toBe(1);
347 });
348
349 it("derives the active question and advancement state", () => {
350 expect(
351 derivePendingUserInputProgress(
352 questions,
353 {
354 scope: {
355 selectedOptionValues: ["Orchestration-first"],
356 },
357 },
358 0,
359 ),
360 ).toMatchObject({
361 questionIndex: 0,
362 activeQuestion: questions[0],
363 selectedOptionValues: ["Orchestration-first"],
364 customAnswer: "",
365 resolvedAnswer: "Orchestration-first",
366 answeredQuestionCount: 1,
367 isLastQuestion: false,
368 isComplete: false,
369 canAdvance: true,
370 });
371 });
372
373 it("treats multi-select questions as answered when they have selected options", () => {
374 expect(
375 derivePendingUserInputProgress(
376 [multiSelectQuestion],
377 {
378 areas: {
379 selectedOptionValues: ["Server", "Web"],
380 },
381 },
382 0,
383 ),
384 ).toMatchObject({
385 selectedOptionValues: ["Server", "Web"],
386 resolvedAnswer: ["Server", "Web"],
387 canAdvance: true,
388 isComplete: true,
389 });
390 });
391
392 it("requires an option when custom answers are disabled", () => {
393 const drafts = { result: { customAnswer: "Use another result" } };
394
395 expect(buildPendingUserInputAnswers([nativeChoiceQuestion], drafts)).toBeNull();
396 expect(derivePendingUserInputProgress([nativeChoiceQuestion], drafts, 0)).toMatchObject({
397 customAnswer: "",
398 usingCustomAnswer: false,
399 resolvedAnswer: null,
400 answeredQuestionCount: 0,
401 canAdvance: false,
402 isComplete: false,
403 });
404 });
405});
406
407it("accepts attachment-only answers after every upload finishes", () => {
408 const questions = [
409 { id: "spec", header: "Spec", question: "Provide a spec", options: [], multiSelect: false },
410 ];
411 expect(buildPendingUserInputAnswers(questions, { spec: { attachmentCount: 1 } })).toEqual({
412 spec: "",
413 });
414 expect(
415 buildPendingUserInputAnswers(questions, {
416 spec: { attachmentCount: 1, attachmentsBlocked: true },
417 }),
418 ).toBeNull();
419 expect(
420 buildPendingUserInputAnswers([{ ...questions[0]!, allowCustomAnswer: false }], {
421 spec: { attachmentCount: 1 },
422 }),
423 ).toBeNull();
424});
425
426describe("carryDisplacedCustomAnswerIntoPrompt", () => {
427 it("keeps the thread draft when nothing was typed into the answer", () => {
428 expect(carryDisplacedCustomAnswerIntoPrompt("draft", undefined)).toBe("draft");
429 expect(carryDisplacedCustomAnswerIntoPrompt("draft", " ")).toBe("draft");
430 });
431
432 it("moves the typed answer into an empty thread draft", () => {
433 expect(carryDisplacedCustomAnswerIntoPrompt("", "also rename the flag ")).toBe(
434 "also rename the flag",
435 );
436 });
437
438 it("appends the typed answer after an existing thread draft", () => {
439 expect(carryDisplacedCustomAnswerIntoPrompt("first half\n", "second half")).toBe(
440 "first half\n\nsecond half",
441 );
442 });
443});