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