beads/cmd/bd/dolt_singleprocess_test.go
beads/crew/emma 2137693dad revert: restore embedded Dolt mode (was only supposed to be removed from Gas Town)
Revert commits e5330641 and 0df41523 which removed embedded Dolt mode
from the beads codebase. The embedded Dolt removal was only intended for
Gas Town, not for Beads.

Restores:
- dolthub/driver dependency and embedded connection path
- Advisory flock (AccessLock) for concurrent process protection
- Embedded connector lifecycle management in DoltStore
- All CGO build tags across ~45 files
- Vendored go-icu-regex library
- nocgo stub files for non-CGO builds
- Bootstrap and embedded_uow helpers
- factory_dolt_test.go and dolt_singleprocess_test.go

Merge conflict resolutions:
- store.go: TCP fail-fast check wrapped inside if cfg.ServerMode
- store.go: backtick-quoting for embedded CREATE DATABASE (prefix-based names)
- perf_dolt.go: port references updated from 3306 to 3307
- ci.yml: restored continue-on-error for Nix (dolthub/driver requires Go 1.25.6+)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Executed-By: beads/crew/emma
Rig: beads
Role: crew
2026-02-07 21:21:24 -08:00

135 lines
3.9 KiB
Go

package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
func writeDoltWorkspace(t *testing.T, workspaceDir string) (beadsDir string, doltDir string) {
t.Helper()
beadsDir = filepath.Join(workspaceDir, ".beads")
doltDir = filepath.Join(beadsDir, "dolt")
if err := os.MkdirAll(doltDir, 0o700); err != nil {
t.Fatalf("mkdir: %v", err)
}
metadata := `{
"database": "dolt",
"backend": "dolt"
}`
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(metadata), 0o600); err != nil {
t.Fatalf("write metadata.json: %v", err)
}
return beadsDir, doltDir
}
func TestDoltSingleProcess_ShouldAutoStartDaemonFalse(t *testing.T) {
oldDBPath := dbPath
t.Cleanup(func() { dbPath = oldDBPath })
dbPath = ""
ws := t.TempDir()
beadsDir, _ := writeDoltWorkspace(t, ws)
t.Setenv("BEADS_DIR", beadsDir)
// Ensure the finder sees a workspace root (and not the repo running tests).
oldWD, _ := os.Getwd()
_ = os.Chdir(ws)
t.Cleanup(func() { _ = os.Chdir(oldWD) })
if shouldAutoStartDaemon() {
t.Fatalf("expected shouldAutoStartDaemon() to be false for dolt backend")
}
}
func TestDoltSingleProcess_TryAutoStartDoesNotCreateStartlock(t *testing.T) {
oldDBPath := dbPath
t.Cleanup(func() { dbPath = oldDBPath })
dbPath = ""
ws := t.TempDir()
beadsDir, _ := writeDoltWorkspace(t, ws)
t.Setenv("BEADS_DIR", beadsDir)
socketPath := filepath.Join(ws, "bd.sock")
lockPath := socketPath + ".startlock"
ok := tryAutoStartDaemon(socketPath)
if ok {
t.Fatalf("expected tryAutoStartDaemon() to return false for dolt backend")
}
if _, err := os.Stat(lockPath); err == nil {
t.Fatalf("expected startlock not to be created for dolt backend: %s", lockPath)
} else if !os.IsNotExist(err) {
t.Fatalf("stat startlock: %v", err)
}
}
func TestDoltSingleProcess_DaemonGuardBlocksStartCommand(t *testing.T) {
oldDBPath := dbPath
t.Cleanup(func() { dbPath = oldDBPath })
dbPath = ""
ws := t.TempDir()
beadsDir, _ := writeDoltWorkspace(t, ws)
t.Setenv("BEADS_DIR", beadsDir)
// Use daemonStartCmd which has the guard attached.
// Ensure help and federation flags exist (cobra adds them during execution).
cmd := daemonStartCmd
cmd.Flags().Bool("help", false, "help")
// Note: federation flag is already registered in init()
err := guardDaemonStartForDolt(cmd, nil)
if err == nil {
t.Fatalf("expected daemon guard error for dolt backend without --federation")
}
// Guardrail wording may evolve; assert a stable intent.
if !strings.Contains(err.Error(), "daemon mode is not supported") {
t.Fatalf("expected error to mention daemon mode unsupported, got: %v", err)
}
}
// This test uses a helper subprocess because startDaemon calls os.Exit on failure.
func TestDoltSingleProcess_StartDaemonGuardrailExitsNonZero(t *testing.T) {
if os.Getenv("BD_TEST_HELPER_STARTDAEMON") == "1" {
// Helper mode: set up environment and invoke startDaemon (should os.Exit(1)).
ws := os.Getenv("BD_TEST_WORKSPACE")
_, doltDir := writeDoltWorkspace(t, ws)
// Ensure FindDatabasePath can resolve.
_ = os.Chdir(ws)
_ = os.Setenv("BEADS_DB", doltDir)
dbPath = ""
pidFile := filepath.Join(ws, ".beads", "daemon.pid")
startDaemon(5*time.Second, false, false, false, false, false, "", pidFile, "info", false, false, 0, 0)
return
}
ws := t.TempDir()
// Pre-create workspace structure so helper can just use it.
_, doltDir := writeDoltWorkspace(t, ws)
exe, err := os.Executable()
if err != nil {
t.Fatalf("os.Executable: %v", err)
}
cmd := exec.Command(exe, "-test.run", "^TestDoltSingleProcess_StartDaemonGuardrailExitsNonZero$", "-test.v")
cmd.Env = append(os.Environ(),
"BD_TEST_HELPER_STARTDAEMON=1",
"BD_TEST_WORKSPACE="+ws,
"BEADS_DB="+doltDir,
)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected non-zero exit; output:\n%s", string(out))
}
if !strings.Contains(string(out), "daemon mode is not supported") {
t.Fatalf("expected output to mention daemon unsupported; got:\n%s", string(out))
}
}