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

textGeneration.test.ts

285 lines10.0 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import type { NotificationHandler } from "@muse-code/sdk";
4import { ProviderInstanceId, type ServerProviderModel } from "@t3tools/contracts";
5import { MuseSettings } from "../settings.ts";
6import { createModelSelection } from "@t3tools/shared/model";
7import * as Effect from "effect/Effect";
8import * as Fiber from "effect/Fiber";
9import * as Schema from "effect/Schema";
10import { vi } from "vite-plus/test";
11​
12import type { MuseSdkHost } from "./sdk.ts";
13import { museModelCapabilities } from "./modelCatalog.ts";
14import { makeMuseTextGeneration } from "./textGeneration.ts";
15​
16const settings = Schema.decodeSync(MuseSettings)({ enabled: true });
17const modelSelection = createModelSelection(
18 ProviderInstanceId.make("muse"),
19 "muse-spark-1.3-contributor",
20 [{ id: "reasoningEffort", value: "high" }],
21);
22const titleInput = {
23 cwd: "/project/should-not-be-used",
24 message: "Fix the login form",
25 modelSelection,
26};
27​
28function fixture(
29 onTurn?: (emit: (method: string, params: Record<string, unknown>) => void) => void,
30 acknowledgeTurn = true,
31 modelCatalog?: Effect.Effect<ReadonlyArray<ServerProviderModel>>,
32) {
33 let handler: NotificationHandler = () => {};
34 let commandSequence = 0;
35 let activeTurnId: string | undefined;
36 const mintCommandId = () => `turn-${++commandSequence}`;
37 const started = Promise.withResolvers<void>();
38 const closed = Promise.withResolvers<void>();
39 const emit = (method: string, params: Record<string, unknown>) =>
40 handler({
41 jsonrpc: "2.0",
42 method,
43 params: {
44 sessionId: "session-1",
45 turnId: activeTurnId,
46 ...params,
47 ...(params.item
48 ? {
49 item: {
50 revision: 1,
51 turnId: activeTurnId,
52 ...(params.item as Record<string, unknown>),
53 },
54 }
55 : {}),
56 },
57 });
58 const host: MuseSdkHost = {
59 initializeResult: { grantedCapabilities: [] },
60 connection: {
61 command: vi.fn(async (method, _params, options) => {
62 // Connection.command overwrites params.commandId unless supplied in options.
63 const commandId = options?.commandId ?? mintCommandId();
64 if (method === "session/start") return { session: { sessionId: "session-1" } };
65 if (method === "turn/start") {
66 activeTurnId = commandId;
67 started.resolve();
68 onTurn?.(emit);
69 if (!acknowledgeTurn) return await new Promise<Record<string, unknown>>(() => {});
70 return { turnId: commandId };
71 }
72 return {};
73 }),
74 request: vi.fn(async () => ({})),
75 mintCommandId,
76 onNotification: (nextHandler) => {
77 handler = nextHandler;
78 },
79 onProtocolError: () => {},
80 onServerRequest: () => {},
81 closed: closed.promise,
82 },
83 close: vi.fn(async () => {
84 closed.resolve();
85 }),
86 exited: new Promise(() => {}),
87 };
88 const createHost = vi.fn(async () => host);
89 const make = makeMuseTextGeneration(settings, {
90 createHost,
91 ...(modelCatalog ? { modelCatalog } : {}),
92 });
93 return {
94 host,
95 createHost,
96 started: started.promise,
97 closeConnection: closed.resolve,
98 emit,
99 make,
100 };
102​
103const finish =
104 (text: string) => (emit: (method: string, params: Record<string, unknown>) => void) => {
105 emit("item/updated", { item: { itemId: "answer", kind: "agentMessage", text: "partial" } });
106 emit("item/completed", {
107 item: { itemId: "answer", kind: "agentMessage", revision: 2, text },
108 });
109 emit("turn/completed", { terminal: "completed" });
110 };
111​
112it.layer(NodeServices.layer)("Muse text generation", (it) => {
113 it.effect("validates the final item once and closes an isolated restrictive host", () =>
114 Effect.gen(function* () {
115 const test = fixture(finish('{"title":"Fix login form"}'));
116 const service = yield* test.make;
117 expect(yield* service.generateThreadTitle(titleInput)).toEqual({
118 title: "Fix login form",
119 });
120 expect(test.createHost).toHaveBeenCalledWith(
121 expect.objectContaining({ readOnly: true, sessionLogging: true }),
122 );
123 expect(test.createHost).not.toHaveBeenCalledWith(
124 expect.objectContaining({ cwd: titleInput.cwd }),
125 );
126 expect(test.host.connection.command).toHaveBeenCalledWith(
127 "session/start",
128 expect.objectContaining({
129 providerId: "meta",
130 modelId: modelSelection.model,
131 approvalMode: "denyUnmatched",
132 }),
133 );
134 expect(test.host.connection.command).toHaveBeenCalledWith(
135 "turn/start",
136 expect.objectContaining({
137 reasoningEffort: "high",
138 }),
139 { commandId: "turn-1" },
140 );
141 expect(test.host.close).toHaveBeenCalledOnce();
142 }),
143 );
144​
145 it.effect("uses low effort by default and normalizes stale efforts against the model", () =>
146 Effect.gen(function* () {
147 for (const { saved, variants, expected } of [
148 { saved: undefined, variants: undefined, expected: "low" },
149 { saved: undefined, variants: ["medium", "xhigh"], expected: "medium" },
150 { saved: "max", variants: ["minimal", "max"], expected: "max" },
151 { saved: "ultra", variants: ["medium", "xhigh"], expected: "medium" },
152 { saved: "high", variants: [], expected: undefined },
153 ]) {
154 const test = fixture(
155 finish('{"title":"Fix login form"}'),
156 true,
157 Effect.succeed([
158 {
159 slug: modelSelection.model,
160 name: modelSelection.model,
161 isCustom: false,
162 capabilities: museModelCapabilities(variants ? { variants } : undefined),
163 },
164 ]),
165 );
166 const service = yield* test.make;
167 yield* service.generateThreadTitle({
168 ...titleInput,
169 modelSelection: createModelSelection(
170 ProviderInstanceId.make("muse"),
171 modelSelection.model,
172 saved !== undefined ? [{ id: "reasoningEffort", value: saved }] : undefined,
173 ),
174 });
175 const turn = vi
176 .mocked(test.host.connection.command)
177 .mock.calls.find(([method]) => method === "turn/start");
178 expect(turn?.[1].reasoningEffort).toBe(expected);
179 expect(Object.hasOwn(turn?.[1] ?? {}, "reasoningEffort")).toBe(expected !== undefined);
180 }
181 }),
182 );
183​
184 it.effect("ignores output and terminal notifications for another turn", () =>
185 Effect.gen(function* () {
186 const test = fixture((emit) => {
187 emit("item/completed", {
188 item: {
189 itemId: "other-answer",
190 kind: "agentMessage",
191 turnId: "another-turn",
192 text: '{"title":"Wrong title"}',
193 },
194 });
195 emit("turn/completed", { turnId: "another-turn", terminal: "completed" });
196 finish('{"title":"Fix login form"}')(emit);
197 });
198 const service = yield* test.make;
199 expect(yield* service.generateThreadTitle(titleInput)).toEqual({ title: "Fix login form" });
200 expect(test.host.connection.command).toHaveBeenCalledWith(
201 "turn/start",
202 expect.objectContaining({ sessionId: "session-1" }),
203 { commandId: "turn-1" },
204 );
205 }),
206 );
207​
208 it.effect("rejects a truncated response even when its prefix contains valid JSON", () =>
209 Effect.gen(function* () {
210 const test = fixture((emit) => {
211 emit("item/completed", {
212 item: {
213 itemId: "answer",
214 kind: "agentMessage",
215 text: '{"title":"Incomplete answer"}',
216 truncated: true,
217 },
218 });
219 emit("turn/completed", { terminal: "completed" });
220 });
221 const service = yield* test.make;
222 expect(yield* service.generateThreadTitle(titleInput).pipe(Effect.flip)).toMatchObject({
223 _tag: "TextGenerationError",
224 });
225 expect(test.host.close).toHaveBeenCalledOnce();
226 }),
227 );
228​
229 it.effect("settles a completed turn even when its start acknowledgement never arrives", () =>
230 Effect.gen(function* () {
231 const test = fixture(finish('{"title":"Fix login form"}'), false);
232 const service = yield* test.make;
233 expect(yield* service.generateThreadTitle(titleInput)).toEqual({ title: "Fix login form" });
234 expect(test.host.close).toHaveBeenCalledOnce();
235 }),
236 );
237​
238 it.effect("settles when the transport closes before completion", () =>
239 Effect.gen(function* () {
240 const test = fixture();
241 const service = yield* test.make;
242 const pending = yield* service
243 .generateThreadTitle(titleInput)
244 .pipe(Effect.flip, Effect.forkChild);
245 yield* Effect.promise(() => test.started);
246 test.closeConnection();
247 expect(yield* Fiber.join(pending)).toMatchObject({ _tag: "TextGenerationError" });
248 expect(test.host.close).toHaveBeenCalledOnce();
249 }),
250 );
251​
252 it.effect("releases the host when interrupted after the turn starts", () =>
253 Effect.gen(function* () {
254 const test = fixture();
255 const service = yield* test.make;
256 const fiber = yield* service.generateThreadTitle(titleInput).pipe(Effect.forkChild);
257 yield* Effect.promise(() => test.started);
258 yield* Fiber.interrupt(fiber);
259 expect(test.host.close).toHaveBeenCalledOnce();
260 }),
261 );
262​
263 it.effect("denies approval requests and fails the noninteractive request", () =>
264 Effect.gen(function* () {
265 const test = fixture((emit) =>
266 emit("approval/requested", {
267 approvalId: "approval-1",
268 currentRequirementId: "requirement-1",
269 availableChoices: [{ choiceId: "deny-1", decision: "denied" }],
270 }),
271 );
272 const service = yield* test.make;
273 expect(yield* service.generateThreadTitle(titleInput).pipe(Effect.flip)).toMatchObject({
274 _tag: "TextGenerationError",
275 });
276 expect(test.host.connection.command).toHaveBeenCalledWith("approval/decide", {
277 sessionId: "session-1",
278 approvalId: "approval-1",
279 requirementId: "requirement-1",
280 choiceId: "deny-1",
281 });
282 expect(test.host.close).toHaveBeenCalledOnce();
283 }),
284 );
285});