Repository navigation
Expand file tree
/
Copy pathCodexTextGeneration.test.ts
More file actions
755 lines (727 loc) · 28.4 KB
/
Copy pathCodexTextGeneration.test.ts
File metadata and controls
755 lines (727 loc) · 28.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
import {
DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER,
ProjectId,
ProviderDriverKind,
type ModelSelection,
type ServerProvider,
} from "@t3tools/contracts";
import type { ProviderInstance } from "@t3tools/provider-core/server/driver";
import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts";
import * as ServerSettings from "../serverSettings.ts";
import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { it } from "@effect/vitest";
import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import * as Scope from "effect/Scope";
import { createModelSelection } from "@t3tools/shared/model";
import { expect } from "vite-plus/test";
import { CodexSettings, ProviderInstanceId } from "@t3tools/contracts";
import * as ServerConfig from "../config.ts";
import { makeProcessProbe, processProbeSource } from "../testUtils/processProbe.ts";
import * as TextGeneration from "./TextGeneration.ts";
import * as TextGenerators from "./TextGenerators.ts";
import { makeCodexTextGeneration } from "./CodexTextGeneration.ts";
import { writeFakeCli } from "@t3tools/provider-testing/fakeCli";
const decodeCodexSettings = Schema.decodeSync(CodexSettings);
const DEFAULT_TEST_MODEL_SELECTION = createModelSelection(
ProviderInstanceId.make("codex"),
"gpt-5.4-mini",
);
const layerCodexTextGenerationTest = ServerConfig.ServerConfig.layerTest(process.cwd(), {
prefix: "t3code-codex-text-generation-test-",
}).pipe(Layer.provideMerge(NodeServices.layer));
interface FakeCodexInput {
output: string;
lifecycle?: { port: number; command: "exec" | "mcp list" };
exitCode?: number;
stderr?: string;
requireImage?: boolean;
requireServiceTier?: string;
requireReasoningEffort?: string;
forbidReasoningEffort?: boolean;
requireArg?: string;
forbidArg?: string;
stdinMustContain?: string;
stdinMustNotContain?: string;
mcpServers?: ReadonlyArray<{ name: string; enabled: boolean }> | number;
requiredCwd?: string;
requireInstructions?: string;
}
function makeFakeCodexBinary(dir: string, input: FakeCodexInput) {
const check = JSON.stringify({
requireImage: input.requireImage ?? false,
requireServiceTier: input.requireServiceTier ?? null,
requireReasoningEffort: input.requireReasoningEffort ?? null,
forbidReasoningEffort: input.forbidReasoningEffort ?? false,
requireArg: input.requireArg ?? null,
forbidArg: input.forbidArg ?? null,
stdinMustContain: input.stdinMustContain ?? null,
stdinMustNotContain: input.stdinMustNotContain ?? null,
mcpServers: input.mcpServers ?? [],
requiredCwd: input.requiredCwd ?? null,
requireInstructions: input.requireInstructions ?? null,
stderr: input.stderr ?? null,
output: input.output,
exitCode: input.exitCode ?? 0,
});
return Effect.gen(function* () {
const path = yield* Path.Path;
return writeFakeCli({
directory: path.join(dir, "bin"),
name: "codex",
source: [
'import * as NodeFS from "node:fs";',
`const check = ${check};`,
"const args = process.argv.slice(2);",
'const isMcpList = args.includes("mcp") && args.includes("list");',
...(input.lifecycle === undefined
? []
: [
`if (isMcpList === ${input.lifecycle.command === "mcp list"}) {`,
'process.on("SIGTERM", () => {});',
processProbeSource(input.lifecycle.port),
"await new Promise(() => {});",
"}",
]),
"if (isMcpList) {",
' if (typeof check.mcpServers === "number") process.exit(check.mcpServers);',
" process.stdout.write(JSON.stringify(check.mcpServers));",
" process.exit(0);",
"}",
'const originalArgs = ` ${args.join(" ")} `;',
"let outputPath = null;",
"let seenImage = false;",
'let seenServiceTier = "";',
'let seenReasoningEffort = "";',
'let instructions = "";',
"for (let index = 0; index < args.length; index += 1) {",
' if (args[index] === "--image") {',
" index += 1;",
" if (args[index]) seenImage = true;",
' } else if (args[index] === "--config") {',
" index += 1;",
' const value = args[index] ?? "";',
' if (value.startsWith("developer_instructions=")) instructions = JSON.parse(value.slice("developer_instructions=".length));',
' if (value.startsWith("service_tier=")) seenServiceTier = value;',
' if (value.startsWith("model_reasoning_effort=")) seenReasoningEffort = value;',
' } else if (args[index] === "--output-last-message") {',
" index += 1;",
" outputPath = args[index] ?? null;",
" }",
"}",
"const chunks = [];",
"for await (const chunk of process.stdin) chunks.push(chunk);",
'const stdinContent = Buffer.concat(chunks).toString("utf8");',
"function fail(message, code) {",
' process.stderr.write(message + "\\n");',
" process.exit(code);",
"}",
'if (check.requiredCwd !== null && process.cwd() !== check.requiredCwd) fail("cannot inspect checkout", 10);',
'if (check.requireInstructions !== null && !instructions.includes(check.requireInstructions)) fail("missing writer task", 11);',
"if (check.requireArg !== null && !originalArgs.includes(` ${check.requireArg} `)) {",
' fail("missing arg: " + check.requireArg, 8);',
"}",
"if (check.forbidArg !== null && originalArgs.includes(` ${check.forbidArg} `)) {",
' fail("forbidden arg: " + check.forbidArg, 9);',
"}",
'if (check.requireImage && !seenImage) fail("missing --image input", 2);',
"if (",
" check.requireServiceTier !== null &&",
' seenServiceTier !== `service_tier="${check.requireServiceTier}"`',
") {",
' fail("unexpected service tier config: " + seenServiceTier, 5);',
"}",
"if (",
" check.requireReasoningEffort !== null &&",
' seenReasoningEffort !== `model_reasoning_effort="${check.requireReasoningEffort}"`',
") {",
' fail("unexpected reasoning effort config: " + seenReasoningEffort, 6);',
"}",
"if (check.forbidReasoningEffort && seenReasoningEffort.length > 0) {",
' fail("reasoning effort config should be omitted: " + seenReasoningEffort, 7);',
"}",
"if (check.stdinMustContain !== null && !stdinContent.includes(check.stdinMustContain)) {",
' fail("stdin missing expected content", 3);',
"}",
"if (check.stdinMustNotContain !== null && stdinContent.includes(check.stdinMustNotContain)) {",
' fail("stdin contained forbidden content", 4);',
"}",
'if (check.stderr !== null) process.stderr.write(check.stderr + "\\n");',
'if (outputPath !== null) NodeFS.writeFileSync(outputPath, check.output + "\\n");',
"process.exitCode = check.exitCode;",
"",
].join("\n"),
});
});
}
function withFakeCodexEnv<A, E, R>(
input: FakeCodexInput & {
launchArgs?: string;
environment?: NodeJS.ProcessEnv;
models?: ReadonlyArray<string>;
defaultModel?: string;
customModels?: ReadonlyArray<string>;
managedRuntime?: boolean;
},
effectFn: (textGeneration: TextGeneration.TextGeneration["Service"]) => Effect.Effect<A, E, R>,
) {
return Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-codex-text-" });
const codexPath = yield* makeFakeCodexBinary(tempDir, input);
const config = decodeCodexSettings({
binaryPath: codexPath,
launchArgs: input.launchArgs,
customModels: input.customModels,
});
const textGeneration = yield* makeCodexTextGeneration(
config,
input.environment === undefined ? undefined : { ...process.env, ...input.environment },
Effect.succeed(
(input.models ?? []).map((slug) => ({
slug,
name: slug,
isCustom: false,
...(slug === input.defaultModel ? { isDefault: true } : {}),
capabilities: null,
})),
),
input.managedRuntime
? Effect.succeed({
config,
environment: input.environment ?? process.env,
revision: "test",
})
: undefined,
);
return yield* effectFn(textGeneration);
}).pipe(Effect.scoped);
}
it.layer(layerCodexTextGenerationTest)("CodexTextGeneration", (it) => {
it.effect.each(["exec", "mcp list"] as const)(
"kills a stalled %s CLI when its request is interrupted",
(command) =>
Effect.gen(function* () {
const probe = yield* makeProcessProbe;
yield* withFakeCodexEnv(
{ output: "", lifecycle: { port: probe.port, command } },
(textGeneration) =>
Effect.gen(function* () {
const fiber = yield* textGeneration
.generateThreadTitle({
cwd: process.cwd(),
message: "Name this thread",
modelSelection: DEFAULT_TEST_MODEL_SELECTION,
})
.pipe(Effect.forkScoped);
yield* probe.started;
const interrupted = yield* Fiber.interrupt(fiber).pipe(Effect.forkScoped);
yield* probe.exited;
yield* Fiber.join(interrupted);
const exit = yield* Fiber.await(fiber);
expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true);
}),
);
}),
);
it.effect.each([
{
selectedModel: "gpt-5.6-luna",
models: ["openai.gpt-5.6-luna"],
expected: "openai.gpt-5.6-luna",
},
{
selectedModel: "openai.gpt-5.6-luna",
models: ["openai.gpt-5.6-luna"],
expected: "openai.gpt-5.6-luna",
},
{
selectedModel: "openai.gpt-5.6-luna",
models: ["gpt-6-astra", "gpt-5.6-luna"],
defaultModel: "gpt-6-astra",
expected: "gpt-5.6-luna",
},
{
selectedModel: "gpt-6-luna",
models: ["gpt-5.6-luna", "gpt-6-astra"],
defaultModel: "gpt-6-astra",
expected: "gpt-6-astra",
},
{ selectedModel: "gpt-6-luna", models: ["gpt-5.6-luna"], expected: "gpt-5.6-luna" },
{ selectedModel: "gpt-6-luna", models: [], expected: "gpt-6-luna" },
])(
"dispatches $expected for $selectedModel from $models",
({ selectedModel, models, defaultModel, expected }) =>
withFakeCodexEnv(
{
output: JSON.stringify({ title: "Live title" }),
models,
...(defaultModel ? { defaultModel } : {}),
requireArg: `--model ${expected}`,
},
(textGeneration) =>
Effect.gen(function* () {
const result = yield* textGeneration.generateThreadTitle({
cwd: process.cwd(),
message: "Describe this change",
modelSelection: createModelSelection(ProviderInstanceId.make("codex"), selectedModel),
});
expect(result.title).toBe("Live title");
}),
),
);
it.effect("drops the requested model's options when falling back to another model", () =>
withFakeCodexEnv(
{
output: JSON.stringify({ title: "Live title" }),
models: ["gpt-6-astra"],
requireArg: '--model gpt-6-astra --config model_reasoning_effort="low"',
forbidArg: 'service_tier="priority"',
},
(textGeneration) =>
textGeneration.generateThreadTitle({
cwd: process.cwd(),
message: "Describe this change",
modelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-6-luna", [
{ id: "reasoningEffort", value: "none" },
{ id: "serviceTier", value: "priority" },
]),
}),
),
);
it.effect("sends a configured custom model the managed snapshot leaves out", () =>
withFakeCodexEnv(
{
output: JSON.stringify({ title: "Live title" }),
models: ["gpt-6-astra", "gpt-5.6-luna"],
defaultModel: "gpt-6-astra",
customModels: ["gpt-5.6-luna-preview"],
managedRuntime: true,
requireArg: '--model gpt-5.6-luna-preview --config model_reasoning_effort="high"',
},
(textGeneration) =>
textGeneration.generateThreadTitle({
cwd: process.cwd(),
message: "Describe this change",
modelSelection: createModelSelection(
ProviderInstanceId.make("codex"),
"gpt-5.6-luna-preview",
[{ id: "reasoningEffort", value: "high" }],
),
}),
),
);
it.effect("omits a persisted service tier for managed ChatGPT text generation", () =>
withFakeCodexEnv(
{
output: JSON.stringify({ subject: "Update project", body: "" }),
managedRuntime: true,
forbidArg: 'service_tier="priority"',
},
(textGeneration) =>
textGeneration.generateCommitMessage({
cwd: process.cwd(),
branch: "feature/chatgpt",
stagedSummary: "M README.md",
stagedPatch: "diff --git a/README.md b/README.md",
modelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
{ id: "serviceTier", value: "priority" },
]),
}),
),
);
it.effect("preserves custom PR instructions while allowing checkout inspection", () =>
withFakeCodexEnv(
{
output: JSON.stringify({ title: "Remove default shapes", body: "Removes default shapes." }),
launchArgs: "--config 'developer_instructions=\"Use TEAM: as the PR title prefix\"'",
requiredCwd: process.cwd(),
requireInstructions: "Use TEAM: as the PR title prefix",
forbidArg: "--config features.shell_tool=false",
mcpServers: [
{ name: "augmenter", enabled: true },
{ name: "my.server", enabled: true },
{ name: "already-off", enabled: false },
],
requireArg:
'--config features.plugins=false --config features.apps=false --config mcp_servers={"augmenter"={enabled=false},"my.server"={enabled=false}}',
},
(textGeneration) =>
Effect.gen(function* () {
const result = yield* textGeneration.generatePrContent({
cwd: process.cwd(),
baseBranch: "main",
headBranch: "remove-default-shapes",
commitSummary: "Remove default shapes",
diffSummary: "shapes.ts | 2 --",
diffPatch: "-addDefaultShapes();",
threadContext: "user: in a few minutes PR will be up then $babysit-pr",
modelSelection: DEFAULT_TEST_MODEL_SELECTION,
});
expect(result.title).toBe("Remove default shapes");
}),
),
);
it.effect("still generates when Codex cannot list its MCP servers", () =>
withFakeCodexEnv(
{
output: JSON.stringify({ branch: "remove-default-shapes" }),
mcpServers: 2,
requireArg: "--config features.plugins=false --config features.apps=false --ephemeral",
forbidArg: "mcp_servers=",
},
(textGeneration) =>
Effect.gen(function* () {
const result = yield* textGeneration.generateBranchName({
cwd: process.cwd(),
message: "Remove default shapes from characters",
modelSelection: DEFAULT_TEST_MODEL_SELECTION,
});
expect(result.branch).toBe("remove-default-shapes");
}),
),
);
it.effect("keeps plugins and apps off over launch args that turn them on", () =>
withFakeCodexEnv(
{
output: JSON.stringify({ title: "Remove default shapes" }),
launchArgs: "--config features.plugins=true --config features.apps=true",
requireArg:
"--config features.plugins=true --config features.apps=true --config features.plugins=false --config features.apps=false --ephemeral",
},
(textGeneration) =>
Effect.gen(function* () {
const result = yield* textGeneration.generateThreadTitle({
cwd: process.cwd(),
message: "Remove default shapes from characters",
modelSelection: DEFAULT_TEST_MODEL_SELECTION,
});
expect(result.title).toBe("Remove default shapes");
}),
),
);
it.effect("passes exec-safe launch args into codex exec", () =>
withFakeCodexEnv(
{
output: JSON.stringify({
subject: "Add important change",
body: "",
}),
launchArgs: "--strict-config --listen off",
requireArg: "--strict-config",
forbidArg: "--listen",
},
(textGeneration) =>
textGeneration.generateCommitMessage({
cwd: process.cwd(),
branch: "feature/codex-effect",
stagedSummary: "M README.md",
stagedPatch: "diff --git a/README.md b/README.md",
modelSelection: DEFAULT_TEST_MODEL_SELECTION,
}),
),
);
it.effect("generates branch names even when the ambient scope is already closed", () =>
withFakeCodexEnv(
{
output: JSON.stringify({
branch: "feat/background-generation",
}),
},
(textGeneration) =>
Effect.gen(function* () {
// Background fibers can outlive request scopes; temp files scoped there would be reaped on creation.
const closedScope = yield* Scope.make();
yield* Scope.close(closedScope, Exit.void);
const generated = yield* textGeneration
.generateBranchName({
cwd: process.cwd(),
message: "Please update session handling.",
modelSelection: DEFAULT_TEST_MODEL_SELECTION,
})
.pipe(Effect.provideService(Scope.Scope, closedScope));
expect(generated.branch).toBe("feat/background-generation");
}),
),
);
it.effect("returns the refinement signal for an unresolved subject", () =>
withFakeCodexEnv(
{ output: JSON.stringify({ title: "Investigate issue", needsRefinement: true }) },
(textGeneration) =>
Effect.gen(function* () {
expect(
yield* textGeneration.generateThreadTitle({
cwd: process.cwd(),
message: "Fix this",
modelSelection: DEFAULT_TEST_MODEL_SELECTION,
}),
).toEqual({ title: "Investigate issue", needsRefinement: true });
}),
),
);
it.effect("resolves persisted attachment ids to files for codex image inputs", () =>
withFakeCodexEnv(
{
output: JSON.stringify({
branch: "fix/ui-regression",
}),
requireImage: true,
},
(textGeneration) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const { attachmentsDir } = yield* ServerConfig.ServerConfig;
const attachmentId = "thread-1-attachment";
const imagePath = path.join(attachmentsDir, `${attachmentId}.png`);
yield* fs.makeDirectory(attachmentsDir, { recursive: true });
yield* fs.writeFile(imagePath, Buffer.from("hello"));
const generated = yield* textGeneration
.generateBranchName({
modelSelection: DEFAULT_TEST_MODEL_SELECTION,
cwd: process.cwd(),
message: "Fix layout bug from screenshot.",
attachments: [
{
type: "image",
id: attachmentId,
name: "bug.png",
mimeType: "image/png",
sizeBytes: 5,
},
],
})
.pipe(
Effect.tap(() =>
fs.stat(imagePath).pipe(
Effect.map((fileInfo) => {
expect(fileInfo.type).toBe("File");
}),
),
),
Effect.ensuring(fs.remove(imagePath).pipe(Effect.ignore)),
);
expect(generated.branch).toBe("fix/ui-regression");
}),
),
);
});
const PREFLIGHT_PRIMARY_STATES = [
"ready",
"pending",
"exhausted",
"not installed",
"not installed, fallback disabled for project",
"signed out",
"signed out, fallback failed",
] as const;
it.effect.each(PREFLIGHT_PRIMARY_STATES)("text generation preflight: %s", (primaryState) =>
Effect.gen(function* () {
const primary = ProviderInstanceId.make("codex");
const fallback = ProviderInstanceId.make("claude_work");
const projectId = ProjectId.make("project-a");
const selection = {
instanceId: primary,
model: "chat-model",
options: [{ id: "effort", value: "high" }],
};
const calls: ModelSelection[] = [];
const instances: ProviderInstance[] = [primary, fallback].map((instanceId) => {
const driver = ProviderDriverKind.make(instanceId === primary ? "codex" : "claudeAgent");
const snapshot: ServerProvider = {
instanceId,
driver,
enabled: true,
installed: true,
status: "ready",
auth: { status: "authenticated" },
version: null,
checkedAt: "1970-01-01T00:00:00Z",
models: [],
slashCommands: [],
skills: [],
...(primaryState === "pending" && instanceId === primary
? { installed: false, status: "warning", auth: { status: "unknown" } }
: {}),
...(primaryState.startsWith("not installed") && instanceId === primary
? { installed: false, status: "error", auth: { status: "unknown" } }
: {}),
...(primaryState.startsWith("signed out") && instanceId === primary
? { status: "error", auth: { status: "unauthenticated" } }
: {}),
...(primaryState === "signed out, fallback failed" && instanceId === fallback
? { status: "error", auth: { status: "unknown" } }
: {}),
...(primaryState === "exhausted" && instanceId === primary
? {
usageLimits: {
checkedAt: "1970-01-01T00:00:00Z",
windows: [{ id: "session", kind: "session", label: "Session", usedPercent: 100 }],
},
}
: {}),
};
return {
instanceId,
driverKind: driver,
continuationIdentity: { driverKind: driver, continuationKey: instanceId },
displayName: undefined,
enabled: true,
snapshot: { getSnapshot: Effect.succeed(snapshot) } as ProviderInstance["snapshot"],
orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"],
textGeneration: TextGeneration.TextGeneration.of({
generateCommitMessage: () => Effect.die("unused"),
generatePrContent: () => Effect.die("unused"),
generateBranchName: () => Effect.die("unused"),
generateReviewGuide: () => Effect.die("unused"),
generateTranscriptionPostProcessing: () => Effect.die("unused"),
generateThreadTitle: (input) =>
Effect.sync(() => {
calls.push(input.modelSelection);
return { title: "Generated title" };
}),
}),
};
});
const service = yield* TextGeneration.make.pipe(
Effect.provide(
Layer.mergeAll(
Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({
listInstances: Effect.succeed(instances),
getInstance: (id) =>
Effect.succeed(instances.find((instance) => instance.instanceId === id)),
}),
ServerSettings.layerTest({
providerInstances: {
[fallback]: { driver: ProviderDriverKind.make("claudeAgent"), config: {} },
},
...(primaryState === "not installed, fallback disabled for project"
? {
projectSettingsOverrides: {
[projectId]: { providerInstances: { [fallback]: { enabled: false } } },
},
}
: {}),
}),
Layer.mock(TextGenerators.TextGenerators)({ runner: () => Effect.undefined }),
Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({
resolveLink: () => Effect.die("unused"),
}),
),
),
);
expect(
yield* service.generateThreadTitle({
cwd: "/tmp",
message: "hello",
linkedContext: "",
modelSelection: selection,
projectId,
}),
).toEqual({ title: "Generated title" });
expect(calls).toEqual([
primaryState === "ready" ||
primaryState === "pending" ||
primaryState === "not installed, fallback disabled for project" ||
primaryState === "signed out, fallback failed"
? selection
: {
instanceId: fallback,
model:
DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER[ProviderDriverKind.make("claudeAgent")],
},
]);
}),
);
it.effect(
"a review guide aimed at a text generator falls back to an agent that can read the checkout",
() =>
Effect.gen(function* () {
const claude = ProviderInstanceId.make("claudeAgent");
const driver = ProviderDriverKind.make("claudeAgent");
const calls: ModelSelection[] = [];
const instance = {
instanceId: claude,
driverKind: driver,
continuationIdentity: { driverKind: driver, continuationKey: claude },
displayName: undefined,
enabled: true,
snapshot: {
getSnapshot: Effect.succeed<ServerProvider>({
instanceId: claude,
driver,
enabled: true,
installed: true,
status: "ready",
auth: { status: "authenticated" },
version: null,
checkedAt: "1970-01-01T00:00:00Z",
models: [],
slashCommands: [],
skills: [],
}),
} as ProviderInstance["snapshot"],
orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"],
textGeneration: TextGeneration.TextGeneration.of({
generateCommitMessage: () => Effect.die("unused"),
generatePrContent: () => Effect.die("unused"),
generateBranchName: () => Effect.die("unused"),
generateThreadTitle: () => Effect.die("unused"),
generateTranscriptionPostProcessing: () => Effect.die("unused"),
generateReviewGuide: (input) =>
Effect.sync(() => {
calls.push(input.modelSelection);
return {} as TextGeneration.ReviewGuideGenerationResult;
}),
}),
} satisfies ProviderInstance;
const serviceWith = (instances: ReadonlyArray<ProviderInstance>) =>
TextGeneration.make.pipe(
Effect.provide(
Layer.mergeAll(
Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({
listInstances: Effect.succeed(instances),
getInstance: (id) =>
Effect.succeed(instances.find((candidate) => candidate.instanceId === id)),
}),
ServerSettings.layerTest({}),
Layer.mock(TextGenerators.TextGenerators)({
runner: (id) =>
Effect.succeed(
id === "openrouter"
? () => Effect.die("a text generator cannot investigate")
: undefined,
),
}),
Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({
resolveLink: () => Effect.die("unused"),
}),
),
),
);
const input = {
cwd: "/tmp",
number: 1,
title: "Change",
description: "",
baseBranch: "main",
baseCommit: "abc",
settledReview: "",
modelSelection: { instanceId: ProviderInstanceId.make("openrouter"), model: "some/model" },
};
yield* (yield* serviceWith([instance])).generateReviewGuide(input);
expect(calls.map((selection) => selection.instanceId)).toEqual([claude]);
// With no agent ready, it says what is missing instead of looking up a provider by that id.
const failure = yield* Effect.flip((yield* serviceWith([])).generateReviewGuide(input));
expect(failure.detail).toMatch(
/^Review guides need a coding agent that can read the checkout/,
);
}),
);