Files
odidere/internal/llm/client_test.go
T

152 lines
2.7 KiB
Go
Raw Normal View History

2026-02-13 15:02:30 +00:00
package llm
import (
2026-09-20 12:49:42 +00:00
"log/slog"
2026-02-13 15:02:30 +00:00
"testing"
)
func TestConfigValidate(t *testing.T) {
tests := []struct {
name string
cfg Config
wantErr bool
}{
{
name: "empty config",
cfg: Config{},
wantErr: true,
},
{
2026-06-19 14:31:41 +00:00
name: "missing URL",
cfg: Config{},
2026-02-13 15:02:30 +00:00
wantErr: true,
},
{
name: "invalid timeout",
cfg: Config{
URL: "http://localhost:8080",
Timeout: "not-a-duration",
},
wantErr: true,
},
{
name: "valid minimal config",
cfg: Config{
2026-06-19 14:31:41 +00:00
URL: "http://localhost:8080",
2026-02-13 15:02:30 +00:00
},
wantErr: false,
},
{
name: "valid config with timeout",
cfg: Config{
URL: "http://localhost:8080",
Timeout: "15m",
},
wantErr: false,
},
{
name: "valid config with complex timeout",
cfg: Config{
URL: "http://localhost:8080",
Timeout: "1h30m45s",
},
wantErr: false,
},
{
name: "valid full config",
cfg: Config{
2026-09-20 12:49:42 +00:00
Key: "sk-test-key",
Timeout: "30m",
URL: "http://localhost:8080",
2026-02-13 15:02:30 +00:00
},
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) {
2026-09-20 12:49:42 +00:00
log := slog.Default()
2026-02-13 15:02:30 +00:00
tests := []struct {
name string
cfg Config
2026-09-20 12:49:42 +00:00
log *slog.Logger
wantErr string
2026-02-13 15:02:30 +00:00
}{
{
2026-09-20 12:49:42 +00:00
name: "invalid config precedes nil logger",
2026-02-13 15:02:30 +00:00
cfg: Config{},
2026-09-20 12:49:42 +00:00
wantErr: "invalid config: missing URL",
},
{
name: "nil logger",
cfg: Config{
URL: "http://localhost:8080",
},
wantErr: "missing logger",
2026-02-13 15:02:30 +00:00
},
{
name: "valid config without timeout",
cfg: Config{
2026-06-19 14:31:41 +00:00
URL: "http://localhost:8080",
2026-02-13 15:02:30 +00:00
},
2026-09-20 12:49:42 +00:00
log: log,
2026-02-13 15:02:30 +00:00
},
{
name: "valid config with timeout",
cfg: Config{
URL: "http://localhost:8080",
Timeout: "10m",
},
2026-09-20 12:49:42 +00:00
log: log,
2026-02-13 15:02:30 +00:00
},
{
name: "valid config with all fields",
cfg: Config{
2026-09-20 12:49:42 +00:00
Key: "test-key",
Timeout: "5m",
URL: "http://localhost:8080",
2026-02-13 15:02:30 +00:00
},
2026-09-20 12:49:42 +00:00
log: log,
2026-02-13 15:02:30 +00:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2026-09-20 12:49:42 +00:00
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)
}
2026-02-13 15:02:30 +00:00
return
}
2026-09-20 12:49:42 +00:00
if err != nil {
t.Fatalf("NewClient() error = %v, want nil", err)
}
if client == nil {
t.Error("NewClient() client = nil, want non-nil")
2026-02-13 15:02:30 +00:00
}
})
}
}