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

usageLimits.test.ts

163 lines5.7 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as HttpClient from "effect/http/HttpClient";
4import * as HttpClientResponse from "effect/http/HttpClientResponse";
5import { cursorUsageResponseToLimits, readCursorUsageLimits } from "./usageLimits.ts";
6​
7describe("Cursor usage limits", () => {
8 const checkedAt = "2026-09-16T00:00:00.000Z";
9​
10 it("uses the advertised percentages and billing-cycle reset", () => {
11 const limits = cursorUsageResponseToLimits(
12 {
13 billingCycleEnd: "1789876386000",
14 planUsage: { totalPercentUsed: 72.4, autoPercentUsed: 69.5, apiPercentUsed: 100 },
15 },
16 checkedAt,
17 );
18 expect(limits.windows).toEqual(
19 expect.arrayContaining([
20 {
21 id: "totalPercentUsed",
22 kind: "monthly",
23 label: "Overall",
24 usedPercent: 72.4,
25 resetsAt: "2026-09-20T03:53:06.000Z",
26 },
27 {
28 id: "autoPercentUsed",
29 kind: "monthly",
30 label: "Cursor Models",
31 usedPercent: 69.5,
32 resetsAt: "2026-09-20T03:53:06.000Z",
33 },
34 {
35 id: "apiPercentUsed",
36 kind: "monthly",
37 label: "Other Models",
38 usedPercent: 100,
39 resetsAt: "2026-09-20T03:53:06.000Z",
40 },
41 ]),
42 );
43 });
44​
45 it("does not invent unused allowance for absent buckets", () => {
46 expect(cursorUsageResponseToLimits({ planUsage: {} }, checkedAt).unavailable?.reason).toBe(
47 "unsupported",
48 );
49 expect(
50 cursorUsageResponseToLimits({ planUsage: { totalPercentUsed: 0 } }, checkedAt).windows,
51 ).toEqual([{ id: "totalPercentUsed", kind: "monthly", label: "Overall", usedPercent: 0 }]);
52 expect(
53 cursorUsageResponseToLimits({ planUsage: { totalPercentUsed: 150 } }, checkedAt).windows,
54 ).toEqual([{ id: "totalPercentUsed", kind: "monthly", label: "Overall", usedPercent: 100 }]);
55 });
56​
57 it.effect("requires explicit credentials and accepts an auth token", () =>
58 Effect.gen(function* () {
59 for (const token of [undefined, "explicit-token"]) {
60 const limits = yield* readCursorUsageLimits(
61 { apiEndpoint: "" },
62 token ? { CURSOR_AUTH_TOKEN: token } : {},
63 ).pipe(
64 Effect.provideService(
65 HttpClient.HttpClient,
66 HttpClient.make((request) => {
67 expect(token).toBe("explicit-token");
68 expect(request.headers.authorization).toBe("Bearer explicit-token");
69 return Effect.succeed(
70 HttpClientResponse.fromWeb(
71 request,
72 Response.json({ planUsage: { totalPercentUsed: 10 } }),
73 ),
74 );
75 }),
76 ),
77 );
78 if (token) expect(limits.windows[0]?.usedPercent).toBe(10);
79 else expect(limits.unavailable?.reason).toBe("unsupported");
80 }
81 }),
82 );
83​
84 it.effect("reports failed requests without exposing credentials or response bodies", () =>
85 Effect.gen(function* () {
86 const limits = yield* readCursorUsageLimits(
87 { apiEndpoint: "" },
88 { CURSOR_AUTH_TOKEN: "private-token" },
89 ).pipe(
90 Effect.provideService(
91 HttpClient.HttpClient,
92 HttpClient.make((request) =>
93 Effect.succeed(
94 HttpClientResponse.fromWeb(
95 request,
96 new Response("private response", { status: 401 }),
97 ),
98 ),
99 ),
100 ),
101 );
102 expect(limits.unavailable).toEqual({
103 reason: "probeFailed",
104 message: "Cursor could not read usage limits.",
105 });
106 }),
107 );
108​
109 it.effect("exchanges an API key to read its account limits", () =>
110 Effect.gen(function* () {
111 const requests: Array<readonly [string, string | undefined]> = [];
112 const limits = yield* readCursorUsageLimits(
113 { apiEndpoint: "" },
114 { CURSOR_API_KEY: "api-key" },
115 ).pipe(
116 Effect.provideService(
117 HttpClient.HttpClient,
118 HttpClient.make((request) => {
119 requests.push([request.url, request.headers.authorization]);
120 return Effect.succeed(
121 HttpClientResponse.fromWeb(
122 request,
123 Response.json(
124 request.url.endsWith("/auth/exchange_user_api_key")
125 ? { accessToken: "api-key-access-token" }
126 : { planUsage: { totalPercentUsed: 37 } },
127 ),
128 ),
129 );
130 }),
131 ),
132 );
133 expect(requests).toEqual([
134 ["https://api2.cursor.sh/auth/exchange_user_api_key", "Bearer api-key"],
135 [
136 "https://api2.cursor.sh/aiserver.v1.DashboardService/GetCurrentPeriodUsage",
137 "Bearer api-key-access-token",
138 ],
139 ]);
140 expect(limits.windows[0]?.usedPercent).toBe(37);
141 }),
142 );
143​
144 it.effect("does not send an API key to a custom endpoint", () =>
145 Effect.gen(function* () {
146 for (const [apiEndpoint, environment] of [
147 ["http://localhost:3000", { CURSOR_API_KEY: "api-key" }],
148 ["", { CURSOR_API_ENDPOINT: "http://localhost:3000", CURSOR_API_KEY: "api-key" }],
149 ["", { CURSOR_API_ENDPOINT: "https://cursor-proxy.example", CURSOR_API_KEY: "api-key" }],
150 ["https://cursor-proxy.example", { CURSOR_API_KEY: "api-key" }],
151 ] as const) {
152 const limits = yield* readCursorUsageLimits({ apiEndpoint }, environment).pipe(
153 Effect.provideService(
154 HttpClient.HttpClient,
155 HttpClient.make(() => Effect.die("must not send an API key to a proxy")),
156 ),
157 );
158 expect(limits.unavailable?.reason).toBe("unsupported");
159 expect(limits.unavailable?.message).toContain("default Cursor endpoint");
160 }
161 }),
162 );
163});