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

ClaudeTextGeneration.test.ts

306 lines10.2 KB

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