Files
meta-core/test/config_test.go
T
2026-10-19 20:51:00 +00:00

85 lines
2.0 KiB
Go

package test
import (
"os"
"testing"
"github.com/metazla/meta-core/internal/config"
)
func TestConfigDefaults(t *testing.T) {
// Clear environment
os.Clearenv()
cfg := config.Load()
// Check defaults
if cfg.MetaCorePath != "/meta-core" {
t.Errorf("Expected MetaCorePath '/meta-core', got '%s'", cfg.MetaCorePath)
}
if cfg.FilesPath != "/files" {
t.Errorf("Expected FilesPath '/files', got '%s'", cfg.FilesPath)
}
if cfg.ServiceName != "meta-core" {
t.Errorf("Expected ServiceName 'meta-core', got '%s'", cfg.ServiceName)
}
if cfg.HTTPPort != 9000 {
t.Errorf("Expected HTTPPort 9000, got %d", cfg.HTTPPort)
}
if cfg.DBPath() != cfg.MetaCorePath+"/db/meta-core.db" {
t.Errorf("Expected DBPath under MetaCorePath, got %q", cfg.DBPath())
}
}
func TestConfigFromEnv(t *testing.T) {
// Set environment variables
os.Setenv("META_CORE_PATH", "/custom/meta-core")
os.Setenv("FILES_PATH", "/custom/files")
os.Setenv("SERVICE_NAME", "test-service")
os.Setenv("META_CORE_HTTP_PORT", "8080")
os.Setenv("META_CORE_DB_PATH", "/custom/db.sqlite")
defer os.Clearenv()
cfg := config.Load()
if cfg.MetaCorePath != "/custom/meta-core" {
t.Errorf("Expected MetaCorePath '/custom/meta-core', got '%s'", cfg.MetaCorePath)
}
if cfg.FilesPath != "/custom/files" {
t.Errorf("Expected FilesPath '/custom/files', got '%s'", cfg.FilesPath)
}
if cfg.ServiceName != "test-service" {
t.Errorf("Expected ServiceName 'test-service', got '%s'", cfg.ServiceName)
}
if cfg.HTTPPort != 8080 {
t.Errorf("Expected HTTPPort 8080, got %d", cfg.HTTPPort)
}
if cfg.DBPath() != "/custom/db.sqlite" {
t.Errorf("Expected DBPath '/custom/db.sqlite', got %q", cfg.DBPath())
}
}
func TestConfigPaths(t *testing.T) {
os.Setenv("META_CORE_PATH", "/test")
defer os.Clearenv()
cfg := config.Load()
if cfg.LockFilePath() != "/test/locks/kv-leader.lock" {
t.Errorf("Unexpected LockFilePath: %s", cfg.LockFilePath())
}
if cfg.DBPath() != "/test/db/meta-core.db" {
t.Errorf("Unexpected DBPath: %s", cfg.DBPath())
}
}