|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { |
| 3 | + getCurrentSessionWithChildren, |
| 4 | + getFirstDirectChildSession, |
| 5 | + getSiblingSessions, |
| 6 | +} from "../../../src/cli/cmd/tui/routes/session/navigation" |
| 7 | + |
| 8 | +describe("getCurrentSessionWithChildren", () => { |
| 9 | + test("returns the root session and its direct children", () => { |
| 10 | + const sessions = [ |
| 11 | + { id: "root" }, |
| 12 | + { id: "child-b", parentID: "root" }, |
| 13 | + { id: "grandchild", parentID: "child-a" }, |
| 14 | + { id: "child-a", parentID: "root" }, |
| 15 | + ] |
| 16 | + |
| 17 | + expect(getCurrentSessionWithChildren(sessions, "root")).toEqual([ |
| 18 | + { id: "child-a", parentID: "root" }, |
| 19 | + { id: "child-b", parentID: "root" }, |
| 20 | + { id: "root" }, |
| 21 | + ]) |
| 22 | + }) |
| 23 | + |
| 24 | + test("returns the current child session and its direct children", () => { |
| 25 | + const sessions = [ |
| 26 | + { id: "root" }, |
| 27 | + { id: "child-a", parentID: "root" }, |
| 28 | + { id: "child-b", parentID: "root" }, |
| 29 | + { id: "grandchild-b", parentID: "child-a" }, |
| 30 | + { id: "grandchild-a", parentID: "child-a" }, |
| 31 | + ] |
| 32 | + |
| 33 | + expect(getCurrentSessionWithChildren(sessions, "child-a")).toEqual([ |
| 34 | + { id: "child-a", parentID: "root" }, |
| 35 | + { id: "grandchild-a", parentID: "child-a" }, |
| 36 | + { id: "grandchild-b", parentID: "child-a" }, |
| 37 | + ]) |
| 38 | + }) |
| 39 | + |
| 40 | + test("returns an empty list when the current session is missing", () => { |
| 41 | + expect(getCurrentSessionWithChildren([{ id: "root" }], undefined)).toEqual([]) |
| 42 | + }) |
| 43 | +}) |
| 44 | + |
| 45 | +describe("getFirstDirectChildSession", () => { |
| 46 | + test("preserves first-level descent from the root session", () => { |
| 47 | + const sessions = [ |
| 48 | + { id: "root" }, |
| 49 | + { id: "child-b", parentID: "root" }, |
| 50 | + { id: "child-a", parentID: "root" }, |
| 51 | + ] |
| 52 | + |
| 53 | + const visibleSessions = getCurrentSessionWithChildren(sessions, "root") |
| 54 | + |
| 55 | + expect(getFirstDirectChildSession(visibleSessions, "root")).toEqual({ |
| 56 | + id: "child-a", |
| 57 | + parentID: "root", |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + test("nested descent skips the current child session and selects its direct child", () => { |
| 62 | + const sessions = [ |
| 63 | + { id: "root" }, |
| 64 | + { id: "child-a", parentID: "root" }, |
| 65 | + { id: "child-b", parentID: "root" }, |
| 66 | + { id: "grandchild-b", parentID: "child-a" }, |
| 67 | + { id: "grandchild-a", parentID: "child-a" }, |
| 68 | + ] |
| 69 | + |
| 70 | + const visibleSessions = getCurrentSessionWithChildren(sessions, "child-a") |
| 71 | + |
| 72 | + expect(getFirstDirectChildSession(visibleSessions, "child-a")).toEqual({ |
| 73 | + id: "grandchild-a", |
| 74 | + parentID: "child-a", |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + test("returns nothing when there are no direct children", () => { |
| 79 | + const visibleSessions = getCurrentSessionWithChildren([{ id: "root" }], "root") |
| 80 | + |
| 81 | + expect(getFirstDirectChildSession(visibleSessions, "root")).toBeUndefined() |
| 82 | + }) |
| 83 | +}) |
| 84 | + |
| 85 | +describe("getSiblingSessions", () => { |
| 86 | + test("keeps sibling cycling scoped to the current depth", () => { |
| 87 | + const sessions = [ |
| 88 | + { id: "root" }, |
| 89 | + { id: "child-b", parentID: "root" }, |
| 90 | + { id: "child-a", parentID: "root" }, |
| 91 | + { id: "grandchild-a", parentID: "child-a" }, |
| 92 | + ] |
| 93 | + |
| 94 | + expect(getSiblingSessions(sessions, { id: "child-a", parentID: "root" })).toEqual([ |
| 95 | + { id: "child-a", parentID: "root" }, |
| 96 | + { id: "child-b", parentID: "root" }, |
| 97 | + ]) |
| 98 | + }) |
| 99 | + |
| 100 | + test("returns an empty list for the root session", () => { |
| 101 | + expect(getSiblingSessions([{ id: "root" }], { id: "root" })).toEqual([]) |
| 102 | + }) |
| 103 | +}) |
0 commit comments