@@ -35,6 +35,8 @@ import {
3535} from "../Drivers/CursorSkills.ts" ;
3636import { execScriptSource , writeFakeCli } from "../../testUtils/fakeCli.ts" ;
3737import { HostProcessPlatform } from "@t3tools/shared/hostProcess" ;
38+ import { HttpClient , HttpClientResponse } from "effect/unstable/http" ;
39+ import { cursorUsageResponseToLimits , readCursorUsageLimits } from "./cursorUsageLimits.ts" ;
3840
3941const runNode = < A , E > (
4042 effect : Effect . Effect <
@@ -952,3 +954,171 @@ describe("resolveCursorAcpConfigUpdates", () => {
952954 ] ) ;
953955 } ) ;
954956} ) ;
957+
958+ describe ( "Cursor usage limits" , ( ) => {
959+ const checkedAt = "2026-09-16T00:00:00.000Z" ;
960+
961+ it ( "uses the advertised percentages and billing-cycle reset" , ( ) => {
962+ const limits = cursorUsageResponseToLimits (
963+ {
964+ billingCycleEnd : "1789876386000" ,
965+ planUsage : { totalPercentUsed : 72.4 , autoPercentUsed : 69.5 , apiPercentUsed : 100 } ,
966+ } ,
967+ checkedAt ,
968+ ) ;
969+ expect ( limits . windows ) . toEqual (
970+ expect . arrayContaining ( [
971+ {
972+ id : "totalPercentUsed" ,
973+ kind : "monthly" ,
974+ label : "Monthly" ,
975+ usedPercent : 72.4 ,
976+ resetsAt : "2026-09-20T03:53:06.000Z" ,
977+ } ,
978+ {
979+ id : "autoPercentUsed" ,
980+ kind : "monthly" ,
981+ label : "Monthly · Auto" ,
982+ usedPercent : 69.5 ,
983+ resetsAt : "2026-09-20T03:53:06.000Z" ,
984+ } ,
985+ {
986+ id : "apiPercentUsed" ,
987+ kind : "monthly" ,
988+ label : "Monthly · API" ,
989+ usedPercent : 100 ,
990+ resetsAt : "2026-09-20T03:53:06.000Z" ,
991+ } ,
992+ ] ) ,
993+ ) ;
994+ } ) ;
995+
996+ it ( "does not invent unused allowance for absent buckets" , ( ) => {
997+ expect ( cursorUsageResponseToLimits ( { planUsage : { } } , checkedAt ) . unavailable ?. reason ) . toBe (
998+ "unsupported" ,
999+ ) ;
1000+ expect (
1001+ cursorUsageResponseToLimits ( { planUsage : { totalPercentUsed : 0 } } , checkedAt ) . windows ,
1002+ ) . toEqual ( [ { id : "totalPercentUsed" , kind : "monthly" , label : "Monthly" , usedPercent : 0 } ] ) ;
1003+ expect (
1004+ cursorUsageResponseToLimits ( { planUsage : { totalPercentUsed : 150 } } , checkedAt ) . windows ,
1005+ ) . toEqual ( [ { id : "totalPercentUsed" , kind : "monthly" , label : "Monthly" , usedPercent : 100 } ] ) ;
1006+ } ) ;
1007+
1008+ it ( "reads the instance's credentials and endpoint even when usage enabled is false" , async ( ) => {
1009+ await runNode (
1010+ Effect . gen ( function * ( ) {
1011+ const fs = yield * FileSystem . FileSystem ;
1012+ const path = yield * Path . Path ;
1013+ const directory = yield * fs . makeTempDirectoryScoped ( ) ;
1014+ yield * fs . makeDirectory ( path . join ( directory , "cursor" ) ) ;
1015+ yield * fs . writeFileString (
1016+ path . join ( directory , "cursor" , "auth.json" ) ,
1017+ '{"accessToken":"instance-token"}' ,
1018+ ) ;
1019+ const client = HttpClient . make ( ( request ) => {
1020+ expect ( request . url ) . toBe (
1021+ "https://cursor.example/aiserver.v1.DashboardService/GetCurrentPeriodUsage" ,
1022+ ) ;
1023+ expect ( request . method ) . toBe ( "POST" ) ;
1024+ expect ( request . headers . authorization ) . toBe ( "Bearer instance-token" ) ;
1025+ expect ( request . headers [ "connect-protocol-version" ] ) . toBe ( "1" ) ;
1026+ return Effect . succeed (
1027+ HttpClientResponse . fromWeb (
1028+ request ,
1029+ Response . json ( { enabled : false , planUsage : { totalPercentUsed : 42 } } ) ,
1030+ ) ,
1031+ ) ;
1032+ } ) ;
1033+ yield * fs . makeDirectory ( path . join ( directory , ".cursor" ) ) ;
1034+ yield * fs . writeFileString (
1035+ path . join ( directory , ".cursor" , "auth.json" ) ,
1036+ '{"accessToken":"instance-token"}' ,
1037+ ) ;
1038+ for ( const platform of [ "linux" , "darwin" ] as const ) {
1039+ const limits = yield * readCursorUsageLimits (
1040+ { apiEndpoint : "https://cursor.example/" } ,
1041+ { XDG_CONFIG_HOME : directory , HOME : directory , AGENT_CLI_CREDENTIAL_STORE : "file" } ,
1042+ ) . pipe (
1043+ Effect . provideService ( HostProcessPlatform , platform ) ,
1044+ Effect . provideService ( HttpClient . HttpClient , client ) ,
1045+ ) ;
1046+ expect ( limits . windows [ 0 ] ?. usedPercent ) . toBe ( 42 ) ;
1047+ }
1048+ } ) . pipe ( Effect . scoped ) ,
1049+ ) ;
1050+ } ) ;
1051+
1052+ it ( "never reads stale files for keychain or memory logins, but accepts an explicit auth token" , async ( ) => {
1053+ for ( const platform of [ "linux" , "darwin" ] as const ) {
1054+ for ( const token of [ undefined , "explicit-token" ] ) {
1055+ const limits = await runNode (
1056+ readCursorUsageLimits (
1057+ { apiEndpoint : "" } ,
1058+ {
1059+ AGENT_CLI_CREDENTIAL_STORE : platform === "linux" ? "memory" : "default" ,
1060+ ...( token ? { CURSOR_AUTH_TOKEN : token } : { } ) ,
1061+ } ,
1062+ ) . pipe (
1063+ Effect . provideService ( HostProcessPlatform , platform ) ,
1064+ Effect . provideService (
1065+ FileSystem . FileSystem ,
1066+ FileSystem . makeNoop ( {
1067+ readFileString : ( ) => Effect . die ( "must not read an unrelated credential file" ) ,
1068+ } ) ,
1069+ ) ,
1070+ Effect . provideService (
1071+ HttpClient . HttpClient ,
1072+ HttpClient . make ( ( request ) => {
1073+ expect ( token ) . toBe ( "explicit-token" ) ;
1074+ expect ( request . headers . authorization ) . toBe ( "Bearer explicit-token" ) ;
1075+ return Effect . succeed (
1076+ HttpClientResponse . fromWeb (
1077+ request ,
1078+ Response . json ( { planUsage : { totalPercentUsed : 10 } } ) ,
1079+ ) ,
1080+ ) ;
1081+ } ) ,
1082+ ) ,
1083+ ) ,
1084+ ) ;
1085+ if ( token ) expect ( limits . windows [ 0 ] ?. usedPercent ) . toBe ( 10 ) ;
1086+ else expect ( limits . unavailable ?. reason ) . toBe ( "unsupported" ) ;
1087+ }
1088+ }
1089+ } ) ;
1090+
1091+ it ( "reports failed requests without exposing credentials or response bodies" , async ( ) => {
1092+ const limits = await runNode (
1093+ readCursorUsageLimits ( { apiEndpoint : "" } , { CURSOR_AUTH_TOKEN : "private-token" } ) . pipe (
1094+ Effect . provideService (
1095+ HttpClient . HttpClient ,
1096+ HttpClient . make ( ( request ) =>
1097+ Effect . succeed (
1098+ HttpClientResponse . fromWeb (
1099+ request ,
1100+ new Response ( "private response" , { status : 401 } ) ,
1101+ ) ,
1102+ ) ,
1103+ ) ,
1104+ ) ,
1105+ ) ,
1106+ ) ;
1107+ expect ( limits . unavailable ) . toEqual ( {
1108+ reason : "probeFailed" ,
1109+ message : "Cursor could not read usage limits." ,
1110+ } ) ;
1111+ } ) ;
1112+
1113+ it ( "does not use a stored login for an explicit API key" , async ( ) => {
1114+ const limits = await runNode (
1115+ readCursorUsageLimits ( { apiEndpoint : "" } , { CURSOR_API_KEY : "different-account" } ) . pipe (
1116+ Effect . provideService (
1117+ HttpClient . HttpClient ,
1118+ HttpClient . make ( ( ) => Effect . die ( "must not request usage" ) ) ,
1119+ ) ,
1120+ ) ,
1121+ ) ;
1122+ expect ( limits . unavailable ?. reason ) . toBe ( "unsupported" ) ;
1123+ } ) ;
1124+ } ) ;
0 commit comments