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
54describe("buildPrContentPrompt", () => {
55 it("includes branch names, commits, and diff in the prompt", () => {
56 const result = buildPrContentPrompt({
57 baseBranch: "main",
58 headBranch: "feature/auth",
59 commitSummary: "feat: add login page",
60 diffSummary: "3 files changed",
61 diffPatch: "diff --git a/auth.ts b/auth.ts\n+export function login()",
62 });
63
64 expect(result.prompt).toContain("Base branch: main");
65 expect(result.prompt).toContain("Head branch: feature/auth");
66 expect(result.prompt).toContain("Commits:");
67 expect(result.prompt).toContain("feat: add login page");
68 expect(result.prompt).toContain("Diff stat:");
69 expect(result.prompt).toContain("3 files changed");
70 expect(result.prompt).toContain("Diff patch:");
71 expect(result.prompt).toContain("export function login()");
72 });
73});
74
75describe("buildBranchNamePrompt", () => {
76 it("includes the user message in the prompt", () => {
77 const result = buildBranchNamePrompt({
78 message: "Fix the login timeout bug",
79 });
80
81 expect(result.prompt).toContain("User message:");
82 expect(result.prompt).toContain("Fix the login timeout bug");
83 expect(result.prompt).not.toContain("Attachment metadata:");
84 });
85
86 it("includes attachment metadata when attachments are provided", () => {
87 const result = buildBranchNamePrompt({
88 message: "Fix the layout from screenshot",
89 attachments: [
90 {
91 type: "image" as const,
92 id: "att-123",
93 name: "screenshot.png",
94 mimeType: "image/png",
95 sizeBytes: 12345,
96 },
97 ],
98 });
99
100 expect(result.prompt).toContain("Attachment metadata:");
101 expect(result.prompt).toContain("screenshot.png");
102 expect(result.prompt).toContain("image/png");
103 expect(result.prompt).toContain("12345 bytes");
104 });
105});
106
107describe("buildThreadTitlePrompt", () => {
108 it("includes the user message in the prompt", () => {
109 const result = buildThreadTitlePrompt({
110 message: "Investigate reconnect regressions after session restore",
111 });
112
113 expect(result.prompt).toContain("User message:");
114 expect(result.prompt).toContain("Investigate reconnect regressions after session restore");
115 expect(result.prompt).not.toContain("Attachment metadata:");
116 });
117
118 it("includes attachment metadata when attachments are provided", () => {
119 const result = buildThreadTitlePrompt({
120 message: "Name this thread from the screenshot",
121 attachments: [
122 {
123 type: "image" as const,
124 id: "att-456",
125 name: "thread.png",
126 mimeType: "image/png",
127 sizeBytes: 67890,
128 },
129 ],
130 });
131
132 expect(result.prompt).toContain("Attachment metadata:");
133 expect(result.prompt).toContain("thread.png");
134 expect(result.prompt).toContain("image/png");
135 expect(result.prompt).toContain("67890 bytes");
136 });
137});
138
139describe("sanitizeThreadTitle", () => {
140 it("truncates long titles with the shared sidebar-safe limit", () => {
141 expect(
142 sanitizeThreadTitle(
143 ' "Reconnect failures after restart because the session state does not recover" ',
144 ),
145 ).toBe("Reconnect failures after restart because the se...");
146 });
147});
148
149describe("normalizeCliError", () => {
150 it("detects 'Command not found' and includes CLI name in the message", () => {
151 const error = normalizeCliError(
152 "claude",
153 "generateCommitMessage",
154 new Error("Command not found: claude"),
155 "Something went wrong",
156 );
157
158 expect(error).toBeInstanceOf(TextGenerationError);
159 expect(error.detail).toContain("Claude CLI");
160 expect(error.detail).toContain("not available on PATH");
161 });
162
163 it("uses the CLI name from the first argument for codex", () => {
164 const error = normalizeCliError(
165 "codex",
166 "generateBranchName",
167 new Error("Command not found: codex"),
168 "Something went wrong",
169 );
170
171 expect(error).toBeInstanceOf(TextGenerationError);
172 expect(error.detail).toContain("Codex CLI");
173 expect(error.detail).toContain("not available on PATH");
174 });
175
176 it("returns the error as-is if it is already a TextGenerationError", () => {
177 const existing = new TextGenerationError({
178 operation: "generatePrContent",
179 detail: "Already wrapped",
180 });
181
182 const result = normalizeCliError("claude", "generatePrContent", existing, "fallback");
183
184 expect(result).toBe(existing);
185 });
186
187 it("wraps unknown non-Error values with the fallback message", () => {
188 const result = normalizeCliError("codex", "generateCommitMessage", "string error", "fallback");
189
190 expect(result).toBeInstanceOf(TextGenerationError);
191 expect(result.detail).toBe("fallback");
192 });
193
194 it("does not expose CLI failure details in the public error message", () => {
195 const result = normalizeCliError(
196 "codex",
197 "generateCommitMessage",
198 new Error("request failed with access_token=secret-token"),
199 "Failed to generate a commit message",
200 );
201
202 expect(result.detail).toBe("Failed to generate a commit message");
203 expect(result.message).not.toContain("secret-token");
204 });
205});