beads/cmd/bd/explicit_db_nodb_test.go
Bee 3571331b50
test: fixtures commit with maintenance.auto=false before every git worktree add (wy-7ocweu) (#7358)
Fixes #7349. The same race #7314 closed in internal/beads and
internal/config, in the fixtures it did not cover: a fixture's plain
`git commit` spawns the detached `git maintenance run --auto` child, whose
worktree-prune races the fixture's next `git worktree add` and deletes
.git/worktrees/<name>/locked under it (seen on PR #7312 run 37585997441,
cmd/bd Dolt-server tier shard 4/16, TestBootstrap_WorktreeFallbackDir).

One noAutoMaintenance + gitCommand(args...) helper per test package, each
in an existing untagged shared test file (cmd/bd/helpers_test.go,
doctor/git_test_helpers_test.go, doctor/fix/common_test.go,
formula/parser_test.go, git/gitdir_test.go, storage/domain/git/git_test.go;
cmd/bd/test_helpers_test.go is //go:build cgo so unusable for the pure
lane), and every inline exec.Command("git", ...) in those six packages
swapped for it. The flags ride the command line, never env config: the
routing-key scrub drops env config. No new files, so no BUILD.bazel rows.

Proof: GIT_TRACE on `go test ./cmd/bd -run TestBootstrap_WorktreeFallbackDir`
counts 3 `maintenance run --auto` spawns at d8d8bf587b and 0 with this
change.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 08:01:46 +00:00

640 lines
22 KiB
Go

//go:build cgo
package main
import (
"context"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/steveyegge/beads/internal/configfile"
)
// buildBDUnderTest builds the bd binary once per test process and returns the path.
// Previously each caller built a fresh binary in t.TempDir(), which on slow runners
// (macOS arm64) took 30-240s each and blew the 10m package timeout when many
// buildBDUnderTest-using tests ran together.
var (
buildBDOnce sync.Once
buildBDPath string
buildBDErr error
buildBDDir string
)
func buildBDUnderTest(t *testing.T) string {
t.Helper()
buildBDOnce.Do(func() {
prebuilt, err := findPrebuiltBDBinary()
if err != nil {
buildBDErr = err
return
}
if prebuilt != "" {
buildBDPath = prebuilt
return
}
dir, err := testTempDir("bd-testbin-*")
if err != nil {
buildBDErr = err
return
}
buildBDDir = dir
binName := "bd"
if runtime.GOOS == "windows" {
binName = "bd.exe"
}
buildBDPath = filepath.Join(dir, binName)
buildCmd := goBuildBDCommand(buildBDPath)
if out, err := buildCmd.CombinedOutput(); err != nil {
buildBDErr = &buildBDError{err: err, output: out}
return
}
})
if buildBDErr != nil {
t.Fatalf("go build failed: %v", buildBDErr)
}
return buildBDPath
}
type buildBDError struct {
err error
output []byte
}
func (e *buildBDError) Error() string {
return e.err.Error() + "\n" + string(e.output)
}
func initGitRepo(t *testing.T, dir string) {
t.Helper()
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatalf("mkdir repo: %v", err)
}
cmd := gitCommand("init", "-q")
cmd.Dir = dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git init %s: %v\n%s", dir, err, out)
}
writeFile(t, filepath.Join(dir, ".gitignore"), []byte(".beads/.env\n"))
commitCmd := gitCommand("add", ".")
commitCmd.Dir = dir
_, _ = commitCmd.CombinedOutput()
commitCmd = gitCommand("commit", "-q", "-m", "init")
commitCmd.Dir = dir
commitCmd.Env = append(os.Environ(),
"GIT_AUTHOR_NAME=Test",
"GIT_AUTHOR_EMAIL=test@example.com",
"GIT_COMMITTER_NAME=Test",
"GIT_COMMITTER_EMAIL=test@example.com",
)
_, _ = commitCmd.CombinedOutput()
}
func writeFile(t *testing.T, path string, content []byte) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("mkdir parent for %s: %v", path, err)
}
if err := os.WriteFile(path, content, 0o644); err != nil {
t.Fatalf("write %s: %v", path, err)
}
}
func writeServerRepo(t *testing.T, repoDir, database, host, syncRemote string, port int) string {
return writeServerRepoWithDataDir(t, repoDir, database, host, syncRemote, port, "")
}
func writeServerRepoWithDataDir(t *testing.T, repoDir, database, host, syncRemote string, port int, doltDataDir string) string {
t.Helper()
initGitRepo(t, repoDir)
beadsDir := filepath.Join(repoDir, ".beads")
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
t.Fatalf("mkdir beads dir: %v", err)
}
doltDir := filepath.Join(beadsDir, "dolt")
if doltDataDir != "" {
doltDir = filepath.Join(beadsDir, doltDataDir)
}
if err := os.MkdirAll(doltDir, 0o755); err != nil {
t.Fatalf("mkdir dolt dir: %v", err)
}
cfg := &configfile.Config{
Backend: configfile.BackendDolt,
DoltMode: configfile.DoltModeServer,
DoltServerHost: host,
DoltDatabase: database,
}
if doltDataDir != "" {
cfg.DoltDataDir = doltDataDir
}
if err := cfg.Save(beadsDir); err != nil {
t.Fatalf("save metadata: %v", err)
}
// A server-mode workspace with a local Dolt root is current only when it
// carries a valid post-1.0 witness; otherwise the upgrade guard correctly
// treats the shape as an ambiguous historical workspace.
if err := writeLocalVersion(filepath.Join(beadsDir, localVersionFile), Version); err != nil {
t.Fatalf("write local version: %v", err)
}
writeFile(t, filepath.Join(beadsDir, "dolt-server.port"), []byte(strconv.Itoa(port)))
writeFile(t, filepath.Join(beadsDir, "config.yaml"), []byte("sync:\n git-remote: "+syncRemote+"\n"))
writeFile(t, filepath.Join(beadsDir, ".env"), []byte("BEADS_DOLT_SERVER_HOST="+host+"\n"))
return beadsDir
}
func writeProjectConfig(t *testing.T, beadsDir string, syncRemote string, port int, shared bool) {
t.Helper()
sharedText := "false"
if shared {
sharedText = "true"
}
writeFile(t, filepath.Join(beadsDir, "config.yaml"), []byte(
"sync:\n git-remote: "+syncRemote+"\n"+
"dolt:\n port: "+strconv.Itoa(port)+"\n shared-server: "+sharedText+"\n",
))
}
func writeIssuePrefixConfig(t *testing.T, beadsDir, prefix string) {
t.Helper()
writeFile(t, filepath.Join(beadsDir, "config.yaml"), []byte("issue-prefix: "+prefix+"\n"))
}
// evalPath resolves symlinks in a path for consistent comparison.
// On macOS, t.TempDir() returns /var/folders/... but binaries resolve
// it to /private/var/folders/..., causing string comparison failures.
func evalPath(t *testing.T, path string) string {
t.Helper()
resolved, err := filepath.EvalSymlinks(path)
if err != nil {
t.Fatalf("EvalSymlinks(%s): %v", path, err)
}
return resolved
}
func decodeJSONOutput(t *testing.T, out []byte, target any) {
t.Helper()
trimmed := strings.TrimSpace(string(out))
idx := strings.Index(trimmed, "{")
if idx == -1 {
t.Fatalf("no JSON object found in output:\n%s", out)
}
if err := json.Unmarshal([]byte(trimmed[idx:]), target); err != nil {
t.Fatalf("unmarshal json: %v\n%s", err, out)
}
}
func runBDCommand(t *testing.T, binPath, dir string, extraEnv []string, args ...string) []byte {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, binPath, args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(),
"HOME="+t.TempDir(),
"XDG_CONFIG_HOME="+t.TempDir(),
"BEADS_TEST_IGNORE_REPO_CONFIG=1",
"BEADS_DIR=",
"BEADS_DB=",
"BEADS_DOLT_SERVER_DATABASE=",
"BEADS_DOLT_SERVER_HOST=",
"BEADS_DOLT_SERVER_PORT=",
"BEADS_DOLT_PORT=",
)
cmd.Env = append(cmd.Env, extraEnv...)
out, err := cmd.CombinedOutput()
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
t.Fatalf("%v timed out after %s\n%s", args, 10*time.Second, out)
}
t.Fatalf("%v failed: %v\n%s", args, err, out)
}
return out
}
type whereExpectation struct {
beadsDir string
database string
prefix string
omitPrefix bool
}
func assertWhereOutput(t *testing.T, out []byte, want whereExpectation) {
t.Helper()
var got map[string]any
decodeJSONOutput(t, out, &got)
if evalPath(t, got["path"].(string)) != evalPath(t, want.beadsDir) {
t.Fatalf("path = %v, want %s", got["path"], want.beadsDir)
}
if evalPath(t, got["database_path"].(string)) != evalPath(t, want.database) {
t.Fatalf("database_path = %v, want %s", got["database_path"], want.database)
}
if want.omitPrefix {
if _, ok := got["prefix"]; ok {
t.Fatalf("prefix = %v, want omitted when only server metadata is available", got["prefix"])
}
return
}
if got["prefix"] != want.prefix {
t.Fatalf("prefix = %v, want %s", got["prefix"], want.prefix)
}
}
func TestContextUsesExplicitDBFlagForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
out := runBDCommand(t, binPath, repoA, nil, "--db", filepath.Join(beadsDirB, "dolt"), "context", "--json")
var got map[string]any
decodeJSONOutput(t, out, &got)
if evalPath(t, got["beads_dir"].(string)) != evalPath(t, beadsDirB) {
t.Fatalf("beads_dir = %v, want %s", got["beads_dir"], beadsDirB)
}
if got["database"] != "repo_b_db" {
t.Fatalf("database = %v, want repo_b_db", got["database"])
}
if got["server_host"] != "10.0.0.2" {
t.Fatalf("server_host = %v, want 10.0.0.2", got["server_host"])
}
if got["sync_git_remote"] != "origin-b" {
t.Fatalf("sync_git_remote = %v, want origin-b", got["sync_git_remote"])
}
}
func TestWhereUsesExplicitDBFlagForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
beadsDirA := writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
writeIssuePrefixConfig(t, beadsDirA, "repo-a")
writeIssuePrefixConfig(t, beadsDirB, "repo-b")
out := runBDCommand(t, binPath, repoA, nil, "--db", filepath.Join(beadsDirB, "dolt"), "where", "--json")
assertWhereOutput(t, out, whereExpectation{
beadsDir: beadsDirB,
database: filepath.Join(beadsDirB, "dolt"),
prefix: "repo-b",
})
}
func TestDoltShowUsesExplicitDBFlagForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
out := runBDCommand(t, binPath, repoA, nil, "--db", filepath.Join(beadsDirB, "dolt"), "dolt", "show", "--json")
var got map[string]any
decodeJSONOutput(t, out, &got)
if got["database"] != "repo_b_db" {
t.Fatalf("database = %v, want repo_b_db", got["database"])
}
if got["host"] != "10.0.0.2" {
t.Fatalf("host = %v, want 10.0.0.2", got["host"])
}
if got["port"] != float64(3312) {
t.Fatalf("port = %v, want 3312", got["port"])
}
}
func TestContextUsesBEADSDBForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
out := runBDCommand(t, binPath, repoA, []string{"BEADS_DB=" + filepath.Join(beadsDirB, "dolt")}, "context", "--json")
var got map[string]any
decodeJSONOutput(t, out, &got)
if evalPath(t, got["beads_dir"].(string)) != evalPath(t, beadsDirB) {
t.Fatalf("beads_dir = %v, want %s", got["beads_dir"], beadsDirB)
}
if got["database"] != "repo_b_db" {
t.Fatalf("database = %v, want repo_b_db", got["database"])
}
}
func TestWhereUsesBEADSDBForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
writeIssuePrefixConfig(t, beadsDirB, "repo-b")
out := runBDCommand(t, binPath, repoA, []string{"BEADS_DB=" + filepath.Join(beadsDirB, "dolt")}, "where", "--json")
assertWhereOutput(t, out, whereExpectation{
beadsDir: beadsDirB,
database: filepath.Join(beadsDirB, "dolt"),
prefix: "repo-b",
})
}
func TestContextUsesBEADSDBDirectoryForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
out := runBDCommand(t, binPath, repoA, []string{"BEADS_DB=" + beadsDirB}, "context", "--json")
var got map[string]any
decodeJSONOutput(t, out, &got)
if evalPath(t, got["beads_dir"].(string)) != evalPath(t, beadsDirB) {
t.Fatalf("beads_dir = %v, want %s", got["beads_dir"], beadsDirB)
}
if got["database"] != "repo_b_db" {
t.Fatalf("database = %v, want repo_b_db", got["database"])
}
}
func TestContextUsesExplicitDBFlagForExternalDoltDataDir(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepoWithDataDir(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312, "../external-dolt")
out := runBDCommand(t, binPath, repoA, nil, "--db", filepath.Join(beadsDirB, "../external-dolt"), "context", "--json")
var got map[string]any
decodeJSONOutput(t, out, &got)
if evalPath(t, got["beads_dir"].(string)) != evalPath(t, beadsDirB) {
t.Fatalf("beads_dir = %v, want %s", got["beads_dir"], beadsDirB)
}
if got["database"] != "repo_b_db" {
t.Fatalf("database = %v, want repo_b_db", got["database"])
}
if got["server_host"] != "10.0.0.2" {
t.Fatalf("server_host = %v, want 10.0.0.2", got["server_host"])
}
}
func TestWhereUsesExplicitDBFlagForExternalDoltDataDir(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepoWithDataDir(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312, "../external-dolt")
writeIssuePrefixConfig(t, beadsDirB, "repo-b")
out := runBDCommand(t, binPath, repoA, nil, "--db", filepath.Join(beadsDirB, "../external-dolt"), "where", "--json")
assertWhereOutput(t, out, whereExpectation{
beadsDir: beadsDirB,
database: filepath.Join(beadsDirB, "../external-dolt"),
prefix: "repo-b",
})
}
func TestContextExplicitDBFlagOverridesBEADSDBForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
repoC := filepath.Join(root, "repo-c")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
beadsDirC := writeServerRepo(t, repoC, "repo_c_db", "10.0.0.3", "origin-c", 3313)
out := runBDCommand(t, binPath, repoA, []string{"BEADS_DB=" + filepath.Join(beadsDirC, "dolt")}, "--db", filepath.Join(beadsDirB, "dolt"), "context", "--json")
var got map[string]any
decodeJSONOutput(t, out, &got)
if evalPath(t, got["beads_dir"].(string)) != evalPath(t, beadsDirB) {
t.Fatalf("beads_dir = %v, want %s", got["beads_dir"], beadsDirB)
}
if got["database"] != "repo_b_db" {
t.Fatalf("database = %v, want repo_b_db", got["database"])
}
}
func TestWhereExplicitDBFlagOverridesBEADSDBForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
repoC := filepath.Join(root, "repo-c")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
beadsDirC := writeServerRepo(t, repoC, "repo_c_db", "10.0.0.3", "origin-c", 3313)
writeIssuePrefixConfig(t, beadsDirB, "repo-b")
writeIssuePrefixConfig(t, beadsDirC, "repo-c")
out := runBDCommand(t, binPath, repoA, []string{"BEADS_DB=" + filepath.Join(beadsDirC, "dolt")}, "--db", filepath.Join(beadsDirB, "dolt"), "where", "--json")
assertWhereOutput(t, out, whereExpectation{
beadsDir: beadsDirB,
database: filepath.Join(beadsDirB, "dolt"),
prefix: "repo-b",
})
}
func TestWhereUsesExplicitDBFlagForMetadataOnlyServerRepo(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
out := runBDCommand(t, binPath, repoA, nil, "--db", filepath.Join(beadsDirB, "dolt"), "where", "--json")
assertWhereOutput(t, out, whereExpectation{
beadsDir: beadsDirB,
database: filepath.Join(beadsDirB, "dolt"),
omitPrefix: true,
})
}
func TestWhereBEADSDBOverridesBDDBForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
repoC := filepath.Join(root, "repo-c")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
beadsDirC := writeServerRepo(t, repoC, "repo_c_db", "10.0.0.3", "origin-c", 3313)
writeIssuePrefixConfig(t, beadsDirB, "repo-b")
writeIssuePrefixConfig(t, beadsDirC, "repo-c")
out := runBDCommand(t, binPath, repoA, []string{
"BEADS_DB=" + filepath.Join(beadsDirB, "dolt"),
"BD_DB=" + filepath.Join(beadsDirC, "dolt"),
}, "where", "--json")
assertWhereOutput(t, out, whereExpectation{
beadsDir: beadsDirB,
database: filepath.Join(beadsDirB, "dolt"),
prefix: "repo-b",
})
}
func TestContextBEADSDBOverridesBDDBForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
repoC := filepath.Join(root, "repo-c")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
beadsDirC := writeServerRepo(t, repoC, "repo_c_db", "10.0.0.3", "origin-c", 3313)
out := runBDCommand(t, binPath, repoA, []string{"BEADS_DB=" + filepath.Join(beadsDirB, "dolt"), "BD_DB=" + filepath.Join(beadsDirC, "dolt")}, "context", "--json")
var got map[string]any
decodeJSONOutput(t, out, &got)
if evalPath(t, got["beads_dir"].(string)) != evalPath(t, beadsDirB) {
t.Fatalf("beads_dir = %v, want %s", got["beads_dir"], beadsDirB)
}
if got["database"] != "repo_b_db" {
t.Fatalf("database = %v, want repo_b_db", got["database"])
}
}
func TestContextPreservesSourceDatabaseAcrossRedirectForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
sharedBeadsDir := filepath.Join(repoB, "shared-beads")
if err := os.RemoveAll(beadsDirB); err != nil {
t.Fatalf("remove source beads dir: %v", err)
}
if err := os.MkdirAll(beadsDirB, 0o755); err != nil {
t.Fatalf("mkdir source beads dir: %v", err)
}
writeFile(t, filepath.Join(beadsDirB, "redirect"), []byte("../shared-beads\n"))
if err := (&configfile.Config{
Backend: configfile.BackendDolt,
DoltMode: configfile.DoltModeServer,
DoltServerHost: "10.0.0.2",
DoltDatabase: "repo_b_db",
}).Save(beadsDirB); err != nil {
t.Fatalf("save source metadata: %v", err)
}
writeFile(t, filepath.Join(beadsDirB, "dolt-server.port"), []byte("3312"))
if err := os.MkdirAll(filepath.Join(sharedBeadsDir, "dolt"), 0o755); err != nil {
t.Fatalf("mkdir shared dolt dir: %v", err)
}
if err := (&configfile.Config{
Backend: configfile.BackendDolt,
DoltMode: configfile.DoltModeServer,
DoltServerHost: "10.0.0.9",
DoltDatabase: "shared_db",
}).Save(sharedBeadsDir); err != nil {
t.Fatalf("save shared metadata: %v", err)
}
writeFile(t, filepath.Join(sharedBeadsDir, "dolt-server.port"), []byte("3399"))
writeFile(t, filepath.Join(sharedBeadsDir, "config.yaml"), []byte("sync:\n git-remote: shared-origin\n"))
writeFile(t, filepath.Join(sharedBeadsDir, ".env"), []byte("BEADS_DOLT_SERVER_HOST=10.0.0.9\n"))
out := runBDCommand(t, binPath, repoA, nil, "--db", filepath.Join(beadsDirB, "dolt"), "context", "--json")
var got map[string]any
decodeJSONOutput(t, out, &got)
if got["database"] != "repo_b_db" {
t.Fatalf("database = %v, want repo_b_db", got["database"])
}
}
func TestDoltShowUsesSelectedRepoConfigForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
beadsDirA := writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
if err := os.Remove(filepath.Join(beadsDirA, "dolt-server.port")); err != nil {
t.Fatalf("remove port file A: %v", err)
}
if err := os.Remove(filepath.Join(beadsDirB, "dolt-server.port")); err != nil {
t.Fatalf("remove port file B: %v", err)
}
writeProjectConfig(t, beadsDirA, "origin-a", 4401, true)
writeProjectConfig(t, beadsDirB, "origin-b", 4402, false)
out := runBDCommand(t, binPath, repoA, nil, "--db", filepath.Join(beadsDirB, "dolt"), "dolt", "show", "--json")
var got map[string]any
decodeJSONOutput(t, out, &got)
if got["host"] != "10.0.0.2" {
t.Fatalf("host = %v, want 10.0.0.2", got["host"])
}
if got["port"] != float64(4402) {
t.Fatalf("port = %v, want 4402", got["port"])
}
if got["shared_server"] != false {
t.Fatalf("shared_server = %v, want false", got["shared_server"])
}
if got["database"] != "repo_b_db" {
t.Fatalf("database = %v, want repo_b_db", got["database"])
}
}
func TestContextPreservesShellEnvPrecedenceForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
out := runBDCommand(t, binPath, repoA, []string{"BEADS_DOLT_SERVER_HOST=9.9.9.9"}, "--db", filepath.Join(beadsDirB, "dolt"), "context", "--json")
var got map[string]any
decodeJSONOutput(t, out, &got)
if got["server_host"] != "9.9.9.9" {
t.Fatalf("server_host = %v, want 9.9.9.9", got["server_host"])
}
if got["database"] != "repo_b_db" {
t.Fatalf("database = %v, want repo_b_db", got["database"])
}
}
func TestDoltShowUsesBEADSDBForNoDBCommand(t *testing.T) {
binPath := buildBDUnderTest(t)
root := t.TempDir()
repoA := filepath.Join(root, "repo-a")
repoB := filepath.Join(root, "repo-b")
writeServerRepo(t, repoA, "repo_a_db", "10.0.0.1", "origin-a", 3311)
beadsDirB := writeServerRepo(t, repoB, "repo_b_db", "10.0.0.2", "origin-b", 3312)
out := runBDCommand(t, binPath, repoA, []string{"BEADS_DB=" + filepath.Join(beadsDirB, "dolt")}, "dolt", "show", "--json")
var got map[string]any
decodeJSONOutput(t, out, &got)
if got["database"] != "repo_b_db" {
t.Fatalf("database = %v, want repo_b_db", got["database"])
}
if got["host"] != "10.0.0.2" {
t.Fatalf("host = %v, want 10.0.0.2", got["host"])
}
}