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 { reviewCommentContextRecord, threadContextReference } from "./lib/composerContextRecords";
5import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
6
7import {
8 carryDisplacedCustomAnswerIntoPrompt,
9 derivePendingUserInputProgress,
10 projectPendingUserInputAnswer,
11 threadContextsReferencedByAnswer,
12} from "./pendingUserInput";
13
14describe("projectPendingUserInputAnswer", () => {
15 const record = threadContextRecord(
16 {
17 environmentId: EnvironmentId.make("env-answer"),
18 threadId: ThreadId.make("reference-thread"),
19 },
20 "Reference work",
21 );
22
23 it("sends thread chips as readable links with the actual thread ID", () => {
24 const answer = `Compare ${formatInlineContextReference(threadContextReference(record))}`;
25 expect(projectPendingUserInputAnswer(answer, [record])).toBe(
26 "Compare [Reference work](t3-thread://v2/env-answer/reference-thread)",
27 );
28 });
29
30 // regression: #28022 a review comment chip in an answer reached the agent as a bare link
31 it("sends review comment chips with the comment text and diff", () => {
32 const comment = reviewCommentContextRecord({
33 id: "file-comment-1",
34 sectionId: "branch",
35 sectionTitle: "Changes",
36 filePath: "notes.md",
37 startIndex: 2,
38 endIndex: 2,
39 rangeLabel: "+2",
40 text: "Rename this to line two updated",
41 diff: "@@ -0,0 +2,1 @@\n+line two changed",
42 fenceLanguage: "diff",
43 });
44 const answer = `See my note: ${formatInlineContextReference({
45 kind: "review-comment",
46 contextId: comment.contextId,
47 label: comment.label,
48 })}`;
49 const projected = projectPendingUserInputAnswer(answer, [comment]);
50 expect(projected).toContain("Rename this to line two updated");
51 expect(projected).toContain("+line two changed");
52 });
53
54 // regression: #18789 answers to pending questions lost exact text and option values
55 it("preserves option values and exact answer text", () => {
56 const options = [" first\t", "second"];
57 expect(projectPendingUserInputAnswer(options, [record])).toBe(options);
58 expect(projectPendingUserInputAnswer(" Edited answer\n", [record])).toBe(" Edited answer\n");
59 });
60});
61
62describe("threadContextsReferencedByAnswer", () => {
63 const kept = threadContextRecord(
64 { environmentId: EnvironmentId.make("env-answer"), threadId: ThreadId.make("kept-thread") },
65 "Kept work",
66 );
67 const deleted = threadContextRecord(
68 { environmentId: EnvironmentId.make("env-answer"), threadId: ThreadId.make("deleted-thread") },
69 "Deleted work",
70 );
71
72 it("preserves remaining chips without restoring deleted thread records", () => {
73 const answer = `Compare ${formatInlineContextReference(threadContextReference(kept))}`;
74 expect(threadContextsReferencedByAnswer(answer, [deleted, kept])).toEqual([kept]);
75 });
76});
77
78const singleSelectQuestion = {
79 id: "scope",
80 header: "Scope",
81 question: "What should the plan target first?",
82 options: [
83 {
84 label: "Orchestration-first",
85 description: "Focus on orchestration first",
86 },
87 ],
88 multiSelect: false,
89} as const;
90
91describe("pending user input question progress", () => {
92 const questions = [
93 singleSelectQuestion,
94 {
95 id: "compat",
96 header: "Compat",
97 question: "How strict should compatibility be?",
98 options: [
99 {
100 label: "Keep current envelope",
101 description: "Preserve current wire format",
102 },
103 ],
104 multiSelect: false,
105 },
106 ] as const;
107
108 it("counts a deliberately cleared answer as answered", () => {
109 const question = { ...singleSelectQuestion, initialAnswer: " Proposed message\n" };
110 const drafts = { scope: { customAnswer: "" } };
111 expect(derivePendingUserInputProgress([question], drafts, 0)).toMatchObject({
112 customAnswer: "",
113 resolvedAnswer: "",
114 canAdvance: true,
115 isComplete: true,
116 });
117 });
118
119 it("derives the active question and advancement state", () => {
120 expect(
121 derivePendingUserInputProgress(
122 questions,
123 {
124 scope: {
125 selectedOptionValues: ["Orchestration-first"],
126 },
127 },
128 0,
129 ),
130 ).toMatchObject({
131 questionIndex: 0,
132 activeQuestion: questions[0],
133 selectedOptionValues: ["Orchestration-first"],
134 customAnswer: "",
135 resolvedAnswer: "Orchestration-first",
136 answeredQuestionCount: 1,
137 isLastQuestion: false,
138 isComplete: false,
139 canAdvance: true,
140 });
141 });
142});
143
144describe("carryDisplacedCustomAnswerIntoPrompt", () => {
145 // regression: #12577 clicking a question option discarded typed text
146 it("keeps the thread draft when nothing was typed into the answer", () => {
147 expect(carryDisplacedCustomAnswerIntoPrompt("draft", undefined)).toBe("draft");
148 expect(carryDisplacedCustomAnswerIntoPrompt("draft", " ")).toBe("draft");
149 });
150
151 // regression: #12577 clicking a question option discarded typed text
152 it("moves the typed answer into an empty thread draft", () => {
153 expect(carryDisplacedCustomAnswerIntoPrompt("", "also rename the flag ")).toBe(
154 "also rename the flag",
155 );
156 });
157
158 it("appends the typed answer after an existing thread draft", () => {
159 expect(carryDisplacedCustomAnswerIntoPrompt("first half\n", "second half")).toBe(
160 "first half\n\nsecond half",
161 );
162 });
163});