Files
run/config/config_test.go
T
2026-08-26 09:56:43 +08:00

94 lines
4.3 KiB
Go

package config
import "testing"
func TestLoadUsesDefaults(t *testing.T) {
t.Setenv("RUN_MODE", "")
t.Setenv("RUN_PLATFORM_URL", "")
cfg := Load()
if cfg.Mode != DefaultMode {
t.Fatalf("expected mode %q, got %q", DefaultMode, cfg.Mode)
}
if cfg.PlatformURL != DefaultPlatformURL {
t.Fatalf("expected platform URL %q, got %q", DefaultPlatformURL, cfg.PlatformURL)
}
if cfg.RunEndpointID != DefaultEndpointID || cfg.DisplayName != DefaultDisplayName || cfg.Version != DefaultVersion {
t.Fatalf("expected worker defaults, got %+v", cfg)
}
if cfg.MaxJobs != 1 || cfg.HeartbeatInterval <= 0 || cfg.PollInterval <= 0 || cfg.RetryBackoff <= 0 {
t.Fatalf("expected positive worker scheduling defaults, got %+v", cfg)
}
}
func TestLoadUsesEnvironment(t *testing.T) {
t.Setenv("RUN_MODE", "worker")
t.Setenv("RUN_PLATFORM_URL", "http://platform.test")
t.Setenv("RUN_ENDPOINT_ID", "run-edge")
t.Setenv("RUN_DISPLAY_NAME", "Edge Run")
t.Setenv("RUN_VERSION", "1.2.3")
t.Setenv("RUN_REGISTRATION_TOKEN", "registration-token")
t.Setenv("RUN_WORKSPACE_ROOT", "/tmp/run-workspace")
t.Setenv("RUN_SPOOL_ROOT", "/tmp/run-spool")
t.Setenv("RUN_MAX_JOBS", "3")
t.Setenv("RUN_HEARTBEAT_INTERVAL_MS", "250")
t.Setenv("RUN_POLL_INTERVAL_MS", "125")
t.Setenv("RUN_RETRY_BACKOFF_MS", "75")
cfg := Load()
if cfg.Mode != "worker" {
t.Fatalf("expected configured mode, got %q", cfg.Mode)
}
if cfg.PlatformURL != "http://platform.test" {
t.Fatalf("expected configured platform URL, got %q", cfg.PlatformURL)
}
if cfg.RunEndpointID != "run-edge" || cfg.DisplayName != "Edge Run" || cfg.Version != "1.2.3" || cfg.RegistrationToken != "registration-token" {
t.Fatalf("expected configured worker identity, got %+v", cfg)
}
if cfg.WorkspaceRoot != "/tmp/run-workspace" || cfg.SpoolRoot != "/tmp/run-spool" || cfg.MaxJobs != 3 {
t.Fatalf("expected configured worker paths/capacity, got %+v", cfg)
}
if cfg.HeartbeatInterval.Milliseconds() != 250 || cfg.PollInterval.Milliseconds() != 125 || cfg.RetryBackoff.Milliseconds() != 75 {
t.Fatalf("expected configured durations, got %+v", cfg)
}
}
func TestLoadUsesBuildDefaultsWithEnvironmentOverride(t *testing.T) {
oldMode, oldPlatformURL, oldRunEndpointID, oldDisplayName := BuildMode, BuildPlatformURL, BuildRunEndpointID, BuildDisplayName
oldRegistrationToken, oldServerInstanceID, oldPluginID := BuildRegistrationToken, BuildServerInstanceID, BuildPluginID
oldComponentKind, oldComponentKey, oldKeyGeneration, oldVersion, oldWorkspaceSeed := BuildComponentKind, BuildComponentKey, BuildKeyGeneration, BuildVersion, BuildWorkspaceSeed
defer func() {
BuildMode, BuildPlatformURL, BuildRunEndpointID, BuildDisplayName = oldMode, oldPlatformURL, oldRunEndpointID, oldDisplayName
BuildRegistrationToken, BuildServerInstanceID, BuildPluginID = oldRegistrationToken, oldServerInstanceID, oldPluginID
BuildComponentKind, BuildComponentKey, BuildKeyGeneration, BuildVersion = oldComponentKind, oldComponentKey, oldKeyGeneration, oldVersion
BuildWorkspaceSeed = oldWorkspaceSeed
}()
BuildMode = "worker"
BuildPlatformURL = "https://scum.npc0.com"
BuildRunEndpointID = "run-server-1"
BuildDisplayName = "Run-server-1"
BuildRegistrationToken = "compiled-run-key"
BuildServerInstanceID = "server-1"
BuildPluginID = "game.scum"
BuildComponentKind = "run"
BuildKeyGeneration = "5"
BuildVersion = "run-dist-1"
BuildWorkspaceSeed = "seed-payload"
for _, key := range []string{"RUN_MODE", "RUN_PLATFORM_URL", "RUN_ENDPOINT_ID", "RUN_DISPLAY_NAME", "RUN_REGISTRATION_TOKEN", "RUN_SERVER_INSTANCE_ID", "RUN_PLUGIN_ID", "RUN_COMPONENT_KIND", "RUN_COMPONENT_KEY", "RUN_KEY_GENERATION", "RUN_VERSION", "RUN_WORKSPACE_SEED"} {
t.Setenv(key, "")
}
cfg := Load()
if cfg.Mode != "worker" || cfg.PlatformURL != "https://scum.npc0.com" || cfg.RunEndpointID != "run-server-1" || cfg.RegistrationToken != "compiled-run-key" || cfg.ServerInstanceID != "server-1" || cfg.PluginID != "game.scum" || cfg.ComponentKind != "run" || cfg.KeyGeneration != 5 || cfg.Version != "run-dist-1" || cfg.WorkspaceSeed != "seed-payload" {
t.Fatalf("expected compiled defaults, got %+v", cfg)
}
t.Setenv("RUN_PLATFORM_URL", "http://127.0.0.1:18080")
t.Setenv("RUN_KEY_GENERATION", "7")
cfg = Load()
if cfg.PlatformURL != "http://127.0.0.1:18080" || cfg.KeyGeneration != 7 {
t.Fatalf("expected environment override, got %+v", cfg)
}
}