| 1 | package openai |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/provider" |
| 9 | ) |
| 10 | |
| 11 | func newClient(t *testing.T, baseURL, effort string) *client { |
| 12 | t.Helper() |
| 13 | extra := map[string]any{} |
| 14 | if effort != "" { |
| 15 | extra["effort"] = effort |
| 16 | } |
| 17 | p, err := New(provider.Config{Name: "p", BaseURL: baseURL, Model: "m", APIKey: "k", Extra: extra}) |
| 18 | if err != nil { |
| 19 | t.Fatalf("New(%q, effort=%q): %v", baseURL, effort, err) |
| 20 | } |
| 21 | return p.(*client) |
| 22 | } |
| 23 | |
| 24 | func TestEffortNormalization(t *testing.T) { |
| 25 | const mimo = "https://api.xiaomimimo.com/v1" |
| 26 | const deepseek = "https://api.deepseek.com/v1" |
| 27 | |
| 28 | tests := []struct { |
| 29 | base, effort, want string |
| 30 | }{ |
| 31 | {mimo, "max", "high"}, // DeepSeek-ism clamped to the OpenAI ceiling — MiMo 400s on "max" |
| 32 | {mimo, "high", "high"}, |
| 33 | {mimo, "medium", "medium"}, |
| 34 | {mimo, "low", "low"}, |
| 35 | {mimo, "MAX", "high"}, // case-insensitive |
| 36 | {mimo, "auto", ""}, // UI/config auto means omit provider-specific effort |
| 37 | {mimo, "", ""}, // unset stays omitted |
| 38 | {deepseek, "max", "max"}, |
| 39 | {deepseek, "high", "high"}, |
| 40 | {deepseek, "auto", "high"}, |
| 41 | {deepseek, "", "high"}, // DeepSeek default depth |
| 42 | } |
| 43 | for _, tc := range tests { |
| 44 | if got := newClient(t, tc.base, tc.effort).effort; got != tc.want { |
| 45 | t.Errorf("base=%s effort=%q: got %q, want %q", tc.base, tc.effort, got, tc.want) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestExplicitSupportedEffortsPreservesKimiK3Max(t *testing.T) { |
| 51 | p, err := New(provider.Config{ |
| 52 | Name: "opencode-go", |
| 53 | BaseURL: "https://opencode.ai/zen/go/v1", |
| 54 | Model: "kimi-k3", |
| 55 | APIKey: "k", |
| 56 | Extra: map[string]any{ |
| 57 | "effort": "max", |
| 58 | "supported_efforts": []string{"high", "max"}, |
| 59 | }, |
| 60 | }) |
| 61 | if err != nil { |
| 62 | t.Fatalf("New: %v", err) |
| 63 | } |
| 64 | if got := p.(*client).buildRequest(provider.Request{}).ReasoningEffort; got != "max" { |
| 65 | t.Fatalf("reasoning_effort = %q, want explicitly supported max", got) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestExplicitSupportedEffortsPreserveCustomGenericEffort(t *testing.T) { |
| 70 | const effort = "ultra" |
| 71 | p, err := New(provider.Config{ |
| 72 | Name: "custom-openai", |
| 73 | BaseURL: "https://gateway.example.com/v1", |
| 74 | Model: "custom-reasoning-model", |
| 75 | APIKey: "k", |
| 76 | Extra: map[string]any{ |
| 77 | "effort": effort, |
| 78 | "supported_efforts": []string{"high", effort}, |
| 79 | }, |
| 80 | }) |
| 81 | if err != nil { |
| 82 | t.Fatalf("New custom effort: %v", err) |
| 83 | } |
| 84 | req := p.(*client).buildRequest(provider.Request{}) |
| 85 | if req.ReasoningEffort != effort { |
| 86 | t.Fatalf("reasoning_effort = %q, want %q", req.ReasoningEffort, effort) |
| 87 | } |
| 88 | body, err := json.Marshal(req) |
| 89 | if err != nil { |
| 90 | t.Fatalf("marshal request: %v", err) |
| 91 | } |
| 92 | if !strings.Contains(string(body), `"reasoning_effort":"ultra"`) { |
| 93 | t.Fatalf("custom effort was not preserved on the wire: %s", body) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func TestExplicitSupportedEffortsPreserveCustomDeepSeekEfforts(t *testing.T) { |
| 98 | for _, effort := range []string{"medium", "none"} { |
| 99 | t.Run(effort, func(t *testing.T) { |
| 100 | p, err := New(provider.Config{ |
| 101 | Name: "custom-deepseek", |
| 102 | BaseURL: "https://gateway.example.com/v1", |
| 103 | Model: "deepseek-v4-flash", |
| 104 | APIKey: "k", |
| 105 | Extra: map[string]any{ |
| 106 | "effort": effort, |
| 107 | "reasoning_protocol": "deepseek", |
| 108 | "supported_efforts": []string{"low", "medium", "high", "none"}, |
| 109 | }, |
| 110 | }) |
| 111 | if err != nil { |
| 112 | t.Fatalf("New custom DeepSeek effort: %v", err) |
| 113 | } |
| 114 | req := p.(*client).buildRequest(provider.Request{}) |
| 115 | if req.ReasoningEffort != effort { |
| 116 | t.Fatalf("reasoning_effort = %q, want %q", req.ReasoningEffort, effort) |
| 117 | } |
| 118 | if req.Thinking == nil || req.Thinking.Type != "enabled" { |
| 119 | t.Fatalf("thinking = %#v, want enabled", req.Thinking) |
| 120 | } |
| 121 | body, err := json.Marshal(req) |
| 122 | if err != nil { |
| 123 | t.Fatalf("marshal request: %v", err) |
| 124 | } |
| 125 | if !strings.Contains(string(body), `"reasoning_effort":"`+effort+`"`) { |
| 126 | t.Fatalf("custom DeepSeek effort was not preserved on the wire: %s", body) |
| 127 | } |
| 128 | }) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestExplicitSupportedEffortsRejectUndeclaredEffort(t *testing.T) { |
| 133 | tests := []struct { |
| 134 | name string |
| 135 | extra map[string]any |
| 136 | }{ |
| 137 | { |
| 138 | name: "generic", |
| 139 | extra: map[string]any{ |
| 140 | "effort": "ultra", |
| 141 | "supported_efforts": []string{"high"}, |
| 142 | }, |
| 143 | }, |
| 144 | { |
| 145 | name: "generic-max", |
| 146 | extra: map[string]any{ |
| 147 | "effort": "max", |
| 148 | "supported_efforts": []string{"high"}, |
| 149 | }, |
| 150 | }, |
| 151 | { |
| 152 | name: "deepseek", |
| 153 | extra: map[string]any{ |
| 154 | "effort": "max", |
| 155 | "reasoning_protocol": "deepseek", |
| 156 | "supported_efforts": []string{"medium", "none"}, |
| 157 | }, |
| 158 | }, |
| 159 | } |
| 160 | for _, tc := range tests { |
| 161 | t.Run(tc.name, func(t *testing.T) { |
| 162 | _, err := New(provider.Config{ |
| 163 | Name: "custom-" + tc.name, |
| 164 | BaseURL: "https://gateway.example.com/v1", |
| 165 | Model: "custom-reasoning-model", |
| 166 | APIKey: "k", |
| 167 | Extra: tc.extra, |
| 168 | }) |
| 169 | if err == nil || !strings.Contains(err.Error(), "supported_efforts") { |
| 170 | t.Fatalf("New error = %v, want supported_efforts rejection", err) |
| 171 | } |
| 172 | }) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func TestImplicitOnlySupportedEffortsKeepBuiltInValidation(t *testing.T) { |
| 177 | p, err := New(provider.Config{ |
| 178 | Name: "generic", |
| 179 | BaseURL: "https://gateway.example.com/v1", |
| 180 | Model: "reasoning-model", |
| 181 | APIKey: "k", |
| 182 | Extra: map[string]any{ |
| 183 | "effort": "max", |
| 184 | "supported_efforts": []string{"", " auto ", " "}, |
| 185 | }, |
| 186 | }) |
| 187 | if err != nil { |
| 188 | t.Fatalf("New: %v", err) |
| 189 | } |
| 190 | if got := p.(*client).effort; got != "high" { |
| 191 | t.Fatalf("effort = %q, want built-in max-to-high clamp", got) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | func TestEffortInvalidRejected(t *testing.T) { |
| 196 | _, err := New(provider.Config{ |
| 197 | Name: "p", BaseURL: "https://api.xiaomimimo.com/v1", Model: "m", APIKey: "k", |
| 198 | Extra: map[string]any{"effort": "turbo"}, |
| 199 | }) |
| 200 | if err == nil || !strings.Contains(err.Error(), "low, medium, or high") { |
| 201 | t.Fatalf("expected a low/medium/high validation error, got: %v", err) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | func TestReasoningProtocolOverridesEndpointHeuristic(t *testing.T) { |
| 206 | p, err := New(provider.Config{ |
| 207 | Name: "deepseek-proxy", |
| 208 | BaseURL: "https://proxy.example.com/v1", |
| 209 | Model: "deepseek-v4-flash", |
| 210 | APIKey: "k", |
| 211 | Extra: map[string]any{"reasoning_protocol": "deepseek"}, |
| 212 | }) |
| 213 | if err != nil { |
| 214 | t.Fatalf("New deepseek protocol: %v", err) |
| 215 | } |
| 216 | c := p.(*client) |
| 217 | if !c.deepseek || c.effort != "high" { |
| 218 | t.Fatalf("deepseek=%v effort=%q, want true/high", c.deepseek, c.effort) |
| 219 | } |
| 220 | |
| 221 | p, err = New(provider.Config{ |
| 222 | Name: "deepseek-direct", |
| 223 | BaseURL: "https://api.deepseek.com/v1", |
| 224 | Model: "deepseek-v4-flash", |
| 225 | APIKey: "k", |
| 226 | Extra: map[string]any{ |
| 227 | "reasoning_protocol": "none", |
| 228 | "effort": "max", |
| 229 | "supported_efforts": []string{"low"}, |
| 230 | }, |
| 231 | }) |
| 232 | if err != nil { |
| 233 | t.Fatalf("New none protocol: %v", err) |
| 234 | } |
| 235 | c = p.(*client) |
| 236 | if c.deepseek || c.effort != "" { |
| 237 | t.Fatalf("deepseek=%v effort=%q, want false/empty", c.deepseek, c.effort) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | func TestLongCatThinkingUsesThinkingField(t *testing.T) { |
| 242 | p, err := New(provider.Config{ |
| 243 | Name: "longcat", |
| 244 | BaseURL: "https://api.longcat.chat/openai/v1", |
| 245 | Model: "LongCat-2.0", |
| 246 | APIKey: "k", |
| 247 | Extra: map[string]any{"effort": "disabled", "thinking": "enabled"}, |
| 248 | }) |
| 249 | if err != nil { |
| 250 | t.Fatalf("New longcat: %v", err) |
| 251 | } |
| 252 | c := p.(*client) |
| 253 | if !c.longcat || c.effort != "disabled" { |
| 254 | t.Fatalf("longcat=%v effort=%q, want true/disabled", c.longcat, c.effort) |
| 255 | } |
| 256 | req := c.buildRequest(provider.Request{ |
| 257 | Messages: []provider.Message{{Role: provider.RoleUser, Content: "hi"}}, |
| 258 | }) |
| 259 | b, err := json.Marshal(req) |
| 260 | if err != nil { |
| 261 | t.Fatalf("marshal request: %v", err) |
| 262 | } |
| 263 | var body map[string]any |
| 264 | if err := json.Unmarshal(b, &body); err != nil { |
| 265 | t.Fatalf("decode request: %v", err) |
| 266 | } |
| 267 | thinking, _ := body["thinking"].(map[string]any) |
| 268 | if thinking["type"] != "disabled" { |
| 269 | t.Fatalf("thinking = %#v, want disabled", body["thinking"]) |
| 270 | } |
| 271 | if _, ok := body["reasoning_effort"]; ok { |
| 272 | t.Fatalf("LongCat request must omit reasoning_effort: %s", b) |
| 273 | } |
| 274 | } |
| 275 |