beads/cmd/bd/backup_status_test.go
Julian Knutsen 8adcd25992
fix(backup): point the size-cap pause advice at levers that end it (#7074)
The PAUSED warning, the `bd backup status` PAUSED line, CHANGELOG and
configuration.md told operators to switch to a fresh destination with
`bd backup init <new-path>`. That never ends the pause: backupDir()
reads only backup.git-repo, while `bd backup init` registers the
separate destination that manual `bd backup sync` pushes to, so
auto-backup keeps targeting the over-cap directory and stays paused.

Name the two levers that do end it: raise backup.size-cap-mb, or point
backup.git-repo at a different git repository. Also correct the
backup.git-repo row in configuration.md, which called it a repo URL
although backupDir() requires a local path containing .git (anything
else falls back to .beads/backup/), so the new advice is actionable.

TestPauseAutoBackupForSizeCap_RemediationAdvice and the status PAUSED
line now require backup.git-repo and reject `bd backup init`.
TestMaybeAutoBackup_RemediationLevers runs all three for real: raising
the cap and switching backup.git-repo each resume a sync, while
`bd backup init` leaves auto-backup paused.

Post-merge review follow-up to #6071.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR

Co-authored-by: Eddie the Engineer <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 08:05:46 -07:00

455 lines
14 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/configfile"
)
func TestBackupStatusSizeErrorRespectsOutputMode(t *testing.T) {
prepareBackupStatusTest(t)
missingRoot := filepath.Join(t.TempDir(), "missing")
sizeErr := errors.New("active database directory is missing: " + missingRoot)
sizeDatabase := func(context.Context) (int64, bool, error) {
return 0, false, sizeErr
}
t.Run("json", func(t *testing.T) {
cmd := newBackupStatusTestRoot(sizeDatabase)
cmd.SetArgs([]string{"backup", "status", "--json"})
stdout, stderr, err := executeBackupStatusCommand(t, cmd)
assertExitCode(t, err, 1)
if stderr != "" {
t.Errorf("stderr = %q, want empty", stderr)
}
var response struct {
Error string `json:"error"`
}
if err := json.Unmarshal([]byte(stdout), &response); err != nil {
t.Fatalf("stdout is not a single valid JSON response: %v\nstdout: %s", err, stdout)
}
if !strings.Contains(response.Error, "measure database size") || !strings.Contains(response.Error, missingRoot) {
t.Errorf("error = %q, want database-size diagnostic containing %q", response.Error, missingRoot)
}
if strings.Contains(stdout, "Usage:") {
t.Errorf("stdout contains usage noise: %s", stdout)
}
})
t.Run("human", func(t *testing.T) {
cmd := newBackupStatusTestRoot(sizeDatabase)
cmd.SetArgs([]string{"backup", "status"})
stdout, stderr, err := executeBackupStatusCommand(t, cmd)
assertExitCode(t, err, 1)
if stdout != "" {
t.Errorf("stdout = %q, want empty", stdout)
}
if !strings.Contains(stderr, "Error: measure database size:") || !strings.Contains(stderr, missingRoot) {
t.Errorf("stderr = %q, want database-size diagnostic containing %q", stderr, missingRoot)
}
if strings.Contains(stderr, "Usage:") || strings.Contains(stderr, "exit code 1") {
t.Errorf("stderr contains Cobra noise: %s", stderr)
}
})
}
func TestBackupStatusOmitsUnsupportedDatabaseSize(t *testing.T) {
prepareBackupStatusTest(t)
sizeDatabase := func(context.Context) (int64, bool, error) {
return 0, false, nil
}
t.Run("json", func(t *testing.T) {
cmd := newBackupStatusTestRoot(sizeDatabase)
cmd.SetArgs([]string{"backup", "status", "--json"})
stdout, stderr, err := executeBackupStatusCommand(t, cmd)
if err != nil {
t.Fatalf("backup status: %v", err)
}
if stderr != "" {
t.Errorf("stderr = %q, want empty", stderr)
}
var response map[string]json.RawMessage
if err := json.Unmarshal([]byte(stdout), &response); err != nil {
t.Fatalf("stdout is not valid JSON: %v\nstdout: %s", err, stdout)
}
if _, ok := response["database_size"]; ok {
t.Errorf("database_size should be omitted: %s", stdout)
}
if _, ok := response["backup"]; !ok {
t.Errorf("backup status missing backup field: %s", stdout)
}
if _, ok := response["dolt"]; !ok {
t.Errorf("backup status missing dolt field: %s", stdout)
}
})
t.Run("human", func(t *testing.T) {
cmd := newBackupStatusTestRoot(sizeDatabase)
cmd.SetArgs([]string{"backup", "status"})
stdout, stderr, err := executeBackupStatusCommand(t, cmd)
if err != nil {
t.Fatalf("backup status: %v", err)
}
if stderr != "" {
t.Errorf("stderr = %q, want empty", stderr)
}
if strings.Contains(stdout, "Database size:") {
t.Errorf("unsupported database size should be omitted: %s", stdout)
}
if !strings.Contains(stdout, "No backup has been performed yet.") {
t.Errorf("remaining status output was not rendered: %s", stdout)
}
})
}
func TestBackupStatusIncludesAvailableDatabaseSize(t *testing.T) {
prepareBackupStatusTest(t)
sizeDatabase := func(context.Context) (int64, bool, error) {
return 1536, true, nil
}
t.Run("json", func(t *testing.T) {
cmd := newBackupStatusTestRoot(sizeDatabase)
cmd.SetArgs([]string{"backup", "status", "--json"})
stdout, stderr, err := executeBackupStatusCommand(t, cmd)
if err != nil {
t.Fatalf("backup status: %v", err)
}
if stderr != "" {
t.Errorf("stderr = %q, want empty", stderr)
}
var response struct {
DatabaseSize struct {
Bytes int64 `json:"bytes"`
} `json:"database_size"`
}
if err := json.Unmarshal([]byte(stdout), &response); err != nil {
t.Fatalf("stdout is not valid JSON: %v\nstdout: %s", err, stdout)
}
if response.DatabaseSize.Bytes != 1536 {
t.Errorf("database_size.bytes = %d, want 1536", response.DatabaseSize.Bytes)
}
})
t.Run("human", func(t *testing.T) {
cmd := newBackupStatusTestRoot(sizeDatabase)
cmd.SetArgs([]string{"backup", "status"})
stdout, stderr, err := executeBackupStatusCommand(t, cmd)
if err != nil {
t.Fatalf("backup status: %v", err)
}
if stderr != "" {
t.Errorf("stderr = %q, want empty", stderr)
}
if !strings.Contains(stdout, "Database size: 1.5 KB") {
t.Errorf("available database size missing: %s", stdout)
}
})
}
// TestBackupStatusShowsSizeCapPaused pins the PR #6071 review's second
// major point: `bd --json` / `--quiet` callers never saw the size-cap
// pause — the warning is stderr-only and throttled, and status previously
// said nothing about the cap, so an agent/CI caller saw a reassuring
// "Last backup" line while auto-backup was silently, permanently paused.
func TestBackupStatusShowsSizeCapPaused(t *testing.T) {
t.Setenv("BD_BACKUP_SIZE_CAP_MB", "1")
prepareBackupStatusTest(t)
sizeDatabase := func(context.Context) (int64, bool, error) { return 0, false, nil }
dir, err := backupDir()
if err != nil {
t.Fatalf("backupDir: %v", err)
}
if err := os.WriteFile(filepath.Join(dir, "filler"), make([]byte, 2*1024*1024), 0o600); err != nil {
t.Fatal(err)
}
seeded := &backupState{Timestamp: time.Now().UTC().Add(-time.Hour), LastDoltCommit: "deadbeef"}
if err := saveBackupState(dir, seeded); err != nil {
t.Fatal(err)
}
t.Run("json", func(t *testing.T) {
cmd := newBackupStatusTestRoot(sizeDatabase)
cmd.SetArgs([]string{"backup", "status", "--json"})
stdout, stderr, err := executeBackupStatusCommand(t, cmd)
if err != nil {
t.Fatalf("backup status: %v", err)
}
if stderr != "" {
t.Errorf("stderr = %q, want empty", stderr)
}
var response struct {
SizeCap struct {
Enabled bool `json:"enabled"`
CapMB int `json:"cap_mb"`
CurrentBytes int64 `json:"current_bytes"`
Exceeded bool `json:"exceeded"`
} `json:"size_cap"`
}
if err := json.Unmarshal([]byte(stdout), &response); err != nil {
t.Fatalf("stdout is not valid JSON: %v\nstdout: %s", err, stdout)
}
if !response.SizeCap.Enabled {
t.Errorf("size_cap.enabled = false, want true: %s", stdout)
}
if !response.SizeCap.Exceeded {
t.Errorf("size_cap.exceeded = false, want true: %s", stdout)
}
if response.SizeCap.CapMB != 1 {
t.Errorf("size_cap.cap_mb = %d, want 1: %s", response.SizeCap.CapMB, stdout)
}
if response.SizeCap.CurrentBytes < 2*1024*1024 {
t.Errorf("size_cap.current_bytes = %d, want >= 2MB: %s", response.SizeCap.CurrentBytes, stdout)
}
})
t.Run("human", func(t *testing.T) {
cmd := newBackupStatusTestRoot(sizeDatabase)
cmd.SetArgs([]string{"backup", "status"})
stdout, stderr, err := executeBackupStatusCommand(t, cmd)
if err != nil {
t.Fatalf("backup status: %v", err)
}
if stderr != "" {
t.Errorf("stderr = %q, want empty", stderr)
}
var pausedLine string
for _, line := range strings.Split(stdout, "\n") {
if strings.Contains(line, "PAUSED (cap exceeded)") {
pausedLine = line
}
}
if pausedLine == "" {
t.Fatalf("status text missing PAUSED indicator: %s", stdout)
}
// Same levers as the pause warning — see
// TestPauseAutoBackupForSizeCap_RemediationAdvice.
for _, want := range []string{"backup.size-cap-mb", "backup.git-repo"} {
if !strings.Contains(pausedLine, want) {
t.Errorf("PAUSED line missing %s pointer: %q", want, pausedLine)
}
}
if strings.Contains(pausedLine, "bd backup init") {
t.Errorf("PAUSED line points at `bd backup init`, which does not move the auto-backup destination: %q", pausedLine)
}
})
}
// TestBackupStatusShowsSizeCapDisabled pins the PR #6071 review's first
// major point from the status side: backup.size-cap-mb: 0 must render as
// disabled in status, not silently report the legacy 2048MB default.
func TestBackupStatusShowsSizeCapDisabled(t *testing.T) {
t.Setenv("BD_BACKUP_SIZE_CAP_MB", "0")
prepareBackupStatusTest(t)
sizeDatabase := func(context.Context) (int64, bool, error) { return 0, false, nil }
dir, err := backupDir()
if err != nil {
t.Fatalf("backupDir: %v", err)
}
seeded := &backupState{Timestamp: time.Now().UTC().Add(-time.Hour), LastDoltCommit: "deadbeef"}
if err := saveBackupState(dir, seeded); err != nil {
t.Fatal(err)
}
t.Run("json", func(t *testing.T) {
cmd := newBackupStatusTestRoot(sizeDatabase)
cmd.SetArgs([]string{"backup", "status", "--json"})
stdout, _, err := executeBackupStatusCommand(t, cmd)
if err != nil {
t.Fatalf("backup status: %v", err)
}
var raw map[string]json.RawMessage
if err := json.Unmarshal([]byte(stdout), &raw); err != nil {
t.Fatalf("stdout is not valid JSON: %v\nstdout: %s", err, stdout)
}
sizeCapRaw, ok := raw["size_cap"]
if !ok {
t.Fatalf("size_cap key missing from status JSON: %s", stdout)
}
var sizeCap struct {
Enabled bool `json:"enabled"`
}
if err := json.Unmarshal(sizeCapRaw, &sizeCap); err != nil {
t.Fatalf("size_cap is not valid JSON: %v\nraw: %s", err, sizeCapRaw)
}
if sizeCap.Enabled {
t.Errorf("size_cap.enabled = true with backup.size-cap-mb=0, want false: %s", stdout)
}
})
t.Run("human", func(t *testing.T) {
cmd := newBackupStatusTestRoot(sizeDatabase)
cmd.SetArgs([]string{"backup", "status"})
stdout, _, err := executeBackupStatusCommand(t, cmd)
if err != nil {
t.Fatalf("backup status: %v", err)
}
if !strings.Contains(stdout, "Size cap: disabled") {
t.Errorf("status text missing disabled size-cap line: %s", stdout)
}
})
}
// TestBackupStatusProxiedGuardFollowsLocality replaces a test that asserted a
// blanket proxied refusal. Since slice S3 the guard is scoped to locality: bd
// owns the dolt process on a managed-local workspace and can honor the command
// there, while an external one is still refused — and a refused command must
// not go on to measure anything, because the directory it would measure belongs
// to a server on another host.
func TestBackupStatusProxiedGuardFollowsLocality(t *testing.T) {
for _, tc := range []struct {
name string
sidecar string
wantMeasure bool
}{
{
name: "external topology is refused without measuring",
sidecar: `{"external":{"host":"db.example.com","port":3306}}`,
wantMeasure: false,
},
{
name: "managed local is honored and measures",
sidecar: `{}`,
wantMeasure: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
beadsDir := prepareBackupStatusTest(t)
proxiedServerMode = true
if err := os.WriteFile(configfile.ProxiedServerClientInfoPath(beadsDir), []byte(tc.sidecar), 0o600); err != nil {
t.Fatalf("write proxied sidecar: %v", err)
}
measured := false
sizeDatabase := func(context.Context) (int64, bool, error) {
measured = true
return 1, true, nil
}
cmd := newBackupStatusTestRoot(sizeDatabase)
cmd.SetArgs([]string{"backup", "status"})
_, _, err := executeBackupStatusCommand(t, cmd)
if tc.wantMeasure {
if err != nil {
t.Fatalf("backup status on managed-local: %v", err)
}
} else {
assertExitCode(t, err, 1)
}
if measured != tc.wantMeasure {
t.Fatalf("size provider called = %v, want %v", measured, tc.wantMeasure)
}
})
}
}
// prepareBackupStatusTest builds a throwaway workspace and returns its .beads
// directory, so a caller can drop workspace files (the proxied sidecar) into it.
func prepareBackupStatusTest(t *testing.T) string {
t.Helper()
beadsDir := filepath.Join(t.TempDir(), ".beads")
if err := os.MkdirAll(filepath.Join(beadsDir, "embeddeddolt"), 0o700); err != nil {
t.Fatalf("create workspace marker: %v", err)
}
t.Setenv("BEADS_DIR", beadsDir)
t.Setenv("BD_JSON_ENVELOPE", "0")
initConfigForTest(t)
oldJSONOutput := jsonOutput
oldProxiedServerMode := proxiedServerMode
jsonOutput = false
proxiedServerMode = false
resetCommandContext()
t.Cleanup(func() {
jsonOutput = oldJSONOutput
proxiedServerMode = oldProxiedServerMode
resetCommandContext()
})
return beadsDir
}
func newBackupStatusTestRoot(sizeDatabase backupSizeFunc) *cobra.Command {
root := &cobra.Command{Use: "bd"}
root.PersistentFlags().BoolVar(&jsonOutput, "json", false, "output JSON")
backup := &cobra.Command{Use: "backup"}
backup.AddCommand(newBackupStatusCommand(sizeDatabase))
root.AddCommand(backup)
return root
}
func executeBackupStatusCommand(t *testing.T, cmd *cobra.Command) (string, string, error) {
t.Helper()
stdioMutex.Lock()
defer stdioMutex.Unlock()
captureDir := t.TempDir()
stdoutPath := filepath.Join(captureDir, "stdout")
stderrPath := filepath.Join(captureDir, "stderr")
stdoutFile, err := os.Create(stdoutPath)
if err != nil {
t.Fatalf("create stdout capture: %v", err)
}
stderrFile, err := os.Create(stderrPath)
if err != nil {
_ = stdoutFile.Close()
t.Fatalf("create stderr capture: %v", err)
}
oldStdout := os.Stdout
oldStderr := os.Stderr
var commandErr error
func() {
defer func() {
os.Stdout = oldStdout
os.Stderr = oldStderr
}()
os.Stdout = stdoutFile
os.Stderr = stderrFile
commandErr = cmd.Execute()
}()
if err := stdoutFile.Close(); err != nil {
t.Fatalf("close stdout capture: %v", err)
}
if err := stderrFile.Close(); err != nil {
t.Fatalf("close stderr capture: %v", err)
}
stdout, err := os.ReadFile(stdoutPath)
if err != nil {
t.Fatalf("read stdout capture: %v", err)
}
stderr, err := os.ReadFile(stderrPath)
if err != nil {
t.Fatalf("read stderr capture: %v", err)
}
return string(stdout), string(stderr), commandErr
}
func assertExitCode(t *testing.T, err error, want int) {
t.Helper()
if err == nil {
t.Fatalf("command succeeded, want exit code %d", want)
}
got, ok := exitCodeFromError(err)
if !ok || got != want {
t.Fatalf("command error = %v, want exit code %d", err, want)
}
}