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 "@t3tools/contracts";
6import { CursorSettings as CursorSettingsSchema } from "@t3tools/contracts";
7import { createModelCapabilities } from "@t3tools/shared/model";
8
9import {
10 buildCursorCapabilitiesFromSdkModel,
11 buildCursorDiscoveredModelsFromSdk,
12 buildCursorProviderSnapshot,
13 buildInitialCursorProviderSnapshot,
14 checkCursorProviderStatus,
15 getCursorFallbackModels,
16} from "./CursorProvider.ts";
17import { CursorSdkCatalogError, makeCursorSdkCatalogTestLayer } from "./CursorSdkCatalog.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("getCursorFallbackModels", () => {
97 it("does not publish any built-in cursor models before SDK discovery", () => {
98 expect(
99 getCursorFallbackModels({
100 customModels: ["internal/cursor-model"],
101 }).map((model) => model.slug),
102 ).toEqual(["internal/cursor-model"]);
103 });
104});
105
106describe("buildInitialCursorProviderSnapshot", () => {
107 it.effect("uses SDK-specific pending status copy", () =>
108 Effect.gen(function* () {
109 const provider = yield* buildInitialCursorProviderSnapshot(baseCursorSettings);
110
111 expect(provider).toMatchObject({
112 status: "warning",
113 message: "Checking Cursor SDK availability...",
114 });
115 }),
116 );
117});
118
119describe("buildCursorProviderSnapshot", () => {
120 it("downgrades ready status to warning when SDK model discovery returns no models", () => {
121 expect(
122 buildCursorProviderSnapshot({
123 checkedAt: "2026-01-01T00:00:00.000Z",
124 cursorSettings: baseCursorSettings,
125 parsed: {
126 version: null,
127 status: "ready",
128 auth: { status: "authenticated", type: "api-key", label: "Cursor API key" },
129 },
130 discoveryWarning: "Cursor SDK model discovery returned no built-in models.",
131 }),
132 ).toMatchObject({
133 status: "warning",
134 message: "Cursor SDK model discovery returned no built-in models.",
135 models: [],
136 });
137 });
138});
139
140describe("Cursor SDK model discovery", () => {
141 it("maps native SDK parameter ids and default variant values to model capabilities", () => {
142 expect(buildCursorCapabilitiesFromSdkModel(sdkParameterizedModel)).toEqual(
143 createModelCapabilities({
144 optionDescriptors: [
145 selectDescriptor("effort", "Effort", [
146 { id: "low", label: "Low" },
147 { id: "high", label: "High", isDefault: true },
148 ]),
149 selectDescriptor("contextWindow", "Context", [
150 { id: "300k", label: "300K" },
151 { id: "1m", label: "1M", isDefault: true },
152 ]),
153 booleanDescriptor("fastMode", "Fast", false),
154 booleanDescriptor("thinking", "Thinking", true),
155 ],
156 }),
157 );
158 });
159
160 it("filters invalid and duplicate SDK model entries", () => {
161 expect(
162 buildCursorDiscoveredModelsFromSdk([
163 sdkParameterizedModel,
164 { ...sdkParameterizedModel, displayName: "Duplicate" },
165 { id: "", displayName: "Invalid" },
166 ]),
167 ).toEqual([
168 {
169 slug: "claude-opus-4-8",
170 name: "Opus 4.8",
171 isCustom: false,
172 capabilities: buildCursorCapabilitiesFromSdkModel(sdkParameterizedModel),
173 },
174 ]);
175 });
176});
177
178describe("checkCursorProviderStatus", () => {
179 it("uses the SDK catalog when CURSOR_API_KEY is configured", async () => {
180 const provider = await Effect.runPromise(
181 checkCursorProviderStatus(
182 {
183 ...baseCursorSettings,
184 customModels: ["internal/cursor-model"],
185 },
186 { CURSOR_API_KEY: "test-cursor-key" },
187 ).pipe(
188 Effect.provide(
189 makeCursorSdkCatalogTestLayer((apiKey) => {
190 expect(apiKey).toBe("test-cursor-key");
191 return Effect.succeed({
192 user: {
193 apiKeyName: "test-key",
194 userEmail: "cursor@example.com",
195 createdAt: "2026-01-01T00:00:00.000Z",
196 },
197 models: [sdkParameterizedModel],
198 });
199 }),
200 ),
201 ),
202 );
203
204 expect(provider).toMatchObject({
205 status: "ready",
206 auth: {
207 status: "authenticated",
208 type: "api-key",
209 label: "Cursor API key (test-key)",
210 email: "cursor@example.com",
211 },
212 models: [
213 { slug: "claude-opus-4-8", isCustom: false },
214 { slug: "internal/cursor-model", isCustom: true },
215 ],
216 });
217 });
218
219 it("surfaces SDK authentication failures", async () => {
220 const provider = await Effect.runPromise(
221 checkCursorProviderStatus(baseCursorSettings, {
222 CURSOR_API_KEY: "invalid-test-key",
223 }).pipe(
224 Effect.provide(
225 makeCursorSdkCatalogTestLayer(() =>
226 Effect.fail(
227 new CursorSdkCatalogError({
228 authenticationFailure: true,
229 cause: new Error("unauthorized"),
230 }),
231 ),
232 ),
233 ),
234 ),
235 );
236
237 expect(provider).toMatchObject({
238 status: "error",
239 auth: { status: "unauthenticated" },
240 message: "Cursor SDK authentication failed. Check CURSOR_API_KEY.",
241 });
242 });
243
244 it("requires a Cursor API key without probing any external Cursor binary", async () => {
245 const provider = await Effect.runPromise(
246 checkCursorProviderStatus(baseCursorSettings).pipe(
247 Effect.provide(
248 makeCursorSdkCatalogTestLayer(() =>
249 Effect.die("SDK catalog must not be used without CURSOR_API_KEY"),
250 ),
251 ),
252 ),
253 );
254
255 expect(provider).toMatchObject({
256 installed: true,
257 status: "error",
258 auth: { status: "unauthenticated" },
259 message: "Cursor API key is required. Add CURSOR_API_KEY in provider settings.",
260 });
261 });
262});