beads/cmd/bd/actor_test.go
Harry Miller 9c952e64d7
fix(actor): let BEADS_ACTOR outrank deprecated BD_ACTOR (#4645) (#4790)
* fix(actor): let BEADS_ACTOR outrank deprecated BD_ACTOR

The documented actor priority is `--actor flag > BEADS_ACTOR > BD_ACTOR
(deprecated) > git user.name > $USER`, but with both env vars set BD_ACTOR
won (GH#4645).

Root cause: viper's AutomaticEnv (prefix "BD") binds BD_ACTOR to the "actor"
config key, and viper consults the prefixed auto-binding ahead of any
explicit binding — so `config.GetString("actor")` returns BD_ACTOR's value
even when BEADS_ACTOR is also set. The pre-run hooks pre-populate the global
`actor` from that value before `getActorWithGit()` runs, and its
`if actor != ""` early return then hands back BD_ACTOR, never reaching its
own BEADS_ACTOR check.

Fix: resolve the configured actor through a new `resolveConfiguredActor()`
helper that checks BEADS_ACTOR explicitly before falling back to
config.GetString("actor"), and use it in both pre-run hooks. BD_ACTOR and
config.yaml `actor` still work as before when BEADS_ACTOR is unset.

Adds TestResolveConfiguredActor. Verified end-to-end: with both env vars set,
created_by is now BEADS_ACTOR's value (was BD_ACTOR's before).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(actor): let BEADS_ACTOR outrank BD_ACTOR in `config show` too (PR #4790 review follow-up)

The 2026-07-24 maintainer review found the runtime actor-resolution fix
correct but incomplete: `bd config show` still resolved the "actor" entry
through the raw Viper-backed path, whose AutomaticEnv binds the deprecated
BD_ACTOR ahead of any explicit binding — so with both env vars set,
mutations used BEADS_ACTOR while `config show` reported BD_ACTOR's value
with `env: BD_ACTOR` provenance, contradicting show's "all effective
configuration" contract (GH#4645).

- collectViperEntries now special-cases the "actor" key to reuse
  resolveConfiguredActor() for its value and reports "env: BEADS_ACTOR"
  provenance when that var is set, matching the runtime path.
- Added TestCollectViperEntriesActorPrecedenceOverBDActor asserting both
  value and provenance with both env vars set.
- Added TestCLI_CreateActorPrecedence: a CLI-level `bd create` through
  rootCmd.Execute() (not calling resolveConfiguredActor() directly) so the
  root PersistentPreRunE / refreshBoundCommandConfig wiring stays covered —
  reverting either call site would leave the existing unit test green but
  this test red, per the review's should-fix 2.
- Moved the getActorWithGit doc comment back onto getActorWithGit (nit from
  the same review; it had drifted onto resolveConfiguredActor).

Agent-Signature: claude-fable-5-medium on behalf of maphew

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
2026-07-26 13:05:47 -07:00

249 lines
6.6 KiB
Go

package main
import (
"os"
"os/exec"
"strings"
"testing"
"github.com/steveyegge/beads/internal/config"
)
// TestGetActorWithGit tests the actor resolution fallback chain.
// Priority: --actor flag > BEADS_ACTOR env > BD_ACTOR env (deprecated) > git config user.name > $USER > "unknown"
func TestGetActorWithGit(t *testing.T) {
// Save original environment and actor variable
origActor := actor
origBdActor, bdActorSet := os.LookupEnv("BD_ACTOR")
origBeadsActor, beadsActorSet := os.LookupEnv("BEADS_ACTOR")
origUser, userSet := os.LookupEnv("USER")
// Cleanup after test
defer func() {
actor = origActor
if bdActorSet {
os.Setenv("BD_ACTOR", origBdActor)
} else {
os.Unsetenv("BD_ACTOR")
}
if beadsActorSet {
os.Setenv("BEADS_ACTOR", origBeadsActor)
} else {
os.Unsetenv("BEADS_ACTOR")
}
if userSet {
os.Setenv("USER", origUser)
} else {
os.Unsetenv("USER")
}
}()
// Helper to get current git user.name (may be empty if not configured)
getGitUserName := func() string {
out, err := exec.Command("git", "config", "user.name").Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}
gitUserName := getGitUserName()
tests := []struct {
name string
actorFlag string
bdActor string
beadsActor string
user string
expected string
skipIfNoGit bool // Skip if git user.name is not configured
}{
{
name: "actor flag takes priority",
actorFlag: "flag-actor",
bdActor: "bd-actor",
beadsActor: "beads-actor",
user: "system-user",
expected: "flag-actor",
},
{
name: "BEADS_ACTOR takes priority when no flag",
actorFlag: "",
bdActor: "bd-actor",
beadsActor: "beads-actor",
user: "system-user",
expected: "beads-actor",
},
{
name: "BD_ACTOR used as fallback when no BEADS_ACTOR",
actorFlag: "",
bdActor: "bd-actor",
beadsActor: "",
user: "system-user",
expected: "bd-actor",
},
{
name: "git config user.name used when no env vars",
actorFlag: "",
bdActor: "",
beadsActor: "",
user: "system-user",
expected: gitUserName, // Will be git user.name if configured
skipIfNoGit: true,
},
{
name: "USER fallback when no git config",
actorFlag: "",
bdActor: "",
beadsActor: "",
user: "fallback-user",
expected: "fallback-user",
// Note: This test may fail if git user.name is configured
// We handle this by checking the actual git config in the test
},
{
name: "unknown as final fallback",
actorFlag: "",
bdActor: "",
beadsActor: "",
user: "",
expected: "unknown",
// Note: This test may get git user.name instead if configured
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Skip tests that require git user.name to not be configured
if tt.skipIfNoGit && gitUserName == "" {
t.Skip("Skipping: git config user.name is not configured")
}
// For tests expecting USER or unknown, skip if git user.name is configured
// because git takes priority over USER
if (tt.expected == tt.user || tt.expected == "unknown") && gitUserName != "" && tt.bdActor == "" && tt.beadsActor == "" && tt.actorFlag == "" {
t.Skipf("Skipping: git config user.name (%s) takes priority over expected %s", gitUserName, tt.expected)
}
// Set up test environment
actor = tt.actorFlag
if tt.bdActor != "" {
os.Setenv("BD_ACTOR", tt.bdActor)
} else {
os.Unsetenv("BD_ACTOR")
}
if tt.beadsActor != "" {
os.Setenv("BEADS_ACTOR", tt.beadsActor)
} else {
os.Unsetenv("BEADS_ACTOR")
}
if tt.user != "" {
os.Setenv("USER", tt.user)
} else {
os.Unsetenv("USER")
}
// Call the function
result := getActorWithGit()
// Check result
if result != tt.expected {
t.Errorf("getActorWithGit() = %q, want %q", result, tt.expected)
}
})
}
}
// TestResolveConfiguredActor verifies that BEADS_ACTOR (primary override)
// outranks the deprecated BD_ACTOR alias when the --actor flag is not set,
// even though viper's AutomaticEnv binds BD_ACTOR to the "actor" config key
// (GH#4645). This is the resolution the pre-run hooks use to pre-populate the
// global actor before getActorWithGit runs.
func TestResolveConfiguredActor(t *testing.T) {
origBd, bdSet := os.LookupEnv("BD_ACTOR")
origBeads, beadsSet := os.LookupEnv("BEADS_ACTOR")
defer func() {
if bdSet {
os.Setenv("BD_ACTOR", origBd)
} else {
os.Unsetenv("BD_ACTOR")
}
if beadsSet {
os.Setenv("BEADS_ACTOR", origBeads)
} else {
os.Unsetenv("BEADS_ACTOR")
}
_ = config.Initialize()
}()
t.Run("BEADS_ACTOR outranks BD_ACTOR", func(t *testing.T) {
os.Setenv("BD_ACTOR", "from-bd-actor")
os.Setenv("BEADS_ACTOR", "from-beads-actor")
if err := config.Initialize(); err != nil {
t.Fatalf("config.Initialize(): %v", err)
}
if got := resolveConfiguredActor(); got != "from-beads-actor" {
t.Errorf("resolveConfiguredActor() = %q, want %q (BEADS_ACTOR must win)", got, "from-beads-actor")
}
})
t.Run("BD_ACTOR used when BEADS_ACTOR unset", func(t *testing.T) {
os.Unsetenv("BEADS_ACTOR")
os.Setenv("BD_ACTOR", "from-bd-actor")
if err := config.Initialize(); err != nil {
t.Fatalf("config.Initialize(): %v", err)
}
if got := resolveConfiguredActor(); got != "from-bd-actor" {
t.Errorf("resolveConfiguredActor() = %q, want %q (BD_ACTOR fallback)", got, "from-bd-actor")
}
})
}
// TestGetActorWithGit_PriorityOrder tests that the priority order is respected
func TestGetActorWithGit_PriorityOrder(t *testing.T) {
// Save original state
origActor := actor
origBdActor, bdActorSet := os.LookupEnv("BD_ACTOR")
origBeadsActor, beadsActorSet := os.LookupEnv("BEADS_ACTOR")
defer func() {
actor = origActor
if bdActorSet {
os.Setenv("BD_ACTOR", origBdActor)
} else {
os.Unsetenv("BD_ACTOR")
}
if beadsActorSet {
os.Setenv("BEADS_ACTOR", origBeadsActor)
} else {
os.Unsetenv("BEADS_ACTOR")
}
}()
// Test: flag > BEADS_ACTOR > BD_ACTOR
actor = "from-flag"
os.Setenv("BD_ACTOR", "from-bd-actor")
os.Setenv("BEADS_ACTOR", "from-beads-actor")
result := getActorWithGit()
if result != "from-flag" {
t.Errorf("Expected flag to take priority, got %q", result)
}
// Test: BEADS_ACTOR > BD_ACTOR (no flag)
actor = ""
result = getActorWithGit()
if result != "from-beads-actor" {
t.Errorf("Expected BEADS_ACTOR to take priority over BD_ACTOR, got %q", result)
}
// Test: BD_ACTOR as fallback when BEADS_ACTOR is empty
os.Unsetenv("BEADS_ACTOR")
result = getActorWithGit()
if result != "from-bd-actor" {
t.Errorf("Expected BD_ACTOR to be used as fallback, got %q", result)
}
}