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