The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { SDKModel } from "@cursor/sdk"; |
| 2 | import { describe, expect, it } from "@effect/vitest"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Fiber from "effect/Fiber"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | import * as TestClock from "effect/testing/TestClock"; |
| 7 | import type { CursorSettings } from "../settings.ts"; |
| 8 | import { CursorSettings as CursorSettingsSchema } from "../settings.ts"; |
| 9 | import { createModelCapabilities } from "@t3tools/shared/model"; |
| 10 | import { COMPACT_SLASH_COMMAND } from "@t3tools/provider-core/server/snapshotProbe"; |
| 11 | |
| 12 | import { |
| 13 | buildCursorCapabilitiesFromSdkModel, |
| 14 | buildCursorDiscoveredModelsFromSdk, |
| 15 | buildCursorProviderSnapshot, |
| 16 | buildInitialCursorProviderSnapshot, |
| 17 | checkCursorProviderStatus, |
| 18 | } from "./status.ts"; |
| 19 | import * as CursorSdkCatalog from "./CursorSdkCatalog.ts"; |
| 20 | import { loadCursorSdk } from "./sdk.ts"; |
| 21 | |
| 22 | const { CursorSdkError } = loadCursorSdk(); |
| 23 | |
| 24 | const decodeCursorSettings = Schema.decodeSync(CursorSettingsSchema); |
| 25 | |
| 26 | function selectDescriptor( |
| 27 | id: string, |
| 28 | label: string, |
| 29 | options: ReadonlyArray<{ id: string; label: string; isDefault?: boolean }>, |
| 30 | ) { |
| 31 | return { |
| 32 | id, |
| 33 | label, |
| 34 | type: "select" as const, |
| 35 | options: [...options], |
| 36 | ...(options.find((option) => option.isDefault)?.id |
| 37 | ? { currentValue: options.find((option) => option.isDefault)?.id } |
| 38 | : {}), |
| 39 | }; |
| 40 | } |
| 41 | |
| 42 | function booleanDescriptor(id: string, label: string, currentValue?: boolean) { |
| 43 | return { |
| 44 | id, |
| 45 | label, |
| 46 | type: "boolean" as const, |
| 47 | ...(typeof currentValue === "boolean" ? { currentValue } : {}), |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | const baseCursorSettings: CursorSettings = decodeCursorSettings({ |
| 52 | enabled: true, |
| 53 | customModels: [], |
| 54 | }); |
| 55 | |
| 56 | const sdkParameterizedModel = { |
| 57 | id: "claude-opus-4-8", |
| 58 | displayName: "Opus 4.8", |
| 59 | parameters: [ |
| 60 | { |
| 61 | id: "thinking", |
| 62 | displayName: "Thinking", |
| 63 | values: [{ value: "false" }, { value: "true" }], |
| 64 | }, |
| 65 | { |
| 66 | id: "context", |
| 67 | displayName: "Context", |
| 68 | values: [ |
| 69 | { value: "300k", displayName: "300K" }, |
| 70 | { value: "1m", displayName: "1M" }, |
| 71 | ], |
| 72 | }, |
| 73 | { |
| 74 | id: "effort", |
| 75 | displayName: "Effort", |
| 76 | values: [ |
| 77 | { value: "low", displayName: "Low" }, |
| 78 | { value: "high", displayName: "High" }, |
| 79 | ], |
| 80 | }, |
| 81 | { |
| 82 | id: "fast", |
| 83 | displayName: "Fast", |
| 84 | values: [{ value: "false" }, { value: "true", displayName: "Fast" }], |
| 85 | }, |
| 86 | ], |
| 87 | variants: [ |
| 88 | { |
| 89 | displayName: "Opus 4.8", |
| 90 | isDefault: true, |
| 91 | params: [ |
| 92 | { id: "thinking", value: "true" }, |
| 93 | { id: "context", value: "1m" }, |
| 94 | { id: "effort", value: "high" }, |
| 95 | { id: "fast", value: "false" }, |
| 96 | ], |
| 97 | }, |
| 98 | ], |
| 99 | } satisfies SDKModel; |
| 100 | |
| 101 | describe("buildInitialCursorProviderSnapshot", () => { |
| 102 | it.effect.each([true, false])("advertises manual compaction only when enabled (%s)", (enabled) => |
| 103 | Effect.gen(function* () { |
| 104 | const provider = yield* buildInitialCursorProviderSnapshot({ |
| 105 | ...baseCursorSettings, |
| 106 | enabled, |
| 107 | }); |
| 108 | |
| 109 | expect(provider.slashCommands).toEqual(enabled ? [COMPACT_SLASH_COMMAND] : []); |
| 110 | }), |
| 111 | ); |
| 112 | }); |
| 113 | |
| 114 | describe("buildCursorProviderSnapshot", () => { |
| 115 | it("downgrades ready status to warning when SDK model discovery returns no models", () => { |
| 116 | expect( |
| 117 | buildCursorProviderSnapshot({ |
| 118 | checkedAt: "2026-01-01T00:00:00.000Z", |
| 119 | cursorSettings: baseCursorSettings, |
| 120 | parsed: { |
| 121 | version: null, |
| 122 | status: "ready", |
| 123 | auth: { status: "authenticated", type: "api-key", label: "Cursor API key" }, |
| 124 | }, |
| 125 | discoveryWarning: "Cursor SDK model discovery returned no built-in models.", |
| 126 | }), |
| 127 | ).toMatchObject({ |
| 128 | status: "warning", |
| 129 | message: "Cursor SDK model discovery returned no built-in models.", |
| 130 | models: [], |
| 131 | slashCommands: [COMPACT_SLASH_COMMAND], |
| 132 | supportsConversationRollback: true, |
| 133 | }); |
| 134 | }); |
| 135 | }); |
| 136 | |
| 137 | describe("Cursor SDK model discovery", () => { |
| 138 | it("maps native SDK parameter ids and default variant values to model capabilities", () => { |
| 139 | expect(buildCursorCapabilitiesFromSdkModel(sdkParameterizedModel)).toEqual( |
| 140 | createModelCapabilities({ |
| 141 | optionDescriptors: [ |
| 142 | selectDescriptor("effort", "Effort", [ |
| 143 | { id: "low", label: "Low" }, |
| 144 | { id: "high", label: "High", isDefault: true }, |
| 145 | ]), |
| 146 | selectDescriptor("contextWindow", "Context", [ |
| 147 | { id: "300k", label: "300K" }, |
| 148 | { id: "1m", label: "1M", isDefault: true }, |
| 149 | ]), |
| 150 | booleanDescriptor("fastMode", "Fast", false), |
| 151 | booleanDescriptor("thinking", "Thinking", true), |
| 152 | ], |
| 153 | }), |
| 154 | ); |
| 155 | }); |
| 156 | |
| 157 | it("keeps catalog aliases and filters invalid and duplicate SDK model entries", () => { |
| 158 | const composer = { |
| 159 | id: "composer-2", |
| 160 | displayName: "Composer 2", |
| 161 | aliases: [" composer-latest ", "", "composer"], |
| 162 | } satisfies SDKModel; |
| 163 | expect( |
| 164 | buildCursorDiscoveredModelsFromSdk([ |
| 165 | sdkParameterizedModel, |
| 166 | { ...sdkParameterizedModel, displayName: "Duplicate" }, |
| 167 | { id: "", displayName: "Invalid" }, |
| 168 | composer, |
| 169 | ]), |
| 170 | ).toStrictEqual([ |
| 171 | { |
| 172 | slug: "claude-opus-4-8", |
| 173 | name: "Opus 4.8", |
| 174 | isCustom: false, |
| 175 | capabilities: buildCursorCapabilitiesFromSdkModel(sdkParameterizedModel), |
| 176 | }, |
| 177 | { |
| 178 | slug: "composer-2", |
| 179 | name: "Composer 2", |
| 180 | aliases: ["composer-latest", "composer"], |
| 181 | isCustom: false, |
| 182 | capabilities: buildCursorCapabilitiesFromSdkModel(composer), |
| 183 | }, |
| 184 | ]); |
| 185 | }); |
| 186 | }); |
| 187 | |
| 188 | describe("checkCursorProviderStatus", () => { |
| 189 | it.effect("uses the SDK catalog when CURSOR_API_KEY is configured", () => |
| 190 | Effect.gen(function* () { |
| 191 | const provider = yield* checkCursorProviderStatus( |
| 192 | { |
| 193 | ...baseCursorSettings, |
| 194 | customModels: ["internal/cursor-model"], |
| 195 | }, |
| 196 | { CURSOR_API_KEY: "test-cursor-key" }, |
| 197 | ).pipe( |
| 198 | Effect.provide( |
| 199 | CursorSdkCatalog.layerTest((apiKey) => { |
| 200 | expect(apiKey).toBe("test-cursor-key"); |
| 201 | return Effect.succeed({ |
| 202 | user: { |
| 203 | apiKeyName: "test-key", |
| 204 | userEmail: "cursor@example.com", |
| 205 | createdAt: "2026-01-01T00:00:00.000Z", |
| 206 | }, |
| 207 | models: [sdkParameterizedModel], |
| 208 | }); |
| 209 | }), |
| 210 | ), |
| 211 | ); |
| 212 | |
| 213 | expect(provider).toMatchObject({ |
| 214 | status: "ready", |
| 215 | slashCommands: [COMPACT_SLASH_COMMAND], |
| 216 | auth: { |
| 217 | status: "authenticated", |
| 218 | type: "api-key", |
| 219 | label: "Cursor API key (test-key)", |
| 220 | email: "cursor@example.com", |
| 221 | }, |
| 222 | models: [ |
| 223 | { slug: "claude-opus-4-8", isCustom: false }, |
| 224 | { slug: "internal/cursor-model", isCustom: true }, |
| 225 | ], |
| 226 | }); |
| 227 | }), |
| 228 | ); |
| 229 | |
| 230 | it.effect("allows slow SDK discovery with a configured readiness timeout", () => |
| 231 | Effect.gen(function* () { |
| 232 | const fiber = yield* checkCursorProviderStatus(baseCursorSettings, { |
| 233 | CURSOR_API_KEY: "test-key", |
| 234 | T3CODE_CURSOR_PROBE_TIMEOUT_MS: "30000", |
| 235 | }).pipe( |
| 236 | Effect.provide( |
| 237 | CursorSdkCatalog.layerTest(() => |
| 238 | Effect.sleep("20 seconds").pipe( |
| 239 | Effect.as({ |
| 240 | user: { |
| 241 | apiKeyName: "test", |
| 242 | userEmail: "cursor@example.com", |
| 243 | createdAt: "2026-01-01", |
| 244 | }, |
| 245 | models: [sdkParameterizedModel], |
| 246 | }), |
| 247 | ), |
| 248 | ), |
| 249 | ), |
| 250 | Effect.forkChild, |
| 251 | ); |
| 252 | yield* TestClock.adjust("20 seconds"); |
| 253 | expect((yield* Fiber.join(fiber)).status).toBe("ready"); |
| 254 | }), |
| 255 | ); |
| 256 | |
| 257 | it.effect("surfaces SDK authentication failures", () => |
| 258 | Effect.gen(function* () { |
| 259 | const provider = yield* checkCursorProviderStatus(baseCursorSettings, { |
| 260 | CURSOR_API_KEY: "invalid-test-key", |
| 261 | }).pipe( |
| 262 | Effect.provide( |
| 263 | CursorSdkCatalog.layerTest(() => |
| 264 | Effect.fail( |
| 265 | new CursorSdkCatalog.CursorSdkCatalogError({ |
| 266 | authenticationFailure: true, |
| 267 | cause: new Error("unauthorized"), |
| 268 | }), |
| 269 | ), |
| 270 | ), |
| 271 | ), |
| 272 | ); |
| 273 | |
| 274 | expect(provider).toMatchObject({ |
| 275 | status: "error", |
| 276 | auth: { status: "unauthenticated" }, |
| 277 | message: "Cursor SDK authentication failed. Check CURSOR_API_KEY.", |
| 278 | }); |
| 279 | }), |
| 280 | ); |
| 281 | |
| 282 | it.effect.each([ |
| 283 | { |
| 284 | authenticationType: "browser" as const, |
| 285 | message: |
| 286 | "Cursor needs a paid plan to run in T3 Code. Upgrade this account or sign in with a different one.", |
| 287 | }, |
| 288 | { |
| 289 | authenticationType: "api-key" as const, |
| 290 | message: |
| 291 | "Cursor needs a paid plan to run in T3 Code. Upgrade the account that owns CURSOR_API_KEY or use a key from a paid account.", |
| 292 | }, |
| 293 | ])( |
| 294 | "explains a rejected plan for $authenticationType authentication", |
| 295 | ({ authenticationType, message }) => |
| 296 | Effect.gen(function* () { |
| 297 | const provider = yield* checkCursorProviderStatus( |
| 298 | baseCursorSettings, |
| 299 | { CURSOR_API_KEY: "free-plan-key" }, |
| 300 | authenticationType, |
| 301 | ).pipe( |
| 302 | Effect.provide( |
| 303 | CursorSdkCatalog.layerTest(() => |
| 304 | Effect.fail( |
| 305 | new CursorSdkCatalog.CursorSdkCatalogError({ |
| 306 | authenticationFailure: false, |
| 307 | code: "plan_required", |
| 308 | cause: new CursorSdkError( |
| 309 | "[plan_required] Cloud Agent is not available for free users. Please upgrade to Pro.", |
| 310 | { code: "plan_required", status: 403 }, |
| 311 | ), |
| 312 | }), |
| 313 | ), |
| 314 | ), |
| 315 | ), |
| 316 | ); |
| 317 | |
| 318 | expect(provider).toMatchObject({ |
| 319 | status: "error", |
| 320 | auth: { status: "unknown" }, |
| 321 | message, |
| 322 | }); |
| 323 | }), |
| 324 | ); |
| 325 | |
| 326 | it.effect.each([ |
| 327 | { |
| 328 | name: "keeps models when /v1/me is blocked", |
| 329 | meFailure: new CursorSdkError("[feature_unavailable] Not available for this team.", { |
| 330 | code: "feature_unavailable", |
| 331 | status: 403, |
| 332 | }), |
| 333 | expected: { |
| 334 | status: "ready", |
| 335 | auth: { status: "authenticated", type: "api-key", label: "Cursor API key" }, |
| 336 | models: [{ slug: "claude-opus-4-8", isCustom: false }], |
| 337 | }, |
| 338 | }, |
| 339 | { |
| 340 | name: "keeps models when /v1/me returns a 403 without a code", |
| 341 | meFailure: new CursorSdkError("[unknown] Forbidden", { code: "unknown", status: 403 }), |
| 342 | expected: { |
| 343 | status: "ready", |
| 344 | auth: { status: "authenticated", type: "api-key", label: "Cursor API key" }, |
| 345 | models: [{ slug: "claude-opus-4-8", isCustom: false }], |
| 346 | }, |
| 347 | }, |
| 348 | { |
| 349 | name: "keeps the paid-plan message when /v1/me returns plan_required", |
| 350 | meFailure: new CursorSdkError("[plan_required] Not available for free users.", { |
| 351 | code: "plan_required", |
| 352 | status: 403, |
| 353 | }), |
| 354 | expected: { |
| 355 | status: "error", |
| 356 | auth: { status: "unknown" }, |
| 357 | message: |
| 358 | "Cursor needs a paid plan to run in T3 Code. Upgrade the account that owns CURSOR_API_KEY or use a key from a paid account.", |
| 359 | }, |
| 360 | }, |
| 361 | { |
| 362 | name: "fails when /v1/me has a server error", |
| 363 | meFailure: new CursorSdkError("[unknown] Service unavailable", { |
| 364 | code: "unknown", |
| 365 | status: 503, |
| 366 | }), |
| 367 | expected: { |
| 368 | status: "error", |
| 369 | auth: { status: "unknown" }, |
| 370 | message: "Cursor SDK catalog request failed. Check server logs for details.", |
| 371 | }, |
| 372 | }, |
| 373 | { |
| 374 | name: "rejects a 401 even when its code is feature_unavailable", |
| 375 | meFailure: new CursorSdkError("Unauthorized", { |
| 376 | code: "feature_unavailable", |
| 377 | status: 401, |
| 378 | }), |
| 379 | expected: { |
| 380 | status: "error", |
| 381 | auth: { status: "unauthenticated" }, |
| 382 | message: "Cursor SDK authentication failed. Check CURSOR_API_KEY.", |
| 383 | }, |
| 384 | }, |
| 385 | { |
| 386 | name: "fails a key that /v1/me rejects", |
| 387 | meFailure: new CursorSdkError("Unauthorized", { status: 401 }), |
| 388 | expected: { |
| 389 | status: "error", |
| 390 | auth: { status: "unauthenticated" }, |
| 391 | message: "Cursor SDK authentication failed. Check CURSOR_API_KEY.", |
| 392 | }, |
| 393 | }, |
| 394 | ])("$name", ({ meFailure, expected }) => |
| 395 | Effect.gen(function* () { |
| 396 | const catalog = yield* CursorSdkCatalog.makeCursorSdkCatalog({ |
| 397 | readUser: () => |
| 398 | Effect.fail( |
| 399 | new CursorSdkCatalog.CursorSdkCatalogError({ |
| 400 | authenticationFailure: meFailure.status === 401, |
| 401 | ...(meFailure.status === undefined ? {} : { status: meFailure.status }), |
| 402 | ...(meFailure.code === undefined ? {} : { code: meFailure.code }), |
| 403 | cause: meFailure, |
| 404 | }), |
| 405 | ), |
| 406 | readModels: () => Effect.succeed([sdkParameterizedModel]), |
| 407 | }); |
| 408 | const provider = yield* checkCursorProviderStatus(baseCursorSettings, { |
| 409 | CURSOR_API_KEY: "enterprise-key", |
| 410 | }).pipe(Effect.provideService(CursorSdkCatalog.CursorSdkCatalog, catalog)); |
| 411 | |
| 412 | expect(provider).toMatchObject(expected); |
| 413 | }), |
| 414 | ); |
| 415 | |
| 416 | it.effect("does not report a single slow catalog read as a provider error", () => |
| 417 | Effect.gen(function* () { |
| 418 | let reads = 0; |
| 419 | const fiber = yield* checkCursorProviderStatus(baseCursorSettings, { |
| 420 | CURSOR_API_KEY: "test-cursor-key", |
| 421 | }).pipe( |
| 422 | Effect.provide( |
| 423 | CursorSdkCatalog.layerTest(() => { |
| 424 | reads += 1; |
| 425 | return reads === 1 |
| 426 | ? Effect.never |
| 427 | : Effect.succeed({ |
| 428 | user: { |
| 429 | apiKeyName: "test-key", |
| 430 | userEmail: "cursor@example.com", |
| 431 | createdAt: "2026-01-01T00:00:00.000Z", |
| 432 | }, |
| 433 | models: [sdkParameterizedModel], |
| 434 | }); |
| 435 | }), |
| 436 | ), |
| 437 | Effect.forkChild({ startImmediately: true }), |
| 438 | ); |
| 439 | yield* TestClock.adjust("1 minute"); |
| 440 | const provider = yield* Fiber.join(fiber); |
| 441 | |
| 442 | expect(provider.status).not.toBe("error"); |
| 443 | }), |
| 444 | ); |
| 445 | |
| 446 | it.effect("requires a Cursor API key without probing any external Cursor binary", () => |
| 447 | Effect.gen(function* () { |
| 448 | const provider = yield* checkCursorProviderStatus(baseCursorSettings).pipe( |
| 449 | Effect.provide( |
| 450 | CursorSdkCatalog.layerTest(() => |
| 451 | Effect.die("SDK catalog must not be used without CURSOR_API_KEY"), |
| 452 | ), |
| 453 | ), |
| 454 | ); |
| 455 | |
| 456 | expect(provider).toMatchObject({ |
| 457 | installed: true, |
| 458 | status: "error", |
| 459 | auth: { status: "unauthenticated" }, |
| 460 | message: "Sign in with Cursor or add CURSOR_API_KEY in provider settings.", |
| 461 | }); |
| 462 | }), |
| 463 | ); |
| 464 | }); |