use std::time::Duration;
use termlens::{Key, Terminal};
fn sh(script: &str) -> termlens::Result<Terminal> {
Terminal::builder()
.timeout(Duration::from_secs(10))
.args(["-c", script])
.spawn("/bin/sh")
}
fn list_with_highlight(row: u16) -> String {
let (one, two) = match row {
0 => (r"\033[7mitem one\033[0m", "item two"),
_ => ("item one", r"\033[7mitem two\033[0m"),
};
format!(r"printf '{one}\n{two}\n'; read guard")
}
#[test]
fn moving_a_highlight_changes_the_styled_rendering_only() -> termlens::Result<()> {
let settled = |s: &termlens::Screen| s.contains("item two") && s.cursor() == (2, 0, true);
let mut first = sh(&list_with_highlight(0))?;
first.wait_until(settled)?;
let a = first.screen();
first.send(Key::Enter)?;
first.wait_exit()?;
let mut second = sh(&list_with_highlight(1))?;
second.wait_until(settled)?;
let b = second.screen();
second.send(Key::Enter)?;
second.wait_exit()?;
assert_eq!(a.text(), b.text());
assert_eq!(a.to_string(), b.to_string());
assert_ne!(a.with_styles().to_string(), b.with_styles().to_string());
assert!(a.with_styles().to_string().contains("0: 0-7 reverse"));
assert!(b.with_styles().to_string().contains("1: 0-7 reverse"));
Ok(())
}
#[test]
fn styled_screen_snapshot() -> termlens::Result<()> {
let mut t = sh(concat!(
r"printf '\033[1;31mERROR\033[0m plain \033[4;34munderlined\033[0m\n'; ",
r"printf 'second row \033[7mselected\033[0m\n'; read guard"
))?;
t.wait_until(|s| s.contains("selected") && s.cursor() == (2, 0, true))?;
insta::assert_snapshot!(t.screen().with_styles());
t.send(Key::Enter)?;
assert!(t.wait_exit()?.success());
Ok(())
}
#[test]
fn a_masked_field_is_distinguishable_from_clear_text() -> termlens::Result<()> {
let settled = |s: &termlens::Screen| s.contains("pw: hunter2|") && s.cursor() == (0, 12, true);
let mut masked = sh(r"printf 'pw: \033[8mhunter2\033[28m|'; read guard")?;
masked.wait_until(settled)?;
let a = masked.screen();
masked.send(Key::Enter)?;
masked.wait_exit()?;
let mut clear = sh(r"printf 'pw: hunter2|'; read guard")?;
clear.wait_until(settled)?;
let b = clear.screen();
clear.send(Key::Enter)?;
clear.wait_exit()?;
assert_eq!(a.text(), b.text());
let secret_is_masked =
|s: &termlens::Screen| (4..11).all(|col| s.cell(0, col).is_some_and(|c| c.style().conceal));
assert!(
secret_is_masked(&a),
"the field is masked:\n{}",
a.with_styles()
);
assert!(
!secret_is_masked(&b),
"and clear text must fail the same assertion:\n{}",
b.with_styles()
);
assert_ne!(a.with_styles().to_string(), b.with_styles().to_string());
Ok(())
}
#[test]
fn strikethrough_and_blink_appear_in_the_styled_rendering() -> termlens::Result<()> {
let mut t = sh(concat!(
r"printf 'done \033[9mship it\033[29m ",
r"\033[5;31moverdue\033[0m plain'; read guard"
))?;
t.wait_until(|s| s.contains("plain"))?;
let s = t.screen();
let styled = s.with_styles().to_string();
assert!(styled.contains("strikethrough"), "{styled}");
assert!(styled.contains("blink"), "{styled}");
assert!(styled.contains("fg=1"), "{styled}");
let overdue = s.find("overdue").expect("painted");
let badge = *s.cell(overdue.0, overdue.1).unwrap().style();
assert!(badge.blink && badge.fg == termlens::Color::Indexed(1));
t.send(Key::Enter)?;
assert!(t.wait_exit()?.success());
Ok(())
}
#[test]
fn colon_form_rgb_colours_match_semicolon_form() -> termlens::Result<()> {
let mut t = sh(concat!(
r"printf '\033[38;2;10;20;30mA\033[0m\033[38:2::10:20:30mB\033[0m'; ",
"read guard"
))?;
t.wait_until(|s| s.contains("AB"))?;
let s = t.screen();
let semicolon = s.find("A").expect("semicolon-form colour");
let colon = s.find("B").expect("colon-form colour");
assert_eq!(
s.cell(semicolon.0, semicolon.1).unwrap().style().fg,
termlens::Color::Rgb(10, 20, 30)
);
assert_eq!(
s.cell(colon.0, colon.1).unwrap().style().fg,
termlens::Color::Rgb(10, 20, 30)
);
t.send(Key::Enter)?;
assert!(t.wait_exit()?.success());
Ok(())
}
#[test]
fn dim_and_italic_appear_without_shadow_collisions() -> termlens::Result<()> {
let mut t = sh(concat!(
r"printf '\033[2mfaint\033[0m ",
r"\033[3mslanted\033[0m\n'; read guard"
))?;
t.wait_until(|s| s.contains("slanted") && s.cursor() == (1, 0, true))?;
let s = t.screen();
let faint = s.find("faint").expect("dim run");
let dim = *s.cell(faint.0, faint.1).unwrap().style();
assert!(dim.dim && !dim.italic);
let slanted = s.find("slanted").expect("italic run");
let italic = *s.cell(slanted.0, slanted.1).unwrap().style();
assert!(italic.italic && !italic.dim && !italic.conceal);
let styled = s.with_styles().to_string();
assert!(styled.contains("0: 0-4 dim"), "{styled}");
assert!(styled.contains("6-12 italic"), "{styled}");
t.send(Key::Enter)?;
assert!(t.wait_exit()?.success());
Ok(())
}