#![deny(unsafe_code)]
use super::*;
use crate::source::jsonl::JsonlSource;
fn run_of_four() -> String {
concat!(
r#"{"type":"user","timestamp":"2026-08-05T14:01:00.000Z","#,
r#""message":{"role":"user","content":"add a lens"}}"#,
"\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:02:20.000Z","message":{"role":"assistant","#,
r#""content":[{"type":"tool_use","id":"t2","name":"Read","input":{"file_path":"src/lens/mod.rs"}}]}}"#,
"\n",
r#"{"type":"user","timestamp":"2026-08-05T14:02:21.000Z","message":{"role":"user","#,
r#""content":[{"type":"tool_result","tool_use_id":"t2","content":"a\nb"}]}}"#,
"\n",
r#"{"type":"assistant","timestamp":"2026-08-05T14:03:00.000Z","message":{"role":"assistant","#,
r#""content":[{"type":"text","text":"Done."}]}}"#,
"\n"
)
.to_string()
}
fn with_open_run(rows: usize) -> Pager {
let mut src = JsonlSource::from_bytes(run_of_four().into_bytes());
src.set_lens(crate::lens::find("agent").expect("the agent lens"));
let mut p = Pager::new(Box::new(src), "session.jsonl".into(), 80, rows, None);
let _ = p.visible_text();
press(&mut p, "j");
assert!(p.cursor_text().contains('\u{27e8}'), "on the run: {:?}", p.cursor_text());
key(&mut p, Key::Enter);
p
}
#[test]
fn j_after_enter_lands_inside_the_run() {
let mut p = with_open_run(40);
let run = p.cursor;
press(&mut p, "j");
assert_eq!(p.cursor, run + 1, "the first step, not the block after the run");
assert!(p.cursor_text().contains("Bash(git status)"), "{:?}", p.cursor_text());
}
#[test]
fn j_visits_every_member_then_leaves_the_run() {
let mut p = with_open_run(40);
let mut seen = Vec::new();
for _ in 0..5 {
press(&mut p, "j");
seen.push(p.cursor_text());
}
assert!(seen[0].contains("Bash(git status)"), "{seen:#?}");
assert!(seen[1].contains("Bash \u{2192}"), "{seen:#?}");
assert!(seen[2].contains("Read(src/lens/mod.rs)"), "{seen:#?}");
assert!(seen[3].contains("Read \u{2192}"), "{seen:#?}");
assert!(seen[4].contains("Done."), "out of the run: {seen:#?}");
}
#[test]
fn k_mirrors_j_over_an_open_run() {
let mut p = with_open_run(40);
let start = p.cursor;
let mut down = Vec::new();
for _ in 0..5 {
press(&mut p, "j");
down.push(p.cursor);
}
let mut up = Vec::new();
for _ in 0..5 {
press(&mut p, "k");
up.push(p.cursor);
}
let mut want: Vec<usize> = down[..down.len() - 1].to_vec();
want.reverse();
want.push(start);
assert_eq!(up, want, "down {down:?}");
press(&mut p, "k");
assert_eq!(p.cursor, 0, "and out of the run to the block above it");
}
#[test]
fn j_reads_the_tree_a_member_has_open_and_tab_clears_it() {
let mut p = with_open_run(60);
press(&mut p, "j");
let member = p.cursor;
press(&mut p, "r");
let block = p.src_block_at(member).expect("the member's block");
assert_eq!(block.start, member);
assert!(block.end > member + 1, "the tree is in it: {block:?}");
assert_eq!(
p.src_block_at(block.end - 1),
Some(block.clone()),
"its last tree row too"
);
for row in member + 1..block.end {
press(&mut p, "j");
assert_eq!(p.cursor, row, "j walks the tree row by row");
}
press(&mut p, "k");
assert_eq!(p.cursor, block.end - 2, "and k is its mirror, a row at a time");
key(&mut p, Key::Tab);
assert_eq!(p.cursor, block.end, "one Tab clears the whole member");
key(&mut p, Key::BackTab);
assert_eq!(p.cursor, member, "and one S-Tab comes back to it");
}
#[test]
fn the_block_counter_follows_the_open_run() {
let mut src = JsonlSource::from_bytes(run_of_four().into_bytes());
src.set_lens(crate::lens::find("agent").expect("the agent lens"));
let mut p = Pager::new(Box::new(src), "session.jsonl".into(), 80, 40, None);
let _ = p.visible_text();
press(&mut p, "j");
assert!(p.status_line().contains("block 2/3"), "{}", p.status_line());
key(&mut p, Key::Enter);
assert!(p.status_line().contains("block 2/7"), "{}", p.status_line());
press(&mut p, "j");
assert!(p.status_line().contains("block 3/7"), "{}", p.status_line());
press(&mut p, "jjjj");
assert!(p.cursor_text().contains("Done."), "{:?}", p.cursor_text());
assert!(p.status_line().contains("block 7/7"), "{}", p.status_line());
}
#[test]
fn closing_the_run_collapses_it_back_to_one_block() {
let mut p = with_open_run(40);
let run = p.cursor;
let open_rows = p.line_count();
key(&mut p, Key::Enter);
assert_eq!(p.cursor, run);
assert!(p.cursor_text().contains('\u{27e8}'), "{:?}", p.cursor_text());
assert_eq!(p.line_count(), open_rows - 4, "the four steps are folded again");
assert_eq!(p.src_block_at(run), Some(run..run + 1), "one block again");
assert!(p.status_line().contains("block 2/3"), "{}", p.status_line());
key(&mut p, Key::Tab);
assert!(p.cursor_text().contains("Done."), "and Tab steps over it again");
}
fn run_at_the_tail() -> String {
concat!(
r#"{"type":"user","timestamp":"2026-08-05T14:01:00.000Z","#,
r#""message":{"role":"user","content":"add a lens"}}"#,
"\n",
r#"{"type":"assistant","timestamp":"2026-08-05T14:01:30.000Z","message":{"role":"assistant","#,
r#""content":[{"type":"text","text":"Done."}]}}"#,
"\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:02:20.000Z","message":{"role":"assistant","#,
r#""content":[{"type":"tool_use","id":"t2","name":"Read","input":{"file_path":"src/lens/mod.rs"}}]}}"#,
"\n",
r#"{"type":"user","timestamp":"2026-08-05T14:02:21.000Z","message":{"role":"user","#,
r#""content":[{"type":"tool_result","tool_use_id":"t2","content":"a\nb"}]}}"#,
"\n"
)
.to_string()
}
#[test]
fn tab_steps_through_a_trailing_run_that_is_open() {
let mut src = JsonlSource::from_bytes(run_at_the_tail().into_bytes());
src.set_lens(crate::lens::find("agent").expect("the agent lens"));
let mut p = Pager::new(Box::new(src), "session.jsonl".into(), 80, 40, None);
let _ = p.visible_text();
key(&mut p, Key::Tab);
assert!(p.cursor_text().contains("Done."), "the last message: {:?}", p.cursor_text());
key(&mut p, Key::Tab);
let run = p.cursor;
assert!(p.cursor_text().contains('\u{27e8}'), "the trailing run: {:?}", p.cursor_text());
key(&mut p, Key::Enter);
key(&mut p, Key::Tab);
assert_eq!(p.cursor, run + 1, "into the open run rather than nowhere");
assert!(p.cursor_text().contains("Bash(git status)"), "{:?}", p.cursor_text());
}
#[test]
fn j_is_monotone_and_reaches_the_last_row() {
let mut p = with_open_run(40);
press(&mut p, "g");
assert_eq!(p.cursor, 0, "back to the top");
let n = p.line_count();
let mut at = p.cursor;
for _ in 0..n * 2 {
press(&mut p, "j");
assert!(p.cursor > at || p.cursor == n - 1, "stalled at {at} of {n}");
at = p.cursor;
}
assert_eq!(at, n - 1, "the last row is reached");
}