Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pendingUserInput.test.ts

288 lines9.3 KB

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 derivePendingUserInputProgress,
10 findFirstUnansweredPendingUserInputQuestionIndex,
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://v1/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 questions = [question, { ...singleSelectQuestion, id: "follow-up" }];
102 const drafts = { scope: { customAnswer: "" } };
103 expect(findFirstUnansweredPendingUserInputQuestionIndex(questions, drafts)).toBe(1);
104 expect(derivePendingUserInputProgress([question], drafts, 0)).toMatchObject({
105 customAnswer: "",
106 resolvedAnswer: "",
107 canAdvance: true,
108 isComplete: true,
109 });
110 });
111​
112 it("prefers a custom answer over selected options", () => {
113 expect(
114 resolvePendingUserInputAnswer(singleSelectQuestion, {
115 selectedOptionValues: ["Orchestration-first"],
116 customAnswer: "Keep the existing envelope for one release",
117 }),
118 ).toBe("Keep the existing envelope for one release");
119 });
120​
121 it("does not replace a required choice with a custom answer", () => {
122 expect(
123 resolvePendingUserInputAnswer(nativeChoiceQuestion, {
124 selectedOptionValues: ["second"],
125 customAnswer: "Use another result",
126 }),
127 ).toBe("second");
128 });
129​
130 it("does not submit labels or unknown values when an option has a value", () => {
131 expect(
132 resolvePendingUserInputAnswer(nativeChoiceQuestion, {
133 selectedOptionValues: ["Result", "unknown"],
134 }),
135 ).toBeNull();
136 });
137});
138​
139describe("togglePendingUserInputOptionSelection", () => {
140 it("selects and removes options with the same label independently", () => {
141 const question = { ...nativeChoiceQuestion, multiSelect: true };
142 const firstSelected = togglePendingUserInputOptionSelection(question, undefined, " first\t");
143 const bothSelected = togglePendingUserInputOptionSelection(question, firstSelected, "second");
144​
145 expect(buildPendingUserInputAnswers([question], { result: bothSelected })).toEqual({
146 result: [" first\t", "second"],
147 });
148​
149 const secondSelected = togglePendingUserInputOptionSelection(
150 question,
151 bothSelected,
152 " first\t",
153 );
154 expect(buildPendingUserInputAnswers([question], { result: secondSelected })).toEqual({
155 result: ["second"],
156 });
157 });
158});
159​
160describe("buildPendingUserInputAnswers", () => {
161 it("returns a canonical answer map for complete prompts", () => {
162 expect(
163 buildPendingUserInputAnswers(
164 [
165 singleSelectQuestion,
166 {
167 id: "compat",
168 header: "Compat",
169 question: "How strict should compatibility be?",
170 options: [
171 {
172 label: "Keep current envelope",
173 description: "Preserve current wire format",
174 },
175 ],
176 multiSelect: false,
177 },
178 ],
179 {
180 scope: {
181 selectedOptionValues: ["Orchestration-first"],
182 },
183 compat: {
184 customAnswer: "Keep the current envelope for one release window",
185 },
186 },
187 ),
188 ).toEqual({
189 scope: "Orchestration-first",
190 compat: "Keep the current envelope for one release window",
191 });
192 });
193});
194​
195describe("pending user input question progress", () => {
196 const questions = [
197 singleSelectQuestion,
198 {
199 id: "compat",
200 header: "Compat",
201 question: "How strict should compatibility be?",
202 options: [
203 {
204 label: "Keep current envelope",
205 description: "Preserve current wire format",
206 },
207 ],
208 multiSelect: false,
209 },
210 ] as const;
211​
212 it("returns the last question index when all answers are complete", () => {
213 expect(
214 findFirstUnansweredPendingUserInputQuestionIndex(questions, {
215 scope: {
216 selectedOptionValues: ["Orchestration-first"],
217 },
218 compat: {
219 customAnswer: "Keep it for one release window",
220 },
221 }),
222 ).toBe(1);
223 });
224​
225 it("derives the active question and advancement state", () => {
226 expect(
227 derivePendingUserInputProgress(
228 questions,
229 {
230 scope: {
231 selectedOptionValues: ["Orchestration-first"],
232 },
233 },
234 0,
235 ),
236 ).toMatchObject({
237 questionIndex: 0,
238 activeQuestion: questions[0],
239 selectedOptionValues: ["Orchestration-first"],
240 customAnswer: "",
241 resolvedAnswer: "Orchestration-first",
242 answeredQuestionCount: 1,
243 isLastQuestion: false,
244 isComplete: false,
245 canAdvance: true,
246 });
247 });
248});
249​
250it("accepts attachment-only answers after every upload finishes", () => {
251 const questions = [
252 { id: "spec", header: "Spec", question: "Provide a spec", options: [], multiSelect: false },
253 ];
254 expect(buildPendingUserInputAnswers(questions, { spec: { attachmentCount: 1 } })).toEqual({
255 spec: "",
256 });
257 expect(
258 buildPendingUserInputAnswers(questions, {
259 spec: { attachmentCount: 1, attachmentsBlocked: true },
260 }),
261 ).toBeNull();
262 expect(
263 buildPendingUserInputAnswers([{ ...questions[0]!, allowCustomAnswer: false }], {
264 spec: { attachmentCount: 1 },
265 }),
266 ).toBeNull();
267});
268​
269describe("carryDisplacedCustomAnswerIntoPrompt", () => {
270 // regression: #12577 clicking a question option discarded typed text
271 it("keeps the thread draft when nothing was typed into the answer", () => {
272 expect(carryDisplacedCustomAnswerIntoPrompt("draft", undefined)).toBe("draft");
273 expect(carryDisplacedCustomAnswerIntoPrompt("draft", " ")).toBe("draft");
274 });
275​
276 // regression: #12577 clicking a question option discarded typed text
277 it("moves the typed answer into an empty thread draft", () => {
278 expect(carryDisplacedCustomAnswerIntoPrompt("", "also rename the flag ")).toBe(
279 "also rename the flag",
280 );
281 });
282​
283 it("appends the typed answer after an existing thread draft", () => {
284 expect(carryDisplacedCustomAnswerIntoPrompt("first half\n", "second half")).toBe(
285 "first half\n\nsecond half",
286 );
287 });
288});