|
| 1 | +package providers |
| 2 | + |
| 3 | +// Coverage for OpenAIProvider.WithExtraHeaders — the mechanism Kimi Coding |
| 4 | +// uses to send a fixed User-Agent on every request. |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +// TestOpenAIProvider_ExtraHeaders_AppliedOnHTTPRequest verifies that headers |
| 15 | +// set via WithExtraHeaders reach the actual outgoing request — not just the |
| 16 | +// adapter's header map. |
| 17 | +func TestOpenAIProvider_ExtraHeaders_AppliedOnHTTPRequest(t *testing.T) { |
| 18 | + var gotUserAgent, gotXTrace, gotAuth string |
| 19 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 20 | + gotUserAgent = r.Header.Get("User-Agent") |
| 21 | + gotXTrace = r.Header.Get("X-Trace-Id") |
| 22 | + gotAuth = r.Header.Get("Authorization") |
| 23 | + // Minimal non-stream response so doRequest returns cleanly. |
| 24 | + w.Header().Set("Content-Type", "application/json") |
| 25 | + _, _ = w.Write([]byte(`{"id":"x","choices":[{"index":0,"message":{"role":"assistant","content":""},"finish_reason":"stop"}]}`)) |
| 26 | + })) |
| 27 | + defer srv.Close() |
| 28 | + |
| 29 | + p := NewOpenAIProvider("kimi-coding-test", "sk-fake", srv.URL, "kimi-k2-turbo-preview"). |
| 30 | + WithExtraHeaders(map[string]string{ |
| 31 | + "User-Agent": "claude-code/0.1.0", |
| 32 | + "X-Trace-Id": "abc", |
| 33 | + }) |
| 34 | + |
| 35 | + body, err := p.doRequest(context.Background(), map[string]any{ |
| 36 | + "model": "kimi-k2-turbo-preview", |
| 37 | + "messages": []map[string]string{{"role": "user", "content": "hi"}}, |
| 38 | + }) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("doRequest: %v", err) |
| 41 | + } |
| 42 | + _, _ = io.Copy(io.Discard, body) |
| 43 | + _ = body.Close() |
| 44 | + |
| 45 | + if gotUserAgent != "claude-code/0.1.0" { |
| 46 | + t.Errorf("User-Agent = %q, want %q", gotUserAgent, "claude-code/0.1.0") |
| 47 | + } |
| 48 | + if gotXTrace != "abc" { |
| 49 | + t.Errorf("X-Trace-Id = %q, want %q", gotXTrace, "abc") |
| 50 | + } |
| 51 | + // Standard Bearer auth must still apply alongside extra headers. |
| 52 | + if gotAuth != "Bearer sk-fake" { |
| 53 | + t.Errorf("Authorization = %q, want %q", gotAuth, "Bearer sk-fake") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// TestOpenAIAdapter_ExtraHeaders_MirroredInToRequest verifies the adapter path |
| 58 | +// emits the same extra headers as the direct doRequest path — important |
| 59 | +// because some call sites use adapter.ToRequest to produce headers separately. |
| 60 | +func TestOpenAIAdapter_ExtraHeaders_MirroredInToRequest(t *testing.T) { |
| 61 | + p := NewOpenAIProvider("kimi-coding-test", "sk-fake", "https://api.kimi.com/coding/v1", "kimi-k2-turbo-preview"). |
| 62 | + WithExtraHeaders(map[string]string{ |
| 63 | + "User-Agent": "claude-code/0.1.0", |
| 64 | + }) |
| 65 | + a := &OpenAIAdapter{provider: p} |
| 66 | + |
| 67 | + _, headers, err := a.ToRequest(ChatRequest{ |
| 68 | + Messages: []Message{{Role: "user", Content: "hi"}}, |
| 69 | + }) |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("ToRequest: %v", err) |
| 72 | + } |
| 73 | + if got := headers.Get("User-Agent"); got != "claude-code/0.1.0" { |
| 74 | + t.Errorf("adapter User-Agent = %q, want claude-code/0.1.0", got) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// TestOpenAIProvider_ExtraHeaders_NoOpWhenEmpty makes sure the |
| 79 | +// WithExtraHeaders(nil) / WithExtraHeaders({}) calls leave the provider's |
| 80 | +// state alone — protects against accidental nil-map allocations in callers |
| 81 | +// that pass through optional config. |
| 82 | +func TestOpenAIProvider_ExtraHeaders_NoOpWhenEmpty(t *testing.T) { |
| 83 | + p := NewOpenAIProvider("x", "k", "https://example.com", "m"). |
| 84 | + WithExtraHeaders(nil). |
| 85 | + WithExtraHeaders(map[string]string{}) |
| 86 | + |
| 87 | + if got := p.ExtraHeaders(); got != nil { |
| 88 | + t.Errorf("ExtraHeaders after empty calls = %v, want nil", got) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// TestKimiCoding_TemperatureSkipped reproduces the upstream rejection |
| 93 | +// `invalid temperature: only 1 is allowed for this model`. When the provider |
| 94 | +// is kimi_coding, the request body must omit temperature entirely so the |
| 95 | +// upstream applies its mandatory default. |
| 96 | +func TestKimiCoding_TemperatureSkipped(t *testing.T) { |
| 97 | + p := NewOpenAIProvider("kimi-coding", "sk-fake", "https://api.kimi.com/coding/v1", "kimi-k2-turbo-preview"). |
| 98 | + WithProviderType("kimi_coding") |
| 99 | + |
| 100 | + body := p.buildRequestBody("kimi-k2-turbo-preview", ChatRequest{ |
| 101 | + Messages: []Message{{Role: "user", Content: "hi"}}, |
| 102 | + Options: map[string]any{OptTemperature: 0.7}, |
| 103 | + }, true) |
| 104 | + |
| 105 | + if _, present := body["temperature"]; present { |
| 106 | + t.Errorf("temperature must not be sent to kimi_coding; got body[temperature]=%v", body["temperature"]) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// TestKimiCoding_TemperatureSentForOtherProviders is the negative control — |
| 111 | +// without provider_type=kimi_coding, a temperature option still flows through. |
| 112 | +func TestKimiCoding_TemperatureSentForOtherProviders(t *testing.T) { |
| 113 | + p := NewOpenAIProvider("openai", "sk-fake", "https://api.openai.com/v1", "gpt-4o-mini") |
| 114 | + |
| 115 | + body := p.buildRequestBody("gpt-4o-mini", ChatRequest{ |
| 116 | + Messages: []Message{{Role: "user", Content: "hi"}}, |
| 117 | + Options: map[string]any{OptTemperature: 0.7}, |
| 118 | + }, true) |
| 119 | + |
| 120 | + got, ok := body["temperature"] |
| 121 | + if !ok { |
| 122 | + t.Fatal("temperature must be sent for non-kimi providers") |
| 123 | + } |
| 124 | + if got != 0.7 { |
| 125 | + t.Errorf("temperature = %v, want 0.7", got) |
| 126 | + } |
| 127 | +} |
0 commit comments