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 { threadContextReference } from "./lib/composerContextRecords";
5import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
6
7import {
8 buildPendingUserInputAnswers,
9 carryDisplacedCustomAnswerIntoPrompt,
10 derivePendingUserInputProgress,
11 projectPendingUserInputAnswer,
12 resolvePendingUserInputAnswer,
13 setPendingUserInputCustomAnswer,
14 threadContextsReferencedByAnswer,
15 togglePendingUserInputOptionSelection,
16} from "./pendingUserInput";
17
18describe("projectPendingUserInputAnswer", () => {
19 const record = threadContextRecord(
20 {
21 environmentId: EnvironmentId.make("env-answer"),
22 threadId: ThreadId.make("reference-thread"),
23 },
24 "Reference work",
25 );
26
27 it("sends thread chips as readable links with the actual thread ID", () => {
28 const answer = `Compare ${formatInlineContextReference(threadContextReference(record))}`;
29 expect(projectPendingUserInputAnswer(answer, [record])).toBe(
30 "Compare [Reference work](t3-thread://v2/env-answer/reference-thread)",
31 );
32 });
33
34 // regression: #18789 answers to pending questions lost exact text and option values
35 it("preserves option values and exact answer text", () => {
36 const options = [" first\t", "second"];
37 expect(projectPendingUserInputAnswer(options, [record])).toBe(options);
38 expect(projectPendingUserInputAnswer(" Edited answer\n", [record])).toBe(" Edited answer\n");
39 });
40});
41
42describe("threadContextsReferencedByAnswer", () => {
43 const kept = threadContextRecord(
44 { environmentId: EnvironmentId.make("env-answer"), threadId: ThreadId.make("kept-thread") },
45 "Kept work",
46 );
47 const deleted = threadContextRecord(
48 { environmentId: EnvironmentId.make("env-answer"), threadId: ThreadId.make("deleted-thread") },
49 "Deleted work",
50 );
51
52 it("preserves remaining chips without restoring deleted thread records", () => {
53 const answer = `Compare ${formatInlineContextReference(threadContextReference(kept))}`;
54 expect(threadContextsReferencedByAnswer(answer, [deleted, kept])).toEqual([kept]);
55 });
56});
57
58const singleSelectQuestion = {
59 id: "scope",
60 header: "Scope",
61 question: "What should the plan target first?",
62 options: [
63 {
64 label: "Orchestration-first",
65 description: "Focus on orchestration first",
66 },
67 ],
68 multiSelect: false,
69} as const;
70
71const nativeChoiceQuestion = {
72 id: "result",
73 header: "Result",
74 question: "Which result should be used?",
75 options: [
76 { value: " first\t", label: "Result", description: "First result" },
77 { value: "second", label: "Result", description: "Second result" },
78 ],
79 allowCustomAnswer: false,
80 multiSelect: false,
81} as const;
82
83describe("resolvePendingUserInputAnswer", () => {
84 it("preserves exact editor text, including a deliberately cleared answer", () => {
85 const question = { ...singleSelectQuestion, initialAnswer: " Proposed message\n" };
86 expect(resolvePendingUserInputAnswer(question, { customAnswer: question.initialAnswer })).toBe(
87 question.initialAnswer,
88 );
89 expect(resolvePendingUserInputAnswer(question, { customAnswer: " Edited message\n\n" })).toBe(
90 " Edited message\n\n",
91 );
92 expect(resolvePendingUserInputAnswer(question, { customAnswer: "" })).toBe("");
93 expect(resolvePendingUserInputAnswer(question, { customAnswer: " \n" })).toBe(" \n");
94 expect(
95 setPendingUserInputCustomAnswer(
96 { selectedOptionValues: ["Orchestration-first"] },
97 "",
98 question,
99 ),
100 ).toEqual({ customAnswer: "" });
101 const drafts = { scope: { customAnswer: "" } };
102 expect(derivePendingUserInputProgress([question], drafts, 0)).toMatchObject({
103 customAnswer: "",
104 resolvedAnswer: "",
105 canAdvance: true,
106 isComplete: true,
107 });
108 });
109
110 it("prefers a custom answer over selected options", () => {
111 expect(
112 resolvePendingUserInputAnswer(singleSelectQuestion, {
113 selectedOptionValues: ["Orchestration-first"],
114 customAnswer: "Keep the existing envelope for one release",
115 }),
116 ).toBe("Keep the existing envelope for one release");
117 });
118
119 it("does not replace a required choice with a custom answer", () => {
120 expect(
121 resolvePendingUserInputAnswer(nativeChoiceQuestion, {
122 selectedOptionValues: ["second"],
123 customAnswer: "Use another result",
124 }),
125 ).toBe("second");
126 });
127
128 it("does not submit labels or unknown values when an option has a value", () => {
129 expect(
130 resolvePendingUserInputAnswer(nativeChoiceQuestion, {
131 selectedOptionValues: ["Result", "unknown"],
132 }),
133 ).toBeNull();
134 });
135});
136
137describe("togglePendingUserInputOptionSelection", () => {
138 it("selects and removes options with the same label independently", () => {
139 const question = { ...nativeChoiceQuestion, multiSelect: true };
140 const firstSelected = togglePendingUserInputOptionSelection(question, undefined, " first\t");
141 const bothSelected = togglePendingUserInputOptionSelection(question, firstSelected, "second");
142
143 expect(buildPendingUserInputAnswers([question], { result: bothSelected })).toEqual({
144 result: [" first\t", "second"],
145 });
146
147 const secondSelected = togglePendingUserInputOptionSelection(
148 question,
149 bothSelected,
150 " first\t",
151 );
152 expect(buildPendingUserInputAnswers([question], { result: secondSelected })).toEqual({
153 result: ["second"],
154 });
155 });
156});
157
158describe("buildPendingUserInputAnswers", () => {
159 it("returns a canonical answer map for complete prompts", () => {
160 expect(
161 buildPendingUserInputAnswers(
162 [
163 singleSelectQuestion,
164 {
165 id: "compat",
166 header: "Compat",
167 question: "How strict should compatibility be?",
168 options: [
169 {
170 label: "Keep current envelope",
171 description: "Preserve current wire format",
172 },
173 ],
174 multiSelect: false,
175 },
176 ],
177 {
178 scope: {
179 selectedOptionValues: ["Orchestration-first"],
180 },
181 compat: {
182 customAnswer: "Keep the current envelope for one release window",
183 },
184 },
185 ),
186 ).toEqual({
187 scope: "Orchestration-first",
188 compat: "Keep the current envelope for one release window",
189 });
190 });
191});
192
193describe("pending user input question progress", () => {
194 const questions = [
195 singleSelectQuestion,
196 {
197 id: "compat",
198 header: "Compat",
199 question: "How strict should compatibility be?",
200 options: [
201 {
202 label: "Keep current envelope",
203 description: "Preserve current wire format",
204 },
205 ],
206 multiSelect: false,
207 },
208 ] as const;
209
210 it("derives the active question and advancement state", () => {
211 expect(
212 derivePendingUserInputProgress(
213 questions,
214 {
215 scope: {
216 selectedOptionValues: ["Orchestration-first"],
217 },
218 },
219 0,
220 ),
221 ).toMatchObject({
222 questionIndex: 0,
223 activeQuestion: questions[0],
224 selectedOptionValues: ["Orchestration-first"],
225 customAnswer: "",
226 resolvedAnswer: "Orchestration-first",
227 answeredQuestionCount: 1,
228 isLastQuestion: false,
229 isComplete: false,
230 canAdvance: true,
231 });
232 });
233});
234
235it("accepts attachment-only answers after every upload finishes", () => {
236 const questions = [
237 { id: "spec", header: "Spec", question: "Provide a spec", options: [], multiSelect: false },
238 ];
239 expect(buildPendingUserInputAnswers(questions, { spec: { attachmentCount: 1 } })).toEqual({
240 spec: "",
241 });
242 expect(
243 buildPendingUserInputAnswers(questions, {
244 spec: { attachmentCount: 1, attachmentsBlocked: true },
245 }),
246 ).toBeNull();
247 expect(
248 buildPendingUserInputAnswers([{ ...questions[0]!, allowCustomAnswer: false }], {
249 spec: { attachmentCount: 1 },
250 }),
251 ).toBeNull();
252});
253
254describe("carryDisplacedCustomAnswerIntoPrompt", () => {
255 // regression: #12577 clicking a question option discarded typed text
256 it("keeps the thread draft when nothing was typed into the answer", () => {
257 expect(carryDisplacedCustomAnswerIntoPrompt("draft", undefined)).toBe("draft");
258 expect(carryDisplacedCustomAnswerIntoPrompt("draft", " ")).toBe("draft");
259 });
260
261 // regression: #12577 clicking a question option discarded typed text
262 it("moves the typed answer into an empty thread draft", () => {
263 expect(carryDisplacedCustomAnswerIntoPrompt("", "also rename the flag ")).toBe(
264 "also rename the flag",
265 );
266 });
267
268 it("appends the typed answer after an existing thread draft", () => {
269 expect(carryDisplacedCustomAnswerIntoPrompt("first half\n", "second half")).toBe(
270 "first half\n\nsecond half",
271 );
272 });
273});