beads/cmd/bd/context_binding_test.go
Harry Miller 06b3320a67
test(cmd/bd): stop three tests depending on the developer's actor env (#6560) (#6706)
TestPrepareSelectedCommandContext_RebindsTargetConfig and
_DoesNotMergeCallerConfigForUnsetKeys assert that a config.yaml actor is
rebound, but BEADS_ACTOR outranks config.yaml on purpose
(resolveConfiguredActor, the GH#4645 fix), and so does BD_ACTOR through
viper's BD-prefixed AutomaticEnv. Either one exported in a shell made the
assertion unsatisfiable. A new clearActorEnv helper blanks both, following
the t.Setenv("X", "") idiom these tests already use for the Dolt server env.

A full cmd/bd run with both variables set found a third:
TestCollectViperEntriesWithEnvOverride sets BD_ACTOR and expects it as the
reported source, but an exported BEADS_ACTOR wins. It now blanks
BEADS_ACTOR.

CI runners set neither variable, so all three only failed locally. No
product code changes.

Agent-Signature: claude-code-opus-5-5-unknown-reasoning on behalf of Harry Miller

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 06:01:42 -07:00

202 lines
6 KiB
Go

package main
import (
"os"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/doltserver"
)
func writeTestConfigYAML(t *testing.T, beadsDir, contents string) {
t.Helper()
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
t.Fatalf("mkdir beads dir: %v", err)
}
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(contents), 0o600); err != nil {
t.Fatalf("write config.yaml: %v", err)
}
}
type flagSnapshot struct {
value string
changed bool
}
func snapshotRootFlagState() map[string]flagSnapshot {
state := map[string]flagSnapshot{}
for _, name := range []string{"db", "json", "format", "readonly", "actor", "dolt-auto-commit"} {
flag := rootCmd.PersistentFlags().Lookup(name)
if flag == nil {
continue
}
state[name] = flagSnapshot{value: flag.Value.String(), changed: flag.Changed}
}
return state
}
func restoreRootFlagState(t *testing.T, state map[string]flagSnapshot) {
t.Helper()
for name, snapshot := range state {
flag := rootCmd.PersistentFlags().Lookup(name)
if flag == nil {
continue
}
if err := flag.Value.Set(snapshot.value); err != nil {
t.Fatalf("restore %s flag: %v", name, err)
}
flag.Changed = snapshot.changed
}
}
// clearActorEnv blanks the two environment variables that outrank a
// config.yaml actor, so a test asserting the config value is rebound does not
// depend on the developer's shell (GH#6560). BEADS_ACTOR outranks it
// deliberately: resolveConfiguredActor checks the env first, which is the
// GH#4645 fix. BD_ACTOR does too, because viper's BD-prefixed AutomaticEnv
// reads it as the actor key ahead of the file. CI runners set neither, which is
// why these tests only ever failed locally.
func clearActorEnv(t *testing.T) {
t.Helper()
t.Setenv("BEADS_ACTOR", "")
t.Setenv("BD_ACTOR", "")
}
func TestPrepareSelectedCommandContext_RebindsTargetConfig(t *testing.T) {
t.Setenv("BEADS_DOLT_SERVER_DATABASE", "")
t.Setenv("BEADS_DOLT_SERVER_PORT", "")
clearActorEnv(t)
callerDir := t.TempDir()
callerBeadsDir := filepath.Join(callerDir, ".beads")
writeTestConfigYAML(t, callerBeadsDir, "actor: caller-actor\ndolt.auto-start: true\ndolt.port: 1111\ndolt.auto-commit: on\n")
targetDir := t.TempDir()
targetBeadsDir := filepath.Join(targetDir, ".beads")
writeTestConfigYAML(t, targetBeadsDir, "actor: target-actor\ndolt.auto-start: false\ndolt.port: 4242\ndolt.auto-commit: batch\njson: true\nreadonly: true\n")
if err := (&configfile.Config{
Backend: configfile.BackendDolt,
DoltMode: configfile.DoltModeServer,
}).Save(targetBeadsDir); err != nil {
t.Fatalf("save target metadata: %v", err)
}
t.Setenv("BEADS_DIR", callerBeadsDir)
config.ResetForTesting()
t.Cleanup(config.ResetForTesting)
if err := config.Initialize(); err != nil {
t.Fatalf("config.Initialize: %v", err)
}
oldServerMode := serverMode
oldJSONOutput := jsonOutput
oldReadonlyMode := readonlyMode
oldActor := actor
oldDoltAutoCommit := doltAutoCommit
flagState := snapshotRootFlagState()
t.Cleanup(func() {
serverMode = oldServerMode
jsonOutput = oldJSONOutput
readonlyMode = oldReadonlyMode
actor = oldActor
doltAutoCommit = oldDoltAutoCommit
restoreRootFlagState(t, flagState)
})
serverMode = false
jsonOutput = false
readonlyMode = false
actor = ""
doltAutoCommit = ""
for _, name := range []string{"json", "format", "readonly", "actor", "dolt-auto-commit"} {
if flag := rootCmd.PersistentFlags().Lookup(name); flag != nil {
flag.Changed = false
}
}
prepareSelectedCommandContext(targetBeadsDir, false)
refreshBoundCommandConfig(rootCmd)
if got := os.Getenv("BEADS_DIR"); got != targetBeadsDir {
t.Fatalf("BEADS_DIR = %q, want %q", got, targetBeadsDir)
}
if !serverMode {
t.Fatal("serverMode should be true after rebinding to target metadata")
}
if !jsonOutput {
t.Fatal("jsonOutput should be rebound from target config")
}
if !readonlyMode {
t.Fatal("readonlyMode should be rebound from target config")
}
if actor != "target-actor" {
t.Fatalf("actor = %q, want %q", actor, "target-actor")
}
if doltAutoCommit != "batch" {
t.Fatalf("doltAutoCommit = %q, want %q", doltAutoCommit, "batch")
}
if !doltserver.IsAutoStartDisabled() {
t.Fatal("IsAutoStartDisabled should honor target config after rebinding")
}
if got := doltserver.DefaultConfig(targetBeadsDir).Port; got != 4242 {
t.Fatalf("DefaultConfig(target).Port = %d, want %d", got, 4242)
}
}
func TestPrepareSelectedCommandContext_DoesNotMergeCallerConfigForUnsetKeys(t *testing.T) {
t.Setenv("BEADS_DOLT_SERVER_DATABASE", "")
t.Setenv("BEADS_DOLT_SERVER_PORT", "")
clearActorEnv(t)
root := t.TempDir()
callerDir := filepath.Join(root, "caller")
callerBeadsDir := filepath.Join(callerDir, ".beads")
writeTestConfigYAML(t, callerBeadsDir, "readonly: true\njson: true\n")
targetDir := filepath.Join(root, "target")
targetBeadsDir := filepath.Join(targetDir, ".beads")
writeTestConfigYAML(t, targetBeadsDir, "actor: target-actor\n")
t.Chdir(callerDir)
t.Setenv("BEADS_DIR", callerBeadsDir)
config.ResetForTesting()
t.Cleanup(config.ResetForTesting)
if err := config.Initialize(); err != nil {
t.Fatalf("config.Initialize: %v", err)
}
oldJSONOutput := jsonOutput
oldReadonlyMode := readonlyMode
oldActor := actor
flagState := snapshotRootFlagState()
t.Cleanup(func() {
jsonOutput = oldJSONOutput
readonlyMode = oldReadonlyMode
actor = oldActor
restoreRootFlagState(t, flagState)
})
jsonOutput = false
readonlyMode = false
actor = ""
for _, name := range []string{"json", "format", "readonly", "actor"} {
if flag := rootCmd.PersistentFlags().Lookup(name); flag != nil {
flag.Changed = false
}
}
prepareSelectedCommandContext(targetBeadsDir, false)
refreshBoundCommandConfig(rootCmd)
if readonlyMode {
t.Fatal("readonlyMode should stay false when target config leaves readonly unset")
}
if jsonOutput {
t.Fatal("jsonOutput should stay false when target config leaves json unset")
}
if actor != "target-actor" {
t.Fatalf("actor = %q, want %q", actor, "target-actor")
}
}