package app import "testing" func TestVisibleSessionTreeScopesToFocusedRoot(t *testing.T) { root1 := testChild("c1", "root1", "", StatusRunning) child1 := testChild("c2", "child1", "c1", StatusRunning) root2 := testChild("c3", "root2", "", StatusRunning) child2 := testChild("c4", "child2", "c3", StatusRunning) got := visibleSessionTree([]*Child{root1, child1, root2, child2}, "c4") if len(got) != 2 || got[0].ID != "c3" || got[1].ID != "c4" { t.Fatalf("visible tree = %v, want root2 + child2", childIDs(got)) } } func TestVisibleSessionTreeOmitsExited(t *testing.T) { root := testChild("c1", "root", "", StatusRunning) exitedRoot := testChild("c2", "dead-root", "", StatusExited) runningChild := testChild("c3", "child", "c1", StatusRunning) exitedChild := testChild("c4", "dead-child", "c1", StatusExited) got := visibleSessionTree([]*Child{root, exitedRoot, runningChild, exitedChild}, "c1") if len(got) != 2 || got[0].ID != "c1" || got[1].ID != "c3" { t.Fatalf("visible tree = %v, want running root + running child", childIDs(got)) } } func testChild(id, name, parent string, status ChildStatus) *Child { c := &Child{ID: id, Name: name, ParentID: parent} c.status.Store(&status) return c } func childIDs(cs []*Child) []string { ids := make([]string, 0, len(cs)) for _, c := range cs { ids = append(ids, c.ID) } return ids } func TestNextTabIDWrapsAndSkipsCurrent(t *testing.T) { r1 := testChild("c1", "root1", "", StatusRunning) r2 := testChild("c2", "root2", "", StatusRunning) r3 := testChild("c3", "root3", "", StatusRunning) children := []*Child{r1, r2, r3} if got := nextTabID(children, "c1", +1); got != "c2" { t.Fatalf("next from c1: %q", got) } if got := nextTabID(children, "c1", -1); got != "c3" { t.Fatalf("prev from c1: %q", got) } if got := nextTabID(children, "c3", +1); got != "c1" { t.Fatalf("wrap forward from c3: %q", got) } } func TestNextTabIDFromSubAgentJumpsByRoot(t *testing.T) { r1 := testChild("c1", "root1", "", StatusRunning) r1Child := testChild("c2", "child1", "c1", StatusRunning) r2 := testChild("c3", "root2", "", StatusRunning) children := []*Child{r1, r1Child, r2} // Focus is on a sub-agent of root1; Ctrl+D should jump to root2, // not stay inside root1's sub-tree. if got := nextTabID(children, "c2", +1); got != "c3" { t.Fatalf("next from sub-agent: %q want c3", got) } } func TestNextChildIDCyclesWithinTab(t *testing.T) { r1 := testChild("c1", "root1", "", StatusRunning) a := testChild("c2", "a", "c1", StatusRunning) b := testChild("c3", "b", "c1", StatusRunning) other := testChild("c4", "other-root", "", StatusRunning) children := []*Child{r1, a, b, other} if got := nextChildID(children, "c1", +1); got != "c2" { t.Fatalf("root->first child: %q", got) } if got := nextChildID(children, "c2", +1); got != "c3" { t.Fatalf("a->b: %q", got) } if got := nextChildID(children, "c3", +1); got != "c1" { t.Fatalf("wrap b->root: %q", got) } if got := nextChildID(children, "c1", -1); got != "c3" { t.Fatalf("wrap backward root->b: %q", got) } } func TestNextChildIDNoopWhenOnlyOneProcess(t *testing.T) { r := testChild("c1", "solo", "", StatusRunning) if got := nextChildID([]*Child{r}, "c1", +1); got != "" { t.Fatalf("expected empty when only one process in tab, got %q", got) } }