#![deny(unsafe_code)]
use super::*;
#[test]
fn the_registry_answers_by_name() {
assert!(exists("agent"));
assert!(!exists("opencode"), "not implemented yet, so not offered");
assert!(!exists(""));
assert_eq!(find("agent").map(|l| l.name()), Some("agent"));
assert!(find("nope").is_none());
assert!(names().contains(&"agent"));
}
#[test]
fn every_lens_agrees_with_its_entry() {
for name in names() {
let lens = find(name).expect("registered lens builds");
assert_eq!(lens.name(), name);
assert!(!lens.about().is_empty(), "{name} says nothing about itself");
assert!(list_text().contains(name));
assert!(list_text().contains(lens.about()));
}
}
#[test]
fn a_dialect_says_where_its_records_are_and_lines_is_the_default() {
struct Quiet;
impl Lens for Quiet {
fn name(&self) -> &'static str {
"quiet"
}
fn about(&self) -> &'static str {
"a dialect that says nothing about where it lives"
}
fn read(&mut self, _: &Value) -> Option<Summary> {
None
}
}
assert_eq!(Quiet.records_at(), RecordsAt::Lines);
struct Rooted;
impl Lens for Rooted {
fn name(&self) -> &'static str {
"rooted"
}
fn about(&self) -> &'static str {
"a dialect whose records are a document's root array"
}
fn records_at(&self) -> RecordsAt {
RecordsAt::Root
}
fn read(&mut self, _: &Value) -> Option<Summary> {
None
}
}
assert_eq!(Rooted.records_at(), RecordsAt::Root);
assert_eq!(records_at("agent"), Some(RecordsAt::Lines));
assert_eq!(records_at("atif"), Some(RecordsAt::Member("steps")));
assert_eq!(records_at("nope"), None);
}
#[test]
fn each_open_gets_its_own_lens() {
let a = find("agent").expect("agent");
let b = find("agent").expect("agent");
assert_eq!(a.name(), b.name());
assert!(!std::ptr::eq(&*a as *const dyn Lens, &*b as *const dyn Lens));
}
#[test]
fn an_excerpt_is_one_line_of_collapsed_whitespace() {
assert_eq!(excerpt("hello", 40), "hello");
assert_eq!(excerpt(" hello world \n", 40), "hello world");
assert_eq!(excerpt("first\nsecond\nthird", 40), "first second third");
assert_eq!(excerpt("", 40), "");
assert_eq!(excerpt(" \n\t ", 40), "");
}
#[test]
fn an_excerpt_too_wide_is_cut_with_an_ellipsis() {
let cut = excerpt("abcdefghij", 5);
assert_eq!(cut, "abcd\u{2026}");
assert_eq!(crate::render::str_width(&cut), 5);
let wide = excerpt("\u{4f60}\u{597d}\u{4e16}\u{754c}", 5);
assert!(crate::render::str_width(&wide) <= 5, "{wide}");
}
#[test]
fn an_excerpt_carries_no_line_breaks() {
let text = excerpt("a\r\nb\tc", 40);
assert!(!text.contains('\n') && !text.contains('\r') && !text.contains('\t'));
assert_eq!(text, "a b c");
}
#[test]
fn a_clock_is_the_hour_and_minute_of_an_iso_timestamp() {
assert_eq!(clock("2026-08-05T21:28:58.659Z").as_deref(), Some("21:28"));
assert_eq!(clock("2026-08-05T00:00:00Z").as_deref(), Some("00:00"));
for bad in ["", "2026-08-05", "yesterday", "2026-08-05 21:28:58", "xxxxxxxxxxTxx:xx"] {
assert!(clock(bad).is_none(), "{bad} should not be a clock");
}
}
#[test]
fn a_timestamp_with_a_multibyte_character_is_refused_rather_than_split() {
for bad in [
"2026-08-05T21:\u{20ac}z", "2026-08-05T2\u{20ac}:28:58Z", "2026-08-05T21:2\u{1f600}", "2026-08-05T\u{20ac}1:28Z",
"\u{1f600}\u{1f600}\u{1f600}\u{1f600}T21:28Z",
] {
assert!(clock(bad).is_none(), "{bad:?} should not be a clock");
}
assert_eq!(clock("2026-08-05T21:28\u{20ac}").as_deref(), Some("21:28"));
}
#[test]
fn a_token_count_is_spelled_at_the_scale_it_reaches() {
assert_eq!(tokens(0), "0");
assert_eq!(tokens(380), "380");
assert_eq!(tokens(999), "999", "999 is not a thousand of anything");
assert_eq!(tokens(1_000), "1.0k");
assert_eq!(tokens(1_200), "1.2k");
assert_eq!(tokens(9_999), "9.9k");
assert_eq!(tokens(10_000), "10k");
assert_eq!(tokens(999_999), "999k");
assert_eq!(tokens(1_000_000), "1.0M");
}
#[test]
fn a_token_count_floors() {
assert_eq!(tokens(1_999), "1.9k", "not 2.0k");
assert_eq!(tokens(18_999), "18k", "not 19k");
assert_eq!(tokens(1_899_999), "1.8M", "not 1.9M");
assert_eq!(tokens(999_999_999), "999M");
}
#[test]
fn a_token_count_never_outgrows_its_column() {
let cases = [
0,
1,
999,
1_000,
9_999,
10_000,
999_999,
1_000_000,
20_385,
709_541,
802_174,
1_823_744,
u64::MAX / 2,
u64::MAX,
];
for n in cases {
let text = tokens(n);
assert!(
crate::render::str_width(&text) <= 4,
"{n} spelled as {text}, wider than four columns"
);
}
}
#[test]
fn record_helpers_read_only_what_is_there() {
let v = crate::json::parse(br#"{"type":"user","timestamp":"2026-08-05T21:28:58.659Z"}"#)
.expect("parse");
assert_eq!(record_type(&v), Some("user"));
assert_eq!(record_clock(&v).as_deref(), Some("21:28"));
let bare = crate::json::parse(br#"{"type":7}"#).expect("parse");
assert_eq!(record_type(&bare), None, "a non-string type is not a type");
assert_eq!(record_clock(&bare), None);
let array = crate::json::parse(b"[1,2]").expect("parse");
assert_eq!(record_type(&array), None);
}