@@ -4,7 +4,7 @@ import { ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts"
44import * as Effect from "effect/Effect" ;
55import * as Layer from "effect/Layer" ;
66import * as Schema from "effect/Schema" ;
7- import { HttpClient } from "effect/http" ;
7+ import { HttpClient , HttpClientResponse } from "effect/http" ;
88
99import * as BackgroundPolicy from "../background/BackgroundPolicy.ts" ;
1010import * as ServerSettings from "../serverSettings.ts" ;
@@ -83,3 +83,127 @@ it.effect("keeps malformed source URLs encodable for config subscribers", () =>
8383 Effect . scoped ,
8484 ) ,
8585) ;
86+
87+ it . effect . each ( [
88+ { scenario : "multiple windows" , expected : "2099-01-01T02:00:00.000Z" } ,
89+ { scenario : "model scopes" , expected : "2099-01-01T04:00:00.000Z" } ,
90+ { scenario : "disabled account" , expected : "2099-01-01T03:00:00.000Z" } ,
91+ { scenario : "model list unavailable" , expected : "2099-01-01T03:00:00.000Z" } ,
92+ { scenario : "missing resets" , expected : null } ,
93+ { scenario : "hub unavailable" , expected : null } ,
94+ { scenario : "disabled source" , expected : null } ,
95+ ] ) ( "resolves only the matching hub and model: $scenario" , ( { scenario, expected } ) =>
96+ Effect . gen ( function * ( ) {
97+ const sources = yield * UsageLimitSources . make ;
98+ const unrelated = yield * sources . resolveResetAt ( {
99+ endpoint : "https://other.test" ,
100+ model : "claude-opus-4-6" ,
101+ } ) ;
102+ expect ( unrelated ) . toBeNull ( ) ;
103+ const reset = yield * sources . resolveResetAt ( {
104+ endpoint : "https://hub.test/v1" ,
105+ model : "claude-opus-4-6" ,
106+ } ) ;
107+ expect ( reset ) . toBe ( expected ) ;
108+ const unlisted = yield * sources . resolveResetAt ( {
109+ endpoint : "https://hub.test/v1" ,
110+ model : "unlisted" ,
111+ } ) ;
112+ expect ( unlisted ) . toBeNull ( ) ;
113+ } ) . pipe (
114+ Effect . provide ( [
115+ ServerSettings . layerTest ( {
116+ usageLimitSources : {
117+ [ UsageLimitSourceId . make ( "hub" ) ] : {
118+ kind : "cliproxy" ,
119+ url : "https://hub.test" ,
120+ managementKey : "test" ,
121+ enabled : scenario !== "disabled source" ,
122+ } ,
123+ } ,
124+ } ) ,
125+ Layer . mock ( BackgroundPolicy . BackgroundPolicy ) ( {
126+ shouldRunScopeWork : ( ) => Effect . succeed ( false ) ,
127+ } ) ,
128+ Layer . succeed (
129+ HttpClient . HttpClient ,
130+ HttpClient . make ( ( request ) =>
131+ Effect . sync ( ( ) => {
132+ const url = new URL ( request . url ) ;
133+ if (
134+ scenario === "hub unavailable" ||
135+ ( scenario === "model list unavailable" && url . searchParams . get ( "name" ) === "fast" )
136+ ) {
137+ return HttpClientResponse . fromWeb ( request , new Response ( null , { status : 503 } ) ) ;
138+ }
139+ const files = [ "slow" , "fast" , "wrong-model" ] ;
140+ const body = url . pathname . endsWith ( "/auth-files" )
141+ ? {
142+ files : files . map ( ( id ) => ( {
143+ id,
144+ auth_index : id ,
145+ provider : "claude" ,
146+ disabled : scenario === "disabled account" && id === "fast" ,
147+ } ) ) ,
148+ }
149+ : url . pathname . endsWith ( "/auth-files/models" )
150+ ? {
151+ models : [
152+ {
153+ id :
154+ url . searchParams . get ( "name" ) === "wrong-model"
155+ ? "claude-sonnet-4-6"
156+ : "claude-opus-4-6" ,
157+ } ,
158+ ] ,
159+ }
160+ : ( ( ) => {
161+ const input = JSON . parse (
162+ request . body . _tag === "Uint8Array"
163+ ? new TextDecoder ( ) . decode ( request . body . body )
164+ : "{}" ,
165+ ) ;
166+ const hour =
167+ input . auth_index === "slow"
168+ ? "03"
169+ : input . auth_index === "fast"
170+ ? "02"
171+ : "01" ;
172+ return {
173+ status_code : 200 ,
174+ body : JSON . stringify ( {
175+ five_hour : {
176+ utilization : 100 ,
177+ resets_at : scenario === "missing resets" ? null : "2099-01-01T01:00:00Z" ,
178+ } ,
179+ seven_day : { utilization : 100 , resets_at : `2099-01-01T${ hour } :00:00Z` } ,
180+ ...( scenario === "model scopes"
181+ ? {
182+ limits : [
183+ {
184+ kind : "weekly_scoped" ,
185+ percent : 100 ,
186+ resets_at : "2099-01-01T04:00:00Z" ,
187+ scope : { model : { display_name : "Opus 4.6" } } ,
188+ } ,
189+ {
190+ kind : "weekly_scoped" ,
191+ percent : 100 ,
192+ resets_at : "2099-01-01T05:00:00Z" ,
193+ scope : { model : { display_name : "Sonnet" } } ,
194+ } ,
195+ ] ,
196+ }
197+ : { } ) ,
198+ } ) ,
199+ } ;
200+ } ) ( ) ;
201+ return HttpClientResponse . fromWeb ( request , Response . json ( body ) ) ;
202+ } ) ,
203+ ) ,
204+ ) ,
205+ NodeCrypto . layer ,
206+ ] ) ,
207+ Effect . scoped ,
208+ ) ,
209+ ) ;
0 commit comments