use crate::redraw_frame;
#[test]
fn redraw_grow_rewrites_from_old_top() {
let mut buf: Vec<u8> = Vec::new();
let frame = (0..14).map(|i| format!("line{i}")).collect::<Vec<_>>().join("\n");
let n = redraw_frame(&mut buf, &frame, 5);
assert_eq!(n, 14);
let s = String::from_utf8(buf).unwrap();
assert!(s.starts_with("\x1b[4F"), "must move up prev-1 to old top: {s:?}");
assert_eq!(s.matches("line13").count(), 1);
assert!(!s.contains("\x1b[1B"), "no shrink needed: {s:?}");
}
#[test]
fn redraw_shrink_clears_stale_rows_and_returns_to_new_bottom() {
let mut buf: Vec<u8> = Vec::new();
let frame = (0..5).map(|i| format!("line{i}")).collect::<Vec<_>>().join("\n");
let n = redraw_frame(&mut buf, &frame, 14);
assert_eq!(n, 5);
let s = String::from_utf8(buf).unwrap();
assert!(s.starts_with("\x1b[13F"), "must move up prev-1: {s:?}");
assert_eq!(s.matches("\n").count(), 4, "exactly n-1 line feeds: {s:?}");
let shrink = s.split("line4").nth(1).unwrap();
assert!(shrink.starts_with("\x1b[1B"), "down past frame, no clear: {shrink:?}");
assert_eq!(shrink.matches("\x1b[2K\x1b[1B").count(), 8, "stale rows: {shrink:?}");
assert!(shrink.ends_with("\x1b[2K\x1b[9F"), "clear old bottom + up prev-n: {shrink:?}");
}
#[test]
fn redraw_no_previous_frame_just_draws() {
let mut buf: Vec<u8> = Vec::new();
let n = redraw_frame(&mut buf, "a\nb", 0);
assert_eq!(n, 2);
let s = String::from_utf8(buf).unwrap();
assert_eq!(s, "\x1b[2K\ra\n\x1b[2K\rb", "no up-move on first frame, no trailing nl: {s:?}");
}