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("requires a Cursor API key without probing any external Cursor binary", () =>
265 Effect.gen(function* () {
266 const provider = yield* checkCursorProviderStatus(baseCursorSettings).pipe(
267 Effect.provide(
268 CursorSdkCatalog.layerTest(() =>
269 Effect.die("SDK catalog must not be used without CURSOR_API_KEY"),
270 ),
271 ),
272 );
273
274 expect(provider).toMatchObject({
275 installed: true,
276 status: "error",
277 auth: { status: "unauthenticated" },
278 message: "Sign in with Cursor or add CURSOR_API_KEY in provider settings.",
279 });
280 }),
281 );
282});