Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFSP from "node:fs/promises";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import * as NodeURL from "node:url";
6
7import * as NodeServices from "@effect/platform-node/NodeServices";
8import { it } from "@effect/vitest";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import * as Schema from "effect/Schema";
12import { DevinSettings, ProviderInstanceId } from "@t3tools/contracts";
13import { isHostWindows } from "@t3tools/shared/hostProcess";
14import { createModelSelection } from "@t3tools/shared/model";
15import { expect } from "vite-plus/test";
16
17import * as ServerConfig from "../config.ts";
18import * as TextGeneration from "./TextGeneration.ts";
19import { makeDevinTextGeneration } from "./DevinTextGeneration.ts";
20
21const decodeDevinSettings = Schema.decodeSync(DevinSettings);
22const __dirname = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
23const mockAgentPath = NodePath.join(__dirname, "../../scripts/acp-mock-agent.ts");
24const mockAgentCommand = process.execPath;
25const shellQuote = (value: string) => `'${value.replaceAll("'", `'\\''`)}'`;
26
27const DevinTextGenerationTestLayer = ServerConfig.ServerConfig.layerTest(process.cwd(), {
28 prefix: "t3code-devin-text-generation-test-",
29}).pipe(Layer.provideMerge(NodeServices.layer));
30
31// The shared ACP fixture advertises `default`, while the real Devin catalog
32// supplies `adaptive`. Text-generation behavior does not depend on that name.
33const modelSelection = createModelSelection(ProviderInstanceId.make("devin"), "default");
34
35function makeThreadTitleInput() {
36 return {
37 cwd: process.cwd(),
38 message: "Add Devin text generation coverage.",
39 modelSelection,
40 } satisfies TextGeneration.ThreadTitleGenerationInput;
41}
42
43function makeCommitMessageInput() {
44 return {
45 cwd: process.cwd(),
46 branch: "feature/devin-text-generation",
47 stagedSummary: "M apps/server/src/textGeneration/DevinTextGeneration.ts",
48 stagedPatch: "diff --git a/apps/server/src/textGeneration/DevinTextGeneration.ts b/...",
49 modelSelection,
50 } satisfies TextGeneration.CommitMessageGenerationInput;
51}
52
53const makeMockDevinWrapper = Effect.fn("makeMockDevinWrapper")(function* (
54 extraEnv?: Record<string, string>,
55) {
56 const isWindows = yield* isHostWindows;
57 const dir = yield* Effect.promise(() =>
58 NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "devin-text-generation-mock-")),
59 );
60
61 if (isWindows) {
62 const wrapperPath = NodePath.join(dir, "fake-devin.cmd");
63 const envLines = Object.entries(extraEnv ?? {})
64 .map(([key, value]) => `set ${key}=${value}`)
65 .join("\r\n");
66 const script = `@echo off\r\n${envLines}\r\n"${mockAgentCommand}" "${mockAgentPath}" %*\r\n`;
67 yield* Effect.promise(() => NodeFSP.writeFile(wrapperPath, script, "utf8"));
68 return wrapperPath;
69 }
70
71 const wrapperPath = NodePath.join(dir, "fake-devin.sh");
72 const envExports = Object.entries(extraEnv ?? {})
73 .map(([key, value]) => `export ${key}=${shellQuote(value)}`)
74 .join("\n");
75 const script = `#!/bin/sh
76${envExports}
77exec ${shellQuote(mockAgentCommand)} ${shellQuote(mockAgentPath)} "$@"
78`;
79 yield* Effect.promise(() => NodeFSP.writeFile(wrapperPath, script, "utf8"));
80 yield* Effect.promise(() => NodeFSP.chmod(wrapperPath, 0o755));
81 return wrapperPath;
82});
83
84function withFakeDevin<A, E, R>(
85 env: Record<string, string>,
86 effectFn: (textGeneration: TextGeneration.TextGeneration["Service"]) => Effect.Effect<A, E, R>,
87 options?: Parameters<typeof makeDevinTextGeneration>[2],
88) {
89 return Effect.gen(function* () {
90 const wrapperPath = yield* makeMockDevinWrapper(env);
91 const textGeneration = yield* makeDevinTextGeneration(
92 decodeDevinSettings({ enabled: true, binaryPath: wrapperPath, customModels: [] }),
93 undefined,
94 options,
95 );
96 return yield* effectFn(textGeneration);
97 }).pipe(Effect.scoped);
98}
99
100it.layer(DevinTextGenerationTestLayer, { excludeTestServices: true })(
101 "DevinTextGeneration",
102 (it) => {
103 it.effect("decodes successful structured output", () =>
104 withFakeDevin(
105 {
106 T3_ACP_PROMPT_RESPONSE_TEXT: JSON.stringify({
107 subject: "Add Devin text generation coverage",
108 body: "Cover the ACP text-generation boundary.",
109 }),
110 },
111 (textGeneration) =>
112 Effect.gen(function* () {
113 const generated = yield* textGeneration.generateCommitMessage(makeCommitMessageInput());
114 expect(generated.subject).toBe("Add Devin text generation coverage");
115 expect(generated.body).toBe("Cover the ACP text-generation boundary.");
116 }),
117 ),
118 );
119
120 it.effect("accepts a large structured response without truncating it", () => {
121 const body = "large response ".repeat(1_024).trimEnd();
122 return withFakeDevin(
123 {
124 T3_ACP_PROMPT_RESPONSE_TEXT: JSON.stringify({
125 title: "Handle a large Devin response",
126 body,
127 }),
128 },
129 (textGeneration) =>
130 Effect.gen(function* () {
131 const generated = yield* textGeneration.generatePrContent({
132 cwd: process.cwd(),
133 baseBranch: "main",
134 headBranch: "feature/devin-text-generation",
135 commitSummary: "Add Devin text generation coverage",
136 diffSummary: "M apps/server/src/textGeneration/DevinTextGeneration.ts",
137 diffPatch: "diff --git a/apps/server/src/textGeneration/DevinTextGeneration.ts b/...",
138 modelSelection,
139 });
140 expect(generated.body).toBe(body);
141 }),
142 );
143 });
144
145 it.effect("returns a typed error for malformed JSON", () =>
146 withFakeDevin({ T3_ACP_PROMPT_RESPONSE_TEXT: "not structured JSON" }, (textGeneration) =>
147 Effect.gen(function* () {
148 const error = yield* Effect.flip(
149 textGeneration.generateThreadTitle(makeThreadTitleInput()),
150 );
151 expect(error._tag).toBe("TextGenerationError");
152 expect(error.detail).toMatch(/invalid structured output/i);
153 }),
154 ),
155 );
156
157 it.effect("returns a typed error for empty output", () =>
158 withFakeDevin({ T3_ACP_PROMPT_RESPONSE_TEXT: " \n " }, (textGeneration) =>
159 Effect.gen(function* () {
160 const error = yield* Effect.flip(
161 textGeneration.generateThreadTitle(makeThreadTitleInput()),
162 );
163 expect(error._tag).toBe("TextGenerationError");
164 expect(error.detail).toMatch(/empty/i);
165 }),
166 ),
167 );
168
169 it.effect("maps process startup failures to TextGenerationError", () =>
170 Effect.gen(function* () {
171 const isWindows = yield* isHostWindows;
172 const missingDir = yield* Effect.promise(() =>
173 NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "devin-text-generation-missing-")),
174 );
175 const missingBinary = NodePath.join(missingDir, `devin${isWindows ? ".exe" : ""}`);
176 const textGeneration = yield* makeDevinTextGeneration(
177 decodeDevinSettings({ enabled: true, binaryPath: missingBinary, customModels: [] }),
178 );
179 const error = yield* Effect.flip(
180 textGeneration.generateThreadTitle(makeThreadTitleInput()),
181 );
182 expect(error._tag).toBe("TextGenerationError");
183 expect(error.operation).toBe("generateThreadTitle");
184 }).pipe(Effect.scoped),
185 );
186
187 it.effect("times out and terminates a stalled ACP process", () => {
188 // The scoped text-generation effect must close the ACP child after its
189 // timeout. If cleanup regresses, this test hangs on the stalled mock.
190 return withFakeDevin(
191 { T3_ACP_HANG_PROMPT_FOREVER: "1" },
192 (textGeneration) =>
193 Effect.gen(function* () {
194 const error = yield* Effect.flip(
195 textGeneration.generateThreadTitle(makeThreadTitleInput()),
196 );
197 expect(error._tag).toBe("TextGenerationError");
198 expect(error.detail).toMatch(/timed out/i);
199 }),
200 { timeout: 50 },
201 );
202 });
203 },
204);