beads/cmd/bd/prune_embedded_test.go
Jim Wordelman 8b691beaea
feat(prune): add bd prune for deleting closed non-ephemeral beads (#3353)
* feat(prune): add `bd prune` for deleting closed non-ephemeral beads

`bd purge` is scoped to closed ephemeral beads (wisps, transient
molecules). That leaves no built-in way to trim the usual long-lived
cruft: closed tasks/features/bugs/chores that stick around forever,
bloat auto-export, and make `bd list` and other queries slower over
time. Users who don't care about closed history eventually want a way
to drop it.

Add `bd prune` with the same surface as `bd purge` but the opposite
scope — closed non-ephemeral beads.

- Extracted runPurgeOrPrune from the purgeCmd closure. Both commands
  now share filter plumbing, preview/dry-run/force semantics, stats
  printing, and embedded-mode commit-flush. The two cobra commands
  differ only in name, scope, messaging, and safety gate.
- purge's filter stays `Ephemeral: &true`; prune's is `Ephemeral:
  &false`. Scopes are deliberately disjoint: prune never touches what
  purge would, and vice versa.
- Safety gate: `bd prune` requires `--older-than` or `--pattern`. A
  muscle-memory `bd prune --force` would otherwise wipe every closed
  bead in the repo. `--older-than 1d` is the documented escape hatch
  for "sweep everything closed for at least a day".
- Shared flags: --force, --dry-run, --older-than, --pattern. Same
  dependency/label/event reporting, pinned-bead skipping, and
  commandDidWrite tracking as purge.

`bd purge --help` now cross-references `bd prune` (and vice versa) so
users find the right command for their scope.

Tests (cmd/bd/prune_embedded_test.go, gated by BEADS_TEST_EMBEDDED_DOLT=1
like the existing purge tests):

- prune_requires_older_than_or_pattern — safety gate rejects bare
  `--force` and leaves data intact.
- prune_deletes_closed_non_ephemeral — happy path: scoped prune
  removes the target.
- prune_leaves_closed_ephemeral_for_purge — cross-scope invariant:
  prune must not touch wisps; they remain visible to `bd purge`.
- prune_leaves_open_beads_alone — open beads survive even under a
  matching pattern.
- prune_dry_run_reports_without_deleting — dry-run prints preview
  and keeps data intact.
- prune_preview_fails_without_force — preview mode (no --force)
  aborts with a preview message, no data lost.
- prune_older_than_skips_recent — `--older-than 30d` excludes
  just-closed beads.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Fix prune test and polish output

Replace the broken list --ephemeral assertion with a purge dry-run check, and make prune's JSON and help text match its scope.

Co-authored-by: Jim Wordelman <91582+quad341@users.noreply.github.com>

* Tighten prune ephemeral assertion

Assert prune skips closed wisps by scope without relying on a later purge process that auto-imports from issues.jsonl.

Co-authored-by: Jim Wordelman <91582+quad341@users.noreply.github.com>

---------

Co-authored-by: Jim Wordelman <jim@wordelman.name>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
Co-authored-by: Jim Wordelman <91582+quad341@users.noreply.github.com>
2026-04-24 06:44:43 -07:00

181 lines
6 KiB
Go

//go:build cgo
package main
import (
"os"
"os/exec"
"strings"
"testing"
)
// bdPrune runs "bd prune" with the given args and returns stdout.
func bdPrune(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"prune"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd prune %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out)
}
// bdPruneFail runs "bd prune" expecting failure and returns combined output.
func bdPruneFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"prune"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected bd prune %s to fail, got success:\n%s", strings.Join(args, " "), out)
}
return string(out)
}
// createAndClose creates a non-ephemeral task issue and closes it. Returns the ID.
func createAndClose(t *testing.T, bd, dir, title string) string {
t.Helper()
issue := bdCreate(t, bd, dir, title)
bdClose(t, bd, dir, issue.ID)
return issue.ID
}
func TestEmbeddedPrune(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
// ===== Safety gate =====
t.Run("prune_requires_older_than_or_pattern", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "pr")
createAndClose(t, bd, dir, "Some closed task")
// `bd prune --force` alone should fail — the gate blocks it.
out := bdPruneFail(t, bd, dir, "--force")
if !strings.Contains(out, "--older-than or --pattern") {
t.Errorf("expected safety-gate error to mention --older-than or --pattern, got: %s", out)
}
// Confirm the bead wasn't deleted.
listing := bdList(t, bd, dir, "--status=closed", "--json")
if !strings.Contains(listing, "Some closed task") {
t.Error("bd prune --force without a filter must be a no-op, but the bead was deleted")
}
})
// ===== Scope: non-ephemeral closed only =====
t.Run("prune_deletes_closed_non_ephemeral", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "pnx")
target := createAndClose(t, bd, dir, "Closed non-ephemeral task")
// --pattern is a scope filter AND the safety gate.
out := bdPrune(t, bd, dir, "--pattern", "pnx-*", "--force")
if !strings.Contains(out, "Pruned") && !strings.Contains(out, "pruned") {
t.Errorf("expected success message, got: %s", out)
}
// The target should be gone from the closed list.
listing := bdList(t, bd, dir, "--status=closed", "--json")
if strings.Contains(listing, target) {
t.Errorf("expected %s to be deleted, still present in: %s", target, listing)
}
})
// ===== Scope: must NOT touch ephemeral =====
t.Run("prune_leaves_closed_ephemeral_for_purge", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "pw")
wispID := createAndCloseEphemeral(t, bd, dir, "Closed wisp")
out := bdPrune(t, bd, dir, "--pattern", "pw-*", "--force")
// Prune's non-ephemeral scope must not match the wisp at all.
if strings.Contains(out, wispID) {
t.Errorf("prune wrongly touched ephemeral %s: %s", wispID, out)
}
if !strings.Contains(strings.ToLower(out), "no closed beads to prune") {
t.Errorf("expected prune to skip closed ephemeral %s; got: %s", wispID, out)
}
})
// ===== Scope: must NOT touch open beads =====
t.Run("prune_leaves_open_beads_alone", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "po")
openID := bdCreate(t, bd, dir, "Still-open task").ID
out := bdPrune(t, bd, dir, "--pattern", "po-*", "--force")
if strings.Contains(out, openID) {
t.Errorf("prune wrongly touched open bead %s: %s", openID, out)
}
})
// ===== Dry-run shows stats, doesn't touch data =====
t.Run("prune_dry_run_reports_without_deleting", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "pd")
target := createAndClose(t, bd, dir, "For dry-run")
out := bdPrune(t, bd, dir, "--pattern", "pd-*", "--dry-run")
if !strings.Contains(out, "Would prune") {
t.Errorf("expected 'Would prune' in dry-run output, got: %s", out)
}
// The bead must still exist.
listing := bdList(t, bd, dir, "--status=closed", "--json")
if !strings.Contains(listing, target) {
t.Errorf("dry-run deleted %s; listing: %s", target, listing)
}
})
// ===== Preview mode (no --force) fails without deleting =====
t.Run("prune_preview_fails_without_force", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "pp")
target := createAndClose(t, bd, dir, "For preview")
// Passing a scope filter but no --force: preview-and-abort path.
out := bdPruneFail(t, bd, dir, "--pattern", "pp-*")
if !strings.Contains(out, "would prune") && !strings.Contains(out, "Found") {
t.Errorf("expected preview output, got: %s", out)
}
listing := bdList(t, bd, dir, "--status=closed", "--json")
if !strings.Contains(listing, target) {
t.Errorf("preview deleted %s; listing: %s", target, listing)
}
})
// ===== --older-than excludes recent beads =====
t.Run("prune_older_than_skips_recent", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "pot")
// Just-closed — should NOT match --older-than 30d.
target := createAndClose(t, bd, dir, "Recent close")
out := bdPrune(t, bd, dir, "--older-than", "30d", "--force")
if strings.Contains(out, target) {
t.Errorf("prune with --older-than 30d wrongly touched just-closed %s: %s", target, out)
}
// A "no beads to prune" message (or a zero-count result) is the
// expected outcome.
lower := strings.ToLower(out)
if !strings.Contains(lower, "no ") && !strings.Contains(lower, "0 ") {
// Not fatal — wording may vary. Ensure target survives.
listing := bdList(t, bd, dir, "--status=closed", "--json")
if !strings.Contains(listing, target) {
t.Errorf("--older-than 30d deleted recent bead %s; listing: %s", target, listing)
}
}
})
}