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

TextGeneration.test.ts

120 lines4.4 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 "vitest";
7​
8import { ProviderInstanceId } from "@t3tools/contracts";
9import { createModelSelection } from "@t3tools/shared/model";
10​
11import type { ProviderInstance } from "../provider/ProviderDriver.ts";
12import type { ProviderInstanceRegistryShape } from "../provider/Services/ProviderInstanceRegistry.ts";
13import type { TextGenerationShape } from "./TextGeneration.ts";
14​
15import { makeTextGenerationFromRegistry } from "./TextGeneration.ts";
16​
17const makeStubTextGeneration = (overrides: Partial<TextGenerationShape>): TextGenerationShape => ({
18 generateCommitMessage: () =>
19 Effect.die("generateCommitMessage stub not configured for this test"),
20 generatePrContent: () => Effect.die("generatePrContent stub not configured for this test"),
21 generateBranchName: () => Effect.die("generateBranchName stub not configured for this test"),
22 generateThreadTitle: () => Effect.die("generateThreadTitle stub not configured for this test"),
23 ...overrides,
24});
25​
26const makeStubInstance = (
27 instanceId: ProviderInstanceId,
28 textGeneration: TextGenerationShape,
29): ProviderInstance =>
30 ({
31 instanceId,
32 driverKind: instanceId as unknown as ProviderInstance["driverKind"],
33 continuationIdentity: {
34 driverKind: instanceId as unknown as ProviderInstance["driverKind"],
35 continuationKey: `${instanceId}:test`,
36 },
37 displayName: undefined,
38 enabled: true,
39 snapshot: {} as ProviderInstance["snapshot"],
40 adapter: {} as ProviderInstance["adapter"],
41 textGeneration,
42 }) satisfies ProviderInstance;
43​
44const makeStubRegistry = (
45 instances: ReadonlyArray<ProviderInstance>,
46): ProviderInstanceRegistryShape => {
47 const byId = new Map(instances.map((instance) => [instance.instanceId, instance] as const));
48 return {
49 getInstance: (id) => Effect.succeed(byId.get(id)),
50 listInstances: Effect.succeed(instances),
51 listUnavailable: Effect.succeed([]),
52 streamChanges: Stream.empty,
53 // Tests never drive changes through this stub; acquire a throwaway
54 // subscription on an unused PubSub so the shape is satisfied.
55 subscribeChanges: Effect.flatMap(PubSub.unbounded<void>(), (pubsub) =>
56 PubSub.subscribe(pubsub),
57 ),
58 };
59};
60​
61describe("makeTextGenerationFromRegistry", () => {
62 it.effect("delegates to the matching instance's textGeneration closure", () =>
63 Effect.gen(function* () {
64 const personalId = ProviderInstanceId.make("codex_personal");
65 const personalCalls: string[] = [];
66 const personal = makeStubInstance(
67 personalId,
68 makeStubTextGeneration({
69 generateBranchName: (input) => {
70 personalCalls.push(input.message);
71 return Effect.succeed({ branch: "personal-branch" });
72 },
73 }),
74 );
75​
76 const workId = ProviderInstanceId.make("codex_work");
77 const work = makeStubInstance(
78 workId,
79 makeStubTextGeneration({
80 generateBranchName: () => Effect.succeed({ branch: "work-branch" }),
81 }),
82 );
83​
84 const tg = makeTextGenerationFromRegistry(makeStubRegistry([personal, work]));
85​
86 const result = yield* tg.generateBranchName({
87 cwd: process.cwd(),
88 message: "Refactor the routing layer",
89 modelSelection: createModelSelection(ProviderInstanceId.make("codex_personal"), "gpt-5"),
90 });
91​
92 expect(result.branch).toBe("personal-branch");
93 expect(personalCalls).toEqual(["Refactor the routing layer"]);
94 }),
95 );
96​
97 it.effect("fails with TextGenerationError when the instance is unknown", () =>
98 Effect.gen(function* () {
99 const tg = makeTextGenerationFromRegistry(makeStubRegistry([]));
100​
101 const result = yield* tg
102 .generateBranchName({
103 cwd: process.cwd(),
104 message: "anything",
105 modelSelection: createModelSelection(
106 ProviderInstanceId.make("missing_instance"),
107 "gpt-5",
108 ),
109 })
110 .pipe(Effect.result);
111​
112 expect(Result.isFailure(result)).toBe(true);
113 if (Result.isFailure(result)) {
114 expect(result.failure._tag).toBe("TextGenerationError");
115 expect(result.failure.operation).toBe("generateBranchName");
116 expect(result.failure.detail).toContain("missing_instance");
117 }
118 }),
119 );
120});