Cotton code
74 lines · 3.6 KB · RustRaw
use super::*;use serde_json::json;// RULE: The kinds and states this module writes are exactly the table's CHECKs, because a kind the store refuses would fail at commit, on the person's Connect.#[test]fn kinds_and_states_are_the_tables_checks() {    let ddl = cotton_store::schema::ddl::CONNECTIONS_TABLE;    let kinds: Vec<String> = ConnectionKind::ALL.iter().map(|kind| format!("'{}'", kind.as_str())).collect();    assert!(ddl.contains(&format!("kind IN ({})", kinds.join(","))), "kinds drifted from the CHECK");    for state in ["reading", "paused", "needs_sign_in", "failed"] {        assert!(ddl.contains(&format!("'{state}'")));        assert_eq!(ConnectionState::parse(state).map(ConnectionState::as_str), Some(state));    }    for kind in ConnectionKind::ALL {        assert_eq!(ConnectionKind::parse(kind.as_str()), Some(kind));    }}// RULE: The same account connected twice converges on one row, because the id is derived from kind and what it reaches, case folded.#[test]fn the_same_account_is_one_row() {    assert_eq!(        connection_id(ConnectionKind::Google, " Me@Example.com "),        connection_id(ConnectionKind::Google, "me@example.com")    );    assert_ne!(        connection_id(ConnectionKind::Google, "me@example.com"),        connection_id(ConnectionKind::Mail, "me@example.com")    );}// RULE: a connection always has a name the CHECK accepts, and config is an object.#[test]fn a_new_row_is_whole() {    let patch = row("w", ConnectionKind::GitHost, "  ", " git.example.org ", ConnectionState::NeedsSignIn, None, json!(null));    assert_eq!(patch["name"], json!("Code host"));    assert_eq!(patch["reaches"], json!("git.example.org"));    assert_eq!(patch["state"], json!("needs_sign_in"));    assert_eq!(patch["secret_id"], json!(null));    assert_eq!(patch["config"], json!({}));}// RULE: Read now is owed only when asked after the last read, and never while paused.#[test]fn read_now_is_owed_only_when_asked_since() {    assert!(read_requested("reading", Some("2026-10-06T10:00:00Z"), None));    assert!(read_requested("failed", Some("2026-10-06T10:00:00Z"), Some("2026-10-06T09:00:00Z")));    assert!(!read_requested("reading", Some("2026-10-06T09:00:00Z"), Some("2026-10-06T10:00:00Z")));    assert!(!read_requested("paused", Some("2026-10-06T10:00:00Z"), None));    assert!(!read_requested("reading", None, None));}// RULE: a failed read keeps its error in words; a good read clears it.#[test]fn a_read_clears_the_error_a_failure_writes() {    let failed = failed_patch(ConnectionState::Failed, " The server said no. ");    assert_eq!(failed["last_error"], json!("The server said no."));    let read = read_patch("2026-10-06T10:00:00Z");    assert_eq!(read["state"], json!("reading"));    assert_eq!(read["last_error"], json!(null));}/// RULE: The owner's Stories-off switch holds for every WhatsApp number in the database, so a newly linked number cannot bypass it.#[test]fn stories_switch_is_shared_between_numbers() {    let db = cotton_store::Driver::open_in_memory().unwrap();    db.exec("CREATE TABLE cotton_connections (id TEXT, workspace_id TEXT, kind TEXT, config TEXT)").unwrap();    db.exec(r#"INSERT INTO cotton_connections VALUES ('wa','personal','whatsapp','{"account":"invented","statuses":false}'),('wa2','personal','whatsapp','{"account":"another"}'),('other','other-db','whatsapp','{"statuses":true}')"#).unwrap();    assert!(!stories_enabled(&db,"personal").unwrap());    assert!(stories_enabled(&db,"other-db").unwrap());    db.exec("UPDATE cotton_connections SET config=json_set(config,'$.statuses',1) WHERE workspace_id='personal'").unwrap();    assert!(stories_enabled(&db,"personal").unwrap());}