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 answerContextRecords,
9 carryDisplacedCustomAnswerIntoPrompt,
10 derivePendingUserInputProgress,
11 projectPendingUserInputAnswer,
12 threadContextsReferencedByAnswer,
13 withdrawDisplacedCustomAnswerFromPrompt,
14} from "./pendingUserInput";
15
16describe("projectPendingUserInputAnswer", () => {
17 const record = threadContextRecord(
18 {
19 environmentId: EnvironmentId.make("env-answer"),
20 threadId: ThreadId.make("reference-thread"),
21 },
22 "Reference work",
23 );
24
25 it("sends thread chips as readable links with the actual thread ID", () => {
26 const answer = `Compare ${formatInlineContextReference(threadContextReference(record))}`;
27 expect(projectPendingUserInputAnswer(answer, [record])).toBe(
28 "Compare [Reference work](t3-thread://v2/env-answer/reference-thread)",
29 );
30 });
31
32 // regression: #28022, #36293 review comment and preview annotation chips in an answer reached the agent as bare links
33 it("sends thread draft chips with their full content", () => {
34 const reviewComment = {
35 id: "file-comment-1",
36 sectionId: "branch",
37 sectionTitle: "Changes",
38 filePath: "notes.md",
39 startIndex: 2,
40 endIndex: 2,
41 rangeLabel: "+2",
42 text: "Rename this to line two updated",
43 diff: "@@ -0,0 +2,1 @@\n+line two changed",
44 fenceLanguage: "diff",
45 };
46 const annotation = {
47 id: "annotation-1",
48 pageUrl: "http://localhost:3000/",
49 pageTitle: "Home",
50 comment: "FIRST NOTE: this features section looks too bare; add three feature cards",
51 elements: [],
52 regions: [],
53 strokes: [],
54 styleChanges: [],
55 screenshot: null,
56 createdAt: "2026-01-01T00:00:00.000Z",
57 };
58 const records = answerContextRecords(undefined, {
59 reviewComments: [reviewComment],
60 previewAnnotations: [annotation],
61 });
62 const answer = `See my notes: ${records
63 .map((record) =>
64 formatInlineContextReference({
65 kind: record.kind,
66 contextId: record.contextId,
67 label: record.label,
68 }),
69 )
70 .join(" ")}`;
71 const projected = projectPendingUserInputAnswer(answer, records);
72 expect(projected).toContain("Rename this to line two updated");
73 expect(projected).toContain("+line two changed");
74 expect(projected).toContain(`comment: ${annotation.comment}`);
75 });
76
77 // regression: #18789 answers to pending questions lost exact text and option values
78 it("preserves option values and exact answer text", () => {
79 const options = [" first\t", "second"];
80 expect(projectPendingUserInputAnswer(options, [record])).toBe(options);
81 expect(projectPendingUserInputAnswer(" Edited answer\n", [record])).toBe(" Edited answer\n");
82 });
83});
84
85describe("threadContextsReferencedByAnswer", () => {
86 const kept = threadContextRecord(
87 { environmentId: EnvironmentId.make("env-answer"), threadId: ThreadId.make("kept-thread") },
88 "Kept work",
89 );
90 const deleted = threadContextRecord(
91 { environmentId: EnvironmentId.make("env-answer"), threadId: ThreadId.make("deleted-thread") },
92 "Deleted work",
93 );
94
95 it("preserves remaining chips without restoring deleted thread records", () => {
96 const answer = `Compare ${formatInlineContextReference(threadContextReference(kept))}`;
97 expect(threadContextsReferencedByAnswer(answer, [deleted, kept])).toEqual([kept]);
98 });
99});
100
101const singleSelectQuestion = {
102 id: "scope",
103 header: "Scope",
104 question: "What should the plan target first?",
105 options: [
106 {
107 label: "Orchestration-first",
108 description: "Focus on orchestration first",
109 },
110 ],
111 multiSelect: false,
112} as const;
113
114describe("pending user input question progress", () => {
115 const questions = [
116 singleSelectQuestion,
117 {
118 id: "compat",
119 header: "Compat",
120 question: "How strict should compatibility be?",
121 options: [
122 {
123 label: "Keep current envelope",
124 description: "Preserve current wire format",
125 },
126 ],
127 multiSelect: false,
128 },
129 ] as const;
130
131 it("counts a deliberately cleared answer as answered", () => {
132 const question = { ...singleSelectQuestion, initialAnswer: " Proposed message\n" };
133 const drafts = { scope: { customAnswer: "" } };
134 expect(derivePendingUserInputProgress([question], drafts, 0)).toMatchObject({
135 customAnswer: "",
136 resolvedAnswer: "",
137 canAdvance: true,
138 isComplete: true,
139 });
140 });
141
142 it("derives the active question and advancement state", () => {
143 expect(
144 derivePendingUserInputProgress(
145 questions,
146 {
147 scope: {
148 selectedOptionValues: ["Orchestration-first"],
149 },
150 },
151 0,
152 ),
153 ).toMatchObject({
154 questionIndex: 0,
155 activeQuestion: questions[0],
156 selectedOptionValues: ["Orchestration-first"],
157 customAnswer: "",
158 resolvedAnswer: "Orchestration-first",
159 answeredQuestionCount: 1,
160 isLastQuestion: false,
161 isComplete: false,
162 canAdvance: true,
163 });
164 });
165
166 it("advances past an unselected optional single choice", () => {
167 const optionalChoice = {
168 id: "extras",
169 header: "Extras",
170 question: "Include extras?",
171 options: [
172 { label: "true", description: "Yes", value: "true" },
173 { label: "false", description: "No", value: "false" },
174 ],
175 allowCustomAnswer: false,
176 required: false,
177 } as const;
178 const mixed = [optionalChoice, singleSelectQuestion] as const;
179
180 expect(derivePendingUserInputProgress(mixed, {}, 0)).toMatchObject({
181 canAdvance: true,
182 isComplete: false,
183 });
184 expect(
185 derivePendingUserInputProgress(mixed, { extras: { attachmentsBlocked: true } }, 0),
186 ).toMatchObject({ canAdvance: false });
187 expect(derivePendingUserInputProgress(mixed, {}, 1)).toMatchObject({ canAdvance: false });
188 expect(
189 derivePendingUserInputProgress(
190 mixed,
191 { scope: { selectedOptionValues: ["Orchestration-first"] } },
192 1,
193 ),
194 ).toMatchObject({ canAdvance: true, isComplete: true });
195 });
196});
197
198describe("carryDisplacedCustomAnswerIntoPrompt", () => {
199 // regression: #12577 clicking a question option discarded typed text
200 it("keeps the thread draft when nothing was typed into the answer", () => {
201 expect(carryDisplacedCustomAnswerIntoPrompt("draft", undefined)).toBe("draft");
202 expect(carryDisplacedCustomAnswerIntoPrompt("draft", " ")).toBe("draft");
203 });
204
205 // regression: #12577 clicking a question option discarded typed text
206 it("moves the typed answer into an empty thread draft", () => {
207 expect(carryDisplacedCustomAnswerIntoPrompt("", "also rename the flag ")).toBe(
208 "also rename the flag",
209 );
210 });
211
212 it("appends the typed answer after an existing thread draft", () => {
213 expect(carryDisplacedCustomAnswerIntoPrompt("first half\n", "second half")).toBe(
214 "first half\n\nsecond half",
215 );
216 });
217});
218
219describe("withdrawDisplacedCustomAnswerFromPrompt", () => {
220 // regression: #35205 an answer restored with undo stayed in the thread draft and was sent twice
221 it("takes set-aside text back out of the thread draft and leaves the rest", () => {
222 const setAside = carryDisplacedCustomAnswerIntoPrompt("", "first answer ");
223 expect(withdrawDisplacedCustomAnswerFromPrompt(setAside, "first answer ")).toBe("");
224
225 const afterDraft = carryDisplacedCustomAnswerIntoPrompt("draft", "first answer");
226 expect(withdrawDisplacedCustomAnswerFromPrompt(afterDraft, "first answer")).toBe("draft");
227
228 const beforeSecond = carryDisplacedCustomAnswerIntoPrompt(setAside, "second answer");
229 expect(
230 withdrawDisplacedCustomAnswerFromPrompt(beforeSecond, "first answer", ["second answer"]),
231 ).toBe("second answer");
232
233 const between = carryDisplacedCustomAnswerIntoPrompt(afterDraft, "second answer");
234 expect(
235 withdrawDisplacedCustomAnswerFromPrompt(between, "first answer", ["second answer"]),
236 ).toBe("draft\n\nsecond answer");
237 });
238
239 it("removes its own copy, not equal text inside a later set-aside answer", () => {
240 const first = carryDisplacedCustomAnswerIntoPrompt("", "foo");
241 const both = carryDisplacedCustomAnswerIntoPrompt(first, "bar\n\nfoo");
242 expect(withdrawDisplacedCustomAnswerFromPrompt(both, "foo", ["bar\n\nfoo"])).toBe("bar\n\nfoo");
243 });
244
245 it("leaves a thread draft that no longer holds the set-aside text where it was put", () => {
246 expect(withdrawDisplacedCustomAnswerFromPrompt("first answer, edited", "first answer")).toBe(
247 "first answer, edited",
248 );
249 expect(withdrawDisplacedCustomAnswerFromPrompt("my first answer", "first answer")).toBe(
250 "my first answer",
251 );
252 expect(withdrawDisplacedCustomAnswerFromPrompt("draft", " ")).toBe("draft");
253 });
254});