@@ -1183,6 +1183,56 @@ describe("codex-auth API", () => {
11831183 expect ( getAccountQuota ( "preserve-valid" ) ) . toEqual ( before ) ;
11841184 } ) ;
11851185
1186+ test ( "quota cache rebuilds preserve the short-window tuple" , ( ) => {
1187+ setAccountQuotaFromParsed ( "short-cache" , {
1188+ weeklyPercent : 1 ,
1189+ weeklyResetAt : 2_000_586_800 ,
1190+ monthlyPercent : 3 ,
1191+ monthlyResetAt : 2_002_592_000 ,
1192+ shortPercent : 0 ,
1193+ shortResetAt : 2_000_000_000 ,
1194+ shortWindowSeconds : 18_000 ,
1195+ } ) ;
1196+ expect ( getAccountQuota ( "short-cache" ) ) . toMatchObject ( {
1197+ weeklyPercent : 1 ,
1198+ shortPercent : 0 ,
1199+ shortResetAt : 2_000_000_000 ,
1200+ shortWindowSeconds : 18_000 ,
1201+ } ) ;
1202+
1203+ setAccountQuotaFromParsed ( "short-cache" , {
1204+ shortPercent : 4 ,
1205+ shortResetAt : 2_000_000_100 ,
1206+ shortWindowSeconds : 18_000 ,
1207+ } ) ;
1208+ expect ( getAccountQuota ( "short-cache" ) ) . toMatchObject ( {
1209+ weeklyPercent : 1 ,
1210+ monthlyPercent : 3 ,
1211+ shortPercent : 4 ,
1212+ shortResetAt : 2_000_000_100 ,
1213+ shortWindowSeconds : 18_000 ,
1214+ } ) ;
1215+
1216+ updateAccountQuota ( "short-cache" , 2 , 2_000_586_900 ) ;
1217+ expect ( getAccountQuota ( "short-cache" ) ) . toMatchObject ( {
1218+ weeklyPercent : 2 ,
1219+ monthlyPercent : 3 ,
1220+ shortPercent : 4 ,
1221+ shortResetAt : 2_000_000_100 ,
1222+ shortWindowSeconds : 18_000 ,
1223+ } ) ;
1224+
1225+ setAccountQuotaFromParsed ( "short-cache" , { resetCredits : 3 } ) ;
1226+ expect ( getAccountQuota ( "short-cache" ) ) . toMatchObject ( {
1227+ weeklyPercent : 2 ,
1228+ monthlyPercent : 3 ,
1229+ shortPercent : 4 ,
1230+ shortResetAt : 2_000_000_100 ,
1231+ shortWindowSeconds : 18_000 ,
1232+ resetCredits : 3 ,
1233+ } ) ;
1234+ } ) ;
1235+
11861236 test ( "GET /api/codex-auth/quota returns stored quotas" , async ( ) => {
11871237 updateAccountQuota ( "q-test" , 30 ) ;
11881238 const req = new Request ( "http://localhost/api/codex-auth/quota" , { method : "GET" } ) ;
@@ -1230,6 +1280,47 @@ describe("codex-auth API", () => {
12301280 }
12311281 } ) ;
12321282
1283+ test ( "GET /api/codex-auth/accounts preserves a parsed K12 short window through cache and DTO" , async ( ) => {
1284+ const config = makeConfig ( ) ;
1285+ seedPoolAccount ( config , {
1286+ id : "pool-k12-short" ,
1287+ 1288+ plan : "k12" ,
1289+ accessToken : "tok" ,
1290+ refreshToken : "ref" ,
1291+ chatgptAccountId : "acc-pool-k12-short" ,
1292+ } ) ;
1293+
1294+ const originalFetch = globalThis . fetch ;
1295+ globalThis . fetch = ( async ( ) => Response . json ( {
1296+ plan_type : "k12" ,
1297+ rate_limit : {
1298+ primary_window : { used_percent : 0 , reset_at : 2_000_000_000 , limit_window_seconds : 18_000 } ,
1299+ secondary_window : { used_percent : 1 , reset_at : 2_000_586_800 , limit_window_seconds : 604_800 } ,
1300+ } ,
1301+ } ) ) as typeof fetch ;
1302+
1303+ try {
1304+ const req = new Request ( "http://localhost/api/codex-auth/accounts?refresh=1" , { method : "GET" } ) ;
1305+ const resp = await handleCodexAuthAPI ( req , new URL ( req . url ) , config ) ;
1306+ expect ( resp ! . status ) . toBe ( 200 ) ;
1307+ const data = await resp ! . json ( ) as {
1308+ accounts : Array < { id : string ; quota ?: Record < string , unknown > } > ;
1309+ } ;
1310+ const quota = data . accounts . find ( account => account . id === "pool-k12-short" ) ?. quota ;
1311+ expect ( quota ) . toMatchObject ( {
1312+ weeklyPercent : 1 ,
1313+ weeklyResetAt : 2_000_586_800 ,
1314+ shortPercent : 0 ,
1315+ shortResetAt : 2_000_000_000 ,
1316+ shortWindowSeconds : 18_000 ,
1317+ } ) ;
1318+ expect ( getAccountQuota ( "pool-k12-short" ) ) . toMatchObject ( quota ! ) ;
1319+ } finally {
1320+ globalThis . fetch = originalFetch ;
1321+ }
1322+ } ) ;
1323+
12331324 test ( "GET /api/codex-auth/accounts refresh=1 bypasses cached pool quota" , async ( ) => {
12341325 const config = makeConfig ( ) ;
12351326 seedPoolAccount ( config , {
0 commit comments