@@ -226,3 +226,79 @@ func TestFindProviderByID(t *testing.T) {
226226 })
227227 }
228228}
229+
230+ // TestModelSelectionPriority tests the priority order for model selection
231+ func TestModelSelectionPriority (t * testing.T ) {
232+ providers := []opencode.Provider {
233+ {
234+ ID : "anthropic" ,
235+ Models : map [string ]opencode.Model {
236+ "claude-opus" : {ID : "claude-opus" },
237+ },
238+ },
239+ {
240+ ID : "openai" ,
241+ Models : map [string ]opencode.Model {
242+ "gpt-4" : {ID : "gpt-4" },
243+ },
244+ },
245+ }
246+
247+ tests := []struct {
248+ name string
249+ agentProviderID string
250+ agentModelID string
251+ configModel string
252+ expectedProviderID string
253+ expectedModelID string
254+ description string
255+ }{
256+ {
257+ name : "agent model takes priority over config" ,
258+ agentProviderID : "openai" ,
259+ agentModelID : "gpt-4" ,
260+ configModel : "anthropic/claude-opus" ,
261+ expectedProviderID : "openai" ,
262+ expectedModelID : "gpt-4" ,
263+ description : "When agent specifies a model, it should be used even if config has a different model" ,
264+ },
265+ {
266+ name : "config model used when agent has no model" ,
267+ agentProviderID : "" ,
268+ agentModelID : "" ,
269+ configModel : "anthropic/claude-opus" ,
270+ expectedProviderID : "anthropic" ,
271+ expectedModelID : "claude-opus" ,
272+ description : "When agent has no model specified, config model should be used as fallback" ,
273+ },
274+ }
275+
276+ for _ , tt := range tests {
277+ t .Run (tt .name , func (t * testing.T ) {
278+ var selectedProvider * opencode.Provider
279+ var selectedModel * opencode.Model
280+
281+ // Simulate priority 2: Agent model check
282+ if tt .agentModelID != "" {
283+ selectedProvider , selectedModel = findModelByProviderAndModelID (providers , tt .agentProviderID , tt .agentModelID )
284+ }
285+
286+ // Simulate priority 3: Config model fallback
287+ if selectedProvider == nil && tt .configModel != "" {
288+ selectedProvider , selectedModel = findModelByFullID (providers , tt .configModel )
289+ }
290+
291+ if selectedProvider == nil || selectedModel == nil {
292+ t .Fatalf ("Expected to find model, but got nil - %s" , tt .description )
293+ }
294+
295+ if selectedProvider .ID != tt .expectedProviderID {
296+ t .Errorf ("Expected provider %s, got %s - %s" , tt .expectedProviderID , selectedProvider .ID , tt .description )
297+ }
298+
299+ if selectedModel .ID != tt .expectedModelID {
300+ t .Errorf ("Expected model %s, got %s - %s" , tt .expectedModelID , selectedModel .ID , tt .description )
301+ }
302+ })
303+ }
304+ }
0 commit comments