Lines
98.56 %
Functions
100 %
Branches
use cli_core::render::schema;
use super::*;
fn wire_value(id: u64, val: &str) -> String {
format!("(:id {id} :value {val})")
}
fn wire_string(id: u64, s: &str) -> String {
format!("(:id {id} :value \"{s}\")")
fn wire_error(id: u64, code: &str, msg: &str) -> String {
format!("(:id {id} :error (:code {code} :message \"{msg}\"))")
fn list_row(cells: Vec<&str>) -> ListRow {
ListRow {
id: None,
cells: cells.into_iter().map(String::from).collect(),
#[test]
fn on_reply_scalar_number_becomes_single_row() {
let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
tab.on_reply(&wire_value(1, "42"));
assert!(matches!(tab.state, Fetch::Loaded(ref rows) if !rows.is_empty()));
fn on_reply_nil_list_is_loaded_and_empty() {
tab.on_reply(&wire_value(1, "nil"));
assert!(matches!(tab.state, Fetch::Loaded(ref rows) if rows.is_empty()));
fn on_reply_server_error_becomes_error_state() {
tab.on_reply(&wire_error(1, "db", "connection failed"));
assert!(matches!(tab.state, Fetch::Error(ref s) if s.contains("db")));
fn on_reply_string_that_is_not_a_list_is_single_row() {
tab.on_reply(&wire_string(1, "just-a-uuid"));
assert!(!matches!(tab.state, Fetch::Idle));
fn select_next_advances_when_loaded() {
tab.state = Fetch::Loaded(vec![
list_row(vec!["a"]),
list_row(vec!["b"]),
list_row(vec!["c"]),
]);
assert_eq!(tab.selected, 0);
tab.select_next();
assert_eq!(tab.selected, 1);
assert_eq!(tab.selected, 2);
assert_eq!(tab.selected, 2, "must not advance past last row");
fn select_prev_saturates_at_zero() {
tab.selected = 0;
tab.select_prev();
fn reset_returns_to_idle() {
tab.state = Fetch::Loaded(vec![list_row(vec!["x"])]);
tab.selected = 3;
tab.reset();
assert_eq!(tab.state, Fetch::Idle);
assert_eq!(tab.window.top(), 0);
fn set_loading_id_updates_state() {
tab.set_loading_id(42);
assert_eq!(tab.state, Fetch::Loading { id: 42 });
// --- L4: structured ListRow tests ---
// Real list-accounts replies are typed records with a leading `:account`
// tag (exactly as `render_entity` emits), so `:id` sits after the tag.
const TYPED_ACCOUNTS: &str = r#"((:account :id "uuid-1" :name "Checking" :parent "") (:account :id "uuid-2" :name "Savings" :parent ""))"#;
/// Wire reply carrying the typed-record list as the `:value` payload.
fn wire_accounts_list() -> String {
format!("(:id 1 :value {TYPED_ACCOUNTS})")
fn on_reply_list_accounts_rows_carry_ids() {
tab.on_reply(&wire_accounts_list());
let Fetch::Loaded(ref rows) = tab.state else {
panic!("expected Loaded state");
};
assert_eq!(rows.len(), 2);
assert_eq!(rows[0].id.as_deref(), Some("uuid-1"));
assert_eq!(rows[1].id.as_deref(), Some("uuid-2"));
fn on_reply_list_accounts_projects_schema_cells() {
// Name filled, Type/Parent blank (absent from these records)
assert_eq!(rows[0].cells, vec!["Checking", "", ""]);
assert_eq!(rows[1].cells, vec!["Savings", "", ""]);
fn selected_id_returns_focused_row_id() {
id: Some("id-A".into()),
cells: vec!["A".into()],
},
id: Some("id-B".into()),
cells: vec!["B".into()],
assert_eq!(tab.selected_id(), Some("id-A"));
tab.selected = 1;
assert_eq!(tab.selected_id(), Some("id-B"));
fn selected_id_none_when_not_loaded() {
let tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
assert_eq!(tab.selected_id(), None);
fn selected_id_none_when_row_has_no_id() {
tab.state = Fetch::Loaded(vec![ListRow {
cells: vec!["x".into()],
}]);
fn element_without_id_field_has_none_id_cells_still_rendered() {
let list_str = r#"((:account :name "NoId" :parent ""))"#;
let wire = format!(r#"(:id 1 :value "{}")"#, list_str.replace('"', "\\\""));
tab.on_reply(&wire);
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].id, None);
assert!(!rows[0].cells.is_empty());
fn select_next_prev_move_selected_id() {
id: Some("id-0".into()),
cells: vec!["zero".into()],
id: Some("id-1".into()),
cells: vec!["one".into()],
id: Some("id-2".into()),
cells: vec!["two".into()],
assert_eq!(tab.selected_id(), Some("id-0"));
assert_eq!(tab.selected_id(), Some("id-1"));
assert_eq!(tab.selected_id(), Some("id-2"));
fn accounts_wire(ids: &[&str]) -> String {
let rows: Vec<String> = ids
.iter()
.map(|id| format!(r#"(:account :id "{id}" :name "{id}" :parent "")"#))
.collect();
format!("(:id 1 :value ({}))", rows.join(" "))
fn refreshed_at(index: usize, reply: &[&str]) -> ListTab {
tab.on_reply(&accounts_wire(&["a", "b", "c"]));
tab.selected = index;
tab.refresh();
tab.on_reply(&accounts_wire(reply));
tab
fn refresh_keeps_the_focused_row_by_id() {
let tab = refreshed_at(2, &["a", "b", "bb", "c"]);
assert_eq!(tab.selected_id(), Some("c"));
assert_eq!(tab.selected, 3);
fn refresh_falls_back_to_the_same_index_when_the_row_is_gone() {
let tab = refreshed_at(1, &["a", "c", "d"]);
fn refresh_clamps_the_fallback_index_to_the_last_row() {
let tab = refreshed_at(2, &["a"]);
fn marked_cell_adds_the_gutter_to_the_first_cell_only_once_a_row_is_marked() {
assert_eq!(marked_cell("x", true, true, false), "* x");
assert_eq!(marked_cell("x", true, false, false), " x");
assert_eq!(marked_cell("x", true, false, true), "x");
assert_eq!(marked_cell("x", false, true, false), "x");
fn select_first_and_last_jump_to_the_ends() {
tab.select_last();
tab.select_first();
fn select_last_on_an_unloaded_list_stays_put() {
fn a_paged_tab_shows_the_transaction_headers() {
assert_eq!(ListTab::paged().headers(), ["Date", "Note", "Amount"]);