Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ClaudeTextGeneration.test.ts

309 lines10.1 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { it } from "@effect/vitest";
3import { Effect, FileSystem, Layer, Path } from "effect";
4import { expect } from "vitest";
5​
6import { ServerConfig } from "../../config.ts";
7import { TextGeneration } from "../Services/TextGeneration.ts";
8import { sanitizeThreadTitle } from "../Utils.ts";
9import { ClaudeTextGenerationLive } from "./ClaudeTextGeneration.ts";
10import { ServerSettingsService } from "../../serverSettings.ts";
11​
12const ClaudeTextGenerationTestLayer = ClaudeTextGenerationLive.pipe(
13 Layer.provideMerge(ServerSettingsService.layerTest()),
14 Layer.provideMerge(
15 ServerConfig.layerTest(process.cwd(), {
16 prefix: "t3code-claude-text-generation-test-",
17 }),
18 ),
19 Layer.provideMerge(NodeServices.layer),
20);
21​
22function makeFakeClaudeBinary(dir: string) {
23 return Effect.gen(function* () {
24 const fs = yield* FileSystem.FileSystem;
25 const path = yield* Path.Path;
26 const binDir = path.join(dir, "bin");
27 const claudePath = path.join(binDir, "claude");
28 yield* fs.makeDirectory(binDir, { recursive: true });
29​
30 yield* fs.writeFileString(
31 claudePath,
32 [
33 "#!/bin/sh",
34 'args="$*"',
35 'stdin_content="$(cat)"',
36 'if [ -n "$T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN" ]; then',
37 ' printf "%s" "$args" | grep -F -- "$T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN" >/dev/null || {',
38 ' printf "%s\\n" "args missing expected content" >&2',
39 " exit 2",
40 " }",
41 "fi",
42 'if [ -n "$T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN" ]; then',
43 ' if printf "%s" "$args" | grep -F -- "$T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN" >/dev/null; then',
44 ' printf "%s\\n" "args contained forbidden content" >&2',
45 " exit 3",
46 " fi",
47 "fi",
48 'if [ -n "$T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN" ]; then',
49 ' printf "%s" "$stdin_content" | grep -F -- "$T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN" >/dev/null || {',
50 ' printf "%s\\n" "stdin missing expected content" >&2',
51 " exit 4",
52 " }",
53 "fi",
54 'if [ -n "$T3_FAKE_CLAUDE_STDERR" ]; then',
55 ' printf "%s\\n" "$T3_FAKE_CLAUDE_STDERR" >&2',
56 "fi",
57 'printf "%s" "$T3_FAKE_CLAUDE_OUTPUT"',
58 'exit "${T3_FAKE_CLAUDE_EXIT_CODE:-0}"',
59 "",
60 ].join("\n"),
61 );
62 yield* fs.chmod(claudePath, 0o755);
63 return binDir;
64 });
66​
67function withFakeClaudeEnv<A, E, R>(
68 input: {
69 output: string;
70 exitCode?: number;
71 stderr?: string;
72 argsMustContain?: string;
73 argsMustNotContain?: string;
74 stdinMustContain?: string;
75 },
76 effect: Effect.Effect<A, E, R>,
77) {
78 return Effect.acquireUseRelease(
79 Effect.gen(function* () {
80 const fs = yield* FileSystem.FileSystem;
81 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-claude-text-" });
82 const binDir = yield* makeFakeClaudeBinary(tempDir);
83 const previousPath = process.env.PATH;
84 const previousOutput = process.env.T3_FAKE_CLAUDE_OUTPUT;
85 const previousExitCode = process.env.T3_FAKE_CLAUDE_EXIT_CODE;
86 const previousStderr = process.env.T3_FAKE_CLAUDE_STDERR;
87 const previousArgsMustContain = process.env.T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN;
88 const previousArgsMustNotContain = process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN;
89 const previousStdinMustContain = process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN;
90​
91 yield* Effect.sync(() => {
92 process.env.PATH = `${binDir}:${previousPath ?? ""}`;
93 process.env.T3_FAKE_CLAUDE_OUTPUT = input.output;
94​
95 if (input.exitCode !== undefined) {
96 process.env.T3_FAKE_CLAUDE_EXIT_CODE = String(input.exitCode);
97 } else {
98 delete process.env.T3_FAKE_CLAUDE_EXIT_CODE;
99 }
100​
101 if (input.stderr !== undefined) {
102 process.env.T3_FAKE_CLAUDE_STDERR = input.stderr;
103 } else {
104 delete process.env.T3_FAKE_CLAUDE_STDERR;
105 }
106​
107 if (input.argsMustContain !== undefined) {
108 process.env.T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN = input.argsMustContain;
109 } else {
110 delete process.env.T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN;
111 }
112​
113 if (input.argsMustNotContain !== undefined) {
114 process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN = input.argsMustNotContain;
115 } else {
116 delete process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN;
117 }
118​
119 if (input.stdinMustContain !== undefined) {
120 process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN = input.stdinMustContain;
121 } else {
122 delete process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN;
123 }
124 });
125​
126 return {
127 previousPath,
128 previousOutput,
129 previousExitCode,
130 previousStderr,
131 previousArgsMustContain,
132 previousArgsMustNotContain,
133 previousStdinMustContain,
134 };
135 }),
136 () => effect,
137 (previous) =>
138 Effect.sync(() => {
139 process.env.PATH = previous.previousPath;
140​
141 if (previous.previousOutput === undefined) {
142 delete process.env.T3_FAKE_CLAUDE_OUTPUT;
143 } else {
144 process.env.T3_FAKE_CLAUDE_OUTPUT = previous.previousOutput;
145 }
146​
147 if (previous.previousExitCode === undefined) {
148 delete process.env.T3_FAKE_CLAUDE_EXIT_CODE;
149 } else {
150 process.env.T3_FAKE_CLAUDE_EXIT_CODE = previous.previousExitCode;
151 }
152​
153 if (previous.previousStderr === undefined) {
154 delete process.env.T3_FAKE_CLAUDE_STDERR;
155 } else {
156 process.env.T3_FAKE_CLAUDE_STDERR = previous.previousStderr;
157 }
158​
159 if (previous.previousArgsMustContain === undefined) {
160 delete process.env.T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN;
161 } else {
162 process.env.T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN = previous.previousArgsMustContain;
163 }
164​
165 if (previous.previousArgsMustNotContain === undefined) {
166 delete process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN;
167 } else {
168 process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN = previous.previousArgsMustNotContain;
169 }
170​
171 if (previous.previousStdinMustContain === undefined) {
172 delete process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN;
173 } else {
174 process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN = previous.previousStdinMustContain;
175 }
176 }),
177 );
179​
180it.layer(ClaudeTextGenerationTestLayer)("ClaudeTextGenerationLive", (it) => {
181 it.effect("forwards Claude thinking settings for Haiku without passing effort", () =>
182 withFakeClaudeEnv(
183 {
184 output: JSON.stringify({
185 structured_output: {
186 subject: "Add important change",
187 body: "",
188 },
189 }),
190 argsMustContain: '--settings {"alwaysThinkingEnabled":false}',
191 argsMustNotContain: "--effort",
192 },
193 Effect.gen(function* () {
194 const textGeneration = yield* TextGeneration;
195​
196 const generated = yield* textGeneration.generateCommitMessage({
197 cwd: process.cwd(),
198 branch: "feature/claude-effect",
199 stagedSummary: "M README.md",
200 stagedPatch: "diff --git a/README.md b/README.md",
201 modelSelection: {
202 provider: "claudeAgent",
203 model: "claude-haiku-4-5",
204 options: {
205 thinking: false,
206 effort: "high",
207 },
208 },
209 });
210​
211 expect(generated.subject).toBe("Add important change");
212 }),
213 ),
214 );
215​
216 it.effect("forwards Claude fast mode and supported effort", () =>
217 withFakeClaudeEnv(
218 {
219 output: JSON.stringify({
220 structured_output: {
221 title: "Improve orchestration flow",
222 body: "Body",
223 },
224 }),
225 argsMustContain: '--effort max --settings {"fastMode":true}',
226 },
227 Effect.gen(function* () {
228 const textGeneration = yield* TextGeneration;
229​
230 const generated = yield* textGeneration.generatePrContent({
231 cwd: process.cwd(),
232 baseBranch: "main",
233 headBranch: "feature/claude-effect",
234 commitSummary: "Improve orchestration",
235 diffSummary: "1 file changed",
236 diffPatch: "diff --git a/README.md b/README.md",
237 modelSelection: {
238 provider: "claudeAgent",
239 model: "claude-opus-4-6",
240 options: {
241 effort: "max",
242 fastMode: true,
243 },
244 },
245 });
246​
247 expect(generated.title).toBe("Improve orchestration flow");
248 }),
249 ),
250 );
251​
252 it.effect("generates thread titles through the Claude provider", () =>
253 withFakeClaudeEnv(
254 {
255 output: JSON.stringify({
256 structured_output: {
257 title:
258 ' "Reconnect failures after restart because the session state does not recover" ',
259 },
260 }),
261 stdinMustContain: "You write concise thread titles for coding conversations.",
262 },
263 Effect.gen(function* () {
264 const textGeneration = yield* TextGeneration;
265​
266 const generated = yield* textGeneration.generateThreadTitle({
267 cwd: process.cwd(),
268 message: "Please investigate reconnect failures after restarting the session.",
269 modelSelection: {
270 provider: "claudeAgent",
271 model: "claude-sonnet-4-6",
272 },
273 });
274​
275 expect(generated.title).toBe(
276 sanitizeThreadTitle(
277 '"Reconnect failures after restart because the session state does not recover"',
278 ),
279 );
280 }),
281 ),
282 );
283​
284 it.effect("falls back when Claude thread title normalization becomes whitespace-only", () =>
285 withFakeClaudeEnv(
286 {
287 output: JSON.stringify({
288 structured_output: {
289 title: ' """ """ ',
290 },
291 }),
292 },
293 Effect.gen(function* () {
294 const textGeneration = yield* TextGeneration;
295​
296 const generated = yield* textGeneration.generateThreadTitle({
297 cwd: process.cwd(),
298 message: "Name this thread.",
299 modelSelection: {
300 provider: "claudeAgent",
301 model: "claude-sonnet-4-6",
302 },
303 });
304​
305 expect(generated.title).toBe("New thread");
306 }),
307 ),
308 );
309});