diff --git a/cmd/bd/list.go b/cmd/bd/list.go index 441c00c54..bf5d7a6a3 100644 --- a/cmd/bd/list.go +++ b/cmd/bd/list.go @@ -647,14 +647,20 @@ func runListCore(cmd *cobra.Command, _ []string) error { return nil } - allDeps, _ := activeStore.GetAllDependencyRecords(ctx) - displayPrettyListWithDeps(treeIssues, false, allDeps) + allDeps, depErr := activeStore.GetAllDependencyRecords(ctx) + if depErr != nil && in.depsMode != "" { + return HandleError("loading dependencies for --deps: %v", depErr) + } + displayPrettyListWithDepsMode(treeIssues, false, allDeps, in.depsMode) printSkipLabelsFooter(in.skipLabels) return nil } - allDeps, _ := activeStore.GetAllDependencyRecords(ctx) - displayPrettyListWithDeps(issues, false, allDeps) + allDeps, depErr := activeStore.GetAllDependencyRecords(ctx) + if depErr != nil && in.depsMode != "" { + return HandleError("loading dependencies for --deps: %v", depErr) + } + displayPrettyListWithDepsMode(issues, false, allDeps, in.depsMode) printTruncationHint(truncated, in.effectiveLimit) printSkipLabelsFooter(in.skipLabels) return nil @@ -814,6 +820,12 @@ func init() { listCmd.Flags().Bool("tree", true, "Hierarchical tree format (default: true; use --flat to disable)") listCmd.Flags().Bool("flat", false, "Disable tree format and use legacy flat list output") listCmd.Flags().BoolP("watch", "w", false, "Watch for changes and auto-update display (implies --pretty)") + // --deps annotates the tree with dependency edges and orders siblings by them. + // Bare --deps means "scheduling"; --deps=all also shows knowledge-graph edges. + listCmd.Flags().String("deps", "", "Annotate tree with dependency edges and order siblings by them: 'scheduling' (bare --deps) or 'all'") + if f := listCmd.Flags().Lookup("deps"); f != nil { + f.NoOptDefVal = "scheduling" + } // Metadata filtering (GH#1406) listCmd.Flags().StringArray("metadata-field", nil, "Filter by metadata field (key=value, repeatable)") diff --git a/cmd/bd/list_input.go b/cmd/bd/list_input.go index 7f2c37f46..ecadc5172 100644 --- a/cmd/bd/list_input.go +++ b/cmd/bd/list_input.go @@ -81,6 +81,7 @@ type listInput struct { longFormat bool prettyFormat bool flatFormat bool + depsMode string watchMode bool noPager bool formatStr string @@ -290,6 +291,28 @@ func gatherListInput(cmd *cobra.Command) (listInput, error) { in.noPager, _ = cmd.Flags().GetBool("no-pager") in.readyFlag, _ = cmd.Flags().GetBool("ready") + in.depsMode, _ = cmd.Flags().GetString("deps") + if in.depsMode != "" { + if in.depsMode != "scheduling" && in.depsMode != "all" { + return in, HandleErrorRespectJSON("invalid --deps value %q (valid: scheduling, all)", in.depsMode) + } + // --deps annotates and orders the parent-child tree, so it is meaningful + // only in the tree view. Reject the non-tree output modes rather than + // accept the flag and silently ignore it, then imply the tree view so a + // bare `--deps` renders as intended (mirrors --watch implying --pretty). + switch { + case in.jsonOutput: + return in, HandleErrorRespectJSON("--deps is not supported with --json output") + case in.formatStr != "": + return in, HandleErrorRespectJSON("--deps is not supported with --format output") + case in.flatFormat: + return in, HandleErrorRespectJSON("--deps requires the tree view and cannot be combined with --flat") + case in.watchMode: + return in, HandleErrorRespectJSON("--deps is not supported with --watch") + } + in.prettyFormat = true + } + if in.sortBy != "" { validSortFields := map[string]bool{ "priority": true, "created": true, "updated": true, "closed": true, diff --git a/cmd/bd/list_proxied_server.go b/cmd/bd/list_proxied_server.go index 3b7a79f1a..4e8af41eb 100644 --- a/cmd/bd/list_proxied_server.go +++ b/cmd/bd/list_proxied_server.go @@ -107,7 +107,7 @@ func runListProxiedHierarchicalParent(ctx context.Context, uw uow.UnitOfWork, in return err } - displayPrettyListWithDeps(treeIssues, false, depsByIssueID) + displayPrettyListWithDepsMode(treeIssues, false, depsByIssueID, in.depsMode) printSkipLabelsFooter(in.skipLabels) return nil } @@ -296,7 +296,7 @@ func renderProxiedListText(ctx context.Context, uw uow.UnitOfWork, issues []*typ if err != nil { return err } - displayPrettyListWithDeps(issues, false, depsByIssueID) + displayPrettyListWithDepsMode(issues, false, depsByIssueID, in.depsMode) printTruncationHint(truncated, in.effectiveLimit) printSkipLabelsFooter(in.skipLabels) return nil diff --git a/cmd/bd/list_tree.go b/cmd/bd/list_tree.go index e74fb70ec..75c299d4e 100644 --- a/cmd/bd/list_tree.go +++ b/cmd/bd/list_tree.go @@ -111,13 +111,19 @@ func compareIssuesByPriority(a, b *types.Issue) int { return utils.NaturalCompareIDs(a.ID, b.ID) } -// printPrettyTree recursively prints the issue tree -// Children are sorted by priority (P0 first) for intuitive reading -func printPrettyTree(childrenMap map[string][]*types.Issue, parentID string, prefix string) { +// printPrettyTree recursively prints the issue tree. +// Children are ordered by dependency then priority when dr != nil (--deps), else +// by priority (P0 first) for intuitive reading. When dr is set, each node's +// dependency edges are annotated just beneath it. +func printPrettyTree(childrenMap map[string][]*types.Issue, parentID string, prefix string, dr *depRender) { children := childrenMap[parentID] - // Sort children by priority using same comparison as roots for consistency - slices.SortFunc(children, compareIssuesByPriority) + if dr != nil { + children = orderSiblingsByDeps(children, dr.allDeps) + } else { + // Sort children by priority using same comparison as roots for consistency + slices.SortFunc(children, compareIssuesByPriority) + } for i, child := range children { isLast := i == len(children)-1 @@ -131,7 +137,8 @@ func printPrettyTree(childrenMap map[string][]*types.Issue, parentID string, pre if isLast { extension = " " } - printPrettyTree(childrenMap, child.ID, prefix+extension) + dr.annotationsFor(child.ID, prefix+extension) + printPrettyTree(childrenMap, child.ID, prefix+extension, dr) } } @@ -141,8 +148,16 @@ func displayPrettyList(issues []*types.Issue, showHeader bool) { displayPrettyListWithDeps(issues, showHeader, nil) } -// displayPrettyListWithDeps displays issues in tree format using dependency data +// displayPrettyListWithDeps displays issues in tree format using dependency data. func displayPrettyListWithDeps(issues []*types.Issue, showHeader bool, allDeps map[string][]*types.Dependency) { + displayPrettyListWithDepsMode(issues, showHeader, allDeps, "") +} + +// displayPrettyListWithDepsMode displays issues in tree format. When depsMode is +// "scheduling" or "all", the tree also annotates each node's dependency edges and +// orders siblings by their scheduling dependencies (see orderSiblingsByDeps). An +// empty depsMode is the plain parent-child tree. +func displayPrettyListWithDepsMode(issues []*types.Issue, showHeader bool, allDeps map[string][]*types.Dependency, depsMode string) { if showHeader { // Clear screen and show header fmt.Print("\033[2J\033[H") @@ -159,9 +174,20 @@ func displayPrettyListWithDeps(issues []*types.Issue, showHeader bool, allDeps m roots, childrenMap := buildIssueTreeWithDeps(issues, allDeps) + var dr *depRender + if depsMode != "" { + inView := make(map[string]*types.Issue, len(issues)) + for _, issue := range issues { + inView[issue.ID] = issue + } + dr = &depRender{mode: depsMode, allDeps: allDeps, inView: inView} + roots = orderSiblingsByDeps(roots, allDeps) + } + for _, issue := range roots { fmt.Println(formatPrettyIssue(issue)) - printPrettyTree(childrenMap, issue.ID, "") + dr.annotationsFor(issue.ID, "") + printPrettyTree(childrenMap, issue.ID, "", dr) } // Summary @@ -180,4 +206,7 @@ func displayPrettyListWithDeps(issues []*types.Issue, showHeader bool, allDeps m fmt.Printf("Total: %d issues (%d open, %d in progress)\n", len(issues), openCount, inProgressCount) fmt.Println() fmt.Println("Status: ○ open ◐ in_progress ● blocked ✓ closed ❄ deferred") + if dr != nil { + fmt.Printf("Deps: %s = depends-on / relationship (points to target); siblings ordered so dependencies come first; ↗ = target outside current view\n", depGlyph) + } } diff --git a/cmd/bd/list_tree_deps.go b/cmd/bd/list_tree_deps.go new file mode 100644 index 000000000..e36ce8982 --- /dev/null +++ b/cmd/bd/list_tree_deps.go @@ -0,0 +1,201 @@ +package main + +import ( + "cmp" + "fmt" + "slices" + "strings" + + "github.com/steveyegge/beads/internal/types" + "github.com/steveyegge/beads/internal/ui" + "github.com/steveyegge/beads/internal/utils" +) + +// depGlyph marks a dependency/relationship edge in the --deps tree view. It is +// deliberately distinct from the parent-child connectors (├── └──) so a +// dependency can never be mistaken for hierarchy (the confusion GH#4686 fixed). +const depGlyph = "╌╌▷" + +// depRender carries the state needed to annotate and order a --deps tree. +// A nil *depRender means the feature is off; its methods are nil-safe. +type depRender struct { + mode string // "scheduling" or "all" + allDeps map[string][]*types.Dependency // outgoing edges keyed by issue_id + inView map[string]*types.Issue // displayed issues, for titles + in-view test +} + +// depEdgeDisplay classifies a dependency edge for the --deps view. +// - label: how the edge reads from the source node's perspective. A stored +// "blocks" edge (issue_id depends on depends_on_id) reads as "depends-on", +// matching `bd show`. +// - scheduling: true for edges that constrain execution order (depends-on, +// conditional-blocks, waits-for). Only these drive sibling ordering. +// - ok: false for parent-child, which is hierarchy and never appears here. +func depEdgeDisplay(t types.DependencyType) (label string, scheduling, ok bool) { + switch t { + case types.DepParentChild: + return "", false, false + case types.DepBlocks: + return "depends-on", true, true + case types.DepConditionalBlocks: + return "conditional-blocks", true, true + case types.DepWaitsFor: + return "waits-for", true, true + case types.DepRelated, types.DepRelatesTo: + return "related", false, true + case types.DepDiscoveredFrom: + return "discovered-from", false, true + case types.DepDuplicates: + return "duplicates", false, true + case types.DepSupersedes: + return "supersedes", false, true + case types.DepRepliesTo: + return "replies-to", false, true + default: + return string(t), false, true + } +} + +// orderSiblingsByDeps reorders a sibling group so that, within the group, an +// issue that depends on another (via a scheduling edge) sorts after it — giving +// a top-to-bottom reading that is a valid execution order. Ties and dependency +// cycles fall back to compareIssuesByPriority (priority, then natural ID), so +// the result is always total and never hangs on a cycle. Ordering is driven by +// scheduling edges only, regardless of --deps mode: knowledge-graph edges +// (related, discovered-from, ...) carry no ordering meaning. +func orderSiblingsByDeps(siblings []*types.Issue, allDeps map[string][]*types.Dependency) []*types.Issue { + if len(siblings) < 2 || allDeps == nil { + return siblings + } + + byID := make(map[string]*types.Issue, len(siblings)) + for _, s := range siblings { + byID[s.ID] = s + } + + // indeg[N] = count of in-group scheduling targets N must come after. + // dependents[T] = in-group issues that depend on T (T unblocks them). + indeg := make(map[string]int, len(siblings)) + dependents := make(map[string][]string) + for _, s := range siblings { + seen := make(map[string]bool) + for _, dep := range allDeps[s.ID] { + if _, scheduling, ok := depEdgeDisplay(dep.Type); !ok || !scheduling { + continue + } + t := dep.DependsOnID + if t == s.ID || byID[t] == nil || seen[t] { + continue + } + seen[t] = true + indeg[s.ID]++ + dependents[t] = append(dependents[t], s.ID) + } + } + + // Kahn's algorithm with a priority-ordered ready set for a stable result. + ready := make([]*types.Issue, 0, len(siblings)) + for _, s := range siblings { + if indeg[s.ID] == 0 { + ready = append(ready, s) + } + } + slices.SortFunc(ready, compareIssuesByPriority) + + out := make([]*types.Issue, 0, len(siblings)) + emitted := make(map[string]bool, len(siblings)) + for len(ready) > 0 { + n := ready[0] + ready = ready[1:] + out = append(out, n) + emitted[n.ID] = true + grew := false + for _, dID := range dependents[n.ID] { + indeg[dID]-- + if indeg[dID] == 0 { + ready = append(ready, byID[dID]) + grew = true + } + } + if grew { + slices.SortFunc(ready, compareIssuesByPriority) + } + } + + // Cycle fallback: emit any remaining nodes in priority/ID order. + if len(out) < len(siblings) { + rest := make([]*types.Issue, 0, len(siblings)-len(out)) + for _, s := range siblings { + if !emitted[s.ID] { + rest = append(rest, s) + } + } + slices.SortFunc(rest, compareIssuesByPriority) + out = append(out, rest...) + } + return out +} + +// annotationsFor prints the dependency-edge annotation rows for a node, indented +// to childPrefix so they align with (and sit just above) the node's children. +// No-op when the receiver is nil (--deps off). +func (dr *depRender) annotationsFor(nodeID, childPrefix string) { + if dr == nil { + return + } + + type row struct{ label, target, title string } + var inView []row + var outView []string + seen := make(map[string]bool) + for _, dep := range dr.allDeps[nodeID] { + label, scheduling, ok := depEdgeDisplay(dep.Type) + if !ok { + continue // parent-child: hierarchy, not a dependency + } + if dr.mode != "all" && !scheduling { + continue // scheduling mode hides knowledge-graph edges + } + key := string(dep.Type) + "\x00" + dep.DependsOnID + if seen[key] { + continue + } + seen[key] = true + if issue := dr.inView[dep.DependsOnID]; issue != nil { + inView = append(inView, row{label: label, target: dep.DependsOnID, title: issue.Title}) + } else { + outView = append(outView, dep.DependsOnID) + } + } + if len(inView) == 0 && len(outView) == 0 { + return + } + + // In-view edges: one legible, greppable row each (label, then natural ID). + slices.SortFunc(inView, func(a, b row) int { + if a.label != b.label { + return cmp.Compare(a.label, b.label) + } + return utils.NaturalCompareIDs(a.target, b.target) + }) + for _, r := range inView { + tag := ui.RenderMuted(fmt.Sprintf("%s %-20s", depGlyph, "["+r.label+"]")) + fmt.Println(childPrefix + tag + " " + r.target + " " + r.title) + } + + // Out-of-view edges: collapse to a single summary line so a filtered view + // (--parent/--type) isn't buried under rows whose targets aren't on screen, + // while still signaling that cross-view dependencies exist and naming them. + if len(outView) > 0 { + slices.SortFunc(outView, utils.NaturalCompareIDs) + const maxNamed = 4 + named, suffix := outView, "" + if len(outView) > maxNamed { + named = outView[:maxNamed] + suffix = fmt.Sprintf(", +%d more", len(outView)-maxNamed) + } + summary := fmt.Sprintf("%s ↗ %d outside this view: %s%s", + depGlyph, len(outView), strings.Join(named, ", "), suffix) + fmt.Println(childPrefix + ui.RenderMuted(summary)) + } +} diff --git a/cmd/bd/list_tree_deps_test.go b/cmd/bd/list_tree_deps_test.go new file mode 100644 index 000000000..a6b3f08fa --- /dev/null +++ b/cmd/bd/list_tree_deps_test.go @@ -0,0 +1,262 @@ +package main + +import ( + "strings" + "testing" + + "github.com/spf13/cobra" + "github.com/steveyegge/beads/internal/types" +) + +func schedDep(from, to string) *types.Dependency { + return &types.Dependency{IssueID: from, DependsOnID: to, Type: types.DepBlocks} +} + +func idsOf(issues []*types.Issue) []string { + out := make([]string, len(issues)) + for i, is := range issues { + out[i] = is.ID + } + return out +} + +func indexOf(ids []string, id string) int { + for i, v := range ids { + if v == id { + return i + } + } + return -1 +} + +func TestOrderSiblingsByDeps_DependencyBeatsPriority(t *testing.T) { + // B is higher priority (P0) than A (P2) but B depends on A, so A must sort + // first — dependency order overrides priority. + a := &types.Issue{ID: "pa-a", Priority: 2} + b := &types.Issue{ID: "pa-b", Priority: 0} + deps := map[string][]*types.Dependency{"pa-b": {schedDep("pa-b", "pa-a")}} + + got := idsOf(orderSiblingsByDeps([]*types.Issue{b, a}, deps)) + if indexOf(got, "pa-a") > indexOf(got, "pa-b") { + t.Fatalf("expected pa-a before pa-b (dependency order), got %v", got) + } +} + +func TestOrderSiblingsByDeps_ChainIsTopological(t *testing.T) { + // c -> b -> a (c depends on b depends on a). Expect a, b, c. + a := &types.Issue{ID: "pa-a", Priority: 1} + b := &types.Issue{ID: "pa-b", Priority: 1} + c := &types.Issue{ID: "pa-c", Priority: 1} + deps := map[string][]*types.Dependency{ + "pa-c": {schedDep("pa-c", "pa-b")}, + "pa-b": {schedDep("pa-b", "pa-a")}, + } + got := idsOf(orderSiblingsByDeps([]*types.Issue{c, b, a}, deps)) + if indexOf(got, "pa-a") > indexOf(got, "pa-b") || indexOf(got, "pa-b") > indexOf(got, "pa-c") { + t.Fatalf("expected topological order a,b,c, got %v", got) + } +} + +func TestOrderSiblingsByDeps_CycleFallsBackWithoutDropping(t *testing.T) { + // a <-> b cycle: must not hang and must not drop either node. + a := &types.Issue{ID: "pa-a", Priority: 1} + b := &types.Issue{ID: "pa-b", Priority: 1} + deps := map[string][]*types.Dependency{ + "pa-a": {schedDep("pa-a", "pa-b")}, + "pa-b": {schedDep("pa-b", "pa-a")}, + } + got := idsOf(orderSiblingsByDeps([]*types.Issue{b, a}, deps)) + if len(got) != 2 || indexOf(got, "pa-a") < 0 || indexOf(got, "pa-b") < 0 { + t.Fatalf("cycle must preserve all nodes, got %v", got) + } +} + +func TestOrderSiblingsByDeps_OutOfGroupEdgeIgnored(t *testing.T) { + // A depends on something outside the sibling group: no reordering, no panic. + a := &types.Issue{ID: "pa-a", Priority: 2} + b := &types.Issue{ID: "pa-b", Priority: 1} + deps := map[string][]*types.Dependency{"pa-a": {schedDep("pa-a", "pa-external")}} + got := idsOf(orderSiblingsByDeps([]*types.Issue{a, b}, deps)) + // Falls back to priority: b (P1) before a (P2). + if indexOf(got, "pa-b") > indexOf(got, "pa-a") { + t.Fatalf("expected priority order b,a for out-of-group edge, got %v", got) + } +} + +func TestDepEdgeDisplay(t *testing.T) { + cases := []struct { + name string + t types.DependencyType + label string + scheduling bool + ok bool + }{ + {"parent-child", types.DepParentChild, "", false, false}, + {"blocks", types.DepBlocks, "depends-on", true, true}, + {"conditional-blocks", types.DepConditionalBlocks, "conditional-blocks", true, true}, + {"waits-for", types.DepWaitsFor, "waits-for", true, true}, + {"related", types.DepRelated, "related", false, true}, + {"relates-to", types.DepRelatesTo, "related", false, true}, + {"discovered-from", types.DepDiscoveredFrom, "discovered-from", false, true}, + {"duplicates", types.DepDuplicates, "duplicates", false, true}, + {"supersedes", types.DepSupersedes, "supersedes", false, true}, + {"replies-to", types.DepRepliesTo, "replies-to", false, true}, + {"custom-type", types.DependencyType("some-custom-type"), "some-custom-type", false, true}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + label, sched, ok := depEdgeDisplay(c.t) + if label != c.label || sched != c.scheduling || ok != c.ok { + t.Errorf("depEdgeDisplay(%q) = (%q,%v,%v), want (%q,%v,%v)", + c.t, label, sched, ok, c.label, c.scheduling, c.ok) + } + }) + } +} + +func TestAnnotationsFor_InViewRowsAndOutOfViewSummary(t *testing.T) { + // pa-x: one in-view dep (pa-a) + five out-of-view deps. The in-view edge is a + // full row; the five out-of-view collapse to one summary naming four + "1 more". + inView := map[string]*types.Issue{ + "pa-x": {ID: "pa-x"}, + "pa-a": {ID: "pa-a", Title: "Alpha"}, + } + deps := map[string][]*types.Dependency{"pa-x": { + schedDep("pa-x", "pa-a"), + schedDep("pa-x", "pa-o1"), schedDep("pa-x", "pa-o2"), + schedDep("pa-x", "pa-o3"), schedDep("pa-x", "pa-o4"), schedDep("pa-x", "pa-o5"), + }} + dr := &depRender{mode: "scheduling", allDeps: deps, inView: inView} + + out := captureStdout(t, func() error { dr.annotationsFor("pa-x", ""); return nil }) + + if !strings.Contains(out, "pa-a") || !strings.Contains(out, "Alpha") { + t.Errorf("expected in-view row for pa-a Alpha, got:\n%s", out) + } + if !strings.Contains(out, "depends-on") { + t.Errorf("expected [depends-on] label, got:\n%s", out) + } + if !strings.Contains(out, "5 outside this view") || !strings.Contains(out, "+1 more") { + t.Errorf("expected collapsed out-of-view summary with +1 more, got:\n%s", out) + } + // The out-of-view targets must NOT appear as their own full rows. + if strings.Count(out, "\n") > 2 { + t.Errorf("expected 2 lines (1 in-view + 1 summary), got:\n%s", out) + } +} + +func TestAnnotationsFor_ModeFiltersKnowledgeGraph(t *testing.T) { + inView := map[string]*types.Issue{ + "pa-x": {ID: "pa-x"}, + "pa-r": {ID: "pa-r", Title: "Related thing"}, + } + deps := map[string][]*types.Dependency{"pa-x": { + {IssueID: "pa-x", DependsOnID: "pa-r", Type: types.DepRelated}, + }} + + sched := captureStdout(t, func() error { + (&depRender{mode: "scheduling", allDeps: deps, inView: inView}).annotationsFor("pa-x", "") + return nil + }) + if strings.Contains(sched, "pa-r") { + t.Errorf("scheduling mode must hide 'related' edge, got:\n%s", sched) + } + + all := captureStdout(t, func() error { + (&depRender{mode: "all", allDeps: deps, inView: inView}).annotationsFor("pa-x", "") + return nil + }) + if !strings.Contains(all, "related") || !strings.Contains(all, "pa-r") { + t.Errorf("all mode must show 'related' edge, got:\n%s", all) + } +} + +func TestAnnotationsFor_NilReceiverIsNoOp(t *testing.T) { + var dr *depRender + out := captureStdout(t, func() error { dr.annotationsFor("pa-x", ""); return nil }) + if out != "" { + t.Errorf("nil depRender must print nothing, got:\n%s", out) + } +} + +// newListInputTestCmd builds a cobra command carrying only the flags the --deps +// combination checks read. gatherListInput reads many other flags, but GetString +// / GetBool on an unregistered flag return zero values, so this stays minimal. +func newListInputTestCmd() *cobra.Command { + cmd := &cobra.Command{Use: "list"} + f := cmd.Flags() + f.String("deps", "", "") + f.String("format", "", "") + f.Bool("flat", false, "") + f.Bool("watch", false, "") + f.Bool("tree", false, "") + f.Bool("pretty", false, "") + return cmd +} + +// TestGatherListInput_DepsRejectsNonTreeOutputs guards the accept-and-ignore +// regression: --deps annotates the tree, so pairing it with a non-tree output +// mode must error rather than be silently dropped. +func TestGatherListInput_DepsRejectsNonTreeOutputs(t *testing.T) { + cases := []struct { + name string + flag string + val string + }{ + {"json", "format", "json"}, + {"format", "format", "{{.ID}}"}, + {"flat", "flat", "true"}, + {"watch", "watch", "true"}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + defer func(prev bool) { jsonOutput = prev }(jsonOutput) + jsonOutput = false + cmd := newListInputTestCmd() + if err := cmd.Flags().Set("deps", "scheduling"); err != nil { + t.Fatalf("set deps: %v", err) + } + if err := cmd.Flags().Set(c.flag, c.val); err != nil { + t.Fatalf("set %s: %v", c.flag, err) + } + if _, err := gatherListInput(cmd); err == nil { + t.Fatalf("--deps with --%s must be rejected, got nil error", c.name) + } + }) + } +} + +// TestGatherListInput_DepsImpliesTree confirms a bare --deps is accepted and +// turns on the tree view (so it is not silently ignored on the plain-list path). +func TestGatherListInput_DepsImpliesTree(t *testing.T) { + defer func(prev bool) { jsonOutput = prev }(jsonOutput) + jsonOutput = false + cmd := newListInputTestCmd() + if err := cmd.Flags().Set("deps", "scheduling"); err != nil { + t.Fatalf("set deps: %v", err) + } + in, err := gatherListInput(cmd) + if err != nil { + t.Fatalf("bare --deps must be accepted, got error: %v", err) + } + if in.depsMode != "scheduling" { + t.Errorf("depsMode = %q, want scheduling", in.depsMode) + } + if !in.prettyFormat { + t.Error("--deps must imply the tree view (prettyFormat), got false") + } +} + +// TestGatherListInput_DepsRejectsInvalidValue keeps the value validation covered +// alongside the new combination checks. +func TestGatherListInput_DepsRejectsInvalidValue(t *testing.T) { + defer func(prev bool) { jsonOutput = prev }(jsonOutput) + jsonOutput = false + cmd := newListInputTestCmd() + if err := cmd.Flags().Set("deps", "bogus"); err != nil { + t.Fatalf("set deps: %v", err) + } + if _, err := gatherListInput(cmd); err == nil { + t.Fatal("--deps=bogus must be rejected, got nil error") + } +}