11// Verifies models.json planning applies config env vars and discovery scope.
2- import { beforeAll , describe , expect , it , vi } from "vitest" ;
2+ import { afterEach , beforeAll , describe , expect , it , vi } from "vitest" ;
33import type { OpenClawConfig } from "../config/config.js" ;
44import { createConfigRuntimeEnv } from "../config/env-vars.js" ;
55import type { PluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.js" ;
@@ -17,6 +17,15 @@ import {
1717import type { ProviderConfig } from "./models-config.providers.secrets.js" ;
1818import { encodePluginModelCatalogRelativePath } from "./plugin-model-catalog.js" ;
1919
20+ const providerRuntimeMocks = vi . hoisted ( ( ) => ( {
21+ normalizeProviderConfigWithPlugin : vi . fn <
22+ typeof import ( "../plugins/provider-runtime.js" ) . normalizeProviderConfigWithPlugin
23+ > ( ( ) => undefined ) ,
24+ resolveProviderConfigApiKeyWithPlugin : vi . fn <
25+ typeof import ( "../plugins/provider-runtime.js" ) . resolveProviderConfigApiKeyWithPlugin
26+ > ( ( ) => undefined ) ,
27+ } ) ) ;
28+
2029vi . mock ( "./provider-auth-aliases.js" , ( ) => ( {
2130 resolveProviderAuthAliasMap : ( ) => Object . create ( null ) as Record < string , string > ,
2231 resolveProviderIdForAuth : ( provider : string ) => provider . trim ( ) . toLowerCase ( ) ,
@@ -26,8 +35,8 @@ vi.mock("./provider-auth-aliases.js", () => ({
2635// provider markers local instead of loading the bundled plugin/runtime catalog.
2736vi . mock ( "../plugins/provider-runtime.js" , ( ) => ( {
2837 applyProviderNativeStreamingUsageCompatWithPlugin : ( ) => undefined ,
29- normalizeProviderConfigWithPlugin : ( ) => undefined ,
30- resolveProviderConfigApiKeyWithPlugin : ( ) => undefined ,
38+ normalizeProviderConfigWithPlugin : providerRuntimeMocks . normalizeProviderConfigWithPlugin ,
39+ resolveProviderConfigApiKeyWithPlugin : providerRuntimeMocks . resolveProviderConfigApiKeyWithPlugin ,
3140 resolveExternalAuthProfilesWithPlugins : ( ) => [ ] ,
3241 resolveProviderSyntheticAuthWithPlugin : ( ) => undefined ,
3342} ) ) ;
@@ -51,6 +60,13 @@ vi.mock("./model-auth-env-vars.js", () => ({
5160
5261const TEST_ENV_VAR = "OPENCLAW_MODELS_CONFIG_TEST_ENV" ;
5362
63+ afterEach ( ( ) => {
64+ providerRuntimeMocks . normalizeProviderConfigWithPlugin . mockReset ( ) ;
65+ providerRuntimeMocks . normalizeProviderConfigWithPlugin . mockReturnValue ( undefined ) ;
66+ providerRuntimeMocks . resolveProviderConfigApiKeyWithPlugin . mockReset ( ) ;
67+ providerRuntimeMocks . resolveProviderConfigApiKeyWithPlugin . mockReturnValue ( undefined ) ;
68+ } ) ;
69+
5470function createImplicitOpenRouterProvider ( ) : ProviderConfig {
5571 return {
5672 baseUrl : "https://openrouter.ai/api/v1" ,
@@ -393,6 +409,64 @@ describe("models-config", () => {
393409 expect ( observedSnapshot ) . toBe ( pluginMetadataSnapshot ) ;
394410 } ) ;
395411
412+ it . each ( [
413+ { label : "full" , pluginIds : undefined , expectedRegistry : true } ,
414+ { label : "scoped" , pluginIds : [ "owner" ] , expectedRegistry : false } ,
415+ ] ) (
416+ "threads provider policy metadata only from a $label plugin snapshot" ,
417+ async ( { pluginIds, expectedRegistry } ) => {
418+ const manifestRegistry = { plugins : [ ] , diagnostics : [ ] } ;
419+ const pluginMetadataSnapshot = {
420+ index : { plugins : [ ] } ,
421+ manifestRegistry,
422+ owners : {
423+ providers : new Map ( ) ,
424+ modelCatalogProviders : new Map ( ) ,
425+ setupProviders : new Map ( ) ,
426+ } ,
427+ ...( pluginIds ? { pluginIds } : { } ) ,
428+ } as unknown as Pick <
429+ PluginMetadataSnapshot ,
430+ "index" | "manifestRegistry" | "owners" | "pluginIds"
431+ > ;
432+ providerRuntimeMocks . resolveProviderConfigApiKeyWithPlugin . mockReturnValue (
433+ "POLICY_ALIAS_API_KEY" ,
434+ ) ;
435+
436+ await planOpenClawModelsJsonWithDeps (
437+ {
438+ cfg : { models : { providers : { } } } ,
439+ agentDir : "/tmp/openclaw-models-config-policy-registry-test" ,
440+ env : { } ,
441+ existingRaw : "" ,
442+ existingParsed : null ,
443+ pluginMetadataSnapshot,
444+ } ,
445+ {
446+ resolveImplicitProviders : async ( ) => ( {
447+ "policy-alias" : createImplicitOpenAiProvider ( {
448+ baseUrl : "https://policy.example/v1" ,
449+ apiKey : undefined ,
450+ } ) ,
451+ } ) ,
452+ } ,
453+ ) ;
454+
455+ const normalizeParams =
456+ providerRuntimeMocks . normalizeProviderConfigWithPlugin . mock . calls . find (
457+ ( [ params ] ) => params . provider === "policy-alias" ,
458+ ) ?. [ 0 ] ;
459+ const apiKeyParams =
460+ providerRuntimeMocks . resolveProviderConfigApiKeyWithPlugin . mock . calls . find (
461+ ( [ params ] ) => params . provider === "policy-alias" ,
462+ ) ?. [ 0 ] ;
463+ expect ( normalizeParams ?. manifestRegistry ) . toBe (
464+ expectedRegistry ? manifestRegistry : undefined ,
465+ ) ;
466+ expect ( apiKeyParams ?. manifestRegistry ) . toBe ( expectedRegistry ? manifestRegistry : undefined ) ;
467+ } ,
468+ ) ;
469+
396470 it ( "does not write unauthenticated model providers that would invalidate models.json" , async ( ) => {
397471 expect ( unauthenticatedProviderWritePlan . action ) . toBe ( "write" ) ;
398472 expect ( unauthenticatedProviderParsed . providers ?. openai ) . toBeUndefined ( ) ;
0 commit comments