The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | import { formatInlineContextReference } from "./lib/composerContextReferences"; |
| 4 | import { threadContextReference } from "./lib/composerContextRecords"; |
| 5 | import { threadContextRecord } from "@t3tools/shared/composerContextReferences"; |
| 6 | |
| 7 | import { |
| 8 | answerContextRecords, |
| 9 | carryDisplacedCustomAnswerIntoPrompt, |
| 10 | derivePendingUserInputProgress, |
| 11 | projectPendingUserInputAnswer, |
| 12 | threadContextsReferencedByAnswer, |
| 13 | withdrawDisplacedCustomAnswerFromPrompt, |
| 14 | } from "./pendingUserInput"; |
| 15 | |
| 16 | describe("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 | |
| 85 | describe("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 | |
| 101 | const 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 | |
| 114 | describe("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 | |
| 167 | describe("carryDisplacedCustomAnswerIntoPrompt", () => { |
| 168 | // regression: #12577 clicking a question option discarded typed text |
| 169 | it("keeps the thread draft when nothing was typed into the answer", () => { |
| 170 | expect(carryDisplacedCustomAnswerIntoPrompt("draft", undefined)).toBe("draft"); |
| 171 | expect(carryDisplacedCustomAnswerIntoPrompt("draft", " ")).toBe("draft"); |
| 172 | }); |
| 173 | |
| 174 | // regression: #12577 clicking a question option discarded typed text |
| 175 | it("moves the typed answer into an empty thread draft", () => { |
| 176 | expect(carryDisplacedCustomAnswerIntoPrompt("", "also rename the flag ")).toBe( |
| 177 | "also rename the flag", |
| 178 | ); |
| 179 | }); |
| 180 | |
| 181 | it("appends the typed answer after an existing thread draft", () => { |
| 182 | expect(carryDisplacedCustomAnswerIntoPrompt("first half\n", "second half")).toBe( |
| 183 | "first half\n\nsecond half", |
| 184 | ); |
| 185 | }); |
| 186 | }); |
| 187 | |
| 188 | describe("withdrawDisplacedCustomAnswerFromPrompt", () => { |
| 189 | // regression: #35205 an answer restored with undo stayed in the thread draft and was sent twice |
| 190 | it("takes set-aside text back out of the thread draft and leaves the rest", () => { |
| 191 | const setAside = carryDisplacedCustomAnswerIntoPrompt("", "first answer "); |
| 192 | expect(withdrawDisplacedCustomAnswerFromPrompt(setAside, "first answer ")).toBe(""); |
| 193 | |
| 194 | const afterDraft = carryDisplacedCustomAnswerIntoPrompt("draft", "first answer"); |
| 195 | expect(withdrawDisplacedCustomAnswerFromPrompt(afterDraft, "first answer")).toBe("draft"); |
| 196 | |
| 197 | const beforeSecond = carryDisplacedCustomAnswerIntoPrompt(setAside, "second answer"); |
| 198 | expect( |
| 199 | withdrawDisplacedCustomAnswerFromPrompt(beforeSecond, "first answer", ["second answer"]), |
| 200 | ).toBe("second answer"); |
| 201 | |
| 202 | const between = carryDisplacedCustomAnswerIntoPrompt(afterDraft, "second answer"); |
| 203 | expect( |
| 204 | withdrawDisplacedCustomAnswerFromPrompt(between, "first answer", ["second answer"]), |
| 205 | ).toBe("draft\n\nsecond answer"); |
| 206 | }); |
| 207 | |
| 208 | it("removes its own copy, not equal text inside a later set-aside answer", () => { |
| 209 | const first = carryDisplacedCustomAnswerIntoPrompt("", "foo"); |
| 210 | const both = carryDisplacedCustomAnswerIntoPrompt(first, "bar\n\nfoo"); |
| 211 | expect(withdrawDisplacedCustomAnswerFromPrompt(both, "foo", ["bar\n\nfoo"])).toBe("bar\n\nfoo"); |
| 212 | }); |
| 213 | |
| 214 | it("leaves a thread draft that no longer holds the set-aside text where it was put", () => { |
| 215 | expect(withdrawDisplacedCustomAnswerFromPrompt("first answer, edited", "first answer")).toBe( |
| 216 | "first answer, edited", |
| 217 | ); |
| 218 | expect(withdrawDisplacedCustomAnswerFromPrompt("my first answer", "first answer")).toBe( |
| 219 | "my first answer", |
| 220 | ); |
| 221 | expect(withdrawDisplacedCustomAnswerFromPrompt("draft", " ")).toBe("draft"); |
| 222 | }); |
| 223 | }); |