beads/cmd/bd/config_get_backup_enabled_test.go
Jim Wordelman 9973e96289
fix(test): viper singleton state leak across cmd/bd test runs (#5194)
snapshotBootstrapEnv restores via t.Cleanup (first-registered = last-run, so the snapshot has the final word over later t.Setenv cleanups); pin BEADS_DIR in initConfigForTest/ensureCleanGlobalState; clear BD_BACKUP_ENABLED/BEADS_BACKUP_ENABLED at TestMain; stop the test-only config walk at the module root.

Fixes quad341/be-yjp4z.
2026-07-31 12:48:55 -07:00

105 lines
3.1 KiB
Go

package main
import (
"os"
"strings"
"testing"
"github.com/steveyegge/beads/internal/config"
)
type backupEnabledEffectiveValueCase struct {
name string
envVal string // "\x00" = unset
hasRemote bool
sharedServer bool
wantPrefix string // leading "true "/"false "
wantContains string // source annotation substring
}
// TestConfigGetBackupEnabled_EffectiveValue pins wy-zrmqr fix #3: `bd config
// get backup.enabled` must report the EFFECTIVE value (what isBackupAutoEnabled
// actually returns) plus its source, not the raw stored value. Before the fix
// it printed "false"/"not set" even while auto-backup was running via
// primeHasGitRemote — the mismatch that hid the storm from operators.
func TestConfigGetBackupEnabled_EffectiveValue(t *testing.T) {
runConfigGetBackupEnabledEffectiveValueCases(t, []backupEnabledEffectiveValueCase{
{
name: "unset + remote + sql-server → off (server mode)",
envVal: "\x00",
hasRemote: true,
sharedServer: true,
wantPrefix: "false ",
wantContains: "sql-server mode",
},
{
name: "explicit true + sql-server → on (env var)",
envVal: "true",
hasRemote: false,
sharedServer: true,
wantPrefix: "true ",
wantContains: "env var",
},
{
name: "explicit false → off (env var)",
envVal: "false",
hasRemote: true,
wantPrefix: "false ",
wantContains: "env var",
},
})
}
func runConfigGetBackupEnabledEffectiveValueCases(t *testing.T, tests []backupEnabledEffectiveValueCase) {
t.Helper()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Isolate CWD and BEADS_DIR before config.Initialize() below.
// Without this, a leaked BEADS_DIR (or cwd left inside the repo
// tree by an earlier no-DB command in the same test binary) lets
// Initialize() load a real ambient config.yaml with an explicit
// backup.enabled value, which wins over the computed default
// these cases assert on (be-yjp4z).
t.Chdir(t.TempDir())
t.Setenv("BEADS_DIR", "")
t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1")
orig := primeHasGitRemote
primeHasGitRemote = func() bool { return tt.hasRemote }
t.Cleanup(func() { primeHasGitRemote = orig })
if tt.sharedServer {
t.Setenv("BEADS_DOLT_SHARED_SERVER", "1")
} else {
os.Unsetenv("BEADS_DOLT_SHARED_SERVER")
t.Cleanup(func() { os.Unsetenv("BEADS_DOLT_SHARED_SERVER") })
}
if tt.envVal == "\x00" {
os.Unsetenv("BD_BACKUP_ENABLED")
t.Cleanup(func() { os.Unsetenv("BD_BACKUP_ENABLED") })
} else {
t.Setenv("BD_BACKUP_ENABLED", tt.envVal)
}
config.ResetForTesting()
t.Cleanup(config.ResetForTesting)
if err := config.Initialize(); err != nil {
t.Fatalf("config.Initialize: %v", err)
}
oldJSON := jsonOutput
jsonOutput = false
t.Cleanup(func() { jsonOutput = oldJSON })
out := captureStdout(t, runConfigGetBackupEnabled)
if !strings.HasPrefix(out, tt.wantPrefix) {
t.Errorf("output %q: want prefix %q", out, tt.wantPrefix)
}
if !strings.Contains(out, tt.wantContains) {
t.Errorf("output %q: want to contain %q", out, tt.wantContains)
}
})
}
}