test(web): pin the :PiSessions session ordering

Covers root ranking by tab/tabRank (including a child-view parent inheriting
its viewing child's tab and a lineage taking its earliest slot), the root
tie-break chain, sibling order by createdAt desc + id asc, the manifest
entry winning the sort key over the row, cycle/duplicate/self-parent safety
and determinism under input shuffles.
This commit is contained in:
yuez committed 2026-10-27 19:08:47 +08:00
1 parent 37941aea19
commit 2712c6c76c
2 files changed
+481 -5

No files matched your search

+126 -5
View File
@@ -126,7 +126,9 @@ describe("buildSessionTree", () => {
expect(tree[0]?.children[0]?.hidden).toBe(true);
});
it("preserves input order and counts rendered children only", () => {
it("orders roots canonically and counts rendered children only", () => {
// Roots ranked by tab asc (pi2.nvim manager.lua); ties broken by
// createdAt desc, lastActiveAt desc, sessionId asc (console determinism rule).
const tree = buildSessionTree([
sessionFixture({ sessionId: "s2", name: "Beta" }),
sessionFixture({ sessionId: "s1", name: "Alpha" }),
@@ -134,11 +136,130 @@ describe("buildSessionTree", () => {
childOf({ sessionId: "c2", visible: false }),
]);
// No sorting: roots keep the order the list delivered.
expect(ids(tree)).toEqual(["s2", "s1"]);
// Roots without tab or timestamp tie-break by sessionId asc: s1 before s2.
expect(ids(tree)).toEqual(["s1", "s2"]);
// The hidden child is not rendered, so it is not counted either.
expect(tree[1]?.childCount).toBe(1);
expect(ids(tree[1]?.children ?? [])).toEqual(["c1"]);
expect(tree[0]?.childCount).toBe(1);
expect(ids(tree[0]?.children ?? [])).toEqual(["c1"]);
});
it("sorts roots by tab asc with tab-less roots last", () => {
// Attached tabs ranked by tab visual order (pi2.nvim manager.lua);
// placing tab-less rows last is a console rule (nvim excludes detached sessions from Sessions.list()).
const tree = buildSessionTree([
sessionFixture({ sessionId: "tabless", tab: null, createdAt: 5000 }),
sessionFixture({ sessionId: "tab2", tab: 2, createdAt: 1000 }),
sessionFixture({ sessionId: "tab1", tab: 1, createdAt: 2000 }),
]);
expect(ids(tree)).toEqual(["tab1", "tab2", "tabless"]);
});
it("inherits viewing child tab for a child-view parent with no tab", () => {
// pi2.nvim lua/pi/sessions/manager.lua: in child view the parent row occupies
// the tab's slot (inherits smallest tab in its subtree).
const tree = buildSessionTree([
sessionFixture({ sessionId: "root2", tab: 2 }),
sessionFixture({ sessionId: "parent", tab: null }), // child has tab: 1
sessionFixture({ sessionId: "child", parentId: "parent", tab: 1 }),
sessionFixture({ sessionId: "root3", tab: 3 }),
]);
// "parent" inherits tab 1 from "child", so rank 1 < rank 2 < rank 3.
expect(ids(tree)).toEqual(["parent", "root2", "root3"]);
expect(ids(tree[0]?.children ?? [])).toEqual(["child"]);
});
it("sorts siblings by createdAt desc with sessionId asc tie-break", () => {
// pi2.nvim lua/pi/subsessions/manifest.lua: created_at desc, _id asc.
const tree = buildSessionTree([
sessionFixture({ sessionId: "parent", tab: 1 }),
sessionFixture({ sessionId: "c-old", parentId: "parent", createdAt: 1000 }),
sessionFixture({ sessionId: "c-tie-2", parentId: "parent", createdAt: 2000 }),
sessionFixture({ sessionId: "c-tie-1", parentId: "parent", createdAt: 2000 }),
sessionFixture({ sessionId: "c-new", parentId: "parent", createdAt: 3000 }),
sessionFixture({ sessionId: "c-no-time", parentId: "parent" }),
]);
expect(ids(tree[0]?.children ?? [])).toEqual([
"c-new",
"c-tie-1",
"c-tie-2",
"c-old",
"c-no-time",
]);
});
it("sorts child session rows using the same-id manifest entry createdAt when row createdAt is absent", () => {
// pi2.nvim lua/pi/subsessions/manifest.lua: siblings ordered by manifest entry
// created_at desc. The child session row's sort key inherits manifest createdAt.
const tree = buildSessionTree([
sessionFixture({
sessionId: "parent",
tab: 1,
subagents: [
subagentFixture({ id: "child-undated", createdAt: 5000 }),
],
}),
sessionFixture({ sessionId: "child-undated", parentId: "parent" }),
sessionFixture({ sessionId: "sibling-dated", parentId: "parent", createdAt: 1000 }),
]);
expect(ids(tree[0]?.children ?? [])).toEqual(["child-undated", "sibling-dated"]);
expect(tree[0]?.children[0]?.kind).toBe("session");
});
it("interleaves synthetic subagents among real child sessions by createdAt with session row winning", () => {
// pi2.nvim lua/pi/subsessions/manifest.lua: merged sibling order by manifest created_at desc,
// and a session row wins over a same-id manifest subagent for rendering.
const tree = buildSessionTree([
sessionFixture({
sessionId: "parent",
tab: 1,
subagents: [
subagentFixture({ id: "agent-newest", createdAt: 4000 }),
subagentFixture({ id: "real-child-1", createdAt: 3000, name: "synthetic version" }),
subagentFixture({ id: "agent-middle", createdAt: 2000 }),
subagentFixture({ id: "agent-oldest", createdAt: 500 }),
],
}),
sessionFixture({ sessionId: "real-child-1", parentId: "parent", createdAt: 9999 }),
sessionFixture({ sessionId: "real-child-2", parentId: "parent", createdAt: 1000 }),
]);
// real-child-1 is sorted by manifest createdAt (3000, not row's 9999):
// agent-newest (4000) > real-child-1 (3000) > agent-middle (2000) > real-child-2 (1000) > agent-oldest (500)
expect(ids(tree[0]?.children ?? [])).toEqual([
"agent-newest",
"real-child-1",
"agent-middle",
"real-child-2",
"agent-oldest",
]);
expect(tree[0]?.children[1]?.kind).toBe("session");
});
it("produces deterministic rendered order regardless of input array shuffle", () => {
// Total, deterministic order per rule 4.
const items = [
sessionFixture({ sessionId: "r2", tab: 2, createdAt: 1000 }),
sessionFixture({ sessionId: "r1", tab: 1, createdAt: 2000 }),
sessionFixture({ sessionId: "c1", parentId: "r1", createdAt: 3000 }),
sessionFixture({ sessionId: "c2", parentId: "r1", createdAt: 4000 }),
sessionFixture({ sessionId: "r3", tab: null, createdAt: 5000 }),
sessionFixture({ sessionId: "r4", tab: null, createdAt: 5000, lastActiveAt: 6000 }),
];
const treeOriginal = buildSessionTree(items);
const expectedRoots = ids(treeOriginal);
const expectedR1Children = ids(treeOriginal[0]?.children ?? []);
// Shuffle input and verify output matches
const shuffled = [items[4]!, items[1]!, items[5]!, items[0]!, items[3]!, items[2]!];
const treeShuffled = buildSessionTree(shuffled);
expect(ids(treeShuffled)).toEqual(expectedRoots);
expect(ids(treeShuffled[0]?.children ?? [])).toEqual(expectedR1Children);
});
it("marks the ancestor chain of the routed row forced-open", () => {