stakk 2.1.3

A CLI tool that bridges Jujutsu (jj) bookmarks to GitHub stacked pull requests
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//! Convert a `ChangeGraph` into a jj-log-style row layout for rendering.
//!
//! Commits are deduplicated by `commit_id` into a tree rooted at a trunk
//! pseudo-node, then flattened into display rows: one row per commit plus
//! connector rows (`├─╯`) where a sibling subtree merges into its parent's
//! column. Sibling subtrees are ordered newest-first by committer timestamp,
//! like jj log.

use std::collections::HashMap;

use jiff::Timestamp;

use crate::graph::types::ChangeGraph;
use crate::jj::types::Signature;

/// Glyphs shared by all renderers of the layout (the TUI graph widget and
/// `stakk graph`'s pretty format).
///
/// Node glyph for a commit on the selected path (TUI only).
pub const NODE_SELECTED: &str = "\u{25cf}"; ///// Node glyph for a commit.
pub const NODE_OTHER: &str = "\u{25cb}"; ///// Node glyph for the trunk pseudo-node.
pub const TRUNK_CHAR: &str = "\u{25c6}"; ///// Marker for a collapsed run of commits (TUI only).
pub const ELLIPSIS: &str = "\u{22ef}"; ///// One edge-gutter cell.
pub const GUTTER_CELL: &str = "\u{2502} "; // "│ "
/// First cell of a connector row.
pub const CONNECTOR_TEE: &str = "\u{251c}"; ///// Tail of a connector row.
pub const CONNECTOR_TAIL: &str = "\u{2500}\u{256f}"; // ─╯
/// Marker trailing the selected leaf (TUI only).
pub const LEAF_MARKER: &str = "\u{25c0}"; //
/// A commit node in the layout tree.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LayoutNode {
    /// The jj change ID for this commit.
    pub change_id: String,
    /// The jj commit ID.
    pub commit_id: String,
    /// First line of the commit description.
    pub summary: String,
    /// Full commit description.
    pub description: String,
    /// Segment-boundary bookmark names on this commit (may be empty). Only
    /// the last commit of a segment carries them — not to be conflated with
    /// `excluded_bookmarks`.
    pub bookmark_names: Vec<String>,
    /// Local bookmark names on this commit that are *not* segment-boundary
    /// names — bookmarks the graph's bookmarks revset filtered out (e.g. on
    /// immutable commits).
    pub excluded_bookmarks: Vec<String>,
    /// Whether jj considers this commit immutable.
    pub is_immutable: bool,
    /// Whether this node is the trunk node.
    pub is_trunk: bool,
    /// Whether this node is a leaf (no children).
    pub is_leaf: bool,
    /// Shortest unique change ID prefix (from jj).
    pub short_change_id: String,
    /// Author signature.
    pub author: Signature,
    /// Files changed by this commit.
    pub files: Vec<String>,
    /// Index of the parent node (toward trunk); `None` for the trunk node.
    pub parent: Option<usize>,
}

/// One display row of the graph, in top-to-bottom order.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GraphRow {
    /// A commit (or trunk) row: node index into `GraphLayout::nodes`, drawn
    /// at the given gutter column.
    Commit { node: usize, col: usize },
    /// A connector row (`├─╯`) merging column `col` into `col - 1`.
    Connector { col: usize },
}

/// The complete jj-log-style layout of the change graph.
#[derive(Debug, Clone)]
pub struct GraphLayout {
    /// All nodes. Index 0 is the trunk pseudo-node (when non-empty).
    pub nodes: Vec<LayoutNode>,
    /// Display rows, top to bottom (trunk last).
    pub rows: Vec<GraphRow>,
    /// Node indices of leaves, in display order (top to bottom).
    pub leaves: Vec<usize>,
}

impl GraphLayout {
    /// Return all leaf nodes (selectable branch tips) in display order (top
    /// to bottom), so that index 0 is the topmost leaf.
    pub fn leaf_nodes(&self) -> Vec<&LayoutNode> {
        self.leaves.iter().map(|&i| &self.nodes[i]).collect()
    }

