Replace local TOML loading with mcdsl/config.Load[Config] which adds METACRYPT_ environment variable overrides. ServerConfig and MCIASConfig now embed their mcdsl counterparts, extending with ExternalURL and ServiceToken respectively. DatabaseConfig and LogConfig replaced with mcdsl types directly. TOML structure is preserved — no config file changes needed. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
97 lines
2.0 KiB
Go
97 lines
2.0 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoadValid(t *testing.T) {
|
|
content := `
|
|
[server]
|
|
listen_addr = ":8443"
|
|
tls_cert = "cert.pem"
|
|
tls_key = "key.pem"
|
|
|
|
[database]
|
|
path = "test.db"
|
|
|
|
[mcias]
|
|
server_url = "https://mcias.example.com"
|
|
`
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "test.toml")
|
|
_ = os.WriteFile(path, []byte(content), 0600)
|
|
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Server.ListenAddr != ":8443" {
|
|
t.Errorf("ListenAddr: got %q", cfg.Server.ListenAddr)
|
|
}
|
|
if cfg.Seal.Argon2Time != 3 {
|
|
t.Errorf("Argon2Time default: got %d, want 3", cfg.Seal.Argon2Time)
|
|
}
|
|
if cfg.Seal.Argon2Memory != 128*1024 {
|
|
t.Errorf("Argon2Memory default: got %d", cfg.Seal.Argon2Memory)
|
|
}
|
|
if cfg.Log.Level != "info" {
|
|
t.Errorf("Log.Level default: got %q", cfg.Log.Level)
|
|
}
|
|
}
|
|
|
|
func TestLoadEnvOverride(t *testing.T) {
|
|
content := `
|
|
[server]
|
|
listen_addr = ":8443"
|
|
tls_cert = "cert.pem"
|
|
tls_key = "key.pem"
|
|
|
|
[database]
|
|
path = "test.db"
|
|
|
|
[mcias]
|
|
server_url = "https://mcias.example.com"
|
|
`
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "test.toml")
|
|
_ = os.WriteFile(path, []byte(content), 0600)
|
|
|
|
t.Setenv("METACRYPT_SERVER_LISTEN_ADDR", ":9999")
|
|
t.Setenv("METACRYPT_MCIAS_SERVER_URL", "https://override.example.com")
|
|
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Server.ListenAddr != ":9999" {
|
|
t.Errorf("ListenAddr env override: got %q, want %q", cfg.Server.ListenAddr, ":9999")
|
|
}
|
|
if cfg.MCIAS.ServerURL != "https://override.example.com" {
|
|
t.Errorf("ServerURL env override: got %q", cfg.MCIAS.ServerURL)
|
|
}
|
|
}
|
|
|
|
func TestLoadMissingRequired(t *testing.T) {
|
|
content := `
|
|
[server]
|
|
listen_addr = ":8443"
|
|
`
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "test.toml")
|
|
_ = os.WriteFile(path, []byte(content), 0600)
|
|
|
|
_, err := Load(path)
|
|
if err == nil {
|
|
t.Fatal("expected error for missing required fields")
|
|
}
|
|
}
|
|
|
|
func TestLoadMissingFile(t *testing.T) {
|
|
_, err := Load("/nonexistent/path.toml")
|
|
if err == nil {
|
|
t.Fatal("expected error for missing file")
|
|
}
|
|
}
|