diff --git a/cmd/bd/list_helpers_test.go b/cmd/bd/list_helpers_test.go index 763934a80..5d1d813c9 100644 --- a/cmd/bd/list_helpers_test.go +++ b/cmd/bd/list_helpers_test.go @@ -327,6 +327,50 @@ func TestListDisplayPrettyListHonorsSort(t *testing.T) { t.Fatalf("tree siblings ignore --sort closed:\n%s", out) } }) + + t.Run("deps scheduling", func(t *testing.T) { + // At both levels the --deps tree orders (roots, and one parent's + // children): late depends on dep, so dep prints first although --sort + // closed alone would lead with the newer late; free has no edge, so + // --sort closed puts it ahead of dep, where priority would put it last. + oldest := time.Now().Add(-3 * time.Hour) + parentAt := time.Now().Add(-4 * time.Hour) + parent := &types.Issue{ID: "bd-parent", Title: "Parent", Status: types.StatusClosed, Priority: 2, IssueType: types.TypeEpic, ClosedAt: &parentAt} + rootDep := &types.Issue{ID: "bd-root-dep", Title: "Root dependency", Status: types.StatusClosed, Priority: 0, IssueType: types.TypeTask, ClosedAt: &oldest} + rootLate := &types.Issue{ID: "bd-root-late", Title: "Root dependent", Status: types.StatusClosed, Priority: 1, IssueType: types.TypeTask, ClosedAt: &newer} + rootFree := &types.Issue{ID: "bd-root-free", Title: "Root unlinked", Status: types.StatusClosed, Priority: 2, IssueType: types.TypeTask, ClosedAt: &older} + childDep := &types.Issue{ID: "bd-child-dep", Title: "Child dependency", Status: types.StatusClosed, Priority: 0, IssueType: types.TypeTask, ClosedAt: &oldest} + childLate := &types.Issue{ID: "bd-child-late", Title: "Child dependent", Status: types.StatusClosed, Priority: 1, IssueType: types.TypeTask, ClosedAt: &newer} + childFree := &types.Issue{ID: "bd-child-free", Title: "Child unlinked", Status: types.StatusClosed, Priority: 2, IssueType: types.TypeTask, ClosedAt: &older} + deps := map[string][]*types.Dependency{ + rootLate.ID: {{IssueID: rootLate.ID, DependsOnID: rootDep.ID, Type: types.DepBlocks}}, + childDep.ID: {{IssueID: childDep.ID, DependsOnID: parent.ID, Type: types.DepParentChild}}, + childFree.ID: {{IssueID: childFree.ID, DependsOnID: parent.ID, Type: types.DepParentChild}}, + childLate.ID: { + {IssueID: childLate.ID, DependsOnID: parent.ID, Type: types.DepParentChild}, + {IssueID: childLate.ID, DependsOnID: childDep.ID, Type: types.DepBlocks}, + }, + } + issues := []*types.Issue{parent, rootDep, rootLate, rootFree, childDep, childLate, childFree} + + out := captureStdout(t, func() error { + displayPrettyListWithDepsMode(issues, false, deps, "scheduling", false, false, "closed", "closed", false, nil) + return nil + }) + // A dependency's id also appears on its dependent's annotation row; the + // first occurrence is still the dependency's own row whenever it prints first. + for _, want := range [][]string{ + {rootFree.ID, rootDep.ID, rootLate.ID}, + {childFree.ID, childDep.ID, childLate.ID}, + } { + for i := 1; i < len(want); i++ { + before, after := strings.Index(out, want[i-1]), strings.Index(out, want[i]) + if before < 0 || after < 0 || before > after { + t.Fatalf("--deps scheduling --sort closed: want %s before %s:\n%s", want[i-1], want[i], out) + } + } + } + }) } func TestListDisplayPrettyList(t *testing.T) { diff --git a/cmd/bd/list_tree_deps_test.go b/cmd/bd/list_tree_deps_test.go index 215c76c1e..61b964846 100644 --- a/cmd/bd/list_tree_deps_test.go +++ b/cmd/bd/list_tree_deps_test.go @@ -1,8 +1,10 @@ package main import ( + "slices" "strings" "testing" + "time" "github.com/spf13/cobra" "github.com/steveyegge/beads/internal/types" @@ -83,6 +85,48 @@ func TestOrderSiblingsByDeps_OutOfGroupEdgeIgnored(t *testing.T) { } } +// closedSibling builds a sibling closed hoursAgo hours before a fixed instant, +// so --sort closed (newest first) orders it independently of priority and ID. +func closedSibling(id string, priority, hoursAgo int) *types.Issue { + at := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC).Add(-time.Duration(hoursAgo) * time.Hour) + return &types.Issue{ID: id, Priority: priority, ClosedAt: &at} +} + +func TestOrderSiblingsByDeps_ReadySetFollowsCallerCompare(t *testing.T) { + // b depends on a. Newest-closed first is b, u, a, v; priority order is + // v, a, u, b. The initial ready set {a, u, v} must follow the caller's + // compare (u, a, v), and once a frees b the re-sorted ready set must put + // the newer b ahead of v. + a := closedSibling("pa-a", 1, 3) + b := closedSibling("pa-b", 3, 1) + u := closedSibling("pa-u", 2, 2) + v := closedSibling("pa-v", 0, 4) + deps := map[string][]*types.Dependency{"pa-b": {schedDep("pa-b", "pa-a")}} + + got := idsOf(orderSiblingsByDeps([]*types.Issue{b, a, v, u}, deps, compareIssuesForTree("closed", false))) + want := []string{"pa-u", "pa-a", "pa-b", "pa-v"} + if !slices.Equal(got, want) { + t.Fatalf("--sort closed ready set: got %v, want %v", got, want) + } +} + +func TestOrderSiblingsByDeps_CycleFallbackFollowsCallerCompare(t *testing.T) { + // x <-> y cycle: x wins on priority and ID, but y closed more recently, so + // the cycle fallback under --sort closed must emit y first. + x := closedSibling("pa-x", 0, 2) + y := closedSibling("pa-y", 1, 1) + deps := map[string][]*types.Dependency{ + "pa-x": {schedDep("pa-x", "pa-y")}, + "pa-y": {schedDep("pa-y", "pa-x")}, + } + + got := idsOf(orderSiblingsByDeps([]*types.Issue{x, y}, deps, compareIssuesForTree("closed", false))) + want := []string{"pa-y", "pa-x"} + if !slices.Equal(got, want) { + t.Fatalf("--sort closed cycle fallback: got %v, want %v", got, want) + } +} + func TestDepEdgeDisplay(t *testing.T) { cases := []struct { name string