mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-10-20 10:52:08 +00:00
93c62e691a
Six small refactors aggregated from a simplify-review pass over this session's commits: 1. internal/config/persistence_relay.go — LoadRelayConfig had two ApplyEnvOverrides call sites (one inside the not-exist branch, one on the happy path) and a redundant cfg = DefaultConfig() reassignment. Collapse to a single ApplyEnvOverrides call after the load attempt; the file-absent branch already has the default cfg from line 1. 2. internal/relay/config_env.go — swap two strings.TrimSpace(os.Getenv(...)) calls for utils.GetenvTrim, matching the 30+ existing call sites in internal/config/config.go. Trim narrating comments back to the product-behavior sentences that aren't obvious from the code. 3. internal/relay/config_env_test.go — collapse seven near-identical ApplyEnvOverrides scenarios into a single table-driven test (TestApplyEnvOverridesTable). Reduces ~85 lines to ~60 and gives each subcase a named t.Run for clearer failure output. Keeps the nil-config-safe and parseEnvBool tests separate since they exercise different surfaces. 4. .github/workflows/install-sh-smoke.yml — replace the /api/health bash for-loop (sleep 2; curl; loop 30x) with a single curl --retry 30 --retry-delay 2 --retry-connrefused --retry-all-errors invocation. Curl already implements the same polling behaviour natively; the bash loop was 13 lines of redundant scaffolding. 5. scripts/installtests/build_release_assets_test.go — extract the repeated "read file, iterate required substrings, fail on first miss" boilerplate into assertFileContainsAll(t, path, required...). Migrate the four tests I added in this session; existing tests in the file follow the same shape and can adopt the helper incrementally without churning unrelated code in this commit. Also updated the pinned curl string for the /api/health retry change. Contract-neutral: every change preserves identical user-visible behavior. PULSE_ALLOW_CONTRACT_NEUTRAL_COMMIT applied for the canonical-shape-guard bypass; sensitivity, gitleaks, governance-stage, control-plane, status, registry, contract, and pre-commit hooks still run. Verified locally: - go test ./internal/relay/ ./internal/config/ → all pass - go test ./scripts/installtests/ → all pass - ruby -ryaml install-sh-smoke.yml → parses clean
114 lines
3.3 KiB
Go
114 lines
3.3 KiB
Go
package relay
|
|
|
|
import "testing"
|
|
|
|
const fileServerURL = "wss://file.example/ws/instance"
|
|
|
|
func TestApplyEnvOverridesTable(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
envEnabled string
|
|
envURL string
|
|
in Config
|
|
want Config
|
|
}{
|
|
{
|
|
name: "unset leaves config alone",
|
|
envEnabled: "",
|
|
envURL: "",
|
|
in: Config{Enabled: true, ServerURL: fileServerURL},
|
|
want: Config{Enabled: true, ServerURL: fileServerURL},
|
|
},
|
|
{
|
|
name: "enabled true overrides file",
|
|
envEnabled: "true",
|
|
envURL: "",
|
|
in: Config{Enabled: false, ServerURL: DefaultServerURL},
|
|
want: Config{Enabled: true, ServerURL: DefaultServerURL},
|
|
},
|
|
{
|
|
name: "enabled false overrides file-enabled relay",
|
|
envEnabled: "false",
|
|
envURL: "",
|
|
in: Config{Enabled: true, ServerURL: DefaultServerURL},
|
|
want: Config{Enabled: false, ServerURL: DefaultServerURL},
|
|
},
|
|
{
|
|
name: "garbage bool does not override",
|
|
envEnabled: "maybe",
|
|
envURL: "",
|
|
in: Config{Enabled: true, ServerURL: DefaultServerURL},
|
|
want: Config{Enabled: true, ServerURL: DefaultServerURL},
|
|
},
|
|
{
|
|
name: "valid server URL overrides file",
|
|
envEnabled: "",
|
|
envURL: "wss://relay.test.example/ws/instance",
|
|
in: Config{Enabled: true, ServerURL: DefaultServerURL},
|
|
want: Config{Enabled: true, ServerURL: "wss://relay.test.example/ws/instance"},
|
|
},
|
|
{
|
|
name: "invalid server URL keeps file value",
|
|
envEnabled: "",
|
|
envURL: "http://wrong-scheme.example/",
|
|
in: Config{Enabled: true, ServerURL: fileServerURL},
|
|
want: Config{Enabled: true, ServerURL: fileServerURL},
|
|
},
|
|
{
|
|
name: "both overrides apply together",
|
|
envEnabled: "yes",
|
|
envURL: "wss://relay.test.example/ws/instance",
|
|
in: Config{Enabled: false, ServerURL: DefaultServerURL},
|
|
want: Config{Enabled: true, ServerURL: "wss://relay.test.example/ws/instance"},
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Setenv(EnvRelayEnabled, tc.envEnabled)
|
|
t.Setenv(EnvRelayServerURL, tc.envURL)
|
|
|
|
cfg := tc.in
|
|
ApplyEnvOverrides(&cfg)
|
|
|
|
if cfg.Enabled != tc.want.Enabled {
|
|
t.Errorf("Enabled = %v, want %v", cfg.Enabled, tc.want.Enabled)
|
|
}
|
|
if cfg.ServerURL != tc.want.ServerURL {
|
|
t.Errorf("ServerURL = %q, want %q", cfg.ServerURL, tc.want.ServerURL)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyEnvOverridesNilConfigSafe(t *testing.T) {
|
|
t.Setenv(EnvRelayEnabled, "true")
|
|
t.Setenv(EnvRelayServerURL, "wss://relay.test.example/ws/instance")
|
|
|
|
ApplyEnvOverrides(nil) // must not panic
|
|
}
|
|
|
|
func TestParseEnvBoolRecognizedValues(t *testing.T) {
|
|
truthy := []string{"1", "true", "TRUE", "True", "yes", "y", "on", " on "}
|
|
for _, v := range truthy {
|
|
got, ok := parseEnvBool(v)
|
|
if !ok || !got {
|
|
t.Errorf("parseEnvBool(%q) = (%v, %v), want (true, true)", v, got, ok)
|
|
}
|
|
}
|
|
falsy := []string{"0", "false", "FALSE", "no", "n", "off"}
|
|
for _, v := range falsy {
|
|
got, ok := parseEnvBool(v)
|
|
if !ok || got {
|
|
t.Errorf("parseEnvBool(%q) = (%v, %v), want (false, true)", v, got, ok)
|
|
}
|
|
}
|
|
unrecognized := []string{"", " ", "maybe", "2", "enable"}
|
|
for _, v := range unrecognized {
|
|
_, ok := parseEnvBool(v)
|
|
if ok {
|
|
t.Errorf("parseEnvBool(%q) reported ok=true, want false", v)
|
|
}
|
|
}
|
|
}
|