142 lines
3.7 KiB
Go
142 lines
3.7 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
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",
|
|
"POSTGRES_CONN_STRING": "pg://localhost/db",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "all fields including email",
|
|
env: map[string]string{
|
|
"TELEGRAM_BOT_TOKEN": "tg",
|
|
"DEEPSEEK_API_KEY": "ds",
|
|
"BRAVE_API_KEY": "br",
|
|
"POSTGRES_CONN_STRING": "pg://localhost/db",
|
|
"TELEGRAM_PROXY": "socks5://localhost:1080",
|
|
"EMAIL_SMTP_HOST": "mail.chx.su",
|
|
"EMAIL_SMTP_PORT": "465",
|
|
"EMAIL_IMAP_HOST": "mail.chx.su",
|
|
"EMAIL_IMAP_PORT": "993",
|
|
"EMAIL_USERNAME": "jules@chx.su",
|
|
"EMAIL_PASSWORD": "secret",
|
|
"EMAIL_FROM_ADDR": "jules@chx.su",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "email fields with defaults for ports",
|
|
env: map[string]string{
|
|
"TELEGRAM_BOT_TOKEN": "tg",
|
|
"DEEPSEEK_API_KEY": "ds",
|
|
"BRAVE_API_KEY": "br",
|
|
"POSTGRES_CONN_STRING": "pg://localhost/db",
|
|
"EMAIL_USERNAME": "jules@chx.su",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "missing telegram token",
|
|
env: map[string]string{
|
|
"DEEPSEEK_API_KEY": "ds",
|
|
"BRAVE_API_KEY": "br",
|
|
"POSTGRES_CONN_STRING": "pg://localhost/db",
|
|
},
|
|
wantErr: true,
|
|
errMsg: "TELEGRAM_BOT_TOKEN is required",
|
|
},
|
|
{
|
|
name: "missing deepseek key",
|
|
env: map[string]string{
|
|
"TELEGRAM_BOT_TOKEN": "tg",
|
|
"BRAVE_API_KEY": "br",
|
|
"POSTGRES_CONN_STRING": "pg://localhost/db",
|
|
},
|
|
wantErr: true,
|
|
errMsg: "DEEPSEEK_API_KEY is required",
|
|
},
|
|
{
|
|
name: "missing brave key",
|
|
env: map[string]string{
|
|
"TELEGRAM_BOT_TOKEN": "tg",
|
|
"DEEPSEEK_API_KEY": "ds",
|
|
"POSTGRES_CONN_STRING": "pg://localhost/db",
|
|
},
|
|
wantErr: true,
|
|
errMsg: "BRAVE_API_KEY is required",
|
|
},
|
|
{
|
|
name: "missing postgres conn string",
|
|
env: map[string]string{
|
|
"TELEGRAM_BOT_TOKEN": "tg",
|
|
"DEEPSEEK_API_KEY": "ds",
|
|
"BRAVE_API_KEY": "br",
|
|
},
|
|
wantErr: true,
|
|
errMsg: "POSTGRES_CONN_STRING is required",
|
|
},
|
|
}
|
|
|
|
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 {
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), tt.errMsg)
|
|
return
|
|
}
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.env["TELEGRAM_BOT_TOKEN"], cfg.TelegramBotToken)
|
|
assert.Equal(t, tt.env["DEEPSEEK_API_KEY"], cfg.DeepSeekAPIKey)
|
|
assert.Equal(t, tt.env["BRAVE_API_KEY"], cfg.BraveAPIKey)
|
|
assert.Equal(t, tt.env["POSTGRES_CONN_STRING"], cfg.PostgresConnString)
|
|
assert.Equal(t, tt.env["TELEGRAM_PROXY"], cfg.TelegramProxy)
|
|
|
|
// Email fields
|
|
assert.Equal(t, tt.env["EMAIL_SMTP_HOST"], cfg.EmailSMTPHost)
|
|
assert.Equal(t, tt.env["EMAIL_IMAP_HOST"], cfg.EmailIMAPHost)
|
|
assert.Equal(t, tt.env["EMAIL_USERNAME"], cfg.EmailUsername)
|
|
assert.Equal(t, tt.env["EMAIL_PASSWORD"], cfg.EmailPassword)
|
|
assert.Equal(t, tt.env["EMAIL_FROM_ADDR"], cfg.EmailFromAddr)
|
|
|
|
// Port defaults
|
|
expectedSMTPPort := tt.env["EMAIL_SMTP_PORT"]
|
|
if expectedSMTPPort == "" {
|
|
expectedSMTPPort = "465"
|
|
}
|
|
assert.Equal(t, expectedSMTPPort, cfg.EmailSMTPPort)
|
|
|
|
expectedIMAPPort := tt.env["EMAIL_IMAP_PORT"]
|
|
if expectedIMAPPort == "" {
|
|
expectedIMAPPort = "993"
|
|
}
|
|
assert.Equal(t, expectedIMAPPort, cfg.EmailIMAPPort)
|
|
})
|
|
}
|
|
}
|