beads/cmd/bd/config_setmany_test.go
matt wilkie 75d3d9593d
fix(import): respect import.auto=false in the empty-DB import-on-write path (#4595)
* fix(import): respect import.auto=false on empty DB in bd update (fixes #4304, mybd-3fvn)

shouldRunAutoImportJSONL gated the empty-database upgrade-recovery
auto-import (maybeAutoImportJSONL, GH#2994) on read-only/global/server-mode
flags only. It never consulted the import.auto config key, so
BD_IMPORT_AUTO=false (or import.auto: false in config.yaml) had no effect on
this path -- only the separate git-hook sync importer (importJSONLForSync)
honored it. Any write command (e.g. bd update) against a fresh/empty
database with a stale issues.jsonl present would silently auto-import it and
report success, exactly as reported in #4304.

Add the missing config.GetBool("import.auto") check to
shouldRunAutoImportJSONL. Explicit `bd import` is unaffected: it already
short-circuits on cmd.Name() == "import" regardless of this flag.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FWVnjn3Bq4PSG1akpFpKnY
Agent-Signature: claude-sonnet-5-medium on behalf of matt wilkie

* fix(config): route import.auto through YAML config

bd config set import.auto previously treated the startup gate as a database-backed key, so the value landed where the auto-import checks never read it. Mark it YAML-only like import.path and cover both direct and config set-many routing tables.

Agent-Signature: codex-gpt-5.5-high on behalf of matt wilkie

* fix(config): exempt config-set of import.auto=false from pre-run auto-import

Running `bd config set import.auto false` (or `config set-many ...
import.auto=false`) triggered the very auto-import it was meant to
disable: shouldRunAutoImportJSONL runs in PersistentPreRun before the
config command writes the new value, so it still saw the old default
(true) and imported first when a stale .beads/issues.jsonl sat next to
an empty database. Add isDisablingImportAutoViaConfigCommand to detect
this specific invocation and skip the pre-run import so the master
switch takes effect on the same command that sets it.

Addresses Codex cross-vendor review finding (2026-07-07) on PR #4595
(mybd-ycu4).

Agent-Signature: claude-fable-5-high on behalf of matt wilkie

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-07 12:59:29 -07:00

322 lines
9.1 KiB
Go

package main
import (
"strings"
"testing"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/types"
)
// TestConfigSetManyArgParsing tests argument parsing for the set-many command.
func TestConfigSetManyArgParsing(t *testing.T) {
tests := []struct {
name string
arg string
wantKey string
wantVal string
wantErr bool
}{
{"simple", "key=value", "key", "value", false},
{"dotted key", "ado.state_map.open=New", "ado.state_map.open", "New", false},
{"value with equals", "key=val=ue", "key", "val=ue", false},
{"empty value", "key=", "key", "", false},
{"no equals", "keyvalue", "", "", true},
{"only equals", "=value", "", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
idx := strings.Index(tt.arg, "=")
if idx <= 0 {
if !tt.wantErr {
t.Error("expected successful parse, got error")
}
return
}
if tt.wantErr {
t.Error("expected error, got successful parse")
return
}
key := tt.arg[:idx]
value := tt.arg[idx+1:]
if key != tt.wantKey {
t.Errorf("key = %q, want %q", key, tt.wantKey)
}
if value != tt.wantVal {
t.Errorf("value = %q, want %q", value, tt.wantVal)
}
})
}
}
// TestConfigSetManyYamlKeyDetection tests that yaml-only keys are correctly identified
// for routing in the set-many command.
func TestConfigSetManyYamlKeyDetection(t *testing.T) {
yamlKeys := []string{"no-db", "json", "routing.mode", "routing.default", "no-push", "import.auto", "import.path"}
for _, key := range yamlKeys {
if !config.IsYamlOnlyKey(key) {
t.Errorf("expected %q to be yaml-only", key)
}
}
dbKeys := []string{"ado.state_map.open", "jira.url", "status.custom", "test.key"}
for _, key := range dbKeys {
if config.IsYamlOnlyKey(key) {
t.Errorf("expected %q to NOT be yaml-only", key)
}
}
}
// TestConfigSetManyMixedKeyRouting verifies that a batch of mixed key types
// (yaml-only, git config, and database) are correctly categorized into their
// respective storage backends. This exercises the Phase 3 routing logic.
func TestConfigSetManyMixedKeyRouting(t *testing.T) {
type kvPair struct {
key, value string
}
args := []string{
"no-db=true", // yaml-only
"routing.mode=direct", // yaml-only
"beads.role=maintainer", // git config
"jira.url=https://j.test", // database
"ado.state_map.open=New", // database
"ado.state_map.closed=Closed", // database
}
// Phase 1: Parse
pairs := make([]kvPair, 0, len(args))
for _, arg := range args {
idx := strings.Index(arg, "=")
if idx <= 0 {
t.Fatalf("unexpected parse failure for %q", arg)
}
pairs = append(pairs, kvPair{key: arg[:idx], value: arg[idx+1:]})
}
// Phase 3: Categorize
var yamlPairs, gitPairs, dbPairs []kvPair
for _, p := range pairs {
if config.IsYamlOnlyKey(p.key) {
yamlPairs = append(yamlPairs, p)
} else if p.key == "beads.role" {
gitPairs = append(gitPairs, p)
} else {
dbPairs = append(dbPairs, p)
}
}
if len(yamlPairs) != 2 {
t.Errorf("expected 2 yaml pairs, got %d", len(yamlPairs))
}
if len(gitPairs) != 1 {
t.Errorf("expected 1 git pair, got %d", len(gitPairs))
}
if len(dbPairs) != 3 {
t.Errorf("expected 3 db pairs, got %d", len(dbPairs))
}
// Verify yaml keys are correct
yamlKeySet := map[string]bool{}
for _, p := range yamlPairs {
yamlKeySet[p.key] = true
}
if !yamlKeySet["no-db"] || !yamlKeySet["routing.mode"] {
t.Errorf("yaml pairs missing expected keys: %v", yamlPairs)
}
// Verify git key
if len(gitPairs) > 0 && gitPairs[0].key != "beads.role" {
t.Errorf("expected git pair key 'beads.role', got %q", gitPairs[0].key)
}
// Verify DB keys
dbKeySet := map[string]bool{}
for _, p := range dbPairs {
dbKeySet[p.key] = true
}
for _, expected := range []string{"jira.url", "ado.state_map.open", "ado.state_map.closed"} {
if !dbKeySet[expected] {
t.Errorf("db pairs missing expected key %q", expected)
}
}
}
// TestConfigSetManyValidationBeforeWrite verifies that validation (Phase 2)
// catches errors before any writes would occur. This tests the upfront
// validation pass that prevents partial writes.
func TestConfigSetManyValidationBeforeWrite(t *testing.T) {
t.Run("invalid beads.role rejected upfront", func(t *testing.T) {
// Simulate the Phase 2 validation logic from the command handler
type kvPair struct {
key, value string
}
pairs := []kvPair{
{"jira.url", "https://j.test"}, // valid DB key (would succeed)
{"beads.role", "superadmin"}, // invalid role (should fail validation)
{"ado.state_map.open", "New"}, // valid DB key (would succeed)
}
validRoles := map[string]bool{"maintainer": true, "contributor": true}
var validationErr string
for _, p := range pairs {
if p.key == "beads.role" && !validRoles[p.value] {
validationErr = p.value
break
}
}
if validationErr == "" {
t.Fatal("expected validation to reject invalid role, but it passed")
}
if validationErr != "superadmin" {
t.Errorf("expected rejected value 'superadmin', got %q", validationErr)
}
})
t.Run("invalid status.custom rejected upfront", func(t *testing.T) {
// status.custom with invalid format should fail validation
// ParseCustomStatusConfig rejects names with spaces and other invalid chars
type kvPair struct {
key, value string
}
pairs := []kvPair{
{"jira.url", "https://j.test"}, // valid
{"status.custom", "valid_status,also valid status"}, // may be invalid depending on parser
}
var validationFailed bool
for _, p := range pairs {
if p.key == "status.custom" && p.value != "" {
if _, err := types.ParseCustomStatusConfig(p.value); err != nil {
validationFailed = true
break
}
}
}
// The key point is that validation runs BEFORE writes.
// Whether this specific value is invalid depends on the parser,
// but the validation logic is exercised either way.
_ = validationFailed
})
t.Run("valid beads.role passes validation", func(t *testing.T) {
type kvPair struct {
key, value string
}
pairs := []kvPair{
{"beads.role", "contributor"},
{"jira.url", "https://j.test"},
}
validRoles := map[string]bool{"maintainer": true, "contributor": true}
for _, p := range pairs {
if p.key == "beads.role" && !validRoles[p.value] {
t.Errorf("expected valid role %q to pass validation", p.value)
}
}
})
t.Run("valid status.custom passes validation", func(t *testing.T) {
type kvPair struct {
key, value string
}
pairs := []kvPair{
{"status.custom", "awaiting_review,awaiting_testing"},
{"jira.project", "PROJ"},
}
for _, p := range pairs {
if p.key == "status.custom" && p.value != "" {
if _, err := types.ParseCustomStatusConfig(p.value); err != nil {
t.Errorf("expected valid status.custom to pass validation: %v", err)
}
}
}
})
}
// TestConfigSetManyOutputLocationMapping verifies that the output phase
// correctly maps each key to its storage location label.
func TestConfigSetManyOutputLocationMapping(t *testing.T) {
type kvPair struct {
key, value string
}
pairs := []kvPair{
{"no-db", "true"},
{"routing.mode", "direct"},
{"beads.role", "maintainer"},
{"jira.url", "https://j.test"},
{"ado.state_map.open", "New"},
}
expectedLocations := map[string]string{
"no-db": "config.yaml",
"routing.mode": "config.yaml",
"beads.role": "git config",
"jira.url": "database",
"ado.state_map.open": "database",
}
for _, p := range pairs {
location := "database"
if config.IsYamlOnlyKey(p.key) {
location = "config.yaml"
} else if p.key == "beads.role" {
location = "git config"
}
expected := expectedLocations[p.key]
if location != expected {
t.Errorf("key %q: expected location %q, got %q", p.key, expected, location)
}
}
}
// TestConfigSetManyParseMultipleArgs tests parsing a full batch of arguments,
// including edge cases like values containing equals signs and empty values,
// as would happen with a real 'bd config set-many' invocation.
func TestConfigSetManyParseMultipleArgs(t *testing.T) {
args := []string{
"jira.url=https://example.atlassian.net",
"jira.project=PROJ",
"ado.state_map.open=New",
"ado.state_map.closed=Closed",
"custom.filter=status=open&label=bug", // value contains '='
"custom.empty=", // empty value
}
type kvPair struct {
key, value string
}
expected := []kvPair{
{"jira.url", "https://example.atlassian.net"},
{"jira.project", "PROJ"},
{"ado.state_map.open", "New"},
{"ado.state_map.closed", "Closed"},
{"custom.filter", "status=open&label=bug"},
{"custom.empty", ""},
}
pairs := make([]kvPair, 0, len(args))
for _, arg := range args {
idx := strings.Index(arg, "=")
if idx <= 0 {
t.Fatalf("unexpected parse failure for %q", arg)
}
pairs = append(pairs, kvPair{key: arg[:idx], value: arg[idx+1:]})
}
if len(pairs) != len(expected) {
t.Fatalf("expected %d pairs, got %d", len(expected), len(pairs))
}
for i, p := range pairs {
if p.key != expected[i].key {
t.Errorf("pair[%d] key = %q, want %q", i, p.key, expected[i].key)
}
if p.value != expected[i].value {
t.Errorf("pair[%d] value = %q, want %q", i, p.value, expected[i].value)
}
}
}