beads/cmd/bd/bootstrap_noninteractive_test.go
matt wilkie ef3ffd45b3
feat(migrate): add --force flag as CLI twin of BD_ALLOW_REMOTE_MIGRATE (#4654)
* feat(migrate): add --force flag as CLI twin of BD_ALLOW_REMOTE_MIGRATE

The remote-migrate gate (#4259) refuses to auto-apply pending schema
migrations to a remote-backed database because two clones migrating
independently fork the schema unrecoverably. Until now the only unlock
was the env var BD_ALLOW_REMOTE_MIGRATE=1, which is awkward at the CLI
and can leak into child processes (git hooks, dolt subprocesses). The
--force flag is the natural CLI form of the designated-migrator escape
hatch: it is set before both autoMigrateOnVersionBump and the main store
open in the root PersistentPreRunE via a package-level bool, so it never
leaks out of the process. The env var remains supported for scripted/CI
use and is documented as a parenthetical alongside the new primary form.

Agent-Signature: claude-sonnet-4-6-high on behalf of matt wilkie

* fix(embeddeddolt): point lenient-gate guidance at bd migrate --force

The soft-fail (warn-and-continue) gate warning for read-only and
working-set-reconcile opens still named the env-var escape hatch; align
it with the new primary form introduced for the strict gate.

Agent-Signature: claude-fable-5-high on behalf of matt wilkie

* fix(migrate): reject --force with preview flags; harden override lifecycle

Review findings on the --force flag (cross-vendor: codex gpt-5.5 + claude
opus):

- --force combined with --dry-run/--inspect must fail before the store
  opens: with the gate overridden, the open itself applies pending
  migrations, so the preview flag would be honored only after the
  destructive work it exists to prevent (codex P2).
- Set-or-clear the override unconditionally so it is self-clearing under
  any future in-process root-command reuse (reviewer nit).
- Lock the agent-safety contract against the new flag form: AgentDirective
  must not embed 'bd migrate --force' any more than the env form (nit).

Agent-Signature: claude-fable-5-high on behalf of matt wilkie
2026-07-07 15:58:18 -07:00

157 lines
4.1 KiB
Go

package main
import (
"bytes"
"os"
"strings"
"testing"
"github.com/steveyegge/beads/internal/storage/schema"
)
// TestIsNonInteractiveBootstrap tests the non-interactive detection logic for bootstrap.
func TestIsNonInteractiveBootstrap(t *testing.T) {
origCI := os.Getenv("CI")
origBDNI := os.Getenv("BD_NON_INTERACTIVE")
defer func() {
os.Setenv("CI", origCI)
os.Setenv("BD_NON_INTERACTIVE", origBDNI)
}()
tests := []struct {
name string
flagValue bool
envCI string
envBDNI string
want bool
}{
{
name: "flag true overrides everything",
flagValue: true,
envCI: "",
envBDNI: "",
want: true,
},
{
name: "BD_NON_INTERACTIVE=1",
flagValue: false,
envCI: "",
envBDNI: "1",
want: true,
},
{
name: "BD_NON_INTERACTIVE=true",
flagValue: false,
envCI: "",
envBDNI: "true",
want: true,
},
{
name: "CI=true",
flagValue: false,
envCI: "true",
envBDNI: "",
want: true,
},
{
name: "CI=1",
flagValue: false,
envCI: "1",
envBDNI: "",
want: true,
},
{
name: "no flag no env falls back to terminal detection",
flagValue: false,
envCI: "",
envBDNI: "",
// In test environment, stdin is piped (not a TTY), so non-interactive
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Setenv("CI", tt.envCI)
os.Setenv("BD_NON_INTERACTIVE", tt.envBDNI)
got := isNonInteractiveBootstrap(tt.flagValue)
if got != tt.want {
t.Errorf("isNonInteractiveBootstrap(%v) with CI=%q BD_NON_INTERACTIVE=%q = %v, want %v",
tt.flagValue, tt.envCI, tt.envBDNI, got, tt.want)
}
})
}
}
// TestIsNonInteractiveBootstrapPrecedence tests that flag takes precedence over env vars.
func TestIsNonInteractiveBootstrapPrecedence(t *testing.T) {
origCI := os.Getenv("CI")
origBDNI := os.Getenv("BD_NON_INTERACTIVE")
defer func() {
os.Setenv("CI", origCI)
os.Setenv("BD_NON_INTERACTIVE", origBDNI)
}()
// Flag true should always win
os.Setenv("CI", "")
os.Setenv("BD_NON_INTERACTIVE", "")
if !isNonInteractiveBootstrap(true) {
t.Error("flag=true should always return true regardless of env")
}
// BD_NON_INTERACTIVE should take precedence over CI
os.Setenv("BD_NON_INTERACTIVE", "1")
os.Setenv("CI", "")
if !isNonInteractiveBootstrap(false) {
t.Error("BD_NON_INTERACTIVE=1 should return true")
}
}
// TestConfirmPromptNonInteractive verifies that confirmPrompt returns true
// when nonInteractive is set, without reading stdin.
func TestConfirmPromptNonInteractive(t *testing.T) {
if !confirmPrompt("Proceed?", true) {
t.Error("confirmPrompt should return true when nonInteractive=true")
}
}
// bd-6dnrw.31: a remote-migrate gate refusal during the bootstrap warmup open
// must produce bootstrap-specific guidance — the gate's generic "adopt: bd
// bootstrap" remedy loops forever when the remote itself is behind.
func TestPrintBootstrapRemoteBehindGuidance(t *testing.T) {
var buf bytes.Buffer
printBootstrapRemoteBehindGuidance(&buf, &schema.RemoteMigrateGateError{
CurrentVersion: 49,
LatestVersion: 50,
Pending: 1,
}, "https://doltremoteapi.example.com/org/beads", "bd bootstrap")
out := buf.String()
for _, want := range []string{
"needs 1 schema migration (v49 -> v50)",
"Re-running `bd bootstrap` will NOT fix this",
"bd migrate --force",
"bd dolt push",
} {
if !strings.Contains(out, want) {
t.Errorf("guidance missing %q in:\n%s", want, out)
}
}
}
// bd-4mpy7: bd init's bootstrap path reuses the same guidance but must name
// the command the operator actually ran in the don't-bother-retrying line.
func TestPrintBootstrapRemoteBehindGuidance_InitWording(t *testing.T) {
var buf bytes.Buffer
printBootstrapRemoteBehindGuidance(&buf, &schema.RemoteMigrateGateError{
CurrentVersion: 49,
LatestVersion: 50,
Pending: 1,
}, "git+https://github.com/example/dotfiles.git", "bd init")
out := buf.String()
if !strings.Contains(out, "Re-running `bd init` will NOT fix this") {
t.Errorf("guidance missing init-specific retry line in:\n%s", out)
}
}