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

TextGeneration.test.ts

117 lines4.3 KB

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