The best way to control Claude Code, Codex and more from any computer or phone

main
ts 449 lines 17.1 KB
Raw
1 import type { RunResult } from "@cursor/sdk";
2 import { ProviderInstanceId, TextGenerationError } from "@t3tools/contracts";
3 import { CursorSettings } from "../settings.ts";
4 import { describe, expect, it } from "@effect/vitest";
5 import * as Effect from "effect/Effect";
6 import * as Fiber from "effect/Fiber";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Layer from "effect/Layer";
9 import * as Path from "effect/Path";
10 import * as Schema from "effect/Schema";
11 import * as TestClock from "effect/testing/TestClock";
12 import { createModelSelection } from "@t3tools/shared/model";
13 import { beforeEach, vi } from "vite-plus/test";
14
15 import { makeCursorTextGeneration } from "./textGeneration.ts";
16 import * as CursorSdk from "./CursorSdk.ts";
17
18 const cursorSdkMock = vi.hoisted(() => ({
19 create: vi.fn<(options: unknown) => Promise<unknown>>(),
20 send: vi.fn<(prompt: string) => Promise<unknown>>(),
21 close: vi.fn(),
22 cancel: vi.fn(async () => {}),
23 storeOpen: vi.fn<(options: unknown) => Promise<unknown>>(),
24 storeDispose: vi.fn(async () => {}),
25 prompt: vi.fn<(prompt: string, options: unknown) => Promise<RunResult>>(async () => ({
26 id: "run-cursor-text-generation-test",
27 status: "finished",
28 result:
29 '{"subject":"Add generated commit message","body":"- verify cursor sdk text generation"}',
30 })),
31 }));
32
33 vi.mock("./sdk.ts", () => ({
34 loadCursorSdk: () => ({
35 Agent: { create: cursorSdkMock.create },
36 createAgentPlatform: vi.fn(),
37 }),
38 loadCursorSqlite: () => ({ SqliteLocalAgentStore: { open: cursorSdkMock.storeOpen } }),
39 }));
40
41 let hasCustomPolicy = false;
42 const layerFs = FileSystem.layerNoop({
43 exists: () => Effect.succeed(hasCustomPolicy),
44 makeTempDirectoryScoped: () => Effect.succeed("/isolated-text-generation"),
45 });
46 const layerTest = CursorSdk.layer.pipe(Layer.provideMerge(Layer.merge(layerFs, Path.layer)));
47
48 const decodeCursorSettings = Schema.decodeSync(CursorSettings);
49 const cursorSettings = decodeCursorSettings({ enabled: true });
50
51 beforeEach(() => {
52 hasCustomPolicy = false;
53 cursorSdkMock.create.mockReset();
54 cursorSdkMock.send.mockReset();
55 cursorSdkMock.create.mockImplementation(async (options) => {
56 cursorSdkMock.send.mockImplementation(async (prompt) => {
57 const run = {
58 status: "running",
59 cancel: cursorSdkMock.cancel,
60 wait: async () => {
61 const result = await cursorSdkMock.prompt(prompt, options);
62 run.status = result.status;
63 return result;
64 },
65 };
66 return run;
67 });
68 return {
69 close: cursorSdkMock.close,
70 [Symbol.asyncDispose]: async () => {
71 cursorSdkMock.close();
72 },
73 send: cursorSdkMock.send,
74 };
75 });
76 cursorSdkMock.close.mockClear();
77 cursorSdkMock.cancel.mockClear();
78 cursorSdkMock.storeDispose.mockClear();
79 cursorSdkMock.storeOpen.mockReset();
80 cursorSdkMock.storeOpen.mockResolvedValue({ dispose: cursorSdkMock.storeDispose });
81 cursorSdkMock.prompt.mockReset();
82 cursorSdkMock.prompt.mockResolvedValue({
83 id: "run-cursor-text-generation-test",
84 status: "finished",
85 result:
86 '{"subject":"Add generated commit message","body":"- verify cursor sdk text generation"}',
87 });
88 });
89
90 describe("CursorTextGeneration", () => {
91 it.effect("resolves the browser credential for every request after an account change", () =>
92 Effect.gen(function* () {
93 let apiKey = "first-browser-key";
94 const generation = yield* makeCursorTextGeneration(
95 cursorSettings,
96 {},
97 Effect.sync(() => apiKey),
98 );
99 const input = {
100 cwd: process.cwd(),
101 branch: "feature/cursor",
102 stagedSummary: "M file.ts",
103 stagedPatch: "diff",
104 modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "auto"),
105 };
106 yield* generation.generateCommitMessage(input);
107 expect(cursorSdkMock.create).toHaveBeenLastCalledWith(
108 expect.objectContaining({ apiKey: "first-browser-key" }),
109 );
110 apiKey = "second-browser-key";
111 yield* generation.generateCommitMessage(input);
112 expect(cursorSdkMock.create).toHaveBeenLastCalledWith(
113 expect.objectContaining({ apiKey: "second-browser-key" }),
114 );
115 }).pipe(Effect.provide(layerTest)),
116 );
117
118 it.effect("recovers invalid output after the SDK cannot sandbox", () =>
119 Effect.gen(function* () {
120 // The SDK checks sandbox support lazily, so create succeeds and send rejects.
121 cursorSdkMock.create.mockImplementationOnce(async () => ({
122 close: cursorSdkMock.close,
123 [Symbol.asyncDispose]: async () => {
124 cursorSdkMock.close();
125 },
126 send: async () => {
127 throw new Error(
128 "Local SDK sandboxing was requested, but sandboxing is not supported in this environment. Disable local.sandboxOptions.enabled or remove ~/.cursor/sandbox.json to run without sandboxing.",
129 );
130 },
131 }));
132 cursorSdkMock.prompt.mockResolvedValueOnce({
133 id: "invalid-output-after-sandbox-fallback",
134 status: "finished",
135 result: "Not JSON",
136 });
137 const textGeneration = yield* makeCursorTextGeneration(cursorSettings, {
138 CURSOR_API_KEY: "test-cursor-key",
139 });
140
141 const generated = yield* textGeneration.generateCommitMessage({
142 cwd: process.cwd(),
143 branch: "feature/cursor-text-generation",
144 stagedSummary: "M apps/server/src/textGeneration/CursorTextGeneration.ts",
145 stagedPatch: "diff --git a/apps/server/src/textGeneration/CursorTextGeneration.ts",
146 modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "composer-2"),
147 });
148
149 expect(generated.subject).toBe("Add generated commit message");
150 expect(cursorSdkMock.create).toHaveBeenCalledTimes(3);
151 expect(cursorSdkMock.prompt).toHaveBeenCalledTimes(2);
152 expect(cursorSdkMock.close).toHaveBeenCalledTimes(3);
153 expect(cursorSdkMock.create.mock.calls[0]?.[0]).toMatchObject({
154 local: { sandboxOptions: { enabled: true } },
155 });
156 expect(cursorSdkMock.create.mock.calls[1]?.[0]).toMatchObject({
157 local: {
158 cwd: "/isolated-text-generation",
159 store: { dispose: cursorSdkMock.storeDispose },
160 autoReview: false,
161 sandboxOptions: { enabled: false },
162 settingSources: [],
163 enableAgentRetries: true,
164 },
165 });
166 }).pipe(Effect.provide(layerTest)),
167 );
168
169 it.effect("accepts json objects with extra assistant text around them", () =>
170 Effect.gen(function* () {
171 cursorSdkMock.prompt.mockResolvedValueOnce({
172 id: "run-cursor-text-generation-test",
173 status: "finished",
174 result:
175 'Sure, here is the JSON:\n```json\n{\n "subject": "Update README dummy comment with attribution and date",\n "body": ""\n}\n```\nDone.',
176 });
177 const textGeneration = yield* makeCursorTextGeneration(cursorSettings, {
178 CURSOR_API_KEY: "test-cursor-key",
179 });
180
181 const generated = yield* textGeneration.generateCommitMessage({
182 cwd: process.cwd(),
183 branch: "feature/cursor-noisy-json",
184 stagedSummary: "M README.md",
185 stagedPatch: "diff --git a/README.md b/README.md",
186 modelSelection: {
187 instanceId: ProviderInstanceId.make("cursor"),
188 model: "composer-2",
189 },
190 });
191
192 expect(generated.subject).toBe("Update README dummy comment with attribution and date");
193 expect(generated.body).toBe("");
194 expect(cursorSdkMock.prompt).toHaveBeenCalledOnce();
195 }).pipe(Effect.provide(layerTest)),
196 );
197
198 it.effect.each([
199 { name: "invalid JSON", result: "Add a generated commit message" },
200 {
201 name: "a body with the wrong type",
202 result: '{"subject":"Add generated commit message","body":[]}',
203 },
204 ])("retries $name with a fresh agent before returning a commit message", ({ result }) =>
205 Effect.gen(function* () {
206 cursorSdkMock.prompt
207 .mockResolvedValueOnce({ id: "invalid-output", status: "finished", result })
208 .mockImplementationOnce(async () => {
209 expect(cursorSdkMock.close).toHaveBeenCalledOnce();
210 return {
211 id: "valid-output",
212 status: "finished",
213 result: '{"subject":"Add generated commit message","body":""}',
214 };
215 });
216 const generation = yield* makeCursorTextGeneration(cursorSettings, {
217 CURSOR_API_KEY: "test-cursor-key",
218 });
219 const generated = yield* generation.generateCommitMessage({
220 cwd: process.cwd(),
221 branch: "feature/cursor",
222 stagedSummary: "M file.ts",
223 stagedPatch: "diff",
224 modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "auto"),
225 });
226
227 expect(generated).toEqual({
228 subject: "Add generated commit message",
229 body: "",
230 });
231 expect(cursorSdkMock.create).toHaveBeenCalledTimes(2);
232 expect(cursorSdkMock.close).toHaveBeenCalledTimes(2);
233 expect(cursorSdkMock.storeDispose).toHaveBeenCalledOnce();
234 }).pipe(Effect.provide(layerTest)),
235 );
236
237 it.effect("stops after a second invalid reply and preserves the validation error", () =>
238 Effect.gen(function* () {
239 cursorSdkMock.prompt.mockResolvedValue({
240 id: "invalid-output",
241 status: "finished",
242 result: '{"subject":"Add generated commit message","body":null}',
243 });
244 const generation = yield* makeCursorTextGeneration(cursorSettings, {
245 CURSOR_API_KEY: "test-cursor-key",
246 });
247 const failure = yield* Effect.flip(
248 generation.generateCommitMessage({
249 cwd: process.cwd(),
250 branch: null,
251 stagedSummary: "M file.ts",
252 stagedPatch: "diff",
253 modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "auto"),
254 }),
255 );
256
257 expect(failure.operation).toBe("generateCommitMessage");
258 expect(failure.detail).toBe("Cursor SDK returned invalid structured output.");
259 expect(Schema.isSchemaError(failure.cause)).toBe(true);
260 expect(cursorSdkMock.prompt).toHaveBeenCalledTimes(2);
261 }).pipe(Effect.provide(layerTest)),
262 );
263
264 it.effect("keeps both attempts inside the original deadline and cancels the pending retry", () =>
265 Effect.gen(function* () {
266 const firstStarted = Promise.withResolvers<void>();
267 const retryStarted = Promise.withResolvers<void>();
268 const firstResult = Promise.withResolvers<RunResult>();
269 cursorSdkMock.prompt
270 .mockImplementationOnce(() => {
271 firstStarted.resolve();
272 return firstResult.promise;
273 })
274 .mockImplementationOnce(() => {
275 retryStarted.resolve();
276 return new Promise(() => {});
277 });
278 const generation = yield* makeCursorTextGeneration(cursorSettings, {
279 CURSOR_API_KEY: "test-cursor-key",
280 });
281 const request = yield* generation
282 .generateThreadTitle({
283 cwd: process.cwd(),
284 message: "Fix reconnect",
285 modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "auto"),
286 })
287 .pipe(Effect.flip, Effect.forkScoped);
288
289 yield* Effect.promise(() => firstStarted.promise);
290 yield* TestClock.adjust("2 minutes");
291 firstResult.resolve({ id: "invalid-output", status: "finished", result: "Not JSON" });
292 yield* Effect.promise(() => retryStarted.promise);
293 yield* TestClock.adjust("1 minute");
294
295 expect(cursorSdkMock.prompt).toHaveBeenCalledTimes(2);
296 expect(cursorSdkMock.cancel).toHaveBeenCalledOnce();
297 expect((yield* Fiber.join(request)).detail).toBe("Cursor SDK request timed out.");
298 expect(cursorSdkMock.close).toHaveBeenCalledTimes(2);
299 expect(cursorSdkMock.storeDispose).toHaveBeenCalledOnce();
300 }).pipe(Effect.scoped, Effect.provide(layerTest)),
301 );
302
303 it.effect("generates thread titles through Cursor SDK text generation", () =>
304 Effect.gen(function* () {
305 cursorSdkMock.prompt.mockResolvedValueOnce({
306 id: "run-cursor-title-generation-test",
307 status: "finished",
308 result: '{"title":"\\"Trim reconnect spinner status after resume.\\""}',
309 });
310 const textGeneration = yield* makeCursorTextGeneration(cursorSettings, {
311 CURSOR_API_KEY: "test-cursor-key",
312 });
313
314 const generated = yield* textGeneration.generateThreadTitle({
315 cwd: process.cwd(),
316 message: "Fix the reconnect spinner after a resumed session.",
317 modelSelection: {
318 instanceId: ProviderInstanceId.make("cursor"),
319 model: "composer-2",
320 },
321 });
322
323 expect(generated.title).toBe("Trim reconnect spinner status after resume.");
324 }).pipe(Effect.provide(layerTest)),
325 );
326
327 it.effect.each([
328 {
329 status: "error",
330 error: { message: "Raw Cursor usage text", code: "PRO_USER_USAGE_LIMIT" },
331 detail:
332 "Cursor usage limit reached. Try again later or choose another text-generation provider.",
333 },
334 {
335 status: "error",
336 error: { message: "Raw Cursor backend text", code: "internal" },
337 detail: "Cursor SDK request finished with an error.",
338 },
339 { status: "cancelled", error: undefined, detail: "Cursor SDK request was cancelled." },
340 { status: "finished", error: undefined, detail: "Cursor SDK returned empty output." },
341 ] as const)(
342 "does not retry an unsuccessful or empty $status Cursor SDK run ($error.code)",
343 ({ status, error, detail }) =>
344 Effect.gen(function* () {
345 const promptResult = {
346 id: "run-cursor-partial-title-test",
347 status,
348 result: status === "finished" ? " " : '{"title":"Partial title from a failed run."}',
349 ...(error ? { error } : {}),
350 } satisfies RunResult;
351 cursorSdkMock.prompt.mockResolvedValueOnce(promptResult);
352 const generation = yield* makeCursorTextGeneration(cursorSettings, {
353 CURSOR_API_KEY: "test-cursor-key",
354 });
355 const failure = yield* Effect.flip(
356 generation.generateThreadTitle({
357 cwd: process.cwd(),
358 message: "Fix the reconnect spinner after a resumed session.",
359 modelSelection: {
360 instanceId: ProviderInstanceId.make("cursor"),
361 model: "composer-2",
362 },
363 }),
364 );
365 expect(failure).toBeInstanceOf(TextGenerationError);
366 expect(failure.operation).toBe("generateThreadTitle");
367 expect(failure.detail).toBe(detail);
368 expect(failure.message).not.toContain("Raw Cursor");
369 expect(failure.cause).toBe(error);
370 expect(cursorSdkMock.close).toHaveBeenCalledOnce();
371 expect(cursorSdkMock.prompt).toHaveBeenCalledOnce();
372 }).pipe(Effect.provide(layerTest)),
373 );
374
375 it.effect("fails closed when ambient sandbox policy can expand write access", () =>
376 Effect.gen(function* () {
377 hasCustomPolicy = true;
378 const generation = yield* makeCursorTextGeneration(cursorSettings, { CURSOR_API_KEY: "key" });
379 const failure = yield* Effect.flip(
380 generation.generateThreadTitle({
381 cwd: "/real-workspace",
382 message: "Title",
383 modelSelection: { instanceId: ProviderInstanceId.make("cursor"), model: "composer-2" },
384 }),
385 );
386 expect(failure.detail).toContain("custom ~/.cursor/sandbox.json");
387 expect(cursorSdkMock.prompt).not.toHaveBeenCalled();
388 }).pipe(Effect.provide(layerTest)),
389 );
390
391 it.effect("requires CURSOR_API_KEY before calling the SDK", () =>
392 Effect.gen(function* () {
393 const textGeneration = yield* makeCursorTextGeneration(cursorSettings, {});
394
395 const error = yield* Effect.flip(
396 textGeneration.generateCommitMessage({
397 cwd: process.cwd(),
398 branch: "feature/cursor-api-key",
399 stagedSummary: "M README.md",
400 stagedPatch: "diff --git a/README.md b/README.md",
401 modelSelection: {
402 instanceId: ProviderInstanceId.make("cursor"),
403 model: "composer-2",
404 },
405 }),
406 );
407
408 expect(error.detail).toBe("Sign in with Cursor or add CURSOR_API_KEY in provider settings.");
409 expect(cursorSdkMock.prompt).not.toHaveBeenCalled();
410 }).pipe(Effect.provide(layerTest)),
411 );
412
413 it.effect("delegates to sdkRunner.textGenerate without loading the in-process SDK", () =>
414 Effect.gen(function* () {
415 const textGenerate = vi.fn(() =>
416 Effect.succeed({
417 id: "run-worker-title",
418 status: "finished",
419 result: JSON.stringify({ title: "Worker title" }),
420 } satisfies RunResult),
421 );
422 const generation = yield* makeCursorTextGeneration(
423 cursorSettings,
424 { CURSOR_API_KEY: "test-cursor-key" },
425 undefined,
426 undefined,
427 undefined,
428 undefined,
429 {
430 open: () => Effect.die("unused"),
431 assertComplete: Effect.void,
432 textGenerate,
433 },
434 );
435 const result = yield* generation.generateThreadTitle({
436 cwd: process.cwd(),
437 message: "Run text generation out of process",
438 modelSelection: {
439 instanceId: ProviderInstanceId.make("cursor"),
440 model: "composer-2",
441 },
442 });
443 expect(result.title).toBe("Worker title");
444 expect(textGenerate).toHaveBeenCalledOnce();
445 expect(cursorSdkMock.create).not.toHaveBeenCalled();
446 expect(cursorSdkMock.storeOpen).not.toHaveBeenCalled();
447 }).pipe(Effect.provide(layerTest)),
448 );
449 });