    /// Node indices on the path from trunk to the given node, trunk first.
    pub fn path_node_indices(&self, node: usize) -> Vec<usize> {
        let mut path = Vec::new();
        let mut current = Some(node);
        while let Some(i) = current {
            path.push(i);
            current = self.nodes[i].parent;
        }
        path.reverse();
        path
    }

    /// Collect all nodes on the path from trunk to the given node.
    ///
    /// Returns nodes in trunk-to-leaf order.
    pub fn path_to_leaf(&self, node: usize) -> Vec<&LayoutNode> {
        self.path_node_indices(node)
            .into_iter()
            .map(|i| &self.nodes[i])
            .collect()
    }
}

/// Build a jj-log-style graph layout from a `ChangeGraph`.
///
/// The trunk pseudo-node sits at the bottom; each commit appears exactly
/// once. Sibling subtrees are ordered by their most recent committer
/// timestamp, newest topmost, with the subtree root's `change_id` as a
/// deterministic tiebreaker.
pub fn build_layout(graph: &ChangeGraph) -> GraphLayout {
    if graph.stacks.is_empty() {
        return GraphLayout {
            nodes: vec![],
            rows: vec![],
            leaves: vec![],
        };
    }

    let mut nodes: Vec<LayoutNode> = vec![LayoutNode {
        change_id: String::new(),
        commit_id: String::new(),
        summary: "trunk".to_string(),
        description: String::new(),
        bookmark_names: vec![],
        excluded_bookmarks: vec![],
        is_immutable: false,
        is_trunk: true,
        is_leaf: false,
        short_change_id: String::new(),
        author: Signature {
            name: String::new(),
            email: String::new(),
            timestamp: String::new(),
        },
        files: vec![],
        parent: None,
    }];
    let mut children: Vec<Vec<usize>> = vec![vec![]];
    // Committer timestamp per node; unparseable timestamps sort as oldest.
    let mut own_ts: Vec<Option<Timestamp>> = vec![None];
    // commit_id → node index, for deduplicating shared segments across
    // stacks (change_id is shared by all commits in a segment, so key by
    // commit_id).
    let mut placed: HashMap<String, usize> = HashMap::new();

    for stack in &graph.stacks {
        let mut prev: usize = 0; // trunk

        for (seg_idx, segment) in stack.segments.iter().enumerate() {
            let is_last_segment = seg_idx == stack.segments.len() - 1;

            // Commits are newest-first in the segment; walk them
            // trunk-to-leaf.
            let commits: Vec<_> = segment.commits.iter().rev().collect();

            for (commit_idx, commit) in commits.iter().enumerate() {
                // Shared node (prefix of an earlier stack): continue from it.
                if let Some(&existing) = placed.get(&commit.commit_id) {
                    prev = existing;
                    continue;
                }

                let is_last_commit_in_segment = commit_idx == commits.len() - 1;

                let bookmark_names = if is_last_commit_in_segment {
                    segment.bookmark_names.clone()
                } else {
                    vec![]
                };

                let excluded_bookmarks: Vec<String> = commit
                    .local_bookmark_names
                    .iter()
                    .filter(|name| !bookmark_names.contains(name))
                    .cloned()
                    .collect();

                let summary = commit
                    .description
                    .lines()
                    .next()
                    .map(str::trim)
                    .filter(|l| !l.is_empty())
                    .unwrap_or("(no description)")
                    .to_string();

                let idx = nodes.len();
                nodes.push(LayoutNode {
                    change_id: commit.change_id.clone(),
                    commit_id: commit.commit_id.clone(),
                    summary,
                    description: commit.description.clone(),
                    bookmark_names,
                    excluded_bookmarks,
                    is_immutable: commit.is_immutable,
                    is_trunk: false,
                    is_leaf: is_last_segment && is_last_commit_in_segment,
                    short_change_id: commit.short_change_id.clone(),
                    author: commit.author.clone(),
                    files: commit.files.clone(),
                    parent: Some(prev),
                });
                children.push(vec![]);
                own_ts.push(commit.committer.timestamp.parse().ok());
                children[prev].push(idx);
                placed.insert(commit.commit_id.clone(), idx);
                prev = idx;
            }
        }
    }

    // Max committer timestamp per subtree. Parents are always created before
    // their children, so a reverse index scan sees children first.
    let mut subtree_ts = own_ts;
    for i in (0..nodes.len()).rev() {
        for &child in &children[i] {
            if subtree_ts[child] > subtree_ts[i] {
                subtree_ts[i] = subtree_ts[child];
            }
        }
    }

    // Order siblings newest-subtree-first, tiebreaking on the subtree root's
    // change_id for determinism.
    for siblings in &mut children {
        siblings.sort_by(|&a, &b| {
            subtree_ts[b]
                .cmp(&subtree_ts[a])
                .then_with(|| nodes[a].change_id.cmp(&nodes[b].change_id))
        });
    }

    let rows = build_rows(&children);

    let leaves: Vec<usize> = rows
        .iter()
        .filter_map(|row| match row {
            GraphRow::Commit { node, .. } if nodes[*node].is_leaf => Some(*node),
            _ => None,
        })
        .collect();

    GraphLayout {
        nodes,
        rows,
        leaves,
    }
}

