beads/cmd/bd/bootstrap_embedded_test.go
Okano, Osamu ef6c6e3e48
Follow up remaining runtime .beads fallbacks after GH#3124 (#3183)
* fix(workspace): align resolved workspace messaging and config lookup

intent(workspace): pure worktrees and external BEADS_DIR setups should work without implying a local .beads directory is required
decision(config): derive config repo roots from FindBeadsDir-backed workspace resolution instead of a local-only .beads walk
constraint(workspace-detection): keep FindBeadsDir ordering and storage behavior unchanged while making bd where the primary diagnostic hint
learned(testing): shared-server env and global .beads ignore rules can distort CGO suites, so regression coverage now normalizes those test paths

* fix(bootstrap): align no-workspace JSON wording

intent(bootstrap): machine-readable no-workspace output should match the resolved-workspace guidance used elsewhere on this branch
decision(bootstrap-json): reuse the shared workspace error helpers through a dedicated payload builder so tests can lock the JSON contract
constraint(review-loop): keep the bootstrap CLI flow unchanged outside the no-workspace JSON branch while adding a focused regression test

* fix(where): allow no-workspace diagnostics without db (beads-9f74-5l1)

intent(where): no-workspace guidance should point to a command that actually runs when workspace resolution fails
decision(startup): treat top-level where as a no-db command so it reaches its own handler before database preflight
constraint(where-tests): subprocess mode can change the exact hint text, so the regression test locks the JSON contract and stable guidance fragments instead

* fix(bootstrap): restore bd where guidance for no-workspace output (beads-9f74-3c3)

intent(bootstrap): no-workspace bootstrap guidance should match the resolved-workspace diagnostics introduced on this branch
decision(no-workspace-hints): reuse the shared hint that mentions bd where instead of the bootstrap-specific fallback text
constraint(bootstrap-tests): cover both the payload helper and subprocess CLI path so wording regressions are caught before release

* fix(workspace): remove no-workspace .beads fallbacks (beads-9f74-gr1)

intent(workspace-resolution): finish the GH#3124 follow-up for runtime paths that still fabricated local .beads state
rejected(no-workspace-fallback): inventing ./ .beads paths outside an active workspace hid shared-worktree resolution and created stray files
decision(setup-recipes): keep built-in bd setup recipes usable without a workspace but require an active workspace for custom recipe state
constraint(worktree-support): completion and backup helpers still need to resolve shared main-repo workspaces from a git worktree

* fix(config): skip no-workspace store init for store-free commands

intent(config): review-fix-loop found no-workspace config commands should reach their own graceful paths instead of failing in PersistentPreRun
decision(config-preflight): detect store-free config subcommands before DB synthesis so config.yaml, git-backed, and diagnostic flows can run without an opened store
constraint(json-contract): keep bd config validate --json machine-readable on stdout once pre-run stops short-circuiting into low-level DB errors
learned(config-preflight): the parent config command not being in noDbCommands leaked store init into show, validate, drift, apply, and yaml-only config helpers

* fix(ci): restore init prefix normalization and formatting

intent(ci): clear the current PR failures instead of leaving the branch blocked behind known red checks

decision(init-prefix): normalize dotted prefixes before persisting issue_prefix so config, metadata.json, and Dolt database naming stay aligned for Julia-style repos
constraint(fmt-check): CI runs gofmt over the whole repo, so the existing spacing drift in internal/jira/tracker_test.go had to be cleaned up on this branch to get a green run
learned(ci): the latest main CI was already red on the same formatting file and the same embedded shard, so rebasing surfaced pre-existing blockers rather than introducing a new shard-only regression
2026-04-10 21:23:14 -07:00

