beads/cmd/bd/config_drift_test.go
Harry Miller cc1cb9e937 feat: add 'bd config drift' and 'bd config apply' commands (#3086)
Add two new config subcommands for detecting and fixing configuration drift:

- `bd config drift` (bd-omc) — read-only diagnostic that detects mismatches
  between declared config and actual system state (hooks, remote, server)
- `bd config apply` (bd-3rw) — idempotent command that fixes detected drift

Drift checks three areas: git hooks installed/current, Dolt remote matches
federation.remote config, and server state matches dolt.shared-server config.
Apply detects drift then fixes each mismatch with safe defaults (never stops
a running server, remote update has rollback on failure).

Closes bd-omc, closes bd-3rw

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-04-06 20:56:16 -07:00

159 lines
4.6 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"testing"
)
// TestCheckHooksDriftNotGitRepo verifies hooks check skips when not in a git repo.
func TestCheckHooksDriftNotGitRepo(t *testing.T) {
tmpDir := t.TempDir()
origDir, _ := os.Getwd()
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("chdir: %v", err)
}
defer os.Chdir(origDir) //nolint:errcheck
items := checkHooksDrift()
if len(items) != 1 {
t.Fatalf("expected 1 item, got %d", len(items))
}
if items[0].Status != driftStatusSkipped {
t.Errorf("expected status %q, got %q", driftStatusSkipped, items[0].Status)
}
if items[0].Check != "hooks" {
t.Errorf("expected check %q, got %q", "hooks", items[0].Check)
}
}
// TestCheckServerDriftNoBeadsDir verifies server check skips when no .beads exists.
func TestCheckServerDriftNoBeadsDir(t *testing.T) {
tmpDir := t.TempDir()
origDir, _ := os.Getwd()
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("chdir: %v", err)
}
defer os.Chdir(origDir) //nolint:errcheck
items := checkServerDrift()
if len(items) != 1 {
t.Fatalf("expected 1 item, got %d", len(items))
}
if items[0].Status != driftStatusSkipped {
t.Errorf("expected status %q, got %q", driftStatusSkipped, items[0].Status)
}
}
// TestCheckRemoteDriftNoBeadsDir verifies remote check skips when no .beads exists.
func TestCheckRemoteDriftNoBeadsDir(t *testing.T) {
tmpDir := t.TempDir()
origDir, _ := os.Getwd()
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("chdir: %v", err)
}
defer os.Chdir(origDir) //nolint:errcheck
items := checkRemoteDrift()
if len(items) != 1 {
t.Fatalf("expected 1 item, got %d", len(items))
}
if items[0].Status != driftStatusSkipped {
t.Errorf("expected status %q, got %q", driftStatusSkipped, items[0].Status)
}
}
// TestIsServerProbablyRunningNoPIDFile verifies false when no PID file exists.
func TestIsServerProbablyRunningNoPIDFile(t *testing.T) {
tmpDir := t.TempDir()
if isServerProbablyRunning(tmpDir) {
t.Error("expected false with no PID file")
}
}
// TestIsServerProbablyRunningBadPID verifies false for invalid PID content.
func TestIsServerProbablyRunningBadPID(t *testing.T) {
tmpDir := t.TempDir()
pidFile := filepath.Join(tmpDir, "dolt-server.pid")
if err := os.WriteFile(pidFile, []byte("notanumber\n"), 0600); err != nil {
t.Fatalf("write: %v", err)
}
if isServerProbablyRunning(tmpDir) {
t.Error("expected false with invalid PID")
}
}
// TestIsServerProbablyRunningDeadPID verifies false for a non-existent process.
func TestIsServerProbablyRunningDeadPID(t *testing.T) {
tmpDir := t.TempDir()
pidFile := filepath.Join(tmpDir, "dolt-server.pid")
// Use a very high PID that's unlikely to exist
if err := os.WriteFile(pidFile, []byte("4999999\n"), 0600); err != nil {
t.Fatalf("write: %v", err)
}
if isServerProbablyRunning(tmpDir) {
t.Error("expected false with dead PID")
}
}
// TestIsServerProbablyRunningOwnPID verifies true for our own PID.
func TestIsServerProbablyRunningOwnPID(t *testing.T) {
tmpDir := t.TempDir()
pidFile := filepath.Join(tmpDir, "dolt-server.pid")
pid := os.Getpid()
if err := os.WriteFile(pidFile, []byte(filepath.Join(tmpDir, "")+"\n"), 0600); err != nil {
// Write our own PID
}
if err := os.WriteFile(pidFile, []byte(fmt.Sprintf("%d\n", pid)), 0600); err != nil {
t.Fatalf("write: %v", err)
}
if !isServerProbablyRunning(tmpDir) {
t.Error("expected true for own PID")
}
}
// TestDriftItemStatuses verifies the status constants.
func TestDriftItemStatuses(t *testing.T) {
if driftStatusOK != "ok" {
t.Errorf("driftStatusOK = %q", driftStatusOK)
}
if driftStatusDrift != "drift" {
t.Errorf("driftStatusDrift = %q", driftStatusDrift)
}
if driftStatusInfo != "info" {
t.Errorf("driftStatusInfo = %q", driftStatusInfo)
}
if driftStatusSkipped != "skipped" {
t.Errorf("driftStatusSkipped = %q", driftStatusSkipped)
}
}
// TestRunDriftChecksReturnsResults verifies the aggregator returns results from all checks.
func TestRunDriftChecksReturnsResults(t *testing.T) {
// When run from a non-beads directory, we should still get results (skipped checks)
tmpDir := t.TempDir()
origDir, _ := os.Getwd()
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("chdir: %v", err)
}
defer os.Chdir(origDir) //nolint:errcheck
items := runDriftChecks()
if len(items) == 0 {
t.Fatal("expected at least some drift items")
}
// All items should have a check name and status
for _, item := range items {
if item.Check == "" {
t.Error("drift item has empty check name")
}
if item.Status == "" {
t.Error("drift item has empty status")
}
if item.Message == "" {
t.Error("drift item has empty message")
}
}
}