|
| 1 | +package providermodelcatalog |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/Gitlawb/zero/internal/providercatalog" |
| 7 | +) |
| 8 | + |
| 9 | +func TestModelIDAllowedForProvider(t *testing.T) { |
| 10 | + cases := []struct { |
| 11 | + testName string |
| 12 | + providerID string |
| 13 | + modelID string |
| 14 | + want bool |
| 15 | + }{ |
| 16 | + // Empty model ID is always rejected. |
| 17 | + {"empty model", "any-provider", "", false}, |
| 18 | + {"empty model after trim", "any-provider", " ", false}, |
| 19 | + |
| 20 | + // opencode-go-anthropic-compatible: only qwen and minimax allowed. |
| 21 | + {"opencode-anthropic qwen", "opencode-go-anthropic-compatible", "qwen3.7-plus", true}, |
| 22 | + {"opencode-anthropic minimax", "opencode-go-anthropic-compatible", "MiniMax-M3", true}, |
| 23 | + {"opencode-anthropic non-allowed", "opencode-go-anthropic-compatible", "claude-sonnet-4.5", false}, |
| 24 | + {"opencode-anthropic deepseek", "opencode-go-anthropic-compatible", "deepseek-chat", false}, |
| 25 | + |
| 26 | + // Case insensitivity and trimming for the blocked provider. |
| 27 | + {"opencode-anthropic case insensitive", "opencode-go-anthropic-compatible", "QWEN3.7-PLUS", true}, |
| 28 | + {"opencode-anthropic whitespace", "opencode-go-anthropic-compatible", " minimax-m3 ", true}, |
| 29 | + |
| 30 | + // Normalized provider ID — e.g. NormalizeID converts spaces/dots to dashes. |
| 31 | + {"opencode-anthropic normalized provider", "opencode go anthropic compatible", "qwen3.7-plus", true}, |
| 32 | + |
| 33 | + // Default branch: all model IDs allowed. |
| 34 | + {"default provider allows anything", "openai", "claude-sonnet-4.5", true}, |
| 35 | + {"default provider allows any name", "openai", "x", true}, |
| 36 | + {"default provider groq", "groq", "deepseek-chat", true}, |
| 37 | + } |
| 38 | + |
| 39 | + for _, tc := range cases { |
| 40 | + t.Run(tc.testName, func(t *testing.T) { |
| 41 | + got := ModelIDAllowedForProvider(tc.providerID, tc.modelID) |
| 42 | + if got != tc.want { |
| 43 | + t.Errorf("ModelIDAllowedForProvider(%q, %q) = %v, want %v", |
| 44 | + tc.providerID, tc.modelID, got, tc.want) |
| 45 | + } |
| 46 | + }) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// TestFilterModelsForProvider verifies that filtering delegates correctly. |
| 51 | +func TestFilterModelsForProvider(t *testing.T) { |
| 52 | + models := []Model{ |
| 53 | + {ID: "qwen3.7-plus"}, |
| 54 | + {ID: "MiniMax-M3"}, |
| 55 | + {ID: "claude-sonnet-4.5"}, |
| 56 | + {ID: "deepseek-chat"}, |
| 57 | + } |
| 58 | + |
| 59 | + filtered := FilterModelsForProvider("opencode-go-anthropic-compatible", models) |
| 60 | + if len(filtered) != 2 { |
| 61 | + t.Fatalf("FilterModelsForProvider returned %d models, want 2: %#v", len(filtered), modelIDs(filtered)) |
| 62 | + } |
| 63 | + if filtered[0].ID != "qwen3.7-plus" || filtered[1].ID != "MiniMax-M3" { |
| 64 | + t.Errorf("FilterModelsForProvider returned %#v, want [qwen3.7-plus MiniMax-M3]", modelIDs(filtered)) |
| 65 | + } |
| 66 | + |
| 67 | + // With a default provider nothing is filtered out. |
| 68 | + all := FilterModelsForProvider("openai", models) |
| 69 | + if len(all) != len(models) { |
| 70 | + t.Fatalf("FilterModelsForProvider(openai) returned %d, want %d", len(all), len(models)) |
| 71 | + } |
| 72 | + |
| 73 | + // Empty input. |
| 74 | + empty := FilterModelsForProvider("opencode-go-anthropic-compatible", nil) |
| 75 | + if len(empty) != 0 { |
| 76 | + t.Fatal("expected empty result for nil input") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// TestModelIDAllowedForProvider_NormalizedProviderIDs verifies that various |
| 81 | +// raw provider ID formats that NormalizeID maps to the same canonical form |
| 82 | +// are handled consistently. |
| 83 | +func TestModelIDAllowedForProvider_NormalizedProviderIDs(t *testing.T) { |
| 84 | + canonical := providercatalog.NormalizeID("opencode-go-anthropic-compatible") |
| 85 | + variants := []string{ |
| 86 | + "opencode-go-anthropic-compatible", |
| 87 | + "opencode_go_anthropic_compatible", |
| 88 | + "opencode go anthropic compatible", |
| 89 | + "OpenCode-Go-Anthropic-Compatible", |
| 90 | + " opencode-go-anthropic-compatible ", |
| 91 | + "opencode.go.anthropic.compatible", |
| 92 | + } |
| 93 | + for _, variant := range variants { |
| 94 | + if n := providercatalog.NormalizeID(variant); n != canonical { |
| 95 | + t.Fatalf("NormalizeID(%q) = %q, want %q", variant, n, canonical) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // All variants should produce the same filter result. |
| 100 | + for _, variant := range variants { |
| 101 | + if !ModelIDAllowedForProvider(variant, "qwen3.7-plus") { |
| 102 | + t.Errorf("ModelIDAllowedForProvider(%q, qwen3.7-plus) = false, want true", variant) |
| 103 | + } |
| 104 | + if ModelIDAllowedForProvider(variant, "claude-sonnet-4.5") { |
| 105 | + t.Errorf("ModelIDAllowedForProvider(%q, claude-sonnet-4.5) = true, want false", variant) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments