返回 DeepSeek-Reasonix
opencode_contract_test.go
根目录 / internal / provider / opencode_contract_test.go
1 package provider
2
3 import (
4 "reflect"
5 "testing"
6 )
7
8 func TestOpenCodeGoInferenceRoutesAndDeepSeekV41(t *testing.T) {
9 for _, tc := range []struct{ kind, base, endpoint, model, route string }{
10 {"openai", "https://opencode.ai/inference/go/openai/v1", "chat/completions", "deepseek-v4.1-flash", OpenCodeGoRouteChat},
11 {"anthropic", "https://opencode.ai/inference/go/anthropic", "v1/messages", "qwen3.7-plus", OpenCodeGoRouteAnthropic},
12 {"responses", "https://opencode.ai/inference/go/openai/v1", "responses", "grok-4.5", OpenCodeGoRouteResponses},
13 {"openai", "https://opencode.ai/zen/go/v1", "chat/completions", "deepseek-v4.1-flash", OpenCodeGoRouteChat},
14 } {
15 t.Run(tc.kind+tc.base, func(t *testing.T) {
16 if route, ok := OfficialOpenCodeGoRoute(tc.kind, tc.base); !ok || route != tc.route {
17 t.Fatalf("base route = %q, %v", route, ok)
18 }
19 requestURL := tc.base + "/" + tc.endpoint
20 c, ok := LookupOpenCodeGoContract(tc.kind, "https://custom.example", requestURL, "", tc.model)
21 if !ok || c.Route != tc.route {
22 t.Fatalf("request contract = %+v, %v", c, ok)
23 }
24 if tc.model == "deepseek-v4.1-flash" && (c.ReasoningProtocol != "deepseek" || c.Thinking != "enabled" || c.Reasoning.Validate(tc.model, "low") != nil) {
25 t.Fatalf("V4.1 lost thinking/replay contract: %+v", c)
26 }
27 for _, custom := range []string{requestURL + "?custom=1", requestURL + "/extra", "https://opencode.ai.evil/inference/go/openai/v1/chat/completions"} {
28 if _, ok := LookupOpenCodeGoContract(tc.kind, tc.base, custom, "", tc.model); ok {
29 t.Fatalf("custom endpoint inherited official contract: %s", custom)
30 }
31 }
32 })
33 }
34 }
35
36 func TestOpenCodeGoContractUsesExactEffectiveEndpointAndModel(t *testing.T) {
37 for _, kind := range []string{"openai", "anthropic", "responses"} {
38 endpoint := map[string]string{"openai": "chat/completions", "anthropic": "messages", "responses": "responses"}[kind]
39 url := "https://opencode.ai/zen/go/v1/" + endpoint
40 for _, model := range []string{"deepseek-flash", "deepseek-v4-flash", "deepseek-v4-pro", "deepseek-v4-flash-vision-exp"} {
41 c, ok := LookupOpenCodeGoContract(kind, "https://custom.example", url, "", model)
42 if !ok || c.RecommendedRoute != OpenCodeGoRouteChat || c.Reasoning.Default != "high" || c.Reasoning.Validate(model, "max") != nil {
43 t.Fatalf("%s %s: %+v %v", kind, model, c, ok)
44 }
45 for _, custom := range []string{url + "?route=user", url + "#user", url + "/custom", "https://custom.example/v1/" + endpoint} {
46 if _, ok := LookupOpenCodeGoContract(kind, "https://opencode.ai/zen/go/v1", custom, "", model); ok {
47 t.Fatalf("custom endpoint inherited contract: %s", custom)
48 }
49 }
50 }
51 if _, ok := LookupOpenCodeGoContract(kind, "", url, "", "deepseek-v4-pro-custom"); ok {
52 t.Fatal("unknown model was guessed from its prefix")
53 }
54 if _, ok := LookupOpenCodeGoContract(kind, "", url, "", "DeepSeek-v4-pro"); ok {
55 t.Fatal("model IDs must be exact")
56 }
57 }
58 }
59
60 func TestOpenCodeGoDiscoveryFiltersTheActualRequestRoute(t *testing.T) {
61 models := []string{"deepseek-v4-pro", "qwen3.8-max", "grok-4.5", "user-custom"}
62 got := FilterOpenCodeGoRequestModels("openai", "https://opencode.ai/zen/go/v1", "https://opencode.ai/zen/go/v1/chat/completions", "", models)
63 if !reflect.DeepEqual(got, []string{"deepseek-v4-pro", "qwen3.8-max"}) {
64 t.Fatal(got)
65 }
66 got = FilterOpenCodeGoRequestModels("openai", "https://opencode.ai/zen/go/v1", "https://custom.example/v1/chat/completions", "", models)
67 if !reflect.DeepEqual(got, models) {
68 t.Fatal("custom discovery was filtered using an unrelated official base")
69 }
70 }
71
72 func TestApplyOpenCodeGoContractDoesNotMutateCallerDeclarations(t *testing.T) {
73 extra := map[string]any{"thinking": "enabled"}
74 cfg := Config{BaseURL: "https://opencode.ai/zen/go", Model: "deepseek-v4-pro", Extra: extra}
75 resolved := ApplyOpenCodeGoContract("anthropic", cfg)
76 if len(extra) != 1 || resolved.Extra["reasoning_protocol"] != "deepseek" || resolved.Extra["default_effort"] != "high" {
77 t.Fatalf("contract injection mutated caller or lost default: %+v", resolved.Extra)
78 }
79 extra["reasoning_protocol"] = "custom"
80 extra["supported_efforts"] = []string{"custom-max"}
81 resolved = ApplyOpenCodeGoContract("anthropic", cfg)
82 if resolved.Extra["reasoning_protocol"] != "custom" || !reflect.DeepEqual(resolved.Extra["supported_efforts"], []string{"custom-max"}) {
83 t.Fatal("explicit declaration was overwritten")
84 }
85 }
86
86 lines GO