use tess::line_index::LineIndex;
use tess::render::Cell;
use tess::source::{FileSource, MockSource, Source};
use tess::viewport::Viewport;
fn first_body_row(viewport: &mut Viewport, src: &dyn Source) -> String {
let mut idx = LineIndex::new();
let frame = viewport.frame(src, &mut idx);
let row = &frame.body[0];
let mut s = String::new();
for c in row.iter().take(20) {
if let Cell::Char { ch, .. } = c { s.push(*ch); }
}
s
}
#[test]
fn user_scenario_scroll_walks_wraps() {
let m = MockSource::new();
let mut content = Vec::new();
for i in 0..10 {
let marker = format!("L{:02}_", i);
let mut row = String::new();
while row.len() < 5000 {
row.push_str(&marker);
}
row.truncate(5000);
content.extend_from_slice(row.as_bytes());
content.push(b'\n');
}
m.append(&content);
m.finish();
let mut idx = LineIndex::new();
let mut v = Viewport::new(100, 40, "test".into());
let initial = first_body_row(&mut v, &m);
assert!(initial.starts_with("L00_"), "initial row 0 should start with L00_, got {:?}", initial);
v.scroll_lines(1, &m, &mut idx);
let after_one = first_body_row(&mut v, &m);
assert!(after_one.starts_with("L00_"), "after 1 j we should still be in line 0; got {:?}", after_one);
for _ in 0..11 { v.scroll_lines(1, &m, &mut idx); }
let after_twelve = first_body_row(&mut v, &m);
assert!(after_twelve.starts_with("L00_"), "after 12 j still in line 0; got {:?}", after_twelve);
for _ in 0..38 { v.scroll_lines(1, &m, &mut idx); }
let after_fifty = first_body_row(&mut v, &m);
assert!(after_fifty.starts_with("L01_"), "after 50 j we should be at line 1; got {:?}", after_fifty);
}
#[test]
fn user_scenario_with_real_filesource() {
let path = std::env::temp_dir().join("tess_user_scenario.log");
let mut content = Vec::new();
for i in 0..10 {
let marker = format!("L{:02}_", i);
let mut row = String::new();
while row.len() < 5000 { row.push_str(&marker); }
row.truncate(5000);
content.extend_from_slice(row.as_bytes());
content.push(b'\n');
}
std::fs::write(&path, &content).unwrap();
let src = FileSource::open(&path).unwrap();
let mut idx = LineIndex::new();
let mut v = Viewport::new(100, 40, "test".into());
for _ in 0..11 { v.scroll_lines(1, &src, &mut idx); }
let frame = v.frame(&src, &mut idx);
let body_last = &frame.body[frame.body.len() - 1];
let mut s = String::new();
for c in body_last.iter() {
if let Cell::Char { ch, .. } = c { s.push(*ch); }
}
assert!(s.starts_with("L00_"), "bottom body row should still be wrap 49 of line 0 (the END); got {:?}", s);
}