@@ -7,12 +7,14 @@ import * as Layer from "effect/Layer";
77import * as Option from "effect/Option" ;
88import * as Stream from "effect/Stream" ;
99import { vi } from "vite-plus/test" ;
10+ import * as HttpClientResponse from "effect/http/HttpClientResponse" ;
1011import * as HttpClient from "effect/http/HttpClient" ;
1112import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
1213
1314import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers" ;
1415import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost" ;
1516import { CursorDriver } from "./driver.ts" ;
17+ import { makeCursorCredentialStore } from "./credentialStore.ts" ;
1618import * as CursorAgentSdk from "./CursorAgentSdk.ts" ;
1719import * as CursorSdk from "./CursorSdk.ts" ;
1820import * as CursorKeychain from "./CursorKeychain.ts" ;
@@ -91,7 +93,26 @@ it.layer(layerTest)("CursorDriver", (it) => {
9193 } ;
9294 const openedKeys : Array < string | undefined > = [ ] ;
9395 let closed = 0 ;
96+ const requests : string [ ] = [ ] ;
97+ const usageClient = HttpClient . make ( ( request ) => {
98+ requests . push ( request . url ) ;
99+ const exchange = request . url . endsWith ( "/auth/exchange_user_api_key" ) ;
100+ expect ( request . headers . authorization ) . toBe (
101+ exchange ? "Bearer instance-browser-key" : "Bearer browser-access-token" ,
102+ ) ;
103+ return Effect . succeed (
104+ HttpClientResponse . fromWeb (
105+ request ,
106+ Response . json (
107+ exchange
108+ ? { accessToken : "browser-access-token" }
109+ : { planUsage : { totalPercentUsed : 23 } } ,
110+ ) ,
111+ ) ,
112+ ) ;
113+ } ) ;
94114 const instance = yield * CursorDriver . create ( input ) . pipe (
115+ Effect . provideService ( HttpClient . HttpClient , usageClient ) ,
95116 Effect . provideService ( CursorAgentSdk . CursorAgentSdkRunner , {
96117 assertComplete : Effect . void ,
97118 open : ( request ) =>
@@ -118,6 +139,7 @@ it.layer(layerTest)("CursorDriver", (it) => {
118139 expect ( me ) . toHaveBeenCalledWith ( { apiKey : "instance-browser-key" } ) ;
119140 expect ( yield * instance . snapshot . getSnapshot ) . toMatchObject ( {
120141 status : "ready" ,
142+ usageLimits : { windows : [ { id : "totalPercentUsed" , usedPercent : 23 } ] } ,
121143 auth : {
122144 status : "authenticated" ,
123145 type : "browser" ,
@@ -142,7 +164,13 @@ it.layer(layerTest)("CursorDriver", (it) => {
142164 yield * runtime . ensureThread ( { threadId, modelSelection, runtimePolicy } ) ;
143165 expect ( openedKeys ) . toEqual ( [ "instance-browser-key" ] ) ;
144166 expect ( closed ) . toBe ( 0 ) ;
145- const recreated = yield * CursorDriver . create ( input ) ;
167+ expect ( requests ) . toEqual ( [
168+ "https://api2.cursor.sh/auth/exchange_user_api_key" ,
169+ "https://api2.cursor.sh/aiserver.v1.DashboardService/GetCurrentPeriodUsage" ,
170+ ] ) ;
171+ const recreated = yield * CursorDriver . create ( input ) . pipe (
172+ Effect . provideService ( HttpClient . HttpClient , usageClient ) ,
173+ ) ;
146174 expect ( ( yield * recreated . snapshot . refresh ) . auth . status ) . toBe ( "authenticated" ) ;
147175 yield * instance . auth ! . logout ( Effect . void ) ;
148176 expect ( closed ) . toBe ( 1 ) ;
@@ -157,6 +185,85 @@ it.layer(layerTest)("CursorDriver", (it) => {
157185 } ) . pipe ( Effect . scoped ) ,
158186 ) ;
159187
188+ it . effect ( "keeps env CURSOR_API_KEY limits unsupported without stored credentials" , ( ) =>
189+ Effect . gen ( function * ( ) {
190+ const me = vi . spyOn ( Cursor , "me" ) . mockResolvedValue ( {
191+ apiKeyName : "Configured key" ,
192+ createdAt : "2026-01-01T00:00:00.000Z" ,
193+ } ) ;
194+ const models = vi . spyOn ( Cursor . models , "list" ) . mockResolvedValue ( [ ] ) ;
195+ yield * Effect . addFinalizer ( ( ) =>
196+ Effect . sync ( ( ) => {
197+ me . mockRestore ( ) ;
198+ models . mockRestore ( ) ;
199+ } ) ,
200+ ) ;
201+ const instance = yield * CursorDriver . create ( {
202+ instanceId : ProviderInstanceId . make ( "cursor-configured-key" ) ,
203+ displayName : "Configured Cursor" ,
204+ enabled : true ,
205+ environment : [ { name : "CURSOR_API_KEY" , value : "configured-key" , sensitive : true } ] ,
206+ config : CursorDriver . defaultConfig ( ) ,
207+ } ) . pipe (
208+ Effect . provideService (
209+ HttpClient . HttpClient ,
210+ HttpClient . make ( ( ) => Effect . die ( "must not request usage" ) ) ,
211+ ) ,
212+ ) ;
213+ expect ( ( yield * instance . snapshot . refresh ) . usageLimits ?. unavailable ?. reason ) . toBe (
214+ "unsupported" ,
215+ ) ;
216+ } ) . pipe ( Effect . scoped ) ,
217+ ) ;
218+
219+ it . effect ( "keeps env CURSOR_API_KEY limits unsupported with valid stored credentials" , ( ) =>
220+ Effect . gen ( function * ( ) {
221+ const me = vi . spyOn ( Cursor , "me" ) . mockResolvedValue ( {
222+ apiKeyName : "Configured key" ,
223+ createdAt : "2026-01-01T00:00:00.000Z" ,
224+ } ) ;
225+ const models = vi . spyOn ( Cursor . models , "list" ) . mockResolvedValue ( [ ] ) ;
226+ yield * Effect . addFinalizer ( ( ) =>
227+ Effect . sync ( ( ) => {
228+ me . mockRestore ( ) ;
229+ models . mockRestore ( ) ;
230+ } ) ,
231+ ) ;
232+ const instanceId = ProviderInstanceId . make ( "cursor-configured-key-with-login" ) ;
233+ const { store } = yield * makeCursorCredentialStore ( instanceId ) ;
234+ yield * Effect . promise ( ( ) =>
235+ store . save ( {
236+ version : 1 ,
237+ backendUrl : "https://api2.cursor.sh" ,
238+ apiKey : "stored-browser-key" ,
239+ createdAtMs : 0 ,
240+ apiKeyExpiresAtMs : 4_000_000_000_000 ,
241+ } ) ,
242+ ) ;
243+ const requests : string [ ] = [ ] ;
244+ const instance = yield * CursorDriver . create ( {
245+ instanceId,
246+ displayName : "Configured Cursor" ,
247+ enabled : true ,
248+ environment : [ { name : "CURSOR_API_KEY" , value : "configured-key" , sensitive : true } ] ,
249+ config : CursorDriver . defaultConfig ( ) ,
250+ } ) . pipe (
251+ Effect . provideService (
252+ HttpClient . HttpClient ,
253+ HttpClient . make ( ( request ) => {
254+ requests . push ( request . url ) ;
255+ return Effect . die ( "must not exchange credentials or request usage" ) ;
256+ } ) ,
257+ ) ,
258+ ) ;
259+ expect ( ( yield * instance . snapshot . refresh ) . usageLimits ?. unavailable ?. reason ) . toBe (
260+ "unsupported" ,
261+ ) ;
262+ expect ( requests ) . toEqual ( [ ] ) ;
263+ expect ( me ) . toHaveBeenCalledWith ( { apiKey : "configured-key" } ) ;
264+ } ) . pipe ( Effect . scoped ) ,
265+ ) ;
266+
160267 it . effect ( "keeps the bundled SDK manual-only without probing or updating cursor-agent" , ( ) =>
161268 Effect . gen ( function * ( ) {
162269 const instance = yield * CursorDriver . create ( {
0 commit comments