@@ -74,13 +74,115 @@ describe("scanOpenRouterModels", () => {
7474 if ( byPricing === undefined ) {
7575 throw new Error ( "Expected pricing-based model result." ) ;
7676 }
77+ expect ( byPricing . contextLength ) . toBe ( 16_384 ) ;
78+ expect ( byPricing . maxCompletionTokens ) . toBe ( 1024 ) ;
7779 expect ( byPricing . supportsToolsMeta ) . toBe ( true ) ;
7880 expect ( byPricing . supportedParametersCount ) . toBe ( 3 ) ;
7981 expect ( byPricing . isFree ) . toBe ( true ) ;
8082 expect ( byPricing . tool . skipped ) . toBe ( true ) ;
8183 expect ( byPricing . image . skipped ) . toBe ( true ) ;
8284 } ) ;
8385
86+ it ( "uses OpenRouter top-provider limits for scan metadata" , async ( ) => {
87+ const fetchImpl = createFetchFixture ( {
88+ data : [
89+ {
90+ id : "acme/provider-limited:free" ,
91+ name : "Provider Limited" ,
92+ context_length : 32_768 ,
93+ top_provider : {
94+ context_length : 16_384 ,
95+ max_completion_tokens : 4096 ,
96+ } ,
97+ supported_parameters : [ ] ,
98+ pricing : { prompt : "0" , completion : "0" } ,
99+ } ,
100+ ] ,
101+ } ) ;
102+
103+ const [ result ] = await scanOpenRouterModels ( {
104+ fetchImpl,
105+ probe : false ,
106+ } ) ;
107+
108+ expect ( result ?. contextLength ) . toBe ( 16_384 ) ;
109+ expect ( result ?. maxCompletionTokens ) . toBe ( 4096 ) ;
110+ } ) ;
111+
112+ it ( "falls back when top-provider limits are malformed" , async ( ) => {
113+ for ( const topProvider of [
114+ { context_length : 0 , max_completion_tokens : 0 } ,
115+ { context_length : - 1 , max_completion_tokens : - 1 } ,
116+ { context_length : 8192.5 , max_completion_tokens : 8192.5 } ,
117+ {
118+ context_length : Number . MAX_SAFE_INTEGER + 1 ,
119+ max_completion_tokens : Number . MAX_SAFE_INTEGER + 1 ,
120+ } ,
121+ { context_length : "8192" , max_completion_tokens : "1024" } ,
122+ { context_length : null , max_completion_tokens : null } ,
123+ ] ) {
124+ const fetchImpl = createFetchFixture ( {
125+ data : [
126+ {
127+ id : "acme/provider-limited:free" ,
128+ name : "Provider Limited" ,
129+ context_length : 32_768 ,
130+ max_completion_tokens : 4096 ,
131+ top_provider : topProvider ,
132+ supported_parameters : [ ] ,
133+ pricing : { prompt : "0" , completion : "0" } ,
134+ } ,
135+ ] ,
136+ } ) ;
137+
138+ const [ result ] = await scanOpenRouterModels ( { fetchImpl, probe : false } ) ;
139+
140+ expect ( result ?. contextLength ) . toBe ( 32_768 ) ;
141+ expect ( result ?. maxCompletionTokens ) . toBe ( 4096 ) ;
142+ }
143+ } ) ;
144+
145+ it ( "mixes provider context length with a top-level completion cap" , async ( ) => {
146+ const fetchImpl = createFetchFixture ( {
147+ data : [
148+ {
149+ id : "acme/provider-limited:free" ,
150+ name : "Provider Limited" ,
151+ context_length : 32_768 ,
152+ max_completion_tokens : 4096 ,
153+ top_provider : { context_length : 16_384 , max_completion_tokens : 0 } ,
154+ supported_parameters : [ ] ,
155+ pricing : { prompt : "0" , completion : "0" } ,
156+ } ,
157+ ] ,
158+ } ) ;
159+
160+ const [ result ] = await scanOpenRouterModels ( { fetchImpl, probe : false } ) ;
161+
162+ expect ( result ?. contextLength ) . toBe ( 16_384 ) ;
163+ expect ( result ?. maxCompletionTokens ) . toBe ( 4096 ) ;
164+ } ) ;
165+
166+ it ( "falls back to top-level max_output_tokens" , async ( ) => {
167+ const fetchImpl = createFetchFixture ( {
168+ data : [
169+ {
170+ id : "acme/output-limited:free" ,
171+ name : "Output Limited" ,
172+ context_length : 32_768 ,
173+ max_output_tokens : 2048 ,
174+ top_provider : { max_completion_tokens : 0 } ,
175+ supported_parameters : [ ] ,
176+ pricing : { prompt : "0" , completion : "0" } ,
177+ } ,
178+ ] ,
179+ } ) ;
180+
181+ const [ result ] = await scanOpenRouterModels ( { fetchImpl, probe : false } ) ;
182+
183+ expect ( result ?. maxCompletionTokens ) . toBe ( 2048 ) ;
184+ } ) ;
185+
84186 it ( "drops out-of-range OpenRouter created_at timestamps" , async ( ) => {
85187 const fetchImpl = createFetchFixture ( {
86188 data : [
0 commit comments