#![deny(unsafe_code)]
use super::*;
use crate::source::jsonl::JsonlSource;
fn long_message() -> String {
(1..=12)
.map(|n| format!("line {n} of what was said"))
.collect::<Vec<_>>()
.join("\\n")
}
pub(super) fn run() -> String {
format!(
concat!(
r#"{{"type":"user","timestamp":"2026-08-05T14:01:00.000Z","#,
r#""message":{{"role":"user","content":"{}"}}}}"#,
"\n",
r#"{{"type":"assistant","timestamp":"2026-08-05T14:02:00.000Z","message":{{"role":"assistant","#,
r#""content":[{{"type":"tool_use","id":"t1","name":"Bash","input":{{"command":"git status"}}}}]}}}}"#,
"\n",
r#"{{"type":"user","timestamp":"2026-08-05T14:02:01.000Z","message":{{"role":"user","#,
r#""content":[{{"type":"tool_result","tool_use_id":"t1","content":"clean"}}]}}}}"#,
"\n",
r#"{{"type":"assistant","timestamp":"2026-08-05T14:03:00.000Z","message":{{"role":"assistant","#,
r#""content":[{{"type":"text","text":"Done."}}]}}}}"#,
"\n"
),
long_message()
)
}
fn lens_pager(cols: usize, rows: usize) -> Pager {
let mut src = JsonlSource::from_bytes(run().into_bytes());
src.set_lens(crate::lens::find("agent").expect("the agent lens"));
let mut p = Pager::new(Box::new(src), "session.jsonl".into(), cols, rows, None);
let _ = p.visible_text();
p
}
#[test]
fn a_message_is_read_under_its_row_and_the_clip_says_what_it_hid() {
let mut p = lens_pager(80, 40);
let rows = p.visible_text();
assert!(rows[0].contains("user 14:01"), "{:?}", rows[0]);
assert!(rows[0].ends_with("line 1 of what was said"), "{:?}", rows[0]);
assert_eq!(rows[1], "line 2 of what was said");
assert_eq!(rows[5], "line 6 of what was said", "{rows:#?}");
assert_eq!(rows[6], "\u{22ef} +6 lines", "the clip states what it left out");
assert!(
!rows.iter().any(|r| r.contains("line 12")),
"a clipped body is clipped: {rows:#?}"
);
}
#[test]
fn a_message_s_first_line_is_on_the_screen_exactly_once() {
let mut p = lens_pager(80, 40);
let rows = p.visible_text();
let hits = rows.iter().filter(|r| r.contains("line 1 of what was said")).count();
assert_eq!(hits, 1, "{rows:#?}");
assert_ne!(rows[0].trim(), rows[1].trim(), "{rows:#?}");
press(&mut p, "za");
let open = p.visible_text();
let hits = open.iter().filter(|r| r.contains("line 1 of what was said")).count();
assert_eq!(hits, 1, "{open:#?}");
}
#[test]
fn za_toggles_the_two_levels_and_never_reaches_the_tree() {
let mut p = lens_pager(80, 40);
let clipped = p.line_count();
press(&mut p, "za");
let open = p.visible_text();
assert!(p.line_count() > clipped, "the body grew");
assert!(open.iter().any(|r| r == "line 12 of what was said"), "{open:#?}");
assert!(!open.iter().any(|r| r.starts_with('\u{22ef}')), "nothing left to say");
assert!(!open.iter().any(|r| r.contains("\"type\"")), "no JSON: {open:#?}");
press(&mut p, "za");
assert_eq!(p.line_count(), clipped, "and back to the clip in one press");
let back = p.visible_text();
assert!(back.iter().any(|r| r == "\u{22ef} +6 lines"), "{back:#?}");
assert!(!back.iter().any(|r| r.contains("\"type\"")), "still no JSON: {back:#?}");
press(&mut p, "za");
assert!(p.line_count() > clipped, "and open again");
assert!(
!p.visible_text().iter().any(|r| r.contains("\"type\"")),
"three presses and the JSON never appeared: {:#?}",
p.visible_text()
);
}
#[test]
fn enter_on_a_message_row_opens_its_body_too() {
let mut p = lens_pager(80, 40);
let clipped = p.line_count();
key(&mut p, Key::Enter);
assert!(p.line_count() > clipped);
}
#[test]
fn r_shows_the_raw_record_whatever_the_body_is_doing() {
let mut p = lens_pager(80, 40);
let before = p.line_count();
press(&mut p, "r");
let rows = p.visible_text();
assert!(p.line_count() > before, "the tree was spliced in");
assert!(rows.iter().any(|r| r.contains("\"type\"")), "{rows:#?}");
assert!(rows.iter().any(|r| r.contains("timestamp")), "{rows:#?}");
assert!(rows.iter().any(|r| r == "\u{22ef} +6 lines"), "{rows:#?}");
press(&mut p, "r");
assert_eq!(p.line_count(), before, "and it shuts again");
assert!(!p.visible_text().iter().any(|r| r.contains("\"type\"")));
}
#[test]
fn r_shows_the_raw_record_from_the_open_level_too() {
let mut p = lens_pager(80, 40);
press(&mut p, "za");
let open = p.line_count();
press(&mut p, "r");
let with_tree = p.visible_text();
assert!(p.line_count() > open, "the tree went in");
assert!(with_tree.iter().any(|r| r.contains("\"type\"")), "{with_tree:#?}");
assert!(
with_tree.iter().any(|r| r == "line 12 of what was said"),
"and the level is where it was: {with_tree:#?}"
);
press(&mut p, "r");
assert_eq!(p.line_count(), open, "shut, and the message still open");
assert!(p.visible_text().iter().any(|r| r == "line 12 of what was said"));
}
#[test]
fn enter_with_the_tree_open_leaves_the_tree_and_toggles_the_record() {
let mut p = lens_pager(80, 40);
press(&mut p, "r");
let with_tree = p.line_count();
press(&mut p, "za");
let open = p.visible_text();
assert!(p.line_count() > with_tree, "the message opened under it");
assert!(open.iter().any(|r| r.contains("\"type\"")), "tree still there: {open:#?}");
assert!(open.iter().any(|r| r == "line 12 of what was said"), "{open:#?}");
press(&mut p, "za");
assert_eq!(p.line_count(), with_tree, "back to the clip, tree untouched");
assert!(p.visible_text().iter().any(|r| r.contains("\"type\"")), "and it is still open");
press(&mut p, "r");
assert!(!p.visible_text().iter().any(|r| r.contains("\"type\"")));
}
#[test]
fn r_from_a_body_row_opens_the_record_it_belongs_to() {
let mut p = lens_pager(80, 40);
let before = p.line_count();
press(&mut p, "jj");
assert_eq!(p.cursor_text(), "line 3 of what was said");
press(&mut p, "r");
assert!(p.line_count() > before, "{}", p.status_line());
assert!(p.visible_text().iter().any(|r| r.contains("\"uuid\"") || r.contains("\"type\"")));
}
#[test]
fn r_on_a_run_says_there_is_nothing_to_open() {
let mut p = lens_pager(80, 40);
let rows = p.visible_text();
let run = rows
.iter()
.position(|r| r.contains("\u{27e8}"))
.expect("a folded run");
p.goto(run);
let before = p.line_count();
press(&mut p, "r");
assert_eq!(p.line_count(), before);
assert!(p.status_line().contains("nothing to open"), "{}", p.status_line());
}
#[test]
fn r_is_free_in_every_mode() {
let mut p = lens_pager(80, 40);
press(&mut p, "/");
assert_eq!(p.mode, Mode::Search(Dir::Forward));
let clipped = p.line_count();
press(&mut p, "r");
assert_eq!(p.query, "r", "the prompt takes it as a letter");
assert_eq!(p.line_count(), clipped, "and no tree opened behind it");
key(&mut p, Key::Esc);
assert_eq!(p.line_count(), clipped);
p.mode = Mode::Outline;
p.outline_sel = 0;
press(&mut p, "r");
assert_eq!(p.outline_sel, 0, "the overlay has no `r` of its own");
assert!(p.line_count() > clipped, "it fell through, as any unbound key does");
press(&mut p, "r");
assert_eq!(p.line_count(), clipped, "and the second press shut it again");
p.mode = Mode::Normal;
press(&mut p, "v");
assert!(p.select.is_some(), "visual mode");
press(&mut p, "r");
assert!(p.line_count() > clipped, "and there it is the raw record");
assert!(p.select.is_some(), "the selection survives it");
}
#[test]
fn a_resize_keeps_the_cursor_on_the_same_record() {
let mut p = lens_pager(80, 40);
press(&mut p, "za");
press(&mut p, "G");
let _ = p.visible_text();
let before = p.cursor_mark();
let row = p.cursor;
let said = p.cursor_text();
assert!(said.contains("Done."), "{said:?}");
p.resize(40, 40);
let _ = p.visible_text();
assert_eq!(p.cursor_mark(), before, "the mark is the record, not the row");
assert_ne!(p.cursor, row, "the narrower body pushed the row down");
assert_eq!(p.cursor_text(), said, "and the cursor is on the same content");
}
#[test]
fn a_resize_from_inside_a_body_lands_on_its_summary_row() {
let mut p = lens_pager(80, 40);
press(&mut p, "jjj");
let _ = p.visible_text();
assert_eq!(p.cursor_text(), "line 4 of what was said");
let mark = p.cursor_mark();
p.resize(40, 40);
let _ = p.visible_text();
assert_eq!(p.cursor_mark(), mark, "still the same record");
assert!(p.cursor_text().contains("user 14:01"), "{:?}", p.cursor_text());
}
#[test]
fn zr_opens_the_bodies_and_zm_clips_them_again() {
let mut p = lens_pager(80, 40);
let clipped = p.line_count();
press(&mut p, "zR");
let open = p.visible_text();
assert!(open.iter().any(|r| r == "line 12 of what was said"), "{open:#?}");
press(&mut p, "zM");
assert_eq!(p.line_count(), clipped);
}
fn two_long_messages() -> String {
format!(
concat!(
r#"{{"type":"user","timestamp":"2026-08-05T14:01:00.000Z","#,
r#""message":{{"role":"user","content":"{}"}}}}"#,
"\n",
r#"{{"type":"assistant","timestamp":"2026-08-05T14:02:00.000Z","message":{{"role":"assistant","#,
r#""content":[{{"type":"text","text":"{}"}}]}}}}"#,
"\n"
),
long_message(),
long_message()
)
}
fn pager_over(text: String, cols: usize, rows: usize) -> Pager {
let mut src = JsonlSource::from_bytes(text.into_bytes());
src.set_lens(crate::lens::find("agent").expect("the agent lens"));
let mut p = Pager::new(Box::new(src), "session.jsonl".into(), cols, rows, None);
let _ = p.visible_text();
p
}
#[test]
fn j_and_k_move_one_row_under_a_lens() {
let mut p = lens_pager(80, 40);
assert_eq!(p.cursor, 0);
assert!(p.cursor_text().contains("line 1 of what was said"));
for line in 2..=6 {
press(&mut p, "j");
assert_eq!(p.cursor_text(), format!("line {line} of what was said"));
}
press(&mut p, "j");
assert_eq!(p.cursor_text(), "\u{22ef} +6 lines", "then the clip's own row");
press(&mut p, "j");
assert!(p.cursor_text().contains("\u{27e8}"), "the folded run: {:?}", p.cursor_text());
press(&mut p, "j");
assert!(p.cursor_text().contains("Done."), "{:?}", p.cursor_text());
let last = p.cursor;
assert_eq!(last, p.line_count() - 1, "which is the last row of the document");
press(&mut p, "j");
assert_eq!(p.cursor, last, "and `j` there stays put rather than running off");
for row in (0..last).rev() {
press(&mut p, "k");
assert_eq!(p.cursor, row, "`k` is the mirror, a row at a time");
}
press(&mut p, "k");
assert_eq!(p.cursor, 0, "and `k` on the first row stays put");
}
#[test]
fn every_painted_row_is_reachable_with_j_alone() {
let mut p = lens_pager(80, 40);
press(&mut p, "zR");
let _ = p.visible_text();
let n = p.line_count();
assert!(n > 20, "a full record is on the screen: {n} rows");
let mut seen = vec![p.cursor];
for _ in 0..n * 2 {
let before = p.cursor;
press(&mut p, "j");
assert!(p.cursor <= before + 1, "one row at a time: {before} -> {}", p.cursor);
if p.cursor != before {
seen.push(p.cursor);
}
}
assert_eq!(seen, (0..n).collect::<Vec<_>>(), "every row was reachable by j");
for _ in 0..n * 2 {
press(&mut p, "k");
}
assert_eq!(p.cursor, 0, "and k walks back to the top");
}
#[test]
fn j_walks_a_block_taller_than_the_viewport_and_the_window_follows() {
let mut p = pager_over(two_long_messages(), 80, 6);
key(&mut p, Key::Tab);
let h = p.content_rows();
let block = p.src_block_at(p.cursor).expect("the second block");
assert!(block.end - block.start > h, "the block is taller: {block:?} in {h}");
let (start, top) = (p.cursor, p.top);
for i in 1..h {
press(&mut p, "j");
assert_eq!(p.cursor, start + i, "a row a press");
assert_eq!(p.top, top, "still inside the window");
}
press(&mut p, "j");
assert_eq!(p.top, top + 1, "and now the window follows the cursor down");
}
#[test]
fn landing_on_a_block_that_fits_brings_all_of_it_on_screen() {
let mut p = pager_over(two_long_messages(), 80, 9);
let h = p.content_rows();
assert_eq!((p.cursor, p.top), (0, 0));
key(&mut p, Key::Tab);
let block = p.src_block_at(p.cursor).expect("the second block");
assert!(block.end - block.start <= h, "the block fits: {block:?} in {h}");
assert!(p.top <= block.start, "the block starts on screen: {block:?}, top {}", p.top);
assert!(
block.end <= p.top + h,
"and ends on it: {block:?}, top {} + {h}",
p.top
);
assert!(p.top > 0, "which took a scroll the cursor alone would not have");
}
#[test]
fn landing_on_a_block_taller_than_the_viewport_puts_its_first_row_at_the_top() {
let mut p = pager_over(two_long_messages(), 80, 6);
let h = p.content_rows();
key(&mut p, Key::Tab);
let block = p.src_block_at(p.cursor).expect("the second block");
assert!(block.end - block.start > h, "the block is taller: {block:?} in {h}");
assert_eq!(p.cursor, block.start);
assert_eq!(p.top, block.start, "its first row is the top of the screen");
}
#[test]
fn tab_jumps_block_to_block_and_s_tab_mirrors_it() {
let mut p = lens_pager(80, 40);
let mut down = Vec::new();
for _ in 0..2 {
key(&mut p, Key::Tab);
down.push(p.cursor);
}
assert!(p.cursor_text().contains("Done."), "{:?}", p.cursor_text());
assert_eq!(down.len(), 2);
let last = p.cursor;
key(&mut p, Key::Tab);
assert_eq!(p.cursor, last, "and Tab at the last block stays on it");
assert!(p.cursor < p.line_count());
let mut up = Vec::new();
for _ in 0..2 {
key(&mut p, Key::BackTab);
up.push(p.cursor);
}
assert_eq!(up, vec![down[0], 0], "S-Tab retraces Tab exactly: down {down:?}");
}
#[test]
fn the_exhausted_jump_says_block_under_a_lens() {
let mut p = lens_pager(80, 40);
key(&mut p, Key::BackTab);
assert_eq!(p.message.as_deref(), Some("no previous block"), "at the top");
for _ in 0..3 {
key(&mut p, Key::Tab);
}
assert_eq!(p.message.as_deref(), Some("no further block"), "at the end");
}
#[test]
fn the_status_bar_names_the_block() {
let mut p = lens_pager(80, 40);
assert!(p.status_line().contains("block 1/3"), "{}", p.status_line());
press(&mut p, "j");
assert!(p.status_line().contains("block 1/3"), "a body row is in its block");
key(&mut p, Key::Tab);
assert!(p.status_line().contains("block 2/3"), "{}", p.status_line());
key(&mut p, Key::Tab);
let last = p.status_line();
assert!(last.contains("agent"), "{last}");
assert!(last.contains("record 4/4"), "{last}");
assert!(last.contains("block 3/3"), "{last}");
}
#[test]
fn enter_on_a_record_with_no_rung_leaves_its_tree_alone() {
let mut p = lens_pager(80, 40);
press(&mut p, "G");
assert!(p.cursor_text().contains("Done."), "{:?}", p.cursor_text());
let shut = p.line_count();
key(&mut p, Key::Enter);
assert_eq!(p.line_count(), shut, "no rung, and no tree: `Enter` did nothing");
assert!(!p.visible_text().iter().any(|r| r.contains("\"type\"")), "and no JSON");
press(&mut p, "za");
assert_eq!(p.line_count(), shut, "`za` is the same key and the same answer");
press(&mut p, "r");
let with_tree = p.line_count();
assert!(with_tree > shut, "`r` is what opens it");
key(&mut p, Key::Enter);
assert_eq!(p.line_count(), with_tree, "and `Enter` leaves it exactly where it is");
assert!(p.visible_text().iter().any(|r| r.contains("\"type\"")), "tree still there");
press(&mut p, "r");
assert_eq!(p.line_count(), shut, "the `r` that opened it is the way out");
}
#[test]
fn enter_inside_an_open_tree_does_not_shut_it() {
let mut p = lens_pager(80, 40);
press(&mut p, "G");
press(&mut p, "r");
let with_tree = p.line_count();
press(&mut p, "j");
assert!(p.cursor_text().contains('"'), "a row of the tree: {:?}", p.cursor_text());
key(&mut p, Key::Enter);
assert_eq!(p.line_count(), with_tree, "the tree the cursor is in survives");
press(&mut p, "za");
assert_eq!(p.line_count(), with_tree);
assert!(p.visible_text().iter().any(|r| r.contains("\"type\"")));
}
#[test]
fn enter_on_a_record_with_no_rung_after_zr_is_inert_every_press() {
let mut p = lens_pager(80, 40);
press(&mut p, "zR");
let rows = p.visible_text();
let done = rows.iter().position(|r| r.contains("Done.")).expect("the answer");
p.goto(done);
let open = p.line_count();
for press_no in 0..3 {
key(&mut p, Key::Enter);
assert_eq!(p.line_count(), open, "press {press_no} moved rows");
}
}