Cotton code
56 lines · 3.4 KB · RustRaw
//! UNSENT SEEN (coordinator, 26 Nov 2026): what waits to be sent, counted by table and by the refusal code each//! held write carries (`outbox::coded`), with the oldest write's age, and never a value, a row id or a reason.//! Synthetic rows only.use cotton_store::{create::apply_schema, outbox, Driver};fn queue(db: &Driver, op: &str, table: &str, row: &str, at: &str, error: Option<&str>) {    db.run(        "INSERT INTO _outbox (op_id, kind, author, tbl, row_id, hlc, schema_version, queued_at, bytes, last_error) VALUES (?, 'change', 'device:invented', ?, ?, '0', 1, ?, x'00', ?)",        &[            rusqlite::types::Value::Text(op.into()),            rusqlite::types::Value::Text(table.into()),            rusqlite::types::Value::Text(row.into()),            rusqlite::types::Value::Text(at.into()),            error.map_or(rusqlite::types::Value::Null, |e| rusqlite::types::Value::Text(e.into())),        ],    )    .unwrap();}/// RULE: a held write's last_error starts with its refusal's code when it has one a code can be, and the reason alone otherwise.#[test]fn a_held_write_carries_its_code_first() {    assert_eq!(outbox::coded(Some("seal_key_missing"), "no key yet"), "seal_key_missing: no key yet");    assert_eq!(outbox::coded(None, "the server said 503"), "the server said 503");    assert_eq!(outbox::coded(Some("Not A Code"), "x"), "x");    assert_eq!(outbox::coded(Some(""), "x"), "x");    assert_eq!(outbox::coded(Some("seal_key_missing"), "seal_key_missing: no key for x"), "seal_key_missing: no key for x");    assert_eq!(outbox::coded(Some("seal_does_not_open"), "seal_does_not_open"), "seal_does_not_open: ");}/// RULE: what waits is counted as things by table and as writes by code (`queued` for one never refused, `other` for a reason with no code), with the oldest write's queued_at and each code's; no reason, value or row id is in the answer.#[test]fn what_waits_is_counted_by_table_and_code_with_the_oldest() {    let db = Driver::open_in_memory().unwrap();    apply_schema(&db).unwrap();    // A note edited twice and held for its key; a second note never refused; a task refused by an older build.    queue(&db, "op-1", "cotton_notes", "note-secret-id", "2026-11-24T08:00:00.000Z", Some(&outbox::coded(Some("seal_key_missing"), "the private words of a reason")));    queue(&db, "op-2", "cotton_notes", "note-secret-id", "2026-11-25T08:00:00.000Z", Some(&outbox::coded(Some("seal_key_missing"), "again")));    queue(&db, "op-3", "cotton_notes", "note-two", "2026-11-26T08:00:00.000Z", None);    queue(&db, "op-4", "cotton_todos", "todo-one", "2026-11-25T09:00:00.000Z", Some("the server said 503"));    let waiting = outbox::waiting(&db).unwrap();    assert_eq!((waiting.things, waiting.ops), (3, 4));    assert_eq!(waiting.by_table.get("cotton_notes"), Some(&2));    assert_eq!(waiting.by_table.get("cotton_todos"), Some(&1));    assert_eq!(waiting.by_code.get("seal_key_missing"), Some(&2));    assert_eq!(waiting.by_code.get("queued"), Some(&1));    assert_eq!(waiting.by_code.get("other"), Some(&1));    assert_eq!(waiting.oldest_at.as_deref(), Some("2026-11-24T08:00:00.000Z"));    assert_eq!(waiting.oldest_by_code.get("seal_key_missing").map(String::as_str), Some("2026-11-24T08:00:00.000Z"));    let said = format!("{waiting:?}");    for private in ["note-secret-id", "private words", "503"] {        assert!(!said.contains(private), "{private} in {said}");    }}