Files
GL/infrastructure/config/config_test.go
T

67 lines
1.8 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestLoadUsesDefaultsAndOverrides(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "gl.toml")
contents := []byte("[grpc]\nport = 0\nshutdown-timeout = \"3s\"\n")
if err := os.WriteFile(path, contents, 0o600); err != nil {
t.Fatal(err)
}
cfg, err := Load(path)
if err != nil {
t.Fatal(err)
}
if cfg.GRPC.Port != 0 || cfg.GRPC.ShutdownTimeout != 3*time.Second {
t.Fatalf("unexpected grpc config: %+v", cfg.GRPC)
}
if cfg.Database.Name != "gl_db" || cfg.Database.Port != 5432 {
t.Fatalf("unexpected database defaults: %+v", cfg.Database)
}
}
func TestLoadDashboardUsesHTTPDefaultsAndOverrides(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "dashboard.toml")
contents := []byte("[http]\nport = 0\nshutdown-timeout = \"3s\"\n")
if err := os.WriteFile(path, contents, 0o600); err != nil {
t.Fatal(err)
}
cfg, err := LoadDashboard(path)
if err != nil {
t.Fatal(err)
}
if cfg.HTTP.Port != 0 || cfg.HTTP.ShutdownTimeout != 3*time.Second || cfg.HTTP.ReadHeaderTimeout != 5*time.Second {
t.Fatalf("unexpected http config: %+v", cfg.HTTP)
}
if cfg.Database.Name != "gl_db" || cfg.Database.Port != 5432 {
t.Fatalf("unexpected database defaults: %+v", cfg.Database)
}
}
func TestLoadRejectsInvalidConfiguration(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "gl.toml")
if err := os.WriteFile(path, []byte("[grpc]\nshutdown-timeout = \"0s\"\n"), 0o600); err != nil {
t.Fatal(err)
}
if _, err := Load(path); err == nil {
t.Fatal("expected validation error")
}
}
func TestLoadReturnsMissingFileError(t *testing.T) {
if _, err := Load(filepath.Join(t.TempDir(), "missing.toml")); err == nil {
t.Fatal("expected missing file error")
}
}