/// Flatten the tree into display rows, top to bottom.
///
/// For a node at column `col`, its first (newest) child subtree continues at
/// `col` directly above it; each further sibling subtree renders at
/// `col + 1`, followed by a connector row merging back into `col`. The
/// node's own row comes last (lowest).
fn build_rows(children: &[Vec<usize>]) -> Vec<GraphRow> {
    enum Work {
        Visit { node: usize, col: usize },
        EmitCommit { node: usize, col: usize },
        EmitConnector { col: usize },
    }

    let mut rows = Vec::new();
    let mut work = vec![Work::Visit { node: 0, col: 0 }];

    while let Some(item) = work.pop() {
        match item {
            Work::Visit { node, col } => {
                // Emission order: first child's subtree, then each further
                // sibling's subtree followed by its connector, then this
                // node's own row. Push in reverse (LIFO).
                work.push(Work::EmitCommit { node, col });
                for &sibling in children[node].iter().skip(1).rev() {
                    work.push(Work::EmitConnector { col: col + 1 });
                    work.push(Work::Visit {
                        node: sibling,
                        col: col + 1,
                    });
                }
                if let Some(&first) = children[node].first() {
                    work.push(Work::Visit { node: first, col });
                }
            }
            Work::EmitCommit { node, col } => rows.push(GraphRow::Commit { node, col }),
            Work::EmitConnector { col } => rows.push(GraphRow::Connector { col }),
        }
    }

    rows
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::collections::HashSet;

    use super::*;
    use crate::graph::types::BookmarkSegment;
    use crate::graph::types::BranchStack;
    use crate::graph::types::ChangeGraph;
    use crate::graph::types::SegmentCommit;

    fn make_graph(stacks: Vec<BranchStack>) -> ChangeGraph {
        ChangeGraph {
            adjacency_list: HashMap::new(),
            stack_leaves: HashSet::new(),
            segments: HashMap::new(),
            tainted_change_ids: HashSet::new(),
            bookmark_remote_states: HashMap::new(),
            excluded_bookmarks: Vec::new(),
            excluded_head_count: 0,
            stacks,
        }
    }

    fn make_segment(names: &[&str], change_id: &str, descriptions: &[&str]) -> BookmarkSegment {
        make_segment_at(names, change_id, descriptions, "T")
    }

    /// Like `make_segment`, with an explicit committer timestamp for
    /// ordering tests. The default "T" is unparseable and sorts as oldest.
    fn make_segment_at(
        names: &[&str],
        change_id: &str,
        descriptions: &[&str],
        timestamp: &str,
    ) -> BookmarkSegment {
        BookmarkSegment {
            bookmark_names: names.iter().map(ToString::to_string).collect(),
            change_id: change_id.to_string(),
            commits: descriptions
                .iter()
                .enumerate()
                .map(|(i, desc)| SegmentCommit {
                    commit_id: format!("c_{change_id}_{i}"),
                    change_id: change_id.to_string(),
                    description: desc.to_string(),
                    author: crate::jj::types::Signature {
                        name: "Test".to_string(),
                        email: "test@test.com".to_string(),
                        timestamp: "T".to_string(),
                    },
                    committer: crate::jj::types::Signature {
                        name: "Test".to_string(),
                        email: "test@test.com".to_string(),
                        timestamp: timestamp.to_string(),
                    },
                    files: vec![],
                    short_change_id: change_id[..4.min(change_id.len())].to_string(),
                    is_immutable: false,
                    local_bookmark_names: vec![],
                    remote_bookmark_names: vec![],
                })
                .collect(),
        }
    }

    /// Render the display rows as plain text for structural assertions:
    /// `col:summary` for commit rows, `col:├─╯` for connectors.
    fn rows_to_string(layout: &GraphLayout) -> String {
        layout
            .rows
            .iter()
            .map(|row| match row {
                GraphRow::Commit { node, col } => {
                    format!("{col}:{}", layout.nodes[*node].summary)
                }
                GraphRow::Connector { col } => format!("{col}:├─╯"),
            })
            .collect::<Vec<_>>()
            .join("\n")
    }

    #[test]
    fn empty_graph_layout() {
        let graph = make_graph(vec![]);
        let layout = build_layout(&graph);
        assert!(layout.nodes.is_empty());
        assert!(layout.rows.is_empty());
        assert!(layout.leaves.is_empty());
    }

    #[test]
    fn single_linear_stack() {
        let graph = make_graph(vec![BranchStack {
            segments: vec![
                make_segment(&["base"], "ch_a", &["add base"]),
                make_segment(&["leaf"], "ch_b", &["add leaf"]),
            ],
        }]);

        let layout = build_layout(&graph);

        // trunk + 2 commits = 3 nodes
        assert_eq!(layout.nodes.len(), 3);
        assert!(layout.nodes[0].is_trunk);

        let base = &layout.nodes[1];
        assert_eq!(base.bookmark_names, vec!["base"]);
        assert!(!base.is_leaf);
        assert_eq!(base.parent, Some(0));

        let leaf = &layout.nodes[2];
        assert_eq!(leaf.bookmark_names, vec!["leaf"]);
        assert!(leaf.is_leaf);
        assert_eq!(leaf.parent, Some(1));

        // Display order: leaf on top, trunk at the bottom, all at column 0.
        assert_eq!(rows_to_string(&layout), "0:add leaf\n0:add base\n0:trunk");
    }

    #[test]
    fn two_branching_stacks_ordered_newest_first() {
        let graph = make_graph(vec![
            BranchStack {
                segments: vec![make_segment_at(
                    &["alpha"],
                    "ch_alpha",
                    &["alpha work"],
                    "2026-01-01T00:00:00Z",
                )],
            },
            BranchStack {
                segments: vec![make_segment_at(
                    &["beta"],
                    "ch_beta",
                    &["beta work"],
                    "2026-02-01T00:00:00Z",
                )],
            },
        ]);

        let layout = build_layout(&graph);

        // trunk + 2 commits = 3 nodes; beta is newer so it renders topmost
        // at column 0, alpha below it at column 1 with a connector.
        assert_eq!(layout.nodes.len(), 3);
        assert_eq!(
            rows_to_string(&layout),
            "0:beta work\n1:alpha work\n1:├─╯\n0:trunk"
        );

        // Leaves in display order: beta first (topmost).
        let leaves = layout.leaf_nodes();
        assert_eq!(leaves[0].bookmark_names, vec!["beta"]);
        assert_eq!(leaves[1].bookmark_names, vec!["alpha"]);
    }

    #[test]
    fn sibling_order_uses_offset_aware_timestamps() {
        // 12:00+00:00 is *later* than 12:30+02:00 (= 10:30 UTC), but
        // lexicographic string comparison would say otherwise.
        let graph = make_graph(vec![
            BranchStack {
                segments: vec![make_segment_at(
                    &["utc"],
                    "ch_utc",
                    &["utc work"],
                    "2026-01-01T12:00:00+00:00",
                )],
            },
            BranchStack {
                segments: vec![make_segment_at(
                    &["offset"],
                    "ch_offset",
                    &["offset work"],
                    "2026-01-01T12:30:00+02:00",
                )],
            },
        ]);

        let layout = build_layout(&graph);
        let leaves = layout.leaf_nodes();
        assert_eq!(leaves[0].bookmark_names, vec!["utc"]);
        assert_eq!(leaves[1].bookmark_names, vec!["offset"]);
    }

    #[test]
    fn sibling_order_tiebreaks_on_change_id() {
        // Identical (unparseable) timestamps: order falls back to change_id.
        let graph = make_graph(vec![
            BranchStack {
                segments: vec![make_segment(&["zeta"], "ch_z", &["z work"])],
            },
            BranchStack {
                segments: vec![make_segment(&["alpha"], "ch_a", &["a work"])],
            },
        ]);

        let layout = build_layout(&graph);
        let leaves = layout.leaf_nodes();
        assert_eq!(leaves[0].bookmark_names, vec!["alpha"]);
        assert_eq!(leaves[1].bookmark_names, vec!["zeta"]);
    }

    #[test]
    fn shared_root_segment() {
        // Two stacks sharing a root segment (same change_id).
        let graph = make_graph(vec![
            BranchStack {
                segments: vec![
                    make_segment(&["base"], "ch_shared", &["shared base"]),
                    make_segment_at(&["feat-a"], "ch_a", &["feature a"], "2026-02-01T00:00:00Z"),
                ],
            },
            BranchStack {
                segments: vec![
                    make_segment(&["base"], "ch_shared", &["shared base"]),
                    make_segment_at(&["feat-b"], "ch_b", &["feature b"], "2026-01-01T00:00:00Z"),
                ],
            },
        ]);

        let layout = build_layout(&graph);

        // trunk + shared base + feat-a + feat-b = 4 nodes; the shared base
        // appears exactly once.
        assert_eq!(layout.nodes.len(), 4);
        let shared: Vec<_> = layout
            .nodes
            .iter()
            .filter(|n| n.change_id == "ch_shared")
            .collect();
        assert_eq!(shared.len(), 1);

        // feat-a is newer: it continues in the shared base's column; feat-b
        // branches off at column 1.
        assert_eq!(
            rows_to_string(&layout),
            "0:feature a\n1:feature b\n1:├─╯\n0:shared base\n0:trunk"
        );
    }

    #[test]
    fn multi_commit_segment() {
        let graph = make_graph(vec![BranchStack {
            segments: vec![make_segment(
                &["feat"],
                "ch_a",
                &["second commit", "first commit"],
            )],
        }]);

        let layout = build_layout(&graph);

        // trunk + 2 commits = 3 nodes; newest commit on top.
        assert_eq!(layout.nodes.len(), 3);
        assert_eq!(
            rows_to_string(&layout),
            "0:second commit\n0:first commit\n0:trunk"
        );

        // Only the last commit in the segment carries the bookmark.
        let first = layout
            .nodes
            .iter()
            .find(|n| n.summary == "first commit")
            .unwrap();
        let second = layout
            .nodes
            .iter()
            .find(|n| n.summary == "second commit")
            .unwrap();
        assert!(first.bookmark_names.is_empty());
        assert_eq!(second.bookmark_names, vec!["feat"]);
    }

    #[test]
    fn path_to_leaf_linear() {
        let graph = make_graph(vec![BranchStack {
            segments: vec![
                make_segment(&["base"], "ch_a", &["base work"]),
                make_segment(&["leaf"], "ch_b", &["leaf work"]),
            ],
        }]);

        let layout = build_layout(&graph);
        let path = layout.path_to_leaf(layout.leaves[0]);

        // trunk → base → leaf
        assert_eq!(path.len(), 3);
        assert!(path[0].is_trunk);
        assert_eq!(path[1].change_id, "ch_a");
        assert_eq!(path[2].change_id, "ch_b");
    }

    #[test]
    fn path_to_leaf_branching() {
        let graph = make_graph(vec![
            BranchStack {
                segments: vec![
                    make_segment(&["base"], "ch_shared", &["shared"]),
                    make_segment(&["feat-a"], "ch_a", &["feature a"]),
                ],
            },
            BranchStack {
                segments: vec![
                    make_segment(&["base"], "ch_shared", &["shared"]),
                    make_segment(&["feat-b"], "ch_b", &["feature b"]),
                ],
            },
        ]);

        let layout = build_layout(&graph);

        // Path to feat-b should go through the shared base.
        let feat_b = layout
            .nodes
            .iter()
            .position(|n| n.change_id == "ch_b")
            .unwrap();
        let path = layout.path_to_leaf(feat_b);

        // trunk → shared → feat_b
        assert_eq!(path.len(), 3);
        assert!(path[0].is_trunk);
        assert_eq!(path[1].change_id, "ch_shared");
        assert_eq!(path[2].change_id, "ch_b");
    }

    #[test]
    fn immutable_flag_and_excluded_bookmarks_threaded() {
        // Two-commit segment: the mid-segment commit is immutable and carries
        // a bookmark that the graph filtered out; the boundary commit carries
        // its own boundary name plus a filtered-out extra.
        let mut segment = make_segment(&["feat"], "ch_a", &["second commit", "first commit"]);
        // Commits are newest-first: [0] = boundary ("second"), [1] = mid.
        segment.commits[0].local_bookmark_names =
            vec!["feat".to_string(), "other-user".to_string()];
        segment.commits[1].is_immutable = true;
        segment.commits[1].local_bookmark_names = vec!["pinned".to_string()];

        let graph = make_graph(vec![BranchStack {
            segments: vec![segment],
        }]);
        let layout = build_layout(&graph);

        let trunk = &layout.nodes[0];
        assert!(trunk.is_trunk);
        assert!(!trunk.is_immutable);
        assert!(trunk.excluded_bookmarks.is_empty());

        let mid = layout
            .nodes
            .iter()
            .find(|n| n.summary == "first commit")
            .unwrap();
        assert!(mid.is_immutable);
        assert!(mid.bookmark_names.is_empty());
        assert_eq!(mid.excluded_bookmarks, vec!["pinned"]);

        // Boundary names are subtracted from excluded_bookmarks.
        let boundary = layout
            .nodes
            .iter()
            .find(|n| n.summary == "second commit")
            .unwrap();
        assert!(!boundary.is_immutable);
        assert_eq!(boundary.bookmark_names, vec!["feat"]);
        assert_eq!(boundary.excluded_bookmarks, vec!["other-user"]);
    }

    #[test]
    fn leaf_nodes_returns_only_leaves() {
        let graph = make_graph(vec![
            BranchStack {
                segments: vec![
                    make_segment(&["base"], "ch_a", &["base"]),
                    make_segment(&["leaf-1"], "ch_b", &["leaf 1"]),
                ],
            },
            BranchStack {
                segments: vec![make_segment(&["leaf-2"], "ch_c", &["leaf 2"])],
            },
        ]);

        let layout = build_layout(&graph);
        let leaves = layout.leaf_nodes();
        assert_eq!(leaves.len(), 2);
        assert!(leaves.iter().all(|n| n.is_leaf));
    }

    #[test]
    fn nested_siblings_layout() {
        // trunk → auth → {email chain (newest), api chain}; the api chain
        // itself forks into {integration (newest), ratelimit}.
        let auth = make_segment_at(&["auth"], "ch_auth", &["auth work"], "2026-01-01T00:00:00Z");
        let graph = make_graph(vec![
            BranchStack {
                segments: vec![
                    auth.clone(),
                    make_segment_at(
                        &["email"],
                        "ch_email",
                        &["email work"],
                        "2026-06-01T00:00:00Z",
                    ),
                ],
            },
            BranchStack {
                segments: vec![
                    auth.clone(),
                    make_segment_at(&["api"], "ch_api", &["api work"], "2026-02-01T00:00:00Z"),
                    make_segment_at(
                        &["integration"],
                        "ch_int",
                        &["integration work"],
                        "2026-04-01T00:00:00Z",
                    ),
                ],
            },
            BranchStack {
                segments: vec![
                    auth,
                    make_segment_at(&["api"], "ch_api", &["api work"], "2026-02-01T00:00:00Z"),
                    make_segment_at(
                        &["ratelimit"],
                        "ch_rate",
                        &["ratelimit work"],
                        "2026-03-01T00:00:00Z",
                    ),
                ],
            },
        ]);

        let layout = build_layout(&graph);

        insta::assert_snapshot!(rows_to_string(&layout));

        // Leaves in display order.
        let leaves = layout.leaf_nodes();
        let names: Vec<_> = leaves.iter().map(|l| l.bookmark_names[0].clone()).collect();
        assert_eq!(names, vec!["email", "integration", "ratelimit"]);
    }
}