110 lines
2.4 KiB
Go
110 lines
2.4 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoad(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
env map[string]string
|
|
wantErr bool
|
|
errMsg string
|
|
}{
|
|
{
|
|
name: "all required set",
|
|
env: map[string]string{
|
|
"TELEGRAM_BOT_TOKEN": "tg",
|
|
"DEEPSEEK_API_KEY": "ds",
|
|
"BRAVE_API_KEY": "br",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "missing telegram token",
|
|
env: map[string]string{
|
|
"DEEPSEEK_API_KEY": "ds",
|
|
"BRAVE_API_KEY": "br",
|
|
},
|
|
wantErr: true,
|
|
errMsg: "TELEGRAM_BOT_TOKEN is required",
|
|
},
|
|
{
|
|
name: "missing brave key",
|
|
env: map[string]string{
|
|
"TELEGRAM_BOT_TOKEN": "tg",
|
|
"DEEPSEEK_API_KEY": "ds",
|
|
},
|
|
wantErr: true,
|
|
errMsg: "BRAVE_API_KEY is required",
|
|
},
|
|
{
|
|
name: "default postgres conn string",
|
|
env: map[string]string{
|
|
"TELEGRAM_BOT_TOKEN": "tg",
|
|
"DEEPSEEK_API_KEY": "ds",
|
|
"BRAVE_API_KEY": "br",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "override postgres conn string",
|
|
env: map[string]string{
|
|
"TELEGRAM_BOT_TOKEN": "tg",
|
|
"DEEPSEEK_API_KEY": "ds",
|
|
"BRAVE_API_KEY": "br",
|
|
"POSTGRES_CONN_STRING": "pg://custom",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
os.Clearenv()
|
|
for k, v := range tt.env {
|
|
os.Setenv(k, v)
|
|
}
|
|
|
|
cfg, err := Load()
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if tt.errMsg != "" && err.Error() != tt.errMsg {
|
|
t.Errorf("expected error %q, got %q", tt.errMsg, err.Error())
|
|
}
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
expectedTG := tt.env["TELEGRAM_BOT_TOKEN"]
|
|
if cfg.TelegramBotToken != expectedTG {
|
|
t.Errorf("TelegramBotToken: expected %q, got %q", expectedTG, cfg.TelegramBotToken)
|
|
}
|
|
|
|
expectedDS := tt.env["DEEPSEEK_API_KEY"]
|
|
if cfg.DeepSeekAPIKey != expectedDS {
|
|
t.Errorf("DeepSeekAPIKey: expected %q, got %q", expectedDS, cfg.DeepSeekAPIKey)
|
|
}
|
|
|
|
expectedBR := tt.env["BRAVE_API_KEY"]
|
|
if cfg.BraveAPIKey != expectedBR {
|
|
t.Errorf("BraveAPIKey: expected %q, got %q", expectedBR, cfg.BraveAPIKey)
|
|
}
|
|
|
|
expectedPG := tt.env["POSTGRES_CONN_STRING"]
|
|
if expectedPG == "" {
|
|
expectedPG = "postgres://user:password@localhost:5432/db?sslmode=disable"
|
|
}
|
|
if cfg.PostgresConnString != expectedPG {
|
|
t.Errorf("PostgresConnString: expected %q, got %q", expectedPG, cfg.PostgresConnString)
|
|
}
|
|
})
|
|
}
|
|
}
|