245 lines
7.2 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
)
// bdBootstrap runs "bd bootstrap" with the given args and returns stdout.
func bdBootstrap(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"bootstrap"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd bootstrap %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out)
}
func bdBootstrapAllowError(t *testing.T, bd, dir string, args ...string) (string, error) {
t.Helper()
fullArgs := append([]string{"bootstrap"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
return string(out), err
}
func TestBootstrapNoWorkspace(t *testing.T) {
t.Parallel()
bd := buildEmbeddedBD(t)
dir := t.TempDir()
t.Run("default_output", func(t *testing.T) {
out, err := bdBootstrapAllowError(t, bd, dir)
if err == nil {
t.Fatal("expected bd bootstrap to exit non-zero without a workspace")
}
if !strings.Contains(out, activeWorkspaceNotFoundMessage()) {
t.Fatalf("expected no-workspace message, got: %s", out)
}
if !strings.Contains(out, "bd where") {
t.Fatalf("expected bootstrap hint to mention bd where, got: %s", out)
}
})
t.Run("json_output", func(t *testing.T) {
out, err := bdBootstrapAllowError(t, bd, dir, "--json")
if err == nil {
t.Fatal("expected bd bootstrap --json to exit non-zero without a workspace")
}
s := strings.TrimSpace(out)
start := strings.Index(s, "{")
if start < 0 {
t.Fatalf("expected JSON object in output, got: %s", out)
}
var payload map[string]string
if err := json.Unmarshal([]byte(s[start:]), &payload); err != nil {
t.Fatalf("parse bootstrap JSON: %v\n%s", err, s)
}
if payload["action"] != "none" {
t.Fatalf("action = %q, want %q", payload["action"], "none")
}
if payload["reason"] != activeWorkspaceNotFoundError() {
t.Fatalf("reason = %q, want %q", payload["reason"], activeWorkspaceNotFoundError())
}
if !strings.Contains(payload["suggestion"], "bd where") {
t.Fatalf("suggestion should mention bd where, got: %q", payload["suggestion"])
}
})
}
func TestEmbeddedBootstrap(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
// ===== Already Exists =====
t.Run("bootstrap_existing_db", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "tb")
out := bdBootstrap(t, bd, dir, "--dry-run")
if !strings.Contains(out, "already exists") && !strings.Contains(out, "Nothing to do") {
t.Errorf("expected 'already exists' for initialized db: %s", out)
}
})
// ===== Dry Run (fresh .beads with no db) =====
t.Run("bootstrap_dry_run_fresh", func(t *testing.T) {
dir := t.TempDir()
cmd := exec.Command("git", "init", "-q")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = exec.Command("git", "config", "user.name", "Test")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = exec.Command("git", "config", "user.email", "test@test.com")
cmd.Dir = dir
cmd.CombinedOutput()
// Create .beads with metadata.json so FindBeadsDir detects it
beadsDir := filepath.Join(dir, ".beads")
os.MkdirAll(beadsDir, 0o750)
os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte("{}"), 0o644)
out := bdBootstrap(t, bd, dir, "--dry-run")
if !strings.Contains(out, "create fresh") && !strings.Contains(out, "init") {
t.Errorf("expected fresh init plan: %s", out)
}
})
// ===== Full Bootstrap (init action) =====
t.Run("bootstrap_init", func(t *testing.T) {
dir := t.TempDir()
cmd := exec.Command("git", "init", "-q")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = exec.Command("git", "config", "user.name", "Test")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = exec.Command("git", "config", "user.email", "test@test.com")
cmd.Dir = dir
cmd.CombinedOutput()
beadsDir := filepath.Join(dir, ".beads")
os.MkdirAll(beadsDir, 0o750)
os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte("{}"), 0o644)
bcmd := exec.Command(bd, "bootstrap")
bcmd.Dir = dir
bcmd.Env = bdEnv(dir)
bcmd.Stdin = strings.NewReader("y\n")
out, err := bcmd.CombinedOutput()
if err != nil {
t.Fatalf("bootstrap init failed: %v\n%s", err, out)
}
if !strings.Contains(string(out), "Created fresh database") {
t.Errorf("expected 'Created fresh database': %s", out)
}
})
// ===== JSONL Import =====
t.Run("bootstrap_jsonl_import", func(t *testing.T) {
// First create a db and export
srcDir, _, _ := bdInit(t, bd, "--prefix", "bs")
bdCreate(t, bd, srcDir, "Export for bootstrap", "--type", "task")
cmd := exec.Command(bd, "export", "-o", filepath.Join(srcDir, ".beads", "issues.jsonl"))
cmd.Dir = srcDir
cmd.Env = bdEnv(srcDir)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("export failed: %v\n%s", err, out)
}
// Create new dir with .beads + issues.jsonl but no database
dir := t.TempDir()
gitCmd := exec.Command("git", "init", "-q")
gitCmd.Dir = dir
gitCmd.CombinedOutput()
gitCmd = exec.Command("git", "config", "user.name", "Test")
gitCmd.Dir = dir
gitCmd.CombinedOutput()
gitCmd = exec.Command("git", "config", "user.email", "test@test.com")
gitCmd.Dir = dir
gitCmd.CombinedOutput()
destBeads := filepath.Join(dir, ".beads")
os.MkdirAll(destBeads, 0o750)
os.WriteFile(filepath.Join(destBeads, "metadata.json"), []byte("{}"), 0o644)
// Copy the JSONL file
data, _ := os.ReadFile(filepath.Join(srcDir, ".beads", "issues.jsonl"))
os.WriteFile(filepath.Join(dir, ".beads", "issues.jsonl"), data, 0o644)
// Bootstrap should detect and import JSONL
bcmd := exec.Command(bd, "bootstrap")
bcmd.Dir = dir
bcmd.Env = bdEnv(dir)
bcmd.Stdin = strings.NewReader("y\n")
out, err := bcmd.CombinedOutput()
if err != nil {
t.Fatalf("bootstrap jsonl-import failed: %v\n%s", err, out)
}
if !strings.Contains(string(out), "Imported") {
t.Errorf("expected 'Imported' in output: %s", out)
}
})
}
// TestEmbeddedBootstrapConcurrent exercises bootstrap --dry-run concurrently.
func TestEmbeddedBootstrapConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "bx")
const numWorkers = 8
type workerResult struct {
worker int
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
cmd := exec.Command(bd, "bootstrap", "--dry-run")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("bootstrap --dry-run (worker %d): %v\n%s", worker, err, out)
}
results[worker] = r
}(w)
}
wg.Wait()
for _, r := range results {
if r.err != nil && !strings.Contains(r.err.Error(), "one writer at a time") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
}