beads/cmd/bd/delete_input_test.go
Steve Yegge b5fc63786f
fix(delete): honor cascade policy in proxied-server delete (bd-paurh) (#5371)
* fix(delete): honor cascade policy in proxied-server delete (bd-paurh)

Proxied `bd delete` passed Cascade:true unconditionally and refused
--cascade outright, so `bd delete <gate> --force` (wh-gate-sweep's
delivery-ledger purge) destroyed the gated bead and everything
downstream of it. Bring proxied delete to classic (embedded) parity by
implementing the documented-but-unimplemented EnforceCascadePolicy in
domain deleteMany:

- Cascade=true                -> delete all transitive dependents
- Cascade=false, Force=false  -> refuse with *DeleteBlockedError (classic
  message, blocking dependents on result.OrphanedIssues), before any
  mutation
- Cascade=false, Force=true   -> orphan external dependents, delete only
  the named IDs, dep links to/from them cleaned up
- EnforceCascadePolicy=false  -> legacy behavior preserved exactly for
  the wisp/mol/gc/purge proxied paths and single-ID wrappers

CLI: accept --cascade in proxied mode, thread cascade/force through
preview and delete with EnforceCascadePolicy:true, render classic-style
messaging (cascade warning only when requested; refusal says how to
proceed), and add JSON fields (cascade, would_orphan, error,
orphaned_issues) without removing any.

Tests: domain unit tests for all four policy combos; proxied delete
integration reworked to pin refusal/cascade/orphan parity plus a new
force-orphan integration test; output contract tests extended; the
bd-04vav characterization subtest flipped from pinning the divergent
blast radius (targetSurvives=false) to asserting parity
(wantProxied = wantClassic) — the acceptance test of this fix.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(delete): emit one JSON doc on blocked --json preview (bd-paurh)

Review follow-up from #5371 (lion): the blocked refusal in --json mode
emitted the preview payload (error key included) and then
jsonStdoutError's second doc on top — two JSON documents on stdout.
Return exitError directly when blocked+jsonOutput; integration test pins
the single-doc contract. Also documents the deliberate wisp-as-named-
target divergence from embedded on EnforceCascadePolicy.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y9gMLK4xKXja9VbiH3NxV1

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-06 03:15:55 -07:00

141 lines
4.1 KiB
Go

package main
import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/spf13/cobra"
)
func TestReadIssueIDsFromFile(t *testing.T) {
tmpDir := t.TempDir()
t.Run("read valid IDs from file", func(t *testing.T) {
testFile := filepath.Join(tmpDir, "ids.txt")
content := "bd-1\nbd-2\nbd-3\n"
if err := os.WriteFile(testFile, []byte(content), 0o644); err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
ids, err := readIssueIDsFromFile(testFile)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(ids, []string{"bd-1", "bd-2", "bd-3"}) {
t.Errorf("IDs: got %v, want [bd-1 bd-2 bd-3]", ids)
}
})
t.Run("skip empty lines and comments", func(t *testing.T) {
testFile := filepath.Join(tmpDir, "ids_with_comments.txt")
content := "bd-1\n\n# This is a comment\nbd-2\n \nbd-3\n"
if err := os.WriteFile(testFile, []byte(content), 0o644); err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
ids, err := readIssueIDsFromFile(testFile)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(ids, []string{"bd-1", "bd-2", "bd-3"}) {
t.Errorf("IDs: got %v, want [bd-1 bd-2 bd-3]", ids)
}
})
t.Run("handle non-existent file", func(t *testing.T) {
_, err := readIssueIDsFromFile(filepath.Join(tmpDir, "nonexistent.txt"))
if err == nil {
t.Error("Expected error for non-existent file")
}
})
}
func TestUniqueStrings(t *testing.T) {
t.Run("remove duplicates while preserving the first occurrence order", func(t *testing.T) {
got := uniqueStrings([]string{"a", "b", "a", "c", "b", "d"})
want := []string{"a", "b", "c", "d"}
if !reflect.DeepEqual(got, want) {
t.Errorf("uniqueStrings(): got %v, want %v", got, want)
}
})
t.Run("handle empty input", func(t *testing.T) {
if got := uniqueStrings([]string{}); len(got) != 0 {
t.Errorf("Expected empty result, got %d items", len(got))
}
})
t.Run("handle all unique", func(t *testing.T) {
got := uniqueStrings([]string{"a", "b", "c"})
want := []string{"a", "b", "c"}
if !reflect.DeepEqual(got, want) {
t.Errorf("uniqueStrings(): got %v, want %v", got, want)
}
})
}
func TestGatherDeleteInput(t *testing.T) {
oldJSONOutput, oldQuiet := jsonOutput, quietFlag
t.Cleanup(func() { jsonOutput, quietFlag = oldJSONOutput, oldQuiet })
newCommand := func(t *testing.T) *cobra.Command {
t.Helper()
cmd := &cobra.Command{}
cmd.Flags().String("from-file", "", "")
cmd.Flags().Bool("force", false, "")
cmd.Flags().Bool("dry-run", false, "")
cmd.Flags().Bool("cascade", false, "")
return cmd
}
t.Run("merges positional and file IDs with stable deduplication and projects flags", func(t *testing.T) {
idsPath := filepath.Join(t.TempDir(), "ids.txt")
if err := os.WriteFile(idsPath, []byte(strings.Join([]string{"bd-file", "bd-shared", "bd-file"}, "\n")), 0o600); err != nil {
t.Fatalf("write IDs file: %v", err)
}
cmd := newCommand(t)
for name, value := range map[string]string{"from-file": idsPath, "force": "true", "dry-run": "true"} {
if err := cmd.Flags().Set(name, value); err != nil {
t.Fatalf("set %s: %v", name, err)
}
}
jsonOutput, quietFlag = true, true
got, err := gatherDeleteInput(cmd, []string{"bd-arg", "bd-shared", "bd-arg"})
if err != nil {
t.Fatalf("gatherDeleteInput: %v", err)
}
want := &deleteInput{
ids: []string{"bd-arg", "bd-shared", "bd-file"},
force: true,
dryRun: true,
jsonOutput: true,
quiet: true,
}
if !reflect.DeepEqual(got, want) {
t.Errorf("gatherDeleteInput(): got %#v, want %#v", got, want)
}
})
t.Run("accepts cascade and projects it (embedded parity, bd-paurh)", func(t *testing.T) {
cmd := newCommand(t)
if err := cmd.Flags().Set("cascade", "true"); err != nil {
t.Fatalf("set cascade: %v", err)
}
jsonOutput, quietFlag = false, false
got, err := gatherDeleteInput(cmd, []string{"bd-target"})
if err != nil {
t.Fatalf("gatherDeleteInput() with --cascade: %v", err)
}
want := &deleteInput{ids: []string{"bd-target"}, cascade: true}
if !reflect.DeepEqual(got, want) {
t.Errorf("gatherDeleteInput(): got %#v, want %#v", got, want)
}
})
}