Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CursorProvider.test.ts

251 lines7.2 KB

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