| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestOpenCodeGoContractUsesExactEffectiveEndpointAndModel(t *testing.T) { |
| 9 | for _, kind := range []string{"openai", "anthropic", "responses"} { |
| 10 | endpoint := map[string]string{"openai": "chat/completions", "anthropic": "messages", "responses": "responses"}[kind] |
| 11 | url := "https://opencode.ai/zen/go/v1/" + endpoint |
| 12 | for _, model := range []string{"deepseek-flash", "deepseek-v4-flash", "deepseek-v4-pro", "deepseek-v4-flash-vision-exp"} { |
| 13 | c, ok := LookupOpenCodeGoContract(kind, "https://custom.example", url, "", model) |
| 14 | if !ok || c.RecommendedRoute != OpenCodeGoRouteChat || c.Reasoning.Default != "high" || c.Reasoning.Validate(model, "max") != nil { |
| 15 | t.Fatalf("%s %s: %+v %v", kind, model, c, ok) |
| 16 | } |
| 17 | for _, custom := range []string{url + "?route=user", url + "#user", url + "/custom", "https://custom.example/v1/" + endpoint} { |
| 18 | if _, ok := LookupOpenCodeGoContract(kind, "https://opencode.ai/zen/go/v1", custom, "", model); ok { |
| 19 | t.Fatalf("custom endpoint inherited contract: %s", custom) |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | if _, ok := LookupOpenCodeGoContract(kind, "", url, "", "deepseek-v4-pro-custom"); ok { |
| 24 | t.Fatal("unknown model was guessed from its prefix") |
| 25 | } |
| 26 | if _, ok := LookupOpenCodeGoContract(kind, "", url, "", "DeepSeek-v4-pro"); ok { |
| 27 | t.Fatal("model IDs must be exact") |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestOpenCodeGoDiscoveryFiltersTheActualRequestRoute(t *testing.T) { |
| 33 | models := []string{"deepseek-v4-pro", "qwen3.8-max", "grok-4.5", "user-custom"} |
| 34 | got := FilterOpenCodeGoRequestModels("openai", "https://opencode.ai/zen/go/v1", "https://opencode.ai/zen/go/v1/chat/completions", "", models) |
| 35 | if !reflect.DeepEqual(got, []string{"deepseek-v4-pro", "qwen3.8-max"}) { |
| 36 | t.Fatal(got) |
| 37 | } |
| 38 | got = FilterOpenCodeGoRequestModels("openai", "https://opencode.ai/zen/go/v1", "https://custom.example/v1/chat/completions", "", models) |
| 39 | if !reflect.DeepEqual(got, models) { |
| 40 | t.Fatal("custom discovery was filtered using an unrelated official base") |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestApplyOpenCodeGoContractDoesNotMutateCallerDeclarations(t *testing.T) { |
| 45 | extra := map[string]any{"thinking": "enabled"} |
| 46 | cfg := Config{BaseURL: "https://opencode.ai/zen/go", Model: "deepseek-v4-pro", Extra: extra} |
| 47 | resolved := ApplyOpenCodeGoContract("anthropic", cfg) |
| 48 | if len(extra) != 1 || resolved.Extra["reasoning_protocol"] != "deepseek" || resolved.Extra["default_effort"] != "high" { |
| 49 | t.Fatalf("contract injection mutated caller or lost default: %+v", resolved.Extra) |
| 50 | } |
| 51 | extra["reasoning_protocol"] = "custom" |
| 52 | extra["supported_efforts"] = []string{"custom-max"} |
| 53 | resolved = ApplyOpenCodeGoContract("anthropic", cfg) |
| 54 | if resolved.Extra["reasoning_protocol"] != "custom" || !reflect.DeepEqual(resolved.Extra["supported_efforts"], []string{"custom-max"}) { |
| 55 | t.Fatal("explicit declaration was overwritten") |
| 56 | } |
| 57 | } |
| 58 |