gitgram/aitimemachine/beads/cmd/bd/config_setmany_test.go
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

323 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.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)
}
}
}