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