beads/cmd/bd/bootstrap_test.go
cwalv 4d14fb8ce1 feat: auto-detect beads database on git origin during bootstrap
When running `bd bootstrap` in a fresh clone, probe origin for
refs/dolt/data and automatically clone the existing beads database
instead of creating unrelated state. `bd init` warns if the ref
exists and suggests using `bd bootstrap` instead.

Fixes #2580
2026-03-14 00:18:13 -07:00

192 lines
5.2 KiB
Go

//go:build cgo
package main
import (
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/configfile"
)
func TestDetectBootstrapAction_NoneWhenDatabaseExists(t *testing.T) {
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
t.Fatal(err)
}
// Create dolt directory with content so it's detected as existing
doltDir := filepath.Join(beadsDir, "dolt")
if err := os.MkdirAll(filepath.Join(doltDir, "beads"), 0o750); err != nil {
t.Fatal(err)
}
// Run from tmpDir so auto-detect doesn't find parent git repo
oldWd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.Chdir(oldWd) }()
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
cfg := configfile.DefaultConfig()
plan := detectBootstrapAction(beadsDir, cfg)
if plan.Action != "none" {
t.Errorf("action = %q, want %q", plan.Action, "none")
}
if !plan.HasExisting {
t.Error("HasExisting = false, want true")
}
}
func TestDetectBootstrapAction_RestoreWhenBackupExists(t *testing.T) {
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
backupDir := filepath.Join(beadsDir, "backup")
if err := os.MkdirAll(backupDir, 0o750); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(backupDir, "issues.jsonl"), []byte("{}"), 0o644); err != nil {
t.Fatal(err)
}
// Run from tmpDir so auto-detect doesn't find parent git repo
oldWd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.Chdir(oldWd) }()
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
cfg := configfile.DefaultConfig()
plan := detectBootstrapAction(beadsDir, cfg)
if plan.Action != "restore" {
t.Errorf("action = %q, want %q", plan.Action, "restore")
}
if plan.BackupDir != backupDir {
t.Errorf("BackupDir = %q, want %q", plan.BackupDir, backupDir)
}
}
func TestDetectBootstrapAction_InitWhenNothingExists(t *testing.T) {
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
t.Fatal(err)
}
// Run from the tmpDir so auto-detect doesn't find a git repo
oldWd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.Chdir(oldWd) }()
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
cfg := configfile.DefaultConfig()
plan := detectBootstrapAction(beadsDir, cfg)
if plan.Action != "init" {
t.Errorf("action = %q, want %q", plan.Action, "init")
}
}
func TestDetectBootstrapAction_SyncWhenOriginHasDoltRef(t *testing.T) {
// Create a bare repo with a refs/dolt/data ref
bareDir := filepath.Join(t.TempDir(), "bare.git")
runGitForBootstrapTest(t, "", "init", "--bare", bareDir)
// Create a source repo, commit, push, then create the dolt ref
sourceDir := t.TempDir()
runGitForBootstrapTest(t, sourceDir, "init", "-b", "main")
runGitForBootstrapTest(t, sourceDir, "config", "user.email", "test@test.com")
runGitForBootstrapTest(t, sourceDir, "config", "user.name", "Test User")
runGitForBootstrapTest(t, sourceDir, "commit", "--allow-empty", "-m", "init")
runGitForBootstrapTest(t, sourceDir, "remote", "add", "origin", bareDir)
runGitForBootstrapTest(t, sourceDir, "push", "origin", "main")
// Create refs/dolt/data by pushing HEAD to that ref
runGitForBootstrapTest(t, sourceDir, "push", "origin", "HEAD:refs/dolt/data")
// Create a "clone" repo with origin pointing at the bare repo
cloneDir := t.TempDir()
runGitForBootstrapTest(t, cloneDir, "init", "-b", "main")
runGitForBootstrapTest(t, cloneDir, "remote", "add", "origin", bareDir)
beadsDir := filepath.Join(cloneDir, ".beads")
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
t.Fatal(err)
}
oldWd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.Chdir(oldWd) }()
if err := os.Chdir(cloneDir); err != nil {
t.Fatal(err)
}
cfg := configfile.DefaultConfig()
plan := detectBootstrapAction(beadsDir, cfg)
if plan.Action != "sync" {
t.Errorf("action = %q, want %q", plan.Action, "sync")
}
if plan.SyncRemote == "" {
t.Error("SyncRemote is empty, expected git+ prefixed URL")
}
}
func TestDetectBootstrapAction_InitWhenOriginHasNoDoltRef(t *testing.T) {
// Create a bare repo without refs/dolt/data
bareDir := filepath.Join(t.TempDir(), "bare.git")
runGitForBootstrapTest(t, "", "init", "--bare", bareDir)
cloneDir := t.TempDir()
runGitForBootstrapTest(t, cloneDir, "init", "-b", "main")
runGitForBootstrapTest(t, cloneDir, "remote", "add", "origin", bareDir)
beadsDir := filepath.Join(cloneDir, ".beads")
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
t.Fatal(err)
}
oldWd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.Chdir(oldWd) }()
if err := os.Chdir(cloneDir); err != nil {
t.Fatal(err)
}
cfg := configfile.DefaultConfig()
plan := detectBootstrapAction(beadsDir, cfg)
if plan.Action != "init" {
t.Errorf("action = %q, want %q (no dolt ref on origin)", plan.Action, "init")
}
}
func runGitForBootstrapTest(t *testing.T, dir string, args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
if dir != "" {
cmd.Dir = dir
}
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v failed: %v\n%s", args, err, string(output))
}
}