Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import { NodeServices } from "@effect/platform-node";
3import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Path from "effect/Path";
7import { HttpClient, HttpClientResponse } from "effect/unstable/http";
8import { cursorUsageResponseToLimits, readCursorUsageLimits } from "./cursorUsageLimits.ts";
9
10const withNodeServices = <A, E>(effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path>) =>
11 effect.pipe(Effect.provide(NodeServices.layer));
12
13describe("Cursor usage limits", () => {
14 const checkedAt = "2026-09-16T00:00:00.000Z";
15
16 it("uses the advertised percentages and billing-cycle reset", () => {
17 const limits = cursorUsageResponseToLimits(
18 {
19 billingCycleEnd: "1789876386000",
20 planUsage: { totalPercentUsed: 72.4, autoPercentUsed: 69.5, apiPercentUsed: 100 },
21 },
22 checkedAt,
23 );
24 expect(limits.windows).toEqual(
25 expect.arrayContaining([
26 {
27 id: "totalPercentUsed",
28 kind: "monthly",
29 label: "Overall",
30 usedPercent: 72.4,
31 resetsAt: "2026-09-20T03:53:06.000Z",
32 },
33 {
34 id: "autoPercentUsed",
35 kind: "monthly",
36 label: "Cursor Models",
37 usedPercent: 69.5,
38 resetsAt: "2026-09-20T03:53:06.000Z",
39 },
40 {
41 id: "apiPercentUsed",
42 kind: "monthly",
43 label: "Other Models",
44 usedPercent: 100,
45 resetsAt: "2026-09-20T03:53:06.000Z",
46 },
47 ]),
48 );
49 });
50
51 it("does not invent unused allowance for absent buckets", () => {
52 expect(cursorUsageResponseToLimits({ planUsage: {} }, checkedAt).unavailable?.reason).toBe(
53 "unsupported",
54 );
55 expect(
56 cursorUsageResponseToLimits({ planUsage: { totalPercentUsed: 0 } }, checkedAt).windows,
57 ).toEqual([{ id: "totalPercentUsed", kind: "monthly", label: "Overall", usedPercent: 0 }]);
58 expect(
59 cursorUsageResponseToLimits({ planUsage: { totalPercentUsed: 150 } }, checkedAt).windows,
60 ).toEqual([{ id: "totalPercentUsed", kind: "monthly", label: "Overall", usedPercent: 100 }]);
61 });
62
63 it.effect("reads the instance's credentials and endpoint even when usage enabled is false", () =>
64 Effect.gen(function* () {
65 yield* withNodeServices(
66 Effect.gen(function* () {
67 const fs = yield* FileSystem.FileSystem;
68 const path = yield* Path.Path;
69 const directory = yield* fs.makeTempDirectoryScoped();
70 yield* fs.makeDirectory(path.join(directory, "cursor"));
71 yield* fs.writeFileString(
72 path.join(directory, "cursor", "auth.json"),
73 '{"accessToken":"instance-token"}',
74 );
75 const client = HttpClient.make((request) => {
76 expect(request.url).toBe(
77 "https://cursor.example/aiserver.v1.DashboardService/GetCurrentPeriodUsage",
78 );
79 expect(request.method).toBe("POST");
80 expect(request.headers.authorization).toBe("Bearer instance-token");
81 expect(request.headers["connect-protocol-version"]).toBe("1");
82 return Effect.succeed(
83 HttpClientResponse.fromWeb(
84 request,
85 Response.json({ enabled: false, planUsage: { totalPercentUsed: 42 } }),
86 ),
87 );
88 });
89 yield* fs.makeDirectory(path.join(directory, ".cursor"));
90 yield* fs.writeFileString(
91 path.join(directory, ".cursor", "auth.json"),
92 '{"accessToken":"instance-token"}',
93 );
94 for (const platform of ["linux", "darwin"] as const) {
95 const limits = yield* readCursorUsageLimits(
96 { apiEndpoint: "https://cursor.example/" },
97 { XDG_CONFIG_HOME: directory, HOME: directory, AGENT_CLI_CREDENTIAL_STORE: "file" },
98 ).pipe(
99 Effect.provideService(HostProcessPlatform, platform),
100 Effect.provideService(HttpClient.HttpClient, client),
101 );
102 expect(limits.windows[0]?.usedPercent).toBe(42);
103 }
104 }).pipe(Effect.scoped),
105 );
106 }),
107 );
108
109 it.effect(
110 "never reads stale files for keychain or memory logins, but accepts an explicit auth token",
111 () =>
112 Effect.gen(function* () {
113 for (const platform of ["linux", "darwin"] as const) {
114 for (const token of [undefined, "explicit-token"]) {
115 const limits = yield* withNodeServices(
116 readCursorUsageLimits(
117 { apiEndpoint: "" },
118 {
119 AGENT_CLI_CREDENTIAL_STORE: platform === "linux" ? "memory" : "default",
120 ...(token ? { CURSOR_AUTH_TOKEN: token } : {}),
121 },
122 false,
123 async () => {
124 throw new Error("must not read Keychain before opt-in");
125 },
126 ).pipe(
127 Effect.provideService(HostProcessPlatform, platform),
128 Effect.provideService(
129 FileSystem.FileSystem,
130 FileSystem.makeNoop({
131 readFileString: () => Effect.die("must not read an unrelated credential file"),
132 }),
133 ),
134 Effect.provideService(
135 HttpClient.HttpClient,
136 HttpClient.make((request) => {
137 expect(token).toBe("explicit-token");
138 expect(request.headers.authorization).toBe("Bearer explicit-token");
139 return Effect.succeed(
140 HttpClientResponse.fromWeb(
141 request,
142 Response.json({ planUsage: { totalPercentUsed: 10 } }),
143 ),
144 );
145 }),
146 ),
147 ),
148 );
149 if (token) expect(limits.windows[0]?.usedPercent).toBe(10);
150 else expect(limits.unavailable?.reason).toBe("unsupported");
151 }
152 }
153 }),
154 );
155
156 it.effect("reports failed requests without exposing credentials or response bodies", () =>
157 Effect.gen(function* () {
158 const limits = yield* withNodeServices(
159 readCursorUsageLimits({ apiEndpoint: "" }, { CURSOR_AUTH_TOKEN: "private-token" }).pipe(
160 Effect.provideService(
161 HttpClient.HttpClient,
162 HttpClient.make((request) =>
163 Effect.succeed(
164 HttpClientResponse.fromWeb(
165 request,
166 new Response("private response", { status: 401 }),
167 ),
168 ),
169 ),
170 ),
171 ),
172 );
173 expect(limits.unavailable).toEqual({
174 reason: "probeFailed",
175 message: "Cursor could not read usage limits.",
176 });
177 }),
178 );
179
180 it.effect("does not use a stored login for an explicit API key", () =>
181 Effect.gen(function* () {
182 const limits = yield* withNodeServices(
183 readCursorUsageLimits({ apiEndpoint: "" }, { CURSOR_API_KEY: "different-account" }).pipe(
184 Effect.provideService(
185 HttpClient.HttpClient,
186 HttpClient.make(() => Effect.die("must not request usage")),
187 ),
188 ),
189 );
190 expect(limits.unavailable?.reason).toBe("unsupported");
191 }),
192 );
193
194 it.effect("reads the default macOS Cursor login from Keychain for limits", () =>
195 Effect.gen(function* () {
196 const limits = yield* withNodeServices(
197 readCursorUsageLimits({ apiEndpoint: "" }, {}, true, async () => "keychain-token").pipe(
198 Effect.provideService(HostProcessPlatform, "darwin"),
199 Effect.provideService(
200 FileSystem.FileSystem,
201 FileSystem.makeNoop({
202 readFileString: () => Effect.die("must not read a stale credential file"),
203 }),
204 ),
205 Effect.provideService(
206 HttpClient.HttpClient,
207 HttpClient.make((request) => {
208 expect(request.headers.authorization).toBe("Bearer keychain-token");
209 return Effect.succeed(
210 HttpClientResponse.fromWeb(
211 request,
212 Response.json({ planUsage: { totalPercentUsed: 42 } }),
213 ),
214 );
215 }),
216 ),
217 ),
218 );
219 expect(limits.windows[0]?.usedPercent).toBe(42);
220 }),
221 );
222
223 it.effect("reports a Keychain initialization failure without failing the provider refresh", () =>
224 Effect.gen(function* () {
225 const limits = yield* withNodeServices(
226 readCursorUsageLimits({ apiEndpoint: "" }, {}, true, async () => {
227 throw new Error("Keychain initialization failed");
228 }).pipe(
229 Effect.provideService(HostProcessPlatform, "darwin"),
230 Effect.provideService(
231 HttpClient.HttpClient,
232 HttpClient.make(() => Effect.die("must not request limits without a login")),
233 ),
234 ),
235 );
236 expect(limits.unavailable?.reason).toBe("probeFailed");
237 }),
238 );
239
240 it.effect("does not read Keychain or send its token to a custom endpoint", () =>
241 Effect.gen(function* () {
242 for (const [apiEndpoint, environment] of [
243 ["http://localhost:3000", {}],
244 ["", { CURSOR_API_ENDPOINT: "http://localhost:3000" }],
245 ["https://cursor-proxy.example", {}],
246 ["", { CURSOR_API_ENDPOINT: "https://cursor-proxy.example" }],
247 ] as const) {
248 const limits = yield* withNodeServices(
249 readCursorUsageLimits({ apiEndpoint }, environment, true, async () => {
250 throw new Error("must not read Keychain for a custom endpoint");
251 }).pipe(
252 Effect.provideService(HostProcessPlatform, "darwin"),
253 Effect.provideService(
254 HttpClient.HttpClient,
255 HttpClient.make(() => Effect.die("must not send a Keychain credential to a proxy")),
256 ),
257 ),
258 );
259 expect(limits.unavailable?.reason).toBe("unsupported");
260 expect(limits.unavailable?.message).toContain("default Cursor endpoint");
261 }
262 }),
263 );
264});