88 "net/http/httptest"
99 "strings"
1010 "testing"
11+ "time"
1112
1213 "github.com/gcssloop/codex-router/backend/internal/accountdrv"
1314 "github.com/gcssloop/codex-router/backend/internal/accounts"
@@ -33,7 +34,7 @@ func TestOpenAIOfficialDriverFetchParsesUsageLimits(t *testing.T) {
3334 driver := builtin .NewOpenAIOfficialDriver (http .DefaultClient )
3435 result , err := driver .Fetch (context .Background (), accounts.Account {
3536 ProviderType : accounts .ProviderOpenAIOfficial ,
36- BaseURL : server .URL ,
37+ BaseURL : server .URL + "/backend-api/codex" ,
3738 }, accountdrv.ResolvedCredential {
3839 AccessToken : "at-token" ,
3940 Metadata : map [string ]any {"account_id" : "acct-1" },
@@ -67,6 +68,96 @@ func TestOpenAIOfficialDriverFetchParsesUsageLimits(t *testing.T) {
6768 }
6869}
6970
71+ func TestOpenAIOfficialDriverFetchParsesCurrentCodexRateLimits (t * testing.T ) {
72+ t .Parallel ()
73+
74+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
75+ if r .URL .Path != "/backend-api/wham/usage" {
76+ http .NotFound (w , r )
77+ return
78+ }
79+ w .Header ().Set ("Content-Type" , "application/json" )
80+ _ , _ = w .Write ([]byte (`{
81+ "plan_type":"pro",
82+ "rate_limit":{
83+ "allowed":true,
84+ "limit_reached":false,
85+ "primary_window":{
86+ "used_percent":52,
87+ "limit_window_seconds":18000,
88+ "reset_after_seconds":3600,
89+ "reset_at":1767205800
90+ },
91+ "secondary_window":{
92+ "used_percent":51,
93+ "limit_window_seconds":604800,
94+ "reset_after_seconds":86400,
95+ "reset_at":1783350300
96+ }
97+ },
98+ "credits":{"has_credits":true,"unlimited":false,"balance":"0"},
99+ "additional_rate_limits":[
100+ {
101+ "limit_name":"codex_other",
102+ "metered_feature":"codex_other",
103+ "rate_limit":{
104+ "allowed":true,
105+ "limit_reached":false,
106+ "primary_window":{
107+ "used_percent":88,
108+ "limit_window_seconds":1800,
109+ "reset_after_seconds":600,
110+ "reset_at":1735693200
111+ }
112+ }
113+ }
114+ ],
115+ "rate_limit_reached_type":{"type":"workspace_member_usage_limit_reached"},
116+ "rate_limit_reset_credits":{"available_count":1}
117+ }` ))
118+ }))
119+ defer server .Close ()
120+
121+ driver := builtin .NewOpenAIOfficialDriver (http .DefaultClient )
122+ result , err := driver .Fetch (context .Background (), accounts.Account {
123+ ProviderType : accounts .ProviderOpenAIOfficial ,
124+ BaseURL : server .URL + "/backend-api/codex" ,
125+ }, accountdrv.ResolvedCredential {
126+ AccessToken : "at-token" ,
127+ Metadata : map [string ]any {"account_id" : "acct-1" },
128+ })
129+ if err != nil {
130+ t .Fatalf ("Fetch returned error: %v" , err )
131+ }
132+ if result .Limits .PrimaryUsedPercent == nil || * result .Limits .PrimaryUsedPercent != 52 {
133+ t .Fatalf ("PrimaryUsedPercent = %#v, want 52" , result .Limits .PrimaryUsedPercent )
134+ }
135+ if result .Limits .SecondaryUsedPercent == nil || * result .Limits .SecondaryUsedPercent != 51 {
136+ t .Fatalf ("SecondaryUsedPercent = %#v, want 51" , result .Limits .SecondaryUsedPercent )
137+ }
138+ if result .Limits .RPMRemaining == nil || * result .Limits .RPMRemaining != 48 {
139+ t .Fatalf ("RPMRemaining = %#v, want 48" , result .Limits .RPMRemaining )
140+ }
141+ if result .Limits .TPMRemaining == nil || * result .Limits .TPMRemaining != 49 {
142+ t .Fatalf ("TPMRemaining = %#v, want 49" , result .Limits .TPMRemaining )
143+ }
144+ wantPrimaryReset := time .Unix (1767205800 , 0 ).UTC ()
145+ if result .Limits .PrimaryResetsAt == nil || ! result .Limits .PrimaryResetsAt .Equal (wantPrimaryReset ) {
146+ t .Fatalf ("PrimaryResetsAt = %#v, want %s" , result .Limits .PrimaryResetsAt , wantPrimaryReset )
147+ }
148+ wantSecondaryReset := time .Unix (1783350300 , 0 ).UTC ()
149+ if result .Limits .SecondaryResetsAt == nil || ! result .Limits .SecondaryResetsAt .Equal (wantSecondaryReset ) {
150+ t .Fatalf ("SecondaryResetsAt = %#v, want %s" , result .Limits .SecondaryResetsAt , wantSecondaryReset )
151+ }
152+ if got , ok := result .Meta ["plan_type" ].(string ); ! ok || got != "pro" {
153+ t .Fatalf ("meta.plan_type = %#v, want pro" , result .Meta ["plan_type" ])
154+ }
155+ reachedType , ok := result .Meta ["rate_limit_reached_type" ].(map [string ]any )
156+ if ! ok || reachedType ["type" ] != "workspace_member_usage_limit_reached" {
157+ t .Fatalf ("meta.rate_limit_reached_type = %#v, want workspace_member_usage_limit_reached" , result .Meta ["rate_limit_reached_type" ])
158+ }
159+ }
160+
70161func TestOpenAIOfficialDriverFetchClassifiesErrors (t * testing.T ) {
71162 t .Parallel ()
72163
0 commit comments