@@ -20,6 +20,7 @@ import * as Layer from "effect/Layer";
2020import * as Option from "effect/Option" ;
2121import * as Stream from "effect/Stream" ;
2222import { vi } from "vite-plus/test" ;
23+ import * as HttpClientResponse from "effect/http/HttpClientResponse" ;
2324import * as HttpClient from "effect/http/HttpClient" ;
2425
2526import {
@@ -29,6 +30,7 @@ import {
2930import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers" ;
3031import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost" ;
3132import { CursorDriver } from "./driver.ts" ;
33+ import { makeCursorCredentialStore } from "./credentialStore.ts" ;
3234import * as CursorAgentSdk from "./CursorAgentSdk.ts" ;
3335import * as CursorSdk from "./CursorSdk.ts" ;
3436import * as CursorKeychain from "./CursorKeychain.ts" ;
@@ -107,7 +109,30 @@ it.layer(layerTest)("CursorDriver", (it) => {
107109 } ;
108110 const openedKeys : Array < string | undefined > = [ ] ;
109111 let closed = 0 ;
112+ const requests : string [ ] = [ ] ;
113+ let failureUrl : string | undefined ;
114+ let failureStatus = 500 ;
115+ const usageClient = HttpClient . make ( ( request ) => {
116+ requests . push ( request . url ) ;
117+ const exchange = request . url . endsWith ( "/auth/exchange_user_api_key" ) ;
118+ expect ( request . headers . authorization ) . toBe (
119+ exchange ? "Bearer instance-browser-key" : "Bearer browser-access-token" ,
120+ ) ;
121+ return Effect . succeed (
122+ HttpClientResponse . fromWeb (
123+ request ,
124+ request . url === failureUrl
125+ ? new Response ( null , { status : failureStatus } )
126+ : Response . json (
127+ exchange
128+ ? { accessToken : "browser-access-token" }
129+ : { planUsage : { totalPercentUsed : 23 } } ,
130+ ) ,
131+ ) ,
132+ ) ;
133+ } ) ;
110134 const instance = yield * CursorDriver . create ( input ) . pipe (
135+ Effect . provideService ( HttpClient . HttpClient , usageClient ) ,
111136 Effect . provideService ( CursorAgentSdk . CursorAgentSdkRunner , {
112137 assertComplete : Effect . void ,
113138 open : ( request ) =>
@@ -134,6 +159,7 @@ it.layer(layerTest)("CursorDriver", (it) => {
134159 expect ( me ) . toHaveBeenCalledWith ( { apiKey : "instance-browser-key" } ) ;
135160 expect ( yield * instance . snapshot . getSnapshot ) . toMatchObject ( {
136161 status : "ready" ,
162+ usageLimits : { windows : [ { id : "totalPercentUsed" , usedPercent : 23 } ] } ,
137163 auth : {
138164 status : "authenticated" ,
139165 type : "browser" ,
@@ -158,8 +184,43 @@ it.layer(layerTest)("CursorDriver", (it) => {
158184 yield * runtime . ensureThread ( { threadId, modelSelection, runtimePolicy } ) ;
159185 expect ( openedKeys ) . toEqual ( [ "instance-browser-key" ] ) ;
160186 expect ( closed ) . toBe ( 0 ) ;
161- const recreated = yield * CursorDriver . create ( input ) ;
187+ expect ( requests . slice ( 0 , 2 ) ) . toEqual ( [
188+ "https://api2.cursor.sh/auth/exchange_user_api_key" ,
189+ "https://api2.cursor.sh/aiserver.v1.DashboardService/GetCurrentPeriodUsage" ,
190+ ] ) ;
191+ const recreated = yield * CursorDriver . create ( input ) . pipe (
192+ Effect . provideService ( HttpClient . HttpClient , usageClient ) ,
193+ ) ;
162194 expect ( ( yield * recreated . snapshot . refresh ) . auth . status ) . toBe ( "authenticated" ) ;
195+ for ( const url of requests . slice ( 0 , 2 ) ) {
196+ failureUrl = url ;
197+ const snapshot = yield * instance . snapshot . refresh ;
198+ expect ( snapshot ) . toMatchObject ( {
199+ status : "ready" ,
200+ auth : { status : "authenticated" } ,
201+ usageLimits : { windows : [ { id : "totalPercentUsed" , usedPercent : 23 } ] } ,
202+ } ) ;
203+ expect ( snapshot . message ) . toBeUndefined ( ) ;
204+ }
205+ failureUrl = requests [ 0 ] ;
206+ failureStatus = 401 ;
207+ expect ( yield * instance . snapshot . refresh ) . toMatchObject ( {
208+ status : "ready" ,
209+ auth : { status : "authenticated" } ,
210+ usageLimits : { windows : [ ] , unavailable : { reason : "unsupported" } } ,
211+ } ) ;
212+ failureUrl = undefined ;
213+ expect ( ( yield * instance . snapshot . refresh ) . usageLimits ?. windows [ 0 ] ?. usedPercent ) . toBe ( 23 ) ;
214+ const { store } = yield * makeCursorCredentialStore ( input . instanceId ) ;
215+ yield * Effect . promise ( async ( ) => {
216+ const credentials = await store . load ( ) ;
217+ await store . save ( { ...credentials ! , apiKeyExpiresAtMs : 0 } ) ;
218+ } ) ;
219+ const requestCount = requests . length ;
220+ const expired = yield * instance . snapshot . refresh ;
221+ expect ( expired . auth . status ) . toBe ( "unauthenticated" ) ;
222+ expect ( expired . usageLimits ) . toBeUndefined ( ) ;
223+ expect ( requests ) . toHaveLength ( requestCount ) ;
163224 yield * instance . auth ! . logout ( Effect . void ) ;
164225 expect ( closed ) . toBe ( 1 ) ;
165226 expect ( ( yield * instance . snapshot . getSnapshot ) . auth . status ) . toBe ( "unauthenticated" ) ;
@@ -173,6 +234,56 @@ it.layer(layerTest)("CursorDriver", (it) => {
173234 } ) . pipe ( Effect . scoped ) ,
174235 ) ;
175236
237+ it . effect (
238+ "keeps a configured CURSOR_API_KEY instance off its stored browser login for limits" ,
239+ ( ) =>
240+ Effect . gen ( function * ( ) {
241+ const me = vi . spyOn ( Cursor , "me" ) . mockResolvedValue ( {
242+ apiKeyName : "Configured key" ,
243+ createdAt : "2026-01-01T00:00:00.000Z" ,
244+ } ) ;
245+ const models = vi . spyOn ( Cursor . models , "list" ) . mockResolvedValue ( [ ] ) ;
246+ yield * Effect . addFinalizer ( ( ) =>
247+ Effect . sync ( ( ) => {
248+ me . mockRestore ( ) ;
249+ models . mockRestore ( ) ;
250+ } ) ,
251+ ) ;
252+ const instanceId = ProviderInstanceId . make ( "cursor-configured-key-with-login" ) ;
253+ const { store } = yield * makeCursorCredentialStore ( instanceId ) ;
254+ yield * Effect . promise ( ( ) =>
255+ store . save ( {
256+ version : 1 ,
257+ backendUrl : "https://api2.cursor.sh" ,
258+ apiKey : "stored-browser-key" ,
259+ createdAtMs : 0 ,
260+ apiKeyExpiresAtMs : 4_000_000_000_000 ,
261+ } ) ,
262+ ) ;
263+ const requests : string [ ] = [ ] ;
264+ const instance = yield * CursorDriver . create ( {
265+ instanceId,
266+ displayName : "Configured Cursor" ,
267+ enabled : true ,
268+ environment : [ { name : "CURSOR_API_KEY" , value : "configured-key" , sensitive : true } ] ,
269+ config : CursorDriver . defaultConfig ( ) ,
270+ } ) . pipe (
271+ Effect . provideService (
272+ HttpClient . HttpClient ,
273+ HttpClient . make ( ( request ) => {
274+ requests . push ( request . url ) ;
275+ return Effect . die ( "must not exchange credentials or request usage" ) ;
276+ } ) ,
277+ ) ,
278+ ) ;
279+ expect ( ( yield * instance . snapshot . refresh ) . usageLimits ?. unavailable ?. reason ) . toBe (
280+ "unsupported" ,
281+ ) ;
282+ expect ( requests ) . toEqual ( [ ] ) ;
283+ expect ( me ) . toHaveBeenCalledWith ( { apiKey : "configured-key" } ) ;
284+ } ) . pipe ( Effect . scoped ) ,
285+ ) ;
286+
176287 it . effect ( "budgets handoffs for the SDK model across catalog refreshes and option changes" , ( ) =>
177288 Effect . gen ( function * ( ) {
178289 const fileSystem = yield * FileSystem . FileSystem ;
0 commit comments