waveterm/pkg/aiusechat/usechat_mode_test.go
Copilot 4ec09cb611
Centralize proxy HTTP client creation in aiutil and remove redundant backend tests (#2961)
`makeHTTPClient(proxyURL)` had been duplicated across AI backends with
equivalent behavior. This change consolidates the logic into a single
helper in `aiutil` and updates backends to consume it, then removes
backend-local tests that only re-verified that shared utility behavior.

- **Shared client construction**
- Added `aiutil.MakeHTTPClient(proxyURL string) (*http.Client, error)`
in `pkg/aiusechat/aiutil/aiutil.go`.
- Standardizes proxy parsing and `http.Transport.Proxy` setup in one
place.
- Keeps streaming-safe client semantics (`Timeout: 0`) and existing
invalid proxy URL error behavior.

- **Backend refactor**
  - Removed duplicated client/proxy setup blocks from:
    - `pkg/aiusechat/openaichat/openaichat-backend.go`
    - `pkg/aiusechat/gemini/gemini-backend.go`
    - `pkg/aiusechat/openai/openai-backend.go`
    - `pkg/aiusechat/anthropic/anthropic-backend.go`
  - Replaced with direct calls to the shared helper.

- **Test cleanup**
- Deleted backend tests that only covered basic proxy client creation
and no backend-specific behavior:
    - `pkg/aiusechat/openaichat/openaichat-backend_test.go`
    - `pkg/aiusechat/gemini/gemini-backend_test.go`

```go
httpClient, err := aiutil.MakeHTTPClient(chatOpts.Config.ProxyURL)
if err != nil {
    return nil, nil, nil, err
}
resp, err := httpClient.Do(req)
```

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 We'd love your input! Share your thoughts on Copilot coding agent in
our [2 minute survey](https://gh.io/copilot-coding-agent-survey).

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sawka <2722291+sawka@users.noreply.github.com>
2026-03-02 11:16:50 -08:00

39 lines
1.2 KiB
Go

// Copyright 2026, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package aiusechat
import (
"testing"
"github.com/wavetermdev/waveterm/pkg/aiusechat/uctypes"
"github.com/wavetermdev/waveterm/pkg/wconfig"
)
func TestApplyProviderDefaultsGroq(t *testing.T) {
config := wconfig.AIModeConfigType{
Provider: uctypes.AIProvider_Groq,
}
applyProviderDefaults(&config)
if config.APIType != uctypes.APIType_OpenAIChat {
t.Fatalf("expected API type %q, got %q", uctypes.APIType_OpenAIChat, config.APIType)
}
if config.Endpoint != GroqChatEndpoint {
t.Fatalf("expected endpoint %q, got %q", GroqChatEndpoint, config.Endpoint)
}
if config.APITokenSecretName != GroqAPITokenSecretName {
t.Fatalf("expected API token secret name %q, got %q", GroqAPITokenSecretName, config.APITokenSecretName)
}
}
func TestApplyProviderDefaultsKeepsProxyURL(t *testing.T) {
config := wconfig.AIModeConfigType{
Provider: uctypes.AIProvider_OpenAI,
Model: "gpt-5-mini",
ProxyURL: "http://localhost:8080",
}
applyProviderDefaults(&config)
if config.ProxyURL != "http://localhost:8080" {
t.Fatalf("expected proxy URL to be preserved, got %q", config.ProxyURL)
}
}