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

ClaudeTextGeneration.test.ts

339 lines11.8 KB

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