@@ -51,6 +51,8 @@ function fixture(
5151 options : {
5252 accounts ?: Array < ( typeof accounts ) [ number ] & { disabled ?: boolean } > ;
5353 upstream ?: ( request : RequestBody ) => { status : number ; body : unknown } ;
54+ /** Upstream calls it matches never get an answer from the hub. */
55+ stalls ?: ( request : RequestBody ) => boolean ;
5456 cooldownStatus ?: number ;
5557 } = { } ,
5658) {
@@ -76,6 +78,7 @@ function fixture(
7678 ) ;
7779 expect ( path ) . toMatch ( / \/ v 0 \/ m a n a g e m e n t \/ a p i - c a l l $ / ) ;
7880 expect ( body ?. header ?. Authorization ) . toBe ( "Bearer $TOKEN$" ) ;
81+ if ( options . stalls ?.( body ! ) ) return undefined ;
7982 const upstream = options . upstream ?.( body ! ) ?? {
8083 status : 200 ,
8184 body : body ?. url ?. endsWith ( "/consume" )
@@ -104,7 +107,12 @@ function fixture(
104107 request ,
105108 Response . json ( { status_code : upstream . status , body : encodeJson ( upstream . body ) } ) ,
106109 ) ;
107- } ) ,
110+ } ) . pipe (
111+ Effect . filterOrElse (
112+ ( response ) => response !== undefined ,
113+ ( ) => Effect . never ,
114+ ) ,
115+ ) ,
108116 ) ;
109117 return {
110118 requests,
@@ -334,50 +342,48 @@ describe("CLIProxyAPI built-in management API", () => {
334342 } ) ,
335343 ) ;
336344
337- it . effect ( "labels Claude accounts with the plan tier from their profile " , ( ) =>
345+ it . effect ( "labels a hub Claude account with its plan tier" , ( ) =>
338346 Effect . gen ( function * ( ) {
339- const organizations : Record < string , { organization_type : string ; rate_limit_tier : string } > =
340- {
341- a : { organization_type : "claude_max" , rate_limit_tier : "default_claude_max_5x" } ,
342- b : { organization_type : "claude_max" , rate_limit_tier : "default_claude_max_20x" } ,
343- c : { organization_type : "claude_pro" , rate_limit_tier : "default_claude_ai" } ,
344- } ;
345347 const test = fixture ( {
346- accounts : [
347- ...accounts ,
348- { ...
accounts [ 0 ] ! , id :
"third.json" , auth_index :
"c" , email :
"[email protected] " } , 349- ] . map ( ( account ) => ( { ...account , provider : "claude" } ) ) ,
348+ accounts : [ { ...accounts [ 0 ] ! , provider : "claude" } ] ,
350349 upstream : ( request ) => ( {
351350 status : 200 ,
352351 body : request . url ?. endsWith ( "/profile" )
353- ? { organization : organizations [ request . auth_index ] }
352+ ? {
353+ organization : {
354+ organization_type : "claude_max" ,
355+ rate_limit_tier : "default_claude_max_20x" ,
356+ } ,
357+ }
354358 : { seven_day : { utilization : 50 , resets_at : null } } ,
355359 } ) ,
356360 } ) ;
357361 const api = yield * test . api ;
358- const result = yield * api . readAccounts ( config ) ;
359- expect ( result . map ( ( account ) => account . plan ) ) . toEqual ( [
360- "Claude Max 5x Subscription" ,
361- "Claude Max 20x Subscription" ,
362- "Claude Pro Subscription" ,
363- ] ) ;
362+ const [ account ] = yield * api . readAccounts ( config ) ;
363+ expect ( account ?. plan ) . toBe ( "Claude Max 20x Subscription" ) ;
364364 } ) ,
365365 ) ;
366366
367- it . effect ( "keeps Claude usage when the profile cannot be read " , ( ) =>
367+ it . effect ( "does not hold Claude usage until the hub times out a stalled profile " , ( ) =>
368368 Effect . gen ( function * ( ) {
369369 const test = fixture ( {
370370 accounts : [ { ...accounts [ 0 ] ! , provider : "claude" } ] ,
371- upstream : ( request ) =>
372- request . url ?. endsWith ( "/profile" )
373- ? { status : 503 , body : { token : "do-not-publish" } }
374- : { status : 200 , body : { seven_day : { utilization : 50 , resets_at : null } } } ,
371+ upstream : ( ) => ( {
372+ status : 200 ,
373+ body : { seven_day : { utilization : 50 , resets_at : null } } ,
374+ } ) ,
375+ stalls : ( request ) => request . url ?. endsWith ( "/profile" ) ?? false ,
375376 } ) ;
376377 const api = yield * test . api ;
377- const [ account ] = yield * api . readAccounts ( config ) ;
378+ const pending = yield * api
379+ . readAccounts ( config )
380+ . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
381+ yield * TestClock . adjust ( "14 seconds" ) ;
382+ const exit = pending . pollUnsafe ( ) ;
383+ expect ( exit ?. _tag ) . toBe ( "Success" ) ;
384+ const [ account ] = yield * Fiber . join ( pending ) ;
378385 expect ( account ?. plan ) . toBe ( "Claude Subscription" ) ;
379386 expect ( account ?. usageLimits . windows . map ( ( window ) => window . usedPercent ) ) . toEqual ( [ 50 ] ) ;
380- expect ( encodeJson ( account ) ) . not . toContain ( "do-not-publish" ) ;
381387 } ) ,
382388 ) ;
383389
0 commit comments