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