22import { beforeAll , beforeEach , describe , expect , it , vi } from "vitest" ;
33
44const normalizeProviderModelIdWithPluginMock = vi . fn ( ) ;
5+
6+ function normalizeLegacyFixtureModel ( {
7+ provider,
8+ context,
9+ } : {
10+ provider : string ;
11+ context : { modelId ?: string } ;
12+ } ) {
13+ return provider === "custom-provider" && context . modelId === "custom-legacy-model"
14+ ? "custom-modern-model"
15+ : undefined ;
16+ }
17+
518const emptyPluginMetadataSnapshot = vi . hoisted ( ( ) => ( {
619 configFingerprint : "model-selection-plugin-runtime-test-empty-plugin-metadata" ,
720 plugins : [
@@ -57,15 +70,7 @@ describe("model-selection plugin runtime normalization", () => {
5770 } ) ;
5871
5972 it ( "delegates provider-owned model id normalization to plugin runtime hooks" , async ( ) => {
60- normalizeProviderModelIdWithPluginMock . mockImplementation ( ( { provider, context } ) => {
61- if (
62- provider === "custom-provider" &&
63- ( context as { modelId ?: string } ) . modelId === "custom-legacy-model"
64- ) {
65- return "custom-modern-model" ;
66- }
67- return undefined ;
68- } ) ;
73+ normalizeProviderModelIdWithPluginMock . mockImplementation ( normalizeLegacyFixtureModel ) ;
6974
7075 const { parseModelRef } = await import ( "./model-selection.js" ) ;
7176
@@ -97,15 +102,7 @@ describe("model-selection plugin runtime normalization", () => {
97102 } ) ;
98103
99104 it ( "keeps provider plugin normalization when inferring provider for bare defaults" , async ( ) => {
100- normalizeProviderModelIdWithPluginMock . mockImplementation ( ( { provider, context } ) => {
101- if (
102- provider === "custom-provider" &&
103- ( context as { modelId ?: string } ) . modelId === "custom-legacy-model"
104- ) {
105- return "custom-modern-model" ;
106- }
107- return undefined ;
108- } ) ;
105+ normalizeProviderModelIdWithPluginMock . mockImplementation ( normalizeLegacyFixtureModel ) ;
109106
110107 const { resolveConfiguredModelRef } = await import ( "./model-selection.js" ) ;
111108
@@ -130,86 +127,39 @@ describe("model-selection plugin runtime normalization", () => {
130127 } ) ;
131128 } ) ;
132129
133- it ( "keeps model visibility policy construction off plugin runtime hooks by default" , async ( ) => {
134- // Visibility policy is a hot/static path. It preserves configured keys
135- // unless callers explicitly opt into runtime plugin normalization.
136- normalizeProviderModelIdWithPluginMock . mockImplementation ( ( { provider, context } ) => {
137- if (
138- provider === "custom-provider" &&
139- ( context as { modelId ?: string } ) . modelId === "custom-legacy-model"
140- ) {
141- return "custom-modern-model" ;
142- }
143- return undefined ;
144- } ) ;
145-
130+ it . each ( [
131+ [ "keeps model visibility policy construction off plugin runtime hooks by default" , undefined ] ,
132+ [
133+ "propagates explicit plugin runtime normalization opt-in through model visibility policy" ,
134+ true ,
135+ ] ,
136+ ] as const ) ( "%s" , async ( _name , allowPluginNormalization ) => {
137+ normalizeProviderModelIdWithPluginMock . mockImplementation ( normalizeLegacyFixtureModel ) ;
146138 const { createModelVisibilityPolicy } = await import ( "./model-visibility-policy.js" ) ;
147-
148139 const policy = createModelVisibilityPolicy ( {
149140 cfg : {
150- agents : {
151- defaults : {
152- models : {
153- "custom-provider/custom-legacy-model" : { } ,
154- } ,
155- } ,
156- } ,
141+ agents : { defaults : { models : { "custom-provider/custom-legacy-model" : { } } } } ,
157142 } ,
158143 catalog : [ ] ,
159144 defaultProvider : "custom-provider" ,
160145 defaultModel : "custom-legacy-model" ,
146+ ...( allowPluginNormalization ? { allowPluginNormalization } : { } ) ,
161147 } ) ;
162148
163- expect ( policy . allowedKeys . has ( "custom-provider/custom-legacy-model" ) ) . toBe ( true ) ;
164- expect ( policy . allowedKeys . has ( "custom-provider/custom-modern-model" ) ) . toBe ( false ) ;
165- expect ( normalizeProviderModelIdWithPluginMock ) . not . toHaveBeenCalled ( ) ;
166- } ) ;
167-
168- it ( "propagates explicit plugin runtime normalization opt-in through model visibility policy" , async ( ) => {
169- normalizeProviderModelIdWithPluginMock . mockImplementation ( ( { provider, context } ) => {
170- if (
171- provider === "custom-provider" &&
172- ( context as { modelId ?: string } ) . modelId === "custom-legacy-model"
173- ) {
174- return "custom-modern-model" ;
175- }
176- return undefined ;
177- } ) ;
178-
179- const { createModelVisibilityPolicy } = await import ( "./model-visibility-policy.js" ) ;
180-
181- const policy = createModelVisibilityPolicy ( {
182- cfg : {
183- agents : {
184- defaults : {
185- models : {
186- "custom-provider/custom-legacy-model" : { } ,
187- } ,
188- } ,
189- } ,
190- } ,
191- catalog : [ ] ,
192- defaultProvider : "custom-provider" ,
193- defaultModel : "custom-legacy-model" ,
194- allowPluginNormalization : true ,
195- } ) ;
196-
197- expect ( policy . allowedKeys . has ( "custom-provider/custom-modern-model" ) ) . toBe ( true ) ;
198- expect ( normalizeProviderModelIdWithPluginMock ) . toHaveBeenCalled ( ) ;
149+ if ( allowPluginNormalization ) {
150+ expect ( policy . allowedKeys . has ( "custom-provider/custom-modern-model" ) ) . toBe ( true ) ;
151+ expect ( normalizeProviderModelIdWithPluginMock ) . toHaveBeenCalled ( ) ;
152+ } else {
153+ expect ( policy . allowedKeys . has ( "custom-provider/custom-legacy-model" ) ) . toBe ( true ) ;
154+ expect ( policy . allowedKeys . has ( "custom-provider/custom-modern-model" ) ) . toBe ( false ) ;
155+ expect ( normalizeProviderModelIdWithPluginMock ) . not . toHaveBeenCalled ( ) ;
156+ }
199157 } ) ;
200158
201159 it ( "keeps plugin-normalized stored overrides allowed in auto-reply runtime selection" , async ( ) => {
202160 // Stored session overrides are runtime inputs, so provider-owned
203161 // normalization keeps old persisted ids usable without resetting them.
204- normalizeProviderModelIdWithPluginMock . mockImplementation ( ( { provider, context } ) => {
205- if (
206- provider === "custom-provider" &&
207- ( context as { modelId ?: string } ) . modelId === "custom-legacy-model"
208- ) {
209- return "custom-modern-model" ;
210- }
211- return undefined ;
212- } ) ;
162+ normalizeProviderModelIdWithPluginMock . mockImplementation ( normalizeLegacyFixtureModel ) ;
213163
214164 const cfg = {
215165 agents : {
@@ -268,12 +218,7 @@ describe("model-selection plugin runtime normalization", () => {
268218 } ) ;
269219
270220 it ( "normalizes raw persisted overrides through plugin runtime hooks" , ( ) => {
271- normalizeProviderModelIdWithPluginMock . mockImplementation ( ( { provider, context } ) =>
272- provider === "custom-provider" &&
273- ( context as { modelId ?: string } ) . modelId === "custom-legacy-model"
274- ? "custom-modern-model"
275- : undefined ,
276- ) ;
221+ normalizeProviderModelIdWithPluginMock . mockImplementation ( normalizeLegacyFixtureModel ) ;
277222
278223 expect (
279224 resolveSessionModelRef (
0 commit comments