1+ import { calculateUsageCost } from "@openclaw/llm-core" ;
12import { describe , expect , it } from "vitest" ;
23import {
34 normalizeModelPricingProvider ,
@@ -85,12 +86,17 @@ describe.each([
8586} ) ;
8687
8788describe ( "OpenRouter native pricing" , ( ) => {
88- it ( "sorts static prompt tiers and ignores scheduled overrides without borrowing foreign prices" , ( ) => {
89+ const base = {
90+ prompt : "0.000002" ,
91+ completion : "0.00001" ,
92+ input_cache_read : "0.00000025" ,
93+ input_cache_write : "0.0000025" ,
94+ } ;
95+ const cost = { input : 2 , output : 10 , cacheRead : 0.25 , cacheWrite : 2.5 } ;
96+
97+ it ( "compiles unordered overrides in source order per key without borrowing foreign prices" , ( ) => {
8998 const pricing = {
90- prompt : "0.000002" ,
91- completion : "0.00001" ,
92- input_cache_read : "0.0000002" ,
93- input_cache_write : "0.0000025" ,
99+ ...base ,
94100 // Other feed formats must not supply native OpenRouter rates or tiers.
95101 input : 99 ,
96102 context_over_200k : { input : 99 , output : 99 } ,
@@ -107,31 +113,190 @@ describe("OpenRouter native pricing", () => {
107113 } ,
108114 ] ,
109115 } ;
110- const cost = { input : 2 , output : 10 , cacheRead : expect . closeTo ( 0.2 , 12 ) , cacheWrite : 2.5 } ;
111116 expect ( normalizeOpenRouterModelPricing ( pricing ) ) . toEqual ( {
112117 ...cost ,
113118 tieredPricing : [
114- { ...cost , range : [ 0 , 272_000 ] } ,
119+ { ...cost , range : [ 0 , 272_001 ] } ,
115120 {
116121 input : 4 ,
117122 output : 15 ,
118123 cacheRead : expect . closeTo ( 0.4 , 12 ) ,
119124 cacheWrite : 5 ,
120- range : [ 272_000 , 500_000 ] ,
125+ range : [ 272_001 , 500_001 ] ,
121126 } ,
122- { input : 6 , output : 20 , cacheRead : 0 , cacheWrite : 0 , range : [ 500_000 ] } ,
127+ {
128+ input : 4 ,
129+ output : 15 ,
130+ cacheRead : expect . closeTo ( 0.4 , 12 ) ,
131+ cacheWrite : 5 ,
132+ range : [ 500_001 ] ,
133+ } ,
134+ ] ,
135+ } ) ;
136+ } ) ;
137+
138+ it . each ( [
139+ { prices : { prompt : "0.000004" } , expected : { input : 4 } } ,
140+ { prices : { completion : "0.00002" } , expected : { output : 20 } } ,
141+ { prices : { input_cache_read : "0.0000005" } , expected : { cacheRead : 0.5 } } ,
142+ { prices : { input_cache_write : "0.000005" } , expected : { cacheWrite : 5 } } ,
143+ { prices : { prompt : "0" , input_cache_read : "0" } , expected : { input : 0 , cacheRead : 0 } } ,
144+ ] ) ( "inherits omitted native rates for partial overrides: $prices" , ( { prices, expected } ) => {
145+ expect (
146+ normalizeOpenRouterModelPricing ( {
147+ ...base ,
148+ overrides : [ { min_prompt_tokens : 100 , ...prices } ] ,
149+ } ) ,
150+ ) . toEqual ( {
151+ ...cost ,
152+ tieredPricing : [
153+ { ...cost , range : [ 0 , 101 ] } ,
154+ { ...cost , ...expected , range : [ 101 ] } ,
155+ ] ,
156+ } ) ;
157+ } ) ;
158+
159+ it ( "lets equal and lower thresholds override only their supplied keys in source order" , ( ) => {
160+ expect (
161+ normalizeOpenRouterModelPricing ( {
162+ ...base ,
163+ overrides : [
164+ { min_prompt_tokens : 500 , prompt : "0.000006" , completion : "0.00002" } ,
165+ { min_prompt_tokens : 200 , prompt : "0.000004" , input_cache_read : "0.0000005" } ,
166+ { min_prompt_tokens : 200 , input_cache_write : "0" } ,
167+ ] ,
168+ } ) ,
169+ ) . toEqual ( {
170+ ...cost ,
171+ tieredPricing : [
172+ { ...cost , range : [ 0 , 201 ] } ,
173+ { input : 4 , output : 10 , cacheRead : 0.5 , cacheWrite : 0 , range : [ 201 , 501 ] } ,
174+ { input : 4 , output : 20 , cacheRead : 0.5 , cacheWrite : 0 , range : [ 501 ] } ,
175+ ] ,
176+ } ) ;
177+ } ) ;
178+
179+ it . each ( [
180+ { input : 39 , output : 7 , cacheRead : 30 , cacheWrite : 30 , expected : 0.0002305 } ,
181+ { input : 40 , output : 7 , cacheRead : 30 , cacheWrite : 30 , expected : 0.0002325 } ,
182+ { input : 41 , output : 7 , cacheRead : 30 , cacheWrite : 30 , expected : 0.0003865 } ,
183+ { input : 0 , output : 7 , cacheRead : 101 , cacheWrite : 0 , expected : 0.00016525 } ,
184+ { input : 0 , output : 7 , cacheRead : 0 , cacheWrite : 101 , expected : 0.0003925 } ,
185+ { input : 0 , output : 1_000 , cacheRead : 0 , cacheWrite : 0 , expected : 0.01 } ,
186+ ] ) (
187+ "bills strict total-prompt boundaries with inherited cache prices: %j" ,
188+ ( { expected, ...usage } ) => {
189+ const pricing = normalizeOpenRouterModelPricing ( {
190+ ...base ,
191+ overrides : [ { min_prompt_tokens : 100 , prompt : "0.000004" , completion : "0.00002" } ] ,
192+ } ) ;
193+ expect ( pricing ) . toBeDefined ( ) ;
194+ if ( ! pricing ) {
195+ throw new Error ( "Expected a complete native schedule" ) ;
196+ }
197+ expect ( calculateUsageCost ( usage , pricing ) . total ) . toBeCloseTo ( expected , 12 ) ;
198+ } ,
199+ ) ;
200+
201+ it . each ( [ 0 , 100.5 , Number . MAX_SAFE_INTEGER - 1 ] ) (
202+ "compiles a strict threshold %j to the first matching integral prompt size" ,
203+ ( threshold ) => {
204+ const start = Math . floor ( threshold ) + 1 ;
205+ expect (
206+ normalizeOpenRouterModelPricing ( {
207+ ...base ,
208+ overrides : [ { min_prompt_tokens : threshold , prompt : "0" } ] ,
209+ } ) ,
210+ ) . toEqual ( {
211+ ...cost ,
212+ tieredPricing : [
213+ { ...cost , range : [ 0 , start ] } ,
214+ { ...cost , input : 0 , range : [ start ] } ,
215+ ] ,
216+ } ) ;
217+ } ,
218+ ) ;
219+
220+ it . each ( [
221+ { future_condition : true } ,
222+ { future_price : "0" } ,
223+ { utc_start : 1630 } ,
224+ { utc_end : 30 } ,
225+ { utc_days : [ "monday" ] } ,
226+ ...[ undefined , null , - 1 , "100" , Infinity , Number . NaN , Number . MAX_SAFE_INTEGER ] . map (
227+ ( min_prompt_tokens ) => ( { min_prompt_tokens } ) ,
228+ ) ,
229+ ] ) ( "skips unsupported or invalid predicates: %j" , ( condition ) => {
230+ expect (
231+ normalizeOpenRouterModelPricing ( {
232+ ...base ,
233+ overrides : [ { min_prompt_tokens : 100 , prompt : "0" , ...condition } ] ,
234+ } ) ,
235+ ) . toEqual ( cost ) ;
236+ } ) ;
237+
238+ it ( "ignores known non-token charge dimensions without dropping token overrides" , ( ) => {
239+ expect (
240+ normalizeOpenRouterModelPricing ( {
241+ ...base ,
242+ overrides : [
243+ {
244+ min_prompt_tokens : 100 ,
245+ prompt : "0.000004" ,
246+ request : "1" ,
247+ image : "1" ,
248+ web_search : "1" ,
249+ internal_reasoning : "1" ,
250+ audio : "1" ,
251+ input_audio_cache : "1" ,
252+ input_cache_write_1h : "1" ,
253+ image_output : "1" ,
254+ audio_output : "1" ,
255+ } ,
256+ ] ,
257+ } ) ,
258+ ) . toEqual ( {
259+ ...cost ,
260+ tieredPricing : [
261+ { ...cost , range : [ 0 , 101 ] } ,
262+ { ...cost , input : 4 , range : [ 101 ] } ,
123263 ] ,
124264 } ) ;
125265 } ) ;
126266
127- it ( "rejects an incomplete native context override and per-million overflow" , ( ) => {
267+ it . each ( [ null , "" , "1usd" , - 1 , Infinity , "1e308" ] ) (
268+ "invalidates the schedule for malformed effective prices %j" ,
269+ ( invalid ) => {
270+ for ( const field of Object . keys ( base ) ) {
271+ expect (
272+ normalizeOpenRouterModelPricing ( {
273+ ...base ,
274+ overrides : [ { min_prompt_tokens : 100 , [ field ] : invalid } ] ,
275+ } ) ,
276+ ) . toBeUndefined ( ) ;
277+ }
278+ } ,
279+ ) ;
280+
281+ it ( "validates effective prices after later matching entries replace malformed fields" , ( ) => {
128282 expect (
129283 normalizeOpenRouterModelPricing ( {
130- prompt : "0.000002" ,
131- completion : "0.00001" ,
132- overrides : [ { min_prompt_tokens : 272_000 , prompt : "0.000004" } ] ,
284+ ...base ,
285+ overrides : [
286+ { min_prompt_tokens : 100 , prompt : "bad" } ,
287+ { min_prompt_tokens : 100 , prompt : "0" } ,
288+ ] ,
133289 } ) ,
134- ) . toBeUndefined ( ) ;
290+ ) . toEqual ( {
291+ ...cost ,
292+ tieredPricing : [
293+ { ...cost , range : [ 0 , 101 ] } ,
294+ { ...cost , input : 0 , range : [ 101 ] } ,
295+ ] ,
296+ } ) ;
297+ } ) ;
298+
299+ it ( "rejects per-million overflow and foreign base prices" , ( ) => {
135300 expect ( normalizeOpenRouterModelPricing ( { prompt : "1e308" , completion : "0" } ) ) . toBeUndefined ( ) ;
136301 expect ( normalizeOpenRouterModelPricing ( { input : 2 , output : 10 } ) ) . toBeUndefined ( ) ;
137302 } ) ;
0 commit comments