169 lines
3.0 KiB
Go
169 lines
3.0 KiB
Go
package llm
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestConfigValidate(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
cfg Config
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "empty config",
|
|
cfg: Config{},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "missing model",
|
|
cfg: Config{
|
|
URL: "http://localhost:8080",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "missing URL",
|
|
cfg: Config{
|
|
Model: "test-model",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "invalid timeout",
|
|
cfg: Config{
|
|
Model: "test-model",
|
|
URL: "http://localhost:8080",
|
|
Timeout: "not-a-duration",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "valid minimal config",
|
|
cfg: Config{
|
|
Model: "test-model",
|
|
URL: "http://localhost:8080",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "valid config with timeout",
|
|
cfg: Config{
|
|
Model: "test-model",
|
|
URL: "http://localhost:8080",
|
|
Timeout: "15m",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "valid config with complex timeout",
|
|
cfg: Config{
|
|
Model: "test-model",
|
|
URL: "http://localhost:8080",
|
|
Timeout: "1h30m45s",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "valid full config",
|
|
cfg: Config{
|
|
Key: "sk-test-key",
|
|
Model: "test-model",
|
|
SystemPrompt: "You are a helpful assistant.",
|
|
Timeout: "30m",
|
|
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) {
|
|
tests := []struct {
|
|
name string
|
|
cfg Config
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "invalid config",
|
|
cfg: Config{},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "valid config without timeout",
|
|
cfg: Config{
|
|
Model: "test-model",
|
|
URL: "http://localhost:8080",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "valid config with timeout",
|
|
cfg: Config{
|
|
Model: "test-model",
|
|
URL: "http://localhost:8080",
|
|
Timeout: "10m",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "valid config with all fields",
|
|
cfg: Config{
|
|
Key: "test-key",
|
|
Model: "test-model",
|
|
SystemPrompt: "Test prompt",
|
|
Timeout: "5m",
|
|
URL: "http://localhost:8080",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
client, err := NewClient(tt.cfg, nil, nil)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf(
|
|
"NewClient() error = %v, wantErr %v",
|
|
err, tt.wantErr,
|
|
)
|
|
return
|
|
}
|
|
if !tt.wantErr && client == nil {
|
|
t.Error("NewClient() returned nil client")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestClientDefaultModel(t *testing.T) {
|
|
cfg := Config{
|
|
Model: "my-custom-model",
|
|
URL: "http://localhost:8080",
|
|
}
|
|
|
|
client, err := NewClient(cfg, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewClient() error = %v", err)
|
|
}
|
|
|
|
if got := client.DefaultModel(); got != "my-custom-model" {
|
|
t.Errorf(
|
|
"DefaultModel() = %q, want %q",
|
|
got, "my-custom-model",
|
|
)
|
|
}
|
|
}
|