Files
odidere/internal/llm/client_test.go
T
2026-10-08 01:01:35 +00:00

168 lines
3.2 KiB
Go

package llm
import (
"log/slog"
"testing"
"time"
)
func TestConfigValidate(t *testing.T) {
tests := []struct {
name string
cfg Config
wantErr bool
}{
{
name: "empty config",
cfg: Config{},
wantErr: true,
},
{
name: "missing URL",
cfg: Config{},
wantErr: true,
},
{
name: "negative timeout",
cfg: Config{
URL: "http://localhost:8080",
Timeout: -time.Second,
},
wantErr: true,
},
{
name: "valid minimal config",
cfg: Config{
URL: "http://localhost:8080",
},
wantErr: false,
},
{
name: "valid config with timeout",
cfg: Config{
URL: "http://localhost:8080",
Timeout: 15 * time.Minute,
},
wantErr: false,
},
{
name: "valid config with complex timeout",
cfg: Config{
URL: "http://localhost:8080",
Timeout: time.Hour + 30*time.Minute + 45*time.Second,
},
wantErr: false,
},
{
name: "valid full config",
cfg: Config{
Key: "sk-test-key",
Timeout: 30 * time.Minute,
URL: "http://localhost:8080",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.cfg.Validate()
if (err != nil) != tt.wantErr {
t.Errorf(
"Validate() error = %v, wantErr %v",
err, tt.wantErr,
)
}
})
}
}
func TestNewClient(t *testing.T) {
log := slog.Default()
tests := []struct {
name string
cfg Config
log *slog.Logger
wantErr string
}{
{
name: "invalid config precedes nil logger",
cfg: Config{},
wantErr: "invalid config: missing URL",
},
{
name: "negative timeout precedes nil logger",
cfg: Config{
URL: "http://localhost:8080",
Timeout: -time.Second,
},
wantErr: "invalid config: invalid timeout -1s: must not be negative",
},
{
name: "nil logger",
cfg: Config{
URL: "http://localhost:8080",
},
wantErr: "missing logger",
},
{
name: "valid config without timeout",
cfg: Config{
URL: "http://localhost:8080",
},
log: log,
},
{
name: "valid config with timeout",
cfg: Config{
URL: "http://localhost:8080",
Timeout: 10 * time.Minute,
},
log: log,
},
{
name: "valid config with all fields",
cfg: Config{
Key: "test-key",
Timeout: 5 * time.Minute,
URL: "http://localhost:8080",
},
log: log,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := NewClient(tt.cfg, tt.log)
if tt.wantErr != "" {
if err == nil {
t.Fatalf("NewClient() error = nil, want %q", tt.wantErr)
}
if err.Error() != tt.wantErr {
t.Errorf(
"NewClient() error = %q, want %q",
err, tt.wantErr,
)
}
if client != nil {
t.Errorf("NewClient() client = %#v, want nil", client)
}
return
}
if err != nil {
t.Fatalf("NewClient() error = %v, want nil", err)
}
if client == nil {
t.Fatal("NewClient() client = nil, want non-nil")
}
wantTimeout := tt.cfg.Timeout
if wantTimeout == 0 {
wantTimeout = 5 * time.Minute
}
if client.timeout != wantTimeout {
t.Errorf("timeout = %v, want %v", client.timeout, wantTimeout)
}
})
}
}