Cotton code
116 lines · 4.9 KB · RustRaw
//! The bin's registry and its one formatter. `bin.ts` ships with no test file//! of its own — its guard IS the static assertion at module load, which in Rust//! is the exhaustive `match` — so these pin the parts a `match` cannot: the//! table and column names, which are wire strings against the schema, and the//! title fallback.use super::*;#[test]fn every_bin_kind_names_a_table_a_column_and_words_for_having_no_title() {    for kind in BinKind::ALL {        let source = bin_source(kind);        assert!(source.table.starts_with("cotton_"), "{kind}");        assert!(!title_column(kind).is_empty(), "{kind}");        assert_ne!(bin_title(kind, None, &[]), "", "{kind}");    }}#[test]fn the_sources_are_the_typescripts_own() {    let email = bin_source(BinKind::Email);    assert_eq!(email.table, "cotton_emails");    assert_eq!(title_column(BinKind::Email), "subject");    assert_eq!(bin_title(BinKind::Email, None, &[]), "(no subject)");    // Coming back out of the bin is a change Gmail has to hear about, exactly    // as going in was.    assert_eq!(        email.restore_also,        [("sync_state", RestoreValue::Text("pending"))]    );    let thread = bin_source(BinKind::EmailThread);    assert_eq!(thread.table, "cotton_email_threads");    assert_eq!(title_column(BinKind::EmailThread), "subject");    assert_eq!(bin_title(BinKind::EmailThread, None, &[]), "(no subject)");    // A thread's row carries no `sync_state`: restoring it is restoring its    // MESSAGES, each of which goes `pending` on its own.    assert!(thread.restore_also.is_empty());}#[test]fn a_bin_row_shows_what_the_record_is_called() {    assert_eq!(        bin_title(BinKind::Email, Some("Invoice for March"), &[]),        "Invoice for March"    );    assert_eq!(bin_title(BinKind::Email, Some("  Trimmed  "), &[]), "Trimmed");}/// A bin row with no words on it is a row nobody can decide about.#[test]fn a_bin_row_with_nothing_to_call_it_borrows_its_kinds_own_words() {    assert_eq!(bin_title(BinKind::Email, None, &[]), "(no subject)");    assert_eq!(bin_title(BinKind::Email, Some(""), &[]), "(no subject)");    assert_eq!(bin_title(BinKind::Email, Some("   "), &[]), "(no subject)");    assert_eq!(bin_title(BinKind::EmailThread, None, &[]), "(no subject)");}/// The trim is JavaScript's, not Rust's — a BOM-only subject is blank on both/// sides, and a NEL-only one is blank on neither.#[test]fn the_title_trim_is_javascripts_whitespace_class() {    assert_eq!(bin_title(BinKind::Email, Some("\u{FEFF}"), &[]), "(no subject)");    assert_eq!(bin_title(BinKind::Email, Some("\u{0085}"), &[]), "\u{0085}");}/// RULE: The store's bin tables are exactly the bin engine's sources, in order, each marked by `trashed_at` and naming its kind — so the fold's erase step (`cotton_store::erase`) reaches every binnable kind and no other table.#[test]fn the_store_bin_tables_are_the_engine_bin_sources() {    let engine: Vec<(&str, &str, &str)> = BinKind::ALL        .iter()        .map(|kind| (bin_source(*kind).table, "trashed_at", kind.as_str()))        .collect();    // A retired table's kept rows (`crate::primitives::RETIRED_TABLES`) keep    // their storage's erase step; no bin kind reads them any more.    let store: Vec<(&str, &str, &str)> = cotton_store::erase::BIN_TABLES        .iter()        .copied()        .filter(|(table, _, _)| !crate::primitives::RETIRED_TABLES.contains(table))        .collect();    assert_eq!(store, engine);}/// RULE: a bin kind's title column is its built-in definition's `title`; a/// hand entry stands only for a kind with no built-in definition, or one whose/// definition's title is not the stored column — never a copy of what a/// definition already says (lane T7, 22 Oct 2026: stop hand-listing tables).#[test]fn the_bin_reads_its_title_columns_off_the_definitions() {    let definitions = crate::definitions::builtin_table_definitions();    for (kind, column) in HAND_TITLES {        if let Some(definition) = definitions.iter().find(|definition| definition.id == kind.as_str()) {            assert_ne!(&definition.title, column, "{kind}: the hand title copies its definition's; drop it");        }    }    for kind in BinKind::ALL {        if HAND_TITLES.iter().any(|(hand, _)| *hand == kind) {            continue;        }        let definition = definitions.iter().find(|definition| definition.id == kind.as_str());        assert_eq!(definition.map(|definition| definition.title.as_str()), Some(title_column(kind)), "{kind}");    }}/// A rewording of an untitled row's words is a row of the `bin` set, never a/// build.#[test]fn an_untitled_rows_words_are_the_databases_line() {    let rows = [crate::rules::shell_lines::LineRow {        set: "bin".into(),        line: "untitled.email".into(),        words: "Nothing to call it".into(),    }];    assert_eq!(bin_title(BinKind::Email, None, &rows), "Nothing to call it");    assert_eq!(bin_title(BinKind::Note, None, &rows), "(untitled)");}