Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 buildBranchNamePrompt,
5 buildCommitMessagePrompt,
6 buildPrContentPrompt,
7 buildThreadTitlePrompt,
8} from "./TextGenerationPrompts.ts";
9import { normalizeCliError, sanitizeThreadTitle } from "./TextGenerationUtils.ts";
10import { TextGenerationError } from "@t3tools/contracts";
11
12describe("buildCommitMessagePrompt", () => {
13 it("includes staged patch and summary in the prompt", () => {
14 const result = buildCommitMessagePrompt({
15 branch: "main",
16 stagedSummary: "M README.md",
17 stagedPatch: "diff --git a/README.md b/README.md\n+hello",
18 includeBranch: false,
19 });
20
21 expect(result.prompt).toContain("Staged files:");
22 expect(result.prompt).toContain("M README.md");
23 expect(result.prompt).toContain("Staged patch:");
24 expect(result.prompt).toContain("diff --git a/README.md b/README.md");
25 expect(result.prompt).toContain("Branch: main");
26 // Should NOT include the branch generation instruction
27 expect(result.prompt).not.toContain("branch must be a short semantic git branch fragment");
28 });
29
30 it("includes branch generation instruction when includeBranch is true", () => {
31 const result = buildCommitMessagePrompt({
32 branch: "feature/foo",
33 stagedSummary: "M README.md",
34 stagedPatch: "diff",
35 includeBranch: true,
36 });
37
38 expect(result.prompt).toContain("branch must be a short semantic git branch fragment");
39 expect(result.prompt).toContain("Return a JSON object with keys: subject, body, branch.");
40 });
41
42 it("shows (detached) when branch is null", () => {
43 const result = buildCommitMessagePrompt({
44 branch: null,
45 stagedSummary: "M a.ts",
46 stagedPatch: "diff",
47 includeBranch: false,
48 });
49
50 expect(result.prompt).toContain("Branch: (detached)");
51 });
52
53 it("includes policy instructions", () => {
54 const result = buildCommitMessagePrompt({
55 branch: "main",
56 stagedSummary: "M a.ts",
57 stagedPatch: "diff",
58 includeBranch: false,
59 policy: {
60 kind: "custom",
61 commitInstructions: "Use a terse repository-specific subject.",
62 inferRepositoryConventions: false,
63 },
64 });
65
66 expect(result.prompt).toContain("Additional instructions:");
67 expect(result.prompt).toContain("Use a terse repository-specific subject.");
68 });
69});
70
71describe("buildPrContentPrompt", () => {
72 it("includes branch names, commits, and diff in the prompt", () => {
73 const result = buildPrContentPrompt({
74 baseBranch: "main",
75 headBranch: "feature/auth",
76 commitSummary: "feat: add login page",
77 diffSummary: "3 files changed",
78 diffPatch: "diff --git a/auth.ts b/auth.ts\n+export function login()",
79 });
80
81 expect(result.prompt).toContain("Base branch: main");
82 expect(result.prompt).toContain("Head branch: feature/auth");
83 expect(result.prompt).toContain("Commits:");
84 expect(result.prompt).toContain("feat: add login page");
85 expect(result.prompt).toContain("Diff stat:");
86 expect(result.prompt).toContain("3 files changed");
87 expect(result.prompt).toContain("Diff patch:");
88 expect(result.prompt).toContain("export function login()");
89 expect(result.prompt).toContain("include headings '## Summary' and '## Testing'");
90 });
91
92 it("follows a repository PR template instead of the default body headings", () => {
93 const result = buildPrContentPrompt({
94 baseBranch: "main",
95 headBranch: "feature/auth",
96 commitSummary: "feat: add login page",
97 diffSummary: "3 files changed",
98 diffPatch: "diff",
99 changeRequestTemplate: "<!-- remove me -->\n## What changed\n\n## Verification",
100 policy: {
101 kind: "custom",
102 changeRequestInstructions: "Keep the title in sentence case.",
103 inferRepositoryConventions: false,
104 },
105 });
106
107 expect(result.prompt).toContain("Keep the title in sentence case.");
108 expect(result.prompt).toContain("follow the repository change request template structure");
109 expect(result.prompt).toContain("drop HTML comments from the template");
110 expect(result.prompt).toContain("Repository change request template:");
111 expect(result.prompt).toContain("<!-- remove me -->\n## What changed\n\n## Verification");
112 expect(result.prompt).not.toContain("include headings '## Summary' and '## Testing'");
113 });
114});
115
116describe("buildBranchNamePrompt", () => {
117 it("includes the user message in the prompt", () => {
118 const result = buildBranchNamePrompt({
119 message: "Fix the login timeout bug",
120 });
121
122 expect(result.prompt).toContain("User message:");
123 expect(result.prompt).toContain("Fix the login timeout bug");
124 expect(result.prompt).not.toContain("Attachment metadata:");
125 });
126
127 it("includes attachment metadata when attachments are provided", () => {
128 const result = buildBranchNamePrompt({
129 message: "Fix the layout from screenshot",
130 attachments: [
131 {
132 type: "image" as const,
133 id: "att-123",
134 name: "screenshot.png",
135 mimeType: "image/png",
136 sizeBytes: 12345,
137 },
138 ],
139 });
140
141 expect(result.prompt).toContain("Attachment metadata:");
142 expect(result.prompt).toContain("screenshot.png");
143 expect(result.prompt).toContain("image/png");
144 expect(result.prompt).toContain("12345 bytes");
145 });
146});
147
148describe("buildThreadTitlePrompt", () => {
149 it("includes the user message and the title guidance rules", () => {
150 const result = buildThreadTitlePrompt({
151 message: "Investigate reconnect regressions after session restore",
152 });
153
154 expect(result.prompt).toContain("User message:");
155 expect(result.prompt).toContain("Investigate reconnect regressions after session restore");
156 expect(result.prompt).not.toContain("Attachment metadata:");
157 expect(result.prompt).toContain(
158 "Generate a title that will help the user recognize this T3 Code thread weeks later.",
159 );
160 expect(result.prompt).toContain(
161 "Title the subject and outcome. Discard incidental instructions.",
162 );
163 expect(result.prompt).toContain(
164 "Name the product change, not the mock, plan, report, branch, or PR used to produce it.",
165 );
166 expect(result.prompt).not.toContain(
167 "Title should summarize the user's request, not restate it verbatim.",
168 );
169 });
170
171 it("includes attachment metadata when attachments are provided", () => {
172 const result = buildThreadTitlePrompt({
173 message: "Name this thread from the screenshot",
174 attachments: [
175 {
176 type: "image" as const,
177 id: "att-456",
178 name: "thread.png",
179 mimeType: "image/png",
180 sizeBytes: 67890,
181 },
182 ],
183 });
184
185 expect(result.prompt).toContain("Attachment metadata:");
186 expect(result.prompt).toContain("thread.png");
187 expect(result.prompt).toContain("image/png");
188 expect(result.prompt).toContain("67890 bytes");
189 });
190
191 it.each([
192 { mode: "initial", previousTitle: undefined },
193 { mode: "regeneration", previousTitle: "Open Projects in Desktop App" },
194 ])(
195 "tells the $mode prompt not to title linked PRs from local git history",
196 ({ previousTitle }) => {
197 const result = buildThreadTitlePrompt({
198 message: "$takeover https://github.com/pingdotgg/t3code/pull/8588",
199 ...(previousTitle === undefined ? {} : { previousTitle }),
200 });
201
202 expect(result.prompt).toContain(
203 "Local git history is not evidence of what a linked PR or issue is about.",
204 );
205 expect(result.prompt).toContain('such as "Take Over PR 8588"');
206 },
207 );
208
209 it("regenerates from recent thread contents and identifies the previous title", () => {
210 const result = buildThreadTitlePrompt({
211 message: `USER:\nInvestigate reconnect regressions\n\nASSISTANT:\nThe remaining issue is stale session state`,
212 previousTitle: "Investigate reconnect regressions",
213 });
214
215 expect(result.prompt).toContain(
216 "Regenerate the title for an existing T3 Code thread so the user can recognize it weeks later.",
217 );
218 expect(result.prompt).toContain('The previous title was "Investigate reconnect regressions".');
219 expect(result.prompt).toContain(
220 "Read the USER messages first. Identify the latest explicit durable goal.",
221 );
222 expect(result.prompt).toContain(
223 "Do not promote one assistant finding into the thread subject unless the user adopts it as a new goal.",
224 );
225 expect(result.prompt).toContain(
226 'A subagent-monitoring review that finds a Codex roster bug remains "Review Subagent Monitoring Risks,"',
227 );
228 expect(result.prompt).toContain("Thread contents:");
229 expect(result.prompt).toContain("The remaining issue is stale session state");
230 });
231
232 it("keeps the latest thread contents when regeneration context is truncated", () => {
233 const result = buildThreadTitlePrompt({
234 message: `${"old context ".repeat(1_000)}\n\nASSISTANT:\nCurrent thread state`,
235 previousTitle: "Old title",
236 });
237
238 expect(result.prompt).toContain("[Earlier content truncated]");
239 expect(result.prompt).toContain("Current thread state");
240 expect(result.prompt).not.toContain("[truncated]");
241 });
242
243 it("does not truncate an already-marked regeneration context twice", () => {
244 const retainedContext = "x".repeat(7_998);
245 const result = buildThreadTitlePrompt({
246 message: `[Earlier content truncated]\n\n${retainedContext}`,
247 previousTitle: "Old title",
248 });
249
250 expect(result.prompt).toContain(
251 `Thread contents:\n[Earlier content truncated]\n\n${retainedContext}`,
252 );
253 expect(result.prompt.match(/\[Earlier content truncated\]/g)).toHaveLength(1);
254 });
255});
256
257describe("sanitizeThreadTitle", () => {
258 it("truncates long titles with the shared sidebar-safe limit", () => {
259 expect(
260 sanitizeThreadTitle(
261 ' "Reconnect failures after restart because the session state does not recover" ',
262 ),
263 ).toBe("Reconnect failures after restart because the se...");
264 });
265});
266
267describe("normalizeCliError", () => {
268 it.each([
269 ["claude", "generateCommitMessage", "Claude CLI"],
270 ["codex", "generateBranchName", "Codex CLI"],
271 ])("identifies %s when its command is missing", (cliName, operation, label) => {
272 const error = normalizeCliError(
273 cliName,
274 operation,
275 new Error(`Command not found: ${cliName}`),
276 "Something went wrong",
277 );
278
279 expect(error).toBeInstanceOf(TextGenerationError);
280 expect(error.detail).toContain(label);
281 expect(error.detail).toContain("not available on PATH");
282 });
283
284 it("returns the error as-is if it is already a TextGenerationError", () => {
285 const existing = new TextGenerationError({
286 operation: "generatePrContent",
287 detail: "Already wrapped",
288 });
289
290 const result = normalizeCliError("claude", "generatePrContent", existing, "fallback");
291
292 expect(result).toBe(existing);
293 });
294
295 it("wraps unknown non-Error values with the fallback message", () => {
296 const result = normalizeCliError("codex", "generateCommitMessage", "string error", "fallback");
297
298 expect(result).toBeInstanceOf(TextGenerationError);
299 expect(result.detail).toBe("fallback");
300 });
301
302 it("does not expose CLI failure details in the public error message", () => {
303 const result = normalizeCliError(
304 "codex",
305 "generateCommitMessage",
306 new Error("request failed with access_token=secret-token"),
307 "Failed to generate a commit message",
308 );
309
310 expect(result.detail).toBe("Failed to generate a commit message");
311 expect(result.message).not.toContain("secret-token");
312 });
313});