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

TextGeneration.test.ts

173 lines6.3 KB

1import { it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as PubSub from "effect/PubSub";
4import * as Result from "effect/Result";
5import * as Stream from "effect/Stream";
6import { describe, expect } from "vite-plus/test";
7​
8import { ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts";
9import { createModelSelection } from "@t3tools/shared/model";
10​
11import type { ProviderInstance } from "../provider/ProviderDriver.ts";
12import * as ProviderInstanceRegistry from "../provider/Services/ProviderInstanceRegistry.ts";
13import * as TextGeneration from "./TextGeneration.ts";
14​
15const makeStubTextGeneration = (
16 overrides: Partial<TextGeneration.TextGeneration["Service"]>,
17): TextGeneration.TextGeneration["Service"] =>
18 TextGeneration.TextGeneration.of({
19 generateCommitMessage: () =>
20 Effect.die("generateCommitMessage stub not configured for this test"),
21 generatePrContent: () => Effect.die("generatePrContent stub not configured for this test"),
22 generateBranchName: () => Effect.die("generateBranchName stub not configured for this test"),
23 generateThreadTitle: () => Effect.die("generateThreadTitle stub not configured for this test"),
24 generateHandover: () => Effect.die("generateHandover stub not configured for this test"),
25 ...overrides,
26 });
27​
28const makeStubInstance = (
29 instanceId: ProviderInstanceId,
30 textGeneration: TextGeneration.TextGeneration["Service"],
31 driverKind: ProviderInstance["driverKind"] = instanceId as unknown as ProviderInstance["driverKind"],
32): ProviderInstance =>
33 ({
34 instanceId,
35 driverKind,
36 continuationIdentity: {
37 driverKind,
38 continuationKey: `${instanceId}:test`,
39 },
40 displayName: undefined,
41 enabled: true,
42 snapshot: {} as ProviderInstance["snapshot"],
43 adapter: {} as ProviderInstance["adapter"],
44 textGeneration,
45 }) satisfies ProviderInstance;
46​
47const makeStubRegistry = (
48 instances: ReadonlyArray<ProviderInstance>,
49): ProviderInstanceRegistry.ProviderInstanceRegistry["Service"] => {
50 const byId = new Map(instances.map((instance) => [instance.instanceId, instance] as const));
51 return {
52 getInstance: (id) => Effect.succeed(byId.get(id)),
53 listInstances: Effect.succeed(instances),
54 listUnavailable: Effect.succeed([]),
55 streamChanges: Stream.empty,
56 // Tests never drive changes through this stub; acquire a throwaway
57 // subscription on an unused PubSub so the shape is satisfied.
58 subscribeChanges: Effect.flatMap(PubSub.unbounded<void>(), (pubsub) =>
59 PubSub.subscribe(pubsub),
60 ),
61 };
62};
63​
64describe("makeTextGenerationFromRegistry", () => {
65 it.effect("delegates to the matching instance's textGeneration closure", () =>
66 Effect.gen(function* () {
67 const personalId = ProviderInstanceId.make("codex_personal");
68 const personalCalls: string[] = [];
69 const personal = makeStubInstance(
70 personalId,
71 makeStubTextGeneration({
72 generateBranchName: (input) => {
73 personalCalls.push(input.message);
74 return Effect.succeed({ branch: "personal-branch" });
75 },
76 }),
77 );
78​
79 const workId = ProviderInstanceId.make("codex_work");
80 const work = makeStubInstance(
81 workId,
82 makeStubTextGeneration({
83 generateBranchName: () => Effect.succeed({ branch: "work-branch" }),
84 }),
85 );
86​
87 const tg = TextGeneration.makeTextGenerationFromRegistry(makeStubRegistry([personal, work]));
88​
89 const result = yield* tg.generateBranchName({
90 cwd: process.cwd(),
91 message: "Refactor the routing layer",
92 modelSelection: createModelSelection(ProviderInstanceId.make("codex_personal"), "gpt-5"),
93 });
94​
95 expect(result.branch).toBe("personal-branch");
96 expect(personalCalls).toEqual(["Refactor the routing layer"]);
97 }),
98 );
99​
100 it.effect("delegates handover generation with its exact model selection", () =>
101 Effect.gen(function* () {
102 const instanceId = ProviderInstanceId.make("codex");
103 const selections: Array<{ readonly model: string; readonly effort: unknown }> = [];
104 const instance = makeStubInstance(
105 instanceId,
106 makeStubTextGeneration({
107 generateHandover: (input) => {
108 selections.push({
109 model: input.modelSelection.model,
110 effort: input.modelSelection.options?.find(
111 (option) => option.id === "reasoningEffort",
112 )?.value,
113 });
114 return Effect.succeed({ handover: "# Goal\n\nContinue the migration." });
115 },
116 }),
117 );
118​
119 const tg = TextGeneration.makeTextGenerationFromRegistry(makeStubRegistry([instance]));
120 const result = yield* tg.generateHandover({
121 cwd: process.cwd(),
122 threadContents: "User: Continue the migration",
123 modelSelection: createModelSelection(instanceId, "gpt-5.6-luna", [
124 { id: "reasoningEffort", value: "high" },
125 ]),
126 });
127​
128 expect(result.handover).toContain("Continue the migration");
129 expect(selections).toEqual([{ model: "gpt-5.6-luna", effort: "high" }]);
130 }),
131 );
132​
133 it("finds an enabled Codex driver without assuming its instance id", () => {
134 const renamedCodex = makeStubInstance(
135 ProviderInstanceId.make("codex_work"),
136 makeStubTextGeneration({}),
137 ProviderDriverKind.make("codex"),
138 );
139 const disabledCodex = {
140 ...renamedCodex,
141 instanceId: ProviderInstanceId.make("codex_disabled"),
142 enabled: false,
143 };
144​
145 expect(
146 TextGeneration.findAvailableCodexInstance([disabledCodex, renamedCodex])?.instanceId,
147 ).toBe("codex_work");
148 });
149​
150 it.effect("fails with TextGenerationError when the instance is unknown", () =>
151 Effect.gen(function* () {
152 const tg = TextGeneration.makeTextGenerationFromRegistry(makeStubRegistry([]));
153​
154 const result = yield* tg
155 .generateBranchName({
156 cwd: process.cwd(),
157 message: "anything",
158 modelSelection: createModelSelection(
159 ProviderInstanceId.make("missing_instance"),
160 "gpt-5",
161 ),
162 })
163 .pipe(Effect.result);
164​
165 expect(Result.isFailure(result)).toBe(true);
166 if (Result.isFailure(result)) {
167 expect(result.failure._tag).toBe("TextGenerationError");
168 expect(result.failure.operation).toBe("generateBranchName");
169 expect(result.failure.detail).toContain("missing_instance");
170 }
171 }),
172 );
173});