@@ -3,9 +3,11 @@ import { ProviderInstanceId, TextGenerationError } from "@t3tools/contracts"; |
3 | 3 | import { CursorSettings } from "../settings.ts"; |
4 | 4 | import { describe, expect, it } from "@effect/vitest"; |
5 | 5 | import * as Effect from "effect/Effect"; |
| 6 | +import * as Fiber from "effect/Fiber"; |
6 | 7 | import * as FileSystem from "effect/FileSystem"; |
7 | 8 | import * as Layer from "effect/Layer"; |
8 | 9 | import * as Schema from "effect/Schema"; |
| 10 | +import * as TestClock from "effect/testing/TestClock"; |
9 | 11 | import { createModelSelection } from "@t3tools/shared/model"; |
10 | 12 | import { beforeEach, vi } from "vite-plus/test"; |
11 | 13 |
|
@@ -50,11 +52,18 @@ beforeEach(() => { |
50 | 52 | cursorSdkMock.create.mockReset(); |
51 | 53 | cursorSdkMock.send.mockReset(); |
52 | 54 | cursorSdkMock.create.mockImplementation(async (options) => { |
53 | | - cursorSdkMock.send.mockImplementation(async (prompt) => ({ |
54 | | - status: "running", |
55 | | - cancel: cursorSdkMock.cancel, |
56 | | - wait: () => cursorSdkMock.prompt(prompt, options), |
57 | | - })); |
| 55 | + cursorSdkMock.send.mockImplementation(async (prompt) => { |
| 56 | + const run = { |
| 57 | + status: "running", |
| 58 | + cancel: cursorSdkMock.cancel, |
| 59 | + wait: async () => { |
| 60 | + const result = await cursorSdkMock.prompt(prompt, options); |
| 61 | + run.status = result.status; |
| 62 | + return result; |
| 63 | + }, |
| 64 | + }; |
| 65 | + return run; |
| 66 | + }); |
58 | 67 | return { |
59 | 68 | close: cursorSdkMock.close, |
60 | 69 | [Symbol.asyncDispose]: async () => { |
@@ -95,23 +104,30 @@ describe("CursorTextGeneration", () => { |
95 | 104 | }; |
96 | 105 | yield* generation.generateCommitMessage(input); |
97 | 106 | expect(cursorSdkMock.create).toHaveBeenLastCalledWith( |
98 | | - expect.objectContaining({ apiKey: "first-browser-key" }), |
| 107 | + expect.objectContaining({ apiKey: "first-browser-key", tools: [] }), |
99 | 108 | ); |
100 | 109 | apiKey = "second-browser-key"; |
101 | 110 | yield* generation.generateCommitMessage(input); |
102 | 111 | expect(cursorSdkMock.create).toHaveBeenLastCalledWith( |
103 | | - expect.objectContaining({ apiKey: "second-browser-key" }), |
| 112 | + expect.objectContaining({ apiKey: "second-browser-key", tools: [] }), |
104 | 113 | ); |
105 | 114 | }).pipe(Effect.provide(layerTest)), |
106 | 115 | ); |
107 | 116 |
|
108 | 117 | it.effect("continues in the temp directory when the SDK cannot sandbox", () => |
109 | 118 | Effect.gen(function* () { |
110 | | - cursorSdkMock.create.mockImplementationOnce(async () => { |
111 | | - throw new Error( |
112 | | - "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.", |
113 | | - ); |
114 | | - }); |
| 119 | + // The SDK accepts sandboxOptions at create and rejects them when send builds the executor. |
| 120 | + cursorSdkMock.create.mockImplementationOnce(async () => ({ |
| 121 | + close: cursorSdkMock.close, |
| 122 | + [Symbol.asyncDispose]: async () => { |
| 123 | + cursorSdkMock.close(); |
| 124 | + }, |
| 125 | + send: async () => { |
| 126 | + throw new Error( |
| 127 | + "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.", |
| 128 | + ); |
| 129 | + }, |
| 130 | + })); |
115 | 131 | const textGeneration = yield* makeCursorTextGeneration(cursorSettings, { |
116 | 132 | CURSOR_API_KEY: "test-cursor-key", |
117 | 133 | }); |
@@ -126,10 +142,13 @@ describe("CursorTextGeneration", () => { |
126 | 142 |
|
127 | 143 | expect(generated.subject).toBe("Add generated commit message"); |
128 | 144 | expect(cursorSdkMock.create).toHaveBeenCalledTimes(2); |
| 145 | + expect(cursorSdkMock.close).toHaveBeenCalledTimes(2); |
129 | 146 | expect(cursorSdkMock.create.mock.calls[0]?.[0]).toMatchObject({ |
| 147 | + tools: [], |
130 | 148 | local: { sandboxOptions: { enabled: true } }, |
131 | 149 | }); |
132 | 150 | expect(cursorSdkMock.create.mock.calls[1]?.[0]).toMatchObject({ |
| 151 | + tools: [], |
133 | 152 | local: { |
134 | 153 | cwd: "/isolated-text-generation", |
135 | 154 | store: { dispose: cursorSdkMock.storeDispose }, |
@@ -167,9 +186,117 @@ describe("CursorTextGeneration", () => { |
167 | 186 |
|
168 | 187 | expect(generated.subject).toBe("Update README dummy comment with attribution and date"); |
169 | 188 | expect(generated.body).toBe(""); |
| 189 | + expect(cursorSdkMock.prompt).toHaveBeenCalledOnce(); |
| 190 | + }).pipe(Effect.provide(layerTest)), |
| 191 | + ); |
| 192 | + |
| 193 | + it.effect.each([ |
| 194 | + { name: "invalid JSON", result: "Add a generated commit message" }, |
| 195 | + { |
| 196 | + name: "a body with the wrong type", |
| 197 | + result: '{"subject":"Add generated commit message","body":[]}', |
| 198 | + }, |
| 199 | + { name: "empty output", result: " " }, |
| 200 | + ])("retries $name with a fresh agent before returning a commit message", ({ result }) => |
| 201 | + Effect.gen(function* () { |
| 202 | + cursorSdkMock.prompt |
| 203 | + .mockResolvedValueOnce({ id: "invalid-output", status: "finished", result }) |
| 204 | + .mockImplementationOnce(async (prompt) => { |
| 205 | + expect(cursorSdkMock.close).toHaveBeenCalledOnce(); |
| 206 | + expect(prompt).toContain("Return ONLY the JSON object"); |
| 207 | + return { |
| 208 | + id: "valid-output", |
| 209 | + status: "finished", |
| 210 | + result: '{"subject":"Add generated commit message","body":""}', |
| 211 | + }; |
| 212 | + }); |
| 213 | + const generation = yield* makeCursorTextGeneration(cursorSettings, { |
| 214 | + CURSOR_API_KEY: "test-cursor-key", |
| 215 | + }); |
| 216 | + const generated = yield* generation.generateCommitMessage({ |
| 217 | + cwd: process.cwd(), |
| 218 | + branch: "feature/cursor", |
| 219 | + stagedSummary: "M file.ts", |
| 220 | + stagedPatch: "diff", |
| 221 | + modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "auto"), |
| 222 | + }); |
| 223 | + |
| 224 | + expect(generated).toEqual({ |
| 225 | + subject: "Add generated commit message", |
| 226 | + body: "", |
| 227 | + }); |
| 228 | + expect(cursorSdkMock.create).toHaveBeenCalledTimes(2); |
| 229 | + expect(cursorSdkMock.close).toHaveBeenCalledTimes(2); |
| 230 | + expect(cursorSdkMock.storeDispose).toHaveBeenCalledOnce(); |
| 231 | + }).pipe(Effect.provide(layerTest)), |
| 232 | + ); |
| 233 | + |
| 234 | + it.effect("stops after a second invalid reply and preserves the validation error", () => |
| 235 | + Effect.gen(function* () { |
| 236 | + cursorSdkMock.prompt.mockResolvedValue({ |
| 237 | + id: "invalid-output", |
| 238 | + status: "finished", |
| 239 | + result: '{"subject":"Add generated commit message","body":null}', |
| 240 | + }); |
| 241 | + const generation = yield* makeCursorTextGeneration(cursorSettings, { |
| 242 | + CURSOR_API_KEY: "test-cursor-key", |
| 243 | + }); |
| 244 | + const failure = yield* Effect.flip( |
| 245 | + generation.generateCommitMessage({ |
| 246 | + cwd: process.cwd(), |
| 247 | + branch: null, |
| 248 | + stagedSummary: "M file.ts", |
| 249 | + stagedPatch: "diff", |
| 250 | + modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "auto"), |
| 251 | + }), |
| 252 | + ); |
| 253 | + |
| 254 | + expect(failure.operation).toBe("generateCommitMessage"); |
| 255 | + expect(failure.detail).toBe("Cursor SDK returned invalid structured output."); |
| 256 | + expect(Schema.isSchemaError(failure.cause)).toBe(true); |
| 257 | + expect(cursorSdkMock.prompt).toHaveBeenCalledTimes(2); |
170 | 258 | }).pipe(Effect.provide(layerTest)), |
171 | 259 | ); |
172 | 260 |
|
| 261 | + it.effect("keeps both attempts inside the original deadline and cancels the pending retry", () => |
| 262 | + Effect.gen(function* () { |
| 263 | + const firstStarted = Promise.withResolvers<void>(); |
| 264 | + const retryStarted = Promise.withResolvers<void>(); |
| 265 | + const firstResult = Promise.withResolvers<RunResult>(); |
| 266 | + cursorSdkMock.prompt |
| 267 | + .mockImplementationOnce(() => { |
| 268 | + firstStarted.resolve(); |
| 269 | + return firstResult.promise; |
| 270 | + }) |
| 271 | + .mockImplementationOnce(() => { |
| 272 | + retryStarted.resolve(); |
| 273 | + return new Promise(() => {}); |
| 274 | + }); |
| 275 | + const generation = yield* makeCursorTextGeneration(cursorSettings, { |
| 276 | + CURSOR_API_KEY: "test-cursor-key", |
| 277 | + }); |
| 278 | + const request = yield* generation |
| 279 | + .generateThreadTitle({ |
| 280 | + cwd: process.cwd(), |
| 281 | + message: "Fix reconnect", |
| 282 | + modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "auto"), |
| 283 | + }) |
| 284 | + .pipe(Effect.flip, Effect.forkScoped); |
| 285 | + |
| 286 | + yield* Effect.promise(() => firstStarted.promise); |
| 287 | + yield* TestClock.adjust("2 minutes"); |
| 288 | + firstResult.resolve({ id: "invalid-output", status: "finished", result: "Not JSON" }); |
| 289 | + yield* Effect.promise(() => retryStarted.promise); |
| 290 | + yield* TestClock.adjust("1 minute"); |
| 291 | + |
| 292 | + expect(cursorSdkMock.prompt).toHaveBeenCalledTimes(2); |
| 293 | + expect(cursorSdkMock.cancel).toHaveBeenCalledOnce(); |
| 294 | + expect((yield* Fiber.join(request)).detail).toBe("Cursor SDK request timed out."); |
| 295 | + expect(cursorSdkMock.close).toHaveBeenCalledTimes(2); |
| 296 | + expect(cursorSdkMock.storeDispose).toHaveBeenCalledOnce(); |
| 297 | + }).pipe(Effect.scoped, Effect.provide(layerTest)), |
| 298 | + ); |
| 299 | + |
173 | 300 | it.effect("generates thread titles through Cursor SDK text generation", () => |
174 | 301 | Effect.gen(function* () { |
175 | 302 | cursorSdkMock.prompt.mockResolvedValueOnce({ |
@@ -208,7 +335,7 @@ describe("CursorTextGeneration", () => { |
208 | 335 | }, |
209 | 336 | { status: "cancelled", error: undefined, detail: "Cursor SDK request was cancelled." }, |
210 | 337 | ] as const)( |
211 | | - "rejects a $status Cursor SDK run ($error.code) that includes valid title JSON", |
| 338 | + "does not retry an unsuccessful $status Cursor SDK run ($error.code)", |
212 | 339 | ({ status, error, detail }) => |
213 | 340 | Effect.gen(function* () { |
214 | 341 | const promptResult = { |
@@ -237,9 +364,32 @@ describe("CursorTextGeneration", () => { |
237 | 364 | expect(failure.message).not.toContain("Raw Cursor"); |
238 | 365 | expect(failure.cause).toBe(error); |
239 | 366 | expect(cursorSdkMock.close).toHaveBeenCalledOnce(); |
| 367 | + expect(cursorSdkMock.prompt).toHaveBeenCalledOnce(); |
240 | 368 | }).pipe(Effect.provide(layerTest)), |
241 | 369 | ); |
242 | 370 |
|
| 371 | + it.effect("stops after a second empty reply", () => |
| 372 | + Effect.gen(function* () { |
| 373 | + cursorSdkMock.prompt.mockResolvedValue({ |
| 374 | + id: "empty-output", |
| 375 | + status: "finished", |
| 376 | + result: " ", |
| 377 | + }); |
| 378 | + const generation = yield* makeCursorTextGeneration(cursorSettings, { |
| 379 | + CURSOR_API_KEY: "test-cursor-key", |
| 380 | + }); |
| 381 | + const failure = yield* Effect.flip( |
| 382 | + generation.generateThreadTitle({ |
| 383 | + cwd: process.cwd(), |
| 384 | + message: "Fix reconnect", |
| 385 | + modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "composer-2"), |
| 386 | + }), |
| 387 | + ); |
| 388 | + expect(failure.detail).toBe("Cursor SDK returned empty output."); |
| 389 | + expect(cursorSdkMock.prompt).toHaveBeenCalledTimes(2); |
| 390 | + }).pipe(Effect.provide(layerTest)), |
| 391 | + ); |
| 392 | + |
243 | 393 | it.effect("fails closed when ambient sandbox policy can expand write access", () => |
244 | 394 | Effect.gen(function* () { |
245 | 395 | hasCustomPolicy = true; |
|