@@ -30,6 +30,7 @@ vi.mock("openclaw/plugin-sdk/provider-stream-family", async (importOriginal) =>
3030} ) ;
3131
3232import openrouterPlugin from "./index.js" ;
33+ import * as openRouterCatalog from "./provider-catalog.js" ;
3334import {
3435 buildOpenrouterProvider ,
3536 isOpenRouterProxyReasoningUnsupportedModel ,
@@ -252,6 +253,135 @@ describe("openrouter provider hooks", () => {
252253 expect ( buildOpenrouterProvider ( ) . models ?. map ( ( model ) => model . id ) ) . not . toContain ( "auto" ) ;
253254 } ) ;
254255
256+ it ( "forwards configured proxy destination and request policy into authenticated catalog discovery" , async ( ) => {
257+ const provider = await registerSingleProviderPlugin ( openrouterPlugin ) ;
258+ const configuredProvider = {
259+ apiKey : "synthetic-private-proxy-key" ,
260+ baseUrl : "https://private.example.invalid/router/v1///" ,
261+ request : { headers : { "X-Private-Proxy-Tenant" : "synthetic-tenant" } } ,
262+ models : [ ] ,
263+ } ;
264+ const catalogSpy = vi
265+ . spyOn ( openRouterCatalog , "buildOpenrouterLiveProvider" )
266+ . mockResolvedValue ( buildOpenrouterProvider ( ) ) ;
267+
268+ try {
269+ await provider . catalog ?. run ( {
270+ config : { models : { providers : { openrouter : configuredProvider } } } ,
271+ resolveProviderApiKey : ( ) => ( {
272+ apiKey : "OPENROUTER_API_KEY" ,
273+ discoveryApiKey : "synthetic-private-proxy-key" ,
274+ } ) ,
275+ } as never ) ;
276+
277+ expect ( catalogSpy ) . toHaveBeenCalledWith (
278+ expect . objectContaining ( {
279+ baseUrl : configuredProvider . baseUrl ,
280+ request : configuredProvider . request ,
281+ } ) ,
282+ ) ;
283+ } finally {
284+ catalogSpy . mockRestore ( ) ;
285+ }
286+ } ) ;
287+
288+ it ( "keeps dynamic proxy models on their configured credential destination" , async ( ) => {
289+ const provider = await registerSingleProviderPlugin ( openrouterPlugin ) ;
290+ const model = provider . resolveDynamicModel ?.( {
291+ provider : "openrouter" ,
292+ modelId : "private/unknown-model" ,
293+ modelRegistry : { find : vi . fn ( ( ) => null ) } ,
294+ providerConfig : { baseUrl : "https://private.example.invalid/router/v1///" } ,
295+ } as never ) ;
296+
297+ expect ( model ?. baseUrl ) . toBe ( "https://private.example.invalid/router/v1" ) ;
298+ } ) ;
299+
300+ it ( "resolves dynamic proxy destinations from canonical provider config when runtime config is absent" , async ( ) => {
301+ const provider = await registerSingleProviderPlugin ( openrouterPlugin ) ;
302+ const model = provider . resolveDynamicModel ?.( {
303+ provider : "openrouter" ,
304+ modelId : "private/unknown-model" ,
305+ modelRegistry : { find : vi . fn ( ( ) => null ) } ,
306+ config : {
307+ models : {
308+ providers : {
309+ openrouter : { baseUrl : "https://private.example.invalid/router/v1/" , models : [ ] } ,
310+ } ,
311+ } ,
312+ } ,
313+ } as never ) ;
314+
315+ expect ( model ?. baseUrl ) . toBe ( "https://private.example.invalid/router/v1" ) ;
316+ } ) ;
317+
318+ it ( "preserves the canonical official destination for dynamically resolved default models" , async ( ) => {
319+ const provider = await registerSingleProviderPlugin ( openrouterPlugin ) ;
320+ const model = provider . resolveDynamicModel ?.( {
321+ provider : "openrouter" ,
322+ modelId : "openrouter/auto" ,
323+ modelRegistry : { find : vi . fn ( ( ) => null ) } ,
324+ providerConfig : { baseUrl : "https://openrouter.ai/v1///" } ,
325+ } as never ) ;
326+
327+ expect ( model ?. baseUrl ) . toBe ( "https://openrouter.ai/api/v1" ) ;
328+ } ) ;
329+
330+ it ( "forwards configured proxy destination and headers to both usage requests" , async ( ) => {
331+ const provider = await registerSingleProviderPlugin ( openrouterPlugin ) ;
332+ const fetchFn = vi . fn < typeof fetch > ( async ( ) => Response . json ( { data : { usage : 1 } } ) ) ;
333+
334+ await provider . fetchUsageSnapshot ?.( {
335+ config : {
336+ models : {
337+ providers : {
338+ openrouter : {
339+ baseUrl : "https://private.example.invalid/router/v1///" ,
340+ request : { headers : { "X-Private-Proxy-Tenant" : "synthetic-tenant" } } ,
341+ models : [ ] ,
342+ } ,
343+ } ,
344+ } ,
345+ } ,
346+ env : { } ,
347+ provider : "openrouter" ,
348+ token : "synthetic-private-proxy-key" ,
349+ timeoutMs : 5000 ,
350+ fetchFn : fetchFn as unknown as typeof fetch ,
351+ } ) ;
352+
353+ expect ( fetchFn . mock . calls . map ( ( [ url ] ) => url ) ) . toEqual ( [
354+ "https://private.example.invalid/router/v1/credits" ,
355+ "https://private.example.invalid/router/v1/key" ,
356+ ] ) ;
357+ for ( const [ , options ] of fetchFn . mock . calls ) {
358+ expect ( new Headers ( options ?. headers ) . get ( "x-private-proxy-tenant" ) ) . toBe ( "synthetic-tenant" ) ;
359+ }
360+ } ) ;
361+
362+ it ( "does not start authenticated catalog discovery when no credential exists" , async ( ) => {
363+ const provider = await registerSingleProviderPlugin ( openrouterPlugin ) ;
364+ const catalogSpy = vi . spyOn ( openRouterCatalog , "buildOpenrouterLiveProvider" ) ;
365+
366+ try {
367+ await expect (
368+ provider . catalog ?. run ( {
369+ config : {
370+ models : {
371+ providers : {
372+ openrouter : { baseUrl : "https://private.example.invalid/v1" , models : [ ] } ,
373+ } ,
374+ } ,
375+ } ,
376+ resolveProviderApiKey : ( ) => ( { } ) ,
377+ } as never ) ,
378+ ) . resolves . toBeNull ( ) ;
379+ expect ( catalogSpy ) . not . toHaveBeenCalled ( ) ;
380+ } finally {
381+ catalogSpy . mockRestore ( ) ;
382+ }
383+ } ) ;
384+
255385 it ( "normalizes OpenRouter API ids before capability loading and lookup" , async ( ) => {
256386 getOpenRouterModelCapabilitiesMock . mockReset ( ) ;
257387 loadOpenRouterModelCapabilitiesMock . mockClear ( ) ;
0 commit comments