beads/cmd/bd/vc_embedded_test.go
Jeremy Irish 0e09598c52
fix: cross-rig prefix routing for issue resolution via routes.jsonl (#2954)
* fix: cross-rig prefix routing for issue resolution via routes.jsonl

When bd runs from a redirected .beads directory (e.g., crew/beercan →
town/.beads), issue IDs with prefixes mapped to other rigs in routes.jsonl
were not resolved. The local store (hq database) was queried, the issue
wasn't found, and only contributor auto-routing was tried as fallback.

Add resolveViaPrefixRouting to the resolution chain in routed.go:
1. Try local store (existing)
2. Try prefix routing via routes.jsonl (NEW)
3. Try contributor auto-routing (existing)

The prefix router extracts the prefix from the bead ID, looks up the
matching route in routes.jsonl, opens the target rig's .beads directory
to read its dolt_database from metadata.json, temporarily overrides
BEADS_DOLT_SERVER_DATABASE, and opens a read-only store for that database.

This unblocks cross-rig operations like bd show, bd update, convoy creation,
and auto-dispatch for multi-rig Gas Town deployments.

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

* fix(doctor): add auto-fix for Dolt Format warning (seed .bd-dolt-ok marker)

The "Dolt Format" doctor check detected pre-0.56 dolt databases but had
no automatic fix — it fell through to the default "No automatic fix
available" case. The remedy is trivial: create the .bd-dolt-ok marker
file, which is exactly what ensureDoltInit does non-destructively.

Add a DoltFormat fix function and wire it into the doctor_fix switch.
In server mode the .beads/dolt/.dolt/ directory is vestigial; seeding
the marker acknowledges the database without any destructive action.

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

* fix(vc): bd vc commit now includes config table changes

bd vc commit called store.Commit() which intentionally skips the config
table (GH#2455) to prevent sweeping stale config during auto-commits.
But bd vc commit is an explicit user action — it should commit everything
visible in bd vc status, including config changes from bd config set.

Switch to CommitPending which calls CommitWithConfig internally, ensuring
all dirty tables including config are committed.

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

* fix: suppress gosec G304 warnings for trusted beads paths

The paths are constructed internally from the beads directory, not from
user input. Add nolint:gosec annotations matching the rest of the codebase.

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

* fix(test): accept both committed outcomes in vc commit_json test

bd create auto-commits in embedded mode, so a subsequent bd vc commit
may find nothing pending. CommitPending correctly returns committed=false
in this case. The test now accepts both outcomes, matching the pattern
used by commit_with_message.

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

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-20 17:42:11 -07:00

231 lines
6.8 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"testing"
)
// bdVC runs "bd vc" with extra args. Returns combined output.
func bdVC(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"vc"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd vc %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out)
}
// bdVCFail runs "bd vc" expecting failure.
func bdVCFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"vc"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("bd vc %s should have failed, got: %s", strings.Join(args, " "), out)
}
return string(out)
}
func TestEmbeddedVC(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt vc tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
t.Run("status", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vcst")
out := bdVC(t, bd, dir, "status")
if !strings.Contains(out, "main") {
t.Errorf("expected 'main' branch in status, got: %s", out)
}
})
t.Run("status_json", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vcstj")
out := bdVC(t, bd, dir, "status", "--json")
var result map[string]interface{}
if err := json.Unmarshal([]byte(out), &result); err != nil {
t.Fatalf("failed to parse JSON: %v\n%s", err, out)
}
if branch, _ := result["branch"].(string); branch != "main" {
t.Errorf("expected branch='main', got %q", branch)
}
if commit, _ := result["commit"].(string); commit == "" {
t.Error("expected non-empty commit hash")
}
})
t.Run("commit_with_message", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vccm")
// bd create auto-commits, so vc commit may see "nothing to commit".
// Both outcomes are valid.
bdCreateSilent(t, bd, dir, "commit test issue")
cmd := exec.Command(bd, "vc", "commit", "-m", "test commit message")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil && !strings.Contains(string(out), "nothing to commit") {
t.Fatalf("bd vc commit failed unexpectedly: %v\n%s", err, out)
}
})
t.Run("commit_json", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vccj")
// bd create auto-commits, so vc commit may see "nothing to commit".
// Both committed=true and committed=false are valid outcomes.
bdCreateSilent(t, bd, dir, "commit json issue")
cmd := exec.Command(bd, "vc", "commit", "-m", "json commit", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd vc commit --json failed unexpectedly: %v\n%s", err, out)
}
// Verify valid JSON with committed field (true or false are both valid)
var result map[string]interface{}
if jsonErr := json.Unmarshal(out, &result); jsonErr != nil {
t.Fatalf("failed to parse JSON: %v\n%s", jsonErr, out)
}
if _, ok := result["committed"]; !ok {
t.Error("expected 'committed' field in JSON output")
}
})
t.Run("commit_stdin", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vccs")
bdCreateSilent(t, bd, dir, "stdin commit issue")
cmd := exec.Command(bd, "vc", "commit", "--stdin")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
cmd.Stdin = strings.NewReader("message from stdin")
out, err := cmd.CombinedOutput()
if err != nil && !strings.Contains(string(out), "nothing to commit") {
t.Fatalf("bd vc commit --stdin failed unexpectedly: %v\n%s", err, out)
}
})
t.Run("commit_no_message", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vcnm")
out := bdVCFail(t, bd, dir, "commit")
if !strings.Contains(out, "commit message is required") {
t.Errorf("expected 'commit message is required', got: %s", out)
}
})
t.Run("merge_no_conflicts", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vcmrg")
// Create a branch, switch to it, create an issue, switch back, merge
bdBranch(t, bd, dir, "feature-test")
// Create an issue on main (auto-committed)
bdCreateSilent(t, bd, dir, "main branch issue")
// Merge the feature branch (which is at the same base commit)
out := bdVC(t, bd, dir, "merge", "feature-test")
if !strings.Contains(out, "merged") && !strings.Contains(out, "Merged") {
t.Errorf("expected merge success message, got: %s", out)
}
})
t.Run("merge_json", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vcmj")
bdBranch(t, bd, dir, "merge-json-branch")
out := bdVC(t, bd, dir, "merge", "merge-json-branch", "--json")
var result map[string]interface{}
if err := json.Unmarshal([]byte(out), &result); err != nil {
t.Fatalf("failed to parse JSON: %v\n%s", err, out)
}
if merged, _ := result["merged"].(string); merged != "merge-json-branch" {
t.Errorf("expected merged='merge-json-branch', got %q", merged)
}
})
}
func TestEmbeddedVCConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt vc tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "vcconc")
const numWorkers = 10
type result struct {
worker int
out string
err error
}
// Each worker creates an issue then commits
results := make([]result, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
// Create an issue
createCmd := exec.Command(bd, "create", "--silent", fmt.Sprintf("vc-conc-issue-%d", worker))
createCmd.Dir = dir
createCmd.Env = bdEnv(dir)
if out, err := createCmd.CombinedOutput(); err != nil {
results[worker] = result{worker: worker, out: string(out), err: err}
return
}
// Try to commit
cmd := exec.Command(bd, "vc", "commit", "-m", fmt.Sprintf("worker %d commit", worker))
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
results[worker] = result{worker: worker, out: string(out), err: err}
}(w)
}
wg.Wait()
successes := 0
for _, r := range results {
if strings.Contains(r.out, "panic") {
t.Errorf("worker %d panicked:\n%s", r.worker, r.out)
}
if r.err == nil {
successes++
} else if strings.Contains(r.out, "one writer at a time") ||
strings.Contains(r.out, "nothing to commit") {
// Expected: auto-commit means explicit commit often has nothing left
successes++ // still counts as successful execution
} else {
t.Errorf("worker %d failed with unexpected error: %v\n%s", r.worker, r.err, r.out)
}
}
if successes < 1 {
t.Errorf("expected at least 1 successful commit, got %d", successes)
}
t.Logf("%d/%d vc commit workers succeeded", successes, numWorkers)
}