Cotton code
805 lines · 29.6 KB · RustRaw
//! `packages/store/__tests__/mediaLedger.test.ts`, ported exhaustively.//!//! Every timestamp comes in from the caller — the crate reads no clock — so//! every assertion here is on a fixed instant rather than on "roughly now",//! which is what makes the backoff arithmetic testable at all.use super::*;use crate::test_support::empty_file;const WS: &str = "11111111-1111-4111-8111-111111111111";const OTHER_WS: &str = "22222222-2222-4222-8222-222222222222";const T0: &str = "2026-08-25T10:00:00.000Z";const T1: &str = "2026-08-25T10:05:00.000Z";const T2: &str = "2026-08-25T11:00:00.000Z";fn sha() -> String {    "a".repeat(64)}fn key() -> String {    format!("{WS}/messages/thread-1/{}.jpg", sha())}/// The suite's `entry()` — every field defaulted, the caller overrides.fn entry() -> MediaEnqueueInput {    MediaEnqueueInput {        key: key(),        workspace_id: WS.to_string(),        sha256: sha(),        content_type: "image/jpeg".to_string(),        size_bytes: 4096,        local_path: Some("/var/media/aa/aaa.jpg".to_string()),        state: None,        after: None,        after_row_id: None,        tier: None,        captured_here: false,        now: T0.to_string(),    }}fn keys(rows: &[MediaRow]) -> Vec<String> {    rows.iter().map(|row| row.key.clone()).collect()}// ─────────────────────────────────────────────────────────────────────────────// enqueue_media// ─────────────────────────────────────────────────────────────────────────────#[test]fn records_a_file_as_pending_with_no_attempts_and_no_backoff() {    let db = empty_file();    let row = enqueue_media(&db, &entry()).expect("enqueues");    assert_eq!(        row,        MediaRow {            key: key(),            workspace_id: WS.to_string(),            sha256: sha(),            content_type: "image/jpeg".to_string(),            size_bytes: 4096,            local_path: Some("/var/media/aa/aaa.jpg".to_string()),            state: "pending".to_string(),            after: None,            after_row_id: None,            attempts: 0,            last_error: None,            next_attempt_at: None,            // NULL and 0 are the safe defaults, and both are real states: a            // row this build cannot name a tier for is one it must not act on,            // and only a capture on this device ever sets `captured_here`.            tier: None,            captured_here: false,            // Neither of the budget's own two is anybody's business at enqueue            // time: a view happens long after the file arrived, and a pin is            // the person changing their mind about a row months old.            pinned: false,            last_viewed_at: None,            last_code: None,            network: "any".to_string(),            asked_by: None,            source: None,            created_at: T0.to_string(),            updated_at: T0.to_string(),        }    );    assert_eq!(media_by_key(&db, &key()).expect("reads"), Some(row));}#[test]fn a_tier_survives_a_restatement_that_says_nothing_about_it() {    let db = empty_file();    enqueue_media(        &db,        &MediaEnqueueInput {            tier: Some(crate::media_holdings::Tier::Thumb),            ..entry()        },    )    .expect("enqueues");    // A re-enqueue that says nothing about the tier must not blank one the    // file already knows — the same COALESCE `local_path` has always had.    let row = enqueue_media(&db, &entry()).expect("re-enqueues");    assert_eq!(row.tier, Some(crate::media_holdings::Tier::Thumb));}#[test]fn a_capture_on_this_device_stays_a_capture_however_often_the_row_is_restated() {    let db = empty_file();    enqueue_media(        &db,        &MediaEnqueueInput {            captured_here: true,            ..entry()        },    )    .expect("enqueues");    // "This phone took these bytes" is a fact about the past. An import or a    // hydrate restating the same key may never turn a photograph the person    // shot into something the budgeted cache is allowed to delete.    let row = enqueue_media(&db, &entry()).expect("re-enqueues");    assert!(row.captured_here, "a restatement cleared `captured_here`");}#[test]fn carries_a_post_upload_trigger_and_the_row_it_names() {    let db = empty_file();    let row = enqueue_media(        &db,        &MediaEnqueueInput {            after: Some("document_process".to_string()),            after_row_id: Some("doc-1".to_string()),            content_type: "application/pdf".to_string(),            ..entry()        },    )    .expect("enqueues");    assert_eq!(row.after.as_deref(), Some("document_process"));    assert_eq!(row.after_row_id.as_deref(), Some("doc-1"));}#[test]fn takes_an_explicit_state_the_hydrators_download_is_already_synced() {    let db = empty_file();    let row = enqueue_media(        &db,        &MediaEnqueueInput {            state: Some("synced".to_string()),            ..entry()        },    )    .expect("enqueues");    assert_eq!(row.state, "synced");    assert_eq!(claim_due_media(&db, 10, T2).expect("claims"), vec![]);}#[test]fn stores_a_row_for_bytes_this_device_does_not_hold() {    let db = empty_file();    let row = enqueue_media(        &db,        &MediaEnqueueInput {            local_path: None,            state: Some("synced".to_string()),            ..entry()        },    )    .expect("enqueues");    assert_eq!(row.local_path, None);}#[test]fn is_idempotent_a_second_enqueue_of_the_same_key_never_re_uploads() {    // The key is a content address, so the same key is the same bytes. A    // re-import must not drag a synced row back into the upload queue.    let db = empty_file();    enqueue_media(&db, &entry()).expect("enqueues");    mark_media_synced(&db, &key(), T1).expect("marks");    let again = enqueue_media(        &db,        &MediaEnqueueInput {            now: T2.to_string(),            ..entry()        },    )    .expect("re-enqueues");    assert_eq!(again.state, "synced");    assert_eq!(again.created_at, T0);    assert_eq!(again.updated_at, T2);    assert_eq!(claim_due_media(&db, 10, T2).expect("claims"), vec![]);}#[test]fn keeps_a_failed_row_failed_and_its_attempt_history() {    let db = empty_file();    enqueue_media(&db, &entry()).expect("enqueues");    record_media_attempt(&db, &key(), "network down", T0).expect("records");    mark_media_failed(&db, &key(), "413 too large", T1).expect("marks");    let again = enqueue_media(        &db,        &MediaEnqueueInput {            now: T2.to_string(),            ..entry()        },    )    .expect("re-enqueues");    assert_eq!(again.state, "failed");    assert_eq!(again.attempts, 1);    assert_eq!(again.last_error.as_deref(), Some("413 too large"));}#[test]fn learns_a_local_path_and_a_trigger_it_did_not_have() {    let db = empty_file();    enqueue_media(        &db,        &MediaEnqueueInput {            local_path: None,            ..entry()        },    )    .expect("enqueues");    let again = enqueue_media(        &db,        &MediaEnqueueInput {            local_path: Some("/var/media/aa/aaa.jpg".to_string()),            after: Some("meeting_submit".to_string()),            after_row_id: Some("m-1".to_string()),            now: T1.to_string(),            ..entry()        },    )    .expect("re-enqueues");    assert_eq!(again.local_path.as_deref(), Some("/var/media/aa/aaa.jpg"));    assert_eq!(again.after.as_deref(), Some("meeting_submit"));    assert_eq!(again.after_row_id.as_deref(), Some("m-1"));}#[test]fn revives_a_delete_pending_row_the_bytes_came_back() {    let db = empty_file();    enqueue_media(&db, &entry()).expect("enqueues");    record_media_attempt(&db, &key(), "offline", T0).expect("records");    mark_media_delete_pending(&db, &key(), T1).expect("marks");    let again = enqueue_media(        &db,        &MediaEnqueueInput {            now: T2.to_string(),            ..entry()        },    )    .expect("re-enqueues");    assert_eq!(again.state, "pending");    assert_eq!(again.attempts, 0);    assert_eq!(again.last_error, None);    assert_eq!(again.next_attempt_at, None);    assert_eq!(again.local_path.as_deref(), Some("/var/media/aa/aaa.jpg"));    assert_eq!(        keys(&claim_due_media(&db, 10, T2).expect("claims")),        [key()]    );}// ─────────────────────────────────────────────────────────────────────────────// claim_due_media// ─────────────────────────────────────────────────────────────────────────────fn seed(db: &Driver, key: &str, created_at: &str) -> MediaRow {    enqueue_media(        db,        &MediaEnqueueInput {            key: key.to_string(),            now: created_at.to_string(),            ..entry()        },    )    .expect("enqueues")}#[test]fn returns_only_pending_rows() {    let db = empty_file();    seed(&db, "k-pending", T0);    enqueue_media(        &db,        &MediaEnqueueInput {            key: "k-synced".to_string(),            state: Some("synced".to_string()),            ..entry()        },    )    .expect("enqueues");    seed(&db, "k-failed", T0);    mark_media_failed(&db, "k-failed", "415", T0).expect("marks");    seed(&db, "k-deleted", T0);    mark_media_delete_pending(&db, "k-deleted", T0).expect("marks");    assert_eq!(        keys(&claim_due_media(&db, 10, T2).expect("claims")),        ["k-pending"]    );}#[test]fn holds_a_row_until_its_backoff_expires_then_hands_it_back() {    let db = empty_file();    seed(&db, "k-1", T0);    record_media_attempt(&db, "k-1", "timeout", T0).expect("records"); // due at T0 + 2s    assert_eq!(claim_due_media(&db, 10, T0).expect("claims"), vec![]);    assert_eq!(        claim_due_media(&db, 10, "2026-08-25T10:00:01.000Z").expect("claims"),        vec![]    );    // Exactly due counts as due — a row that waited its full backoff should not    // have to wait for the next tick as well.    assert_eq!(        keys(&claim_due_media(&db, 10, "2026-08-25T10:00:02.000Z").expect("claims")),        ["k-1"]    );}#[test]fn orders_oldest_first_keyed_as_the_tiebreak_and_respects_the_limit() {    let db = empty_file();    seed(&db, "k-c", "2026-08-25T10:00:02.000Z");    seed(&db, "k-b", T0);    seed(&db, "k-a", T0);    seed(&db, "k-d", "2026-08-25T10:00:03.000Z");    assert_eq!(        keys(&claim_due_media(&db, 10, T2).expect("claims")),        ["k-a", "k-b", "k-c", "k-d"]    );    assert_eq!(        keys(&claim_due_media(&db, 3, T2).expect("claims")),        ["k-a", "k-b", "k-c"]    );    assert_eq!(claim_due_media(&db, 0, T2).expect("claims"), vec![]);}#[test]fn claiming_does_not_mutate() {    // The app marks the attempt, because it is the half that knows the outcome.    let db = empty_file();    seed(&db, "k-1", T0);    let before = media_by_key(&db, "k-1").expect("reads");    claim_due_media(&db, 10, T2).expect("claims");    claim_due_media(&db, 10, T2).expect("claims");    assert_eq!(media_by_key(&db, "k-1").expect("reads"), before);}// ─────────────────────────────────────────────────────────────────────────────// record_media_attempt// ─────────────────────────────────────────────────────────────────────────────#[test]fn counts_the_attempt_keeps_the_row_pending_and_spaces_the_retry() {    let db = empty_file();    enqueue_media(&db, &entry()).expect("enqueues");    let first = record_media_attempt(&db, &key(), "connection lost", T0)        .expect("records")        .expect("a row");    assert_eq!(first.state, "pending");    assert_eq!(first.attempts, 1);    assert_eq!(first.last_error.as_deref(), Some("connection lost"));    assert_eq!(        first.next_attempt_at.as_deref(),        Some("2026-08-25T10:00:02.000Z")    );    assert_eq!(first.updated_at, T0);    let second = record_media_attempt(&db, &key(), "503", "2026-08-25T10:00:02.000Z")        .expect("records")        .expect("a row");    assert_eq!(second.attempts, 2);    assert_eq!(        second.next_attempt_at.as_deref(),        Some("2026-08-25T10:00:06.000Z")    );}#[test]fn never_gives_up_the_backoff_flattens_at_the_ceiling_and_stays_pending() {    let db = empty_file();    enqueue_media(&db, &entry()).expect("enqueues");    let mut row = None;    for _ in 0..20 {        row = record_media_attempt(&db, &key(), "offline", T0).expect("records");    }    let row = row.expect("a row");    assert_eq!(row.attempts, 20);    assert_eq!(row.state, "pending");    // The bytes are on this phone and nowhere else; a row given up on is a file    // that never reaches the backup.    assert_eq!(        row.next_attempt_at.as_deref(),        Some("2026-08-25T10:05:00.000Z")    );}#[test]fn returns_none_for_a_key_the_file_does_not_have() {    let db = empty_file();    assert_eq!(        record_media_attempt(&db, "nope", "x", T0).expect("records"),        None    );}// ─────────────────────────────────────────────────────────────────────────────// media_backoff_at// ─────────────────────────────────────────────────────────────────────────────#[test]fn doubles_per_attempt_up_to_five_minutes() {    let cases: [(i64, &str); 11] = [        (0, "2026-08-25T10:00:01.000Z"),        (1, "2026-08-25T10:00:02.000Z"),        (2, "2026-08-25T10:00:04.000Z"),        (3, "2026-08-25T10:00:08.000Z"),        (4, "2026-08-25T10:00:16.000Z"),        (5, "2026-08-25T10:00:32.000Z"),        (6, "2026-08-25T10:01:04.000Z"),        (7, "2026-08-25T10:02:08.000Z"),        (8, "2026-08-25T10:04:16.000Z"),        (9, "2026-08-25T10:05:00.000Z"),        (100, "2026-08-25T10:05:00.000Z"),    ];    for (attempts, expected) in cases {        assert_eq!(            media_backoff_at(T0, attempts).expect("computes"),            expected,            "attempts={attempts}"        );    }    assert_eq!(MEDIA_BACKOFF_CEILING_SECONDS, 300);}#[test]fn refuses_a_timestamp_it_cannot_read_rather_than_writing_an_invalid_date() {    let error = media_backoff_at("not a time", 1).expect_err("refused");    assert!(error.to_string().contains("ISO-8601"), "got {error}");}// ─────────────────────────────────────────────────────────────────────────────// State transitions// ─────────────────────────────────────────────────────────────────────────────#[test]fn mark_synced_clears_the_backoff_and_the_error_keeping_the_attempt_count() {    let db = empty_file();    enqueue_media(&db, &entry()).expect("enqueues");    record_media_attempt(&db, &key(), "timeout", T0).expect("records");    mark_media_synced(&db, &key(), T1).expect("marks");    let row = media_by_key(&db, &key()).expect("reads").expect("a row");    assert_eq!(row.state, "synced");    assert_eq!(row.last_error, None);    assert_eq!(row.next_attempt_at, None);    assert_eq!(row.attempts, 1);    assert_eq!(row.updated_at, T1);    assert_eq!(row.local_path.as_deref(), Some("/var/media/aa/aaa.jpg"));}#[test]fn mark_failed_is_terminal_no_backoff_so_no_sweep_picks_it_up() {    let db = empty_file();    enqueue_media(&db, &entry()).expect("enqueues");    record_media_attempt(&db, &key(), "timeout", T0).expect("records");    mark_media_failed(&db, &key(), "415 unsupported type", T1).expect("marks");    let row = media_by_key(&db, &key()).expect("reads").expect("a row");    assert_eq!(row.state, "failed");    assert_eq!(row.next_attempt_at, None);    // The message is the only thing that can explain to the person why one file    // never backed up, so it is kept, not cleared.    assert_eq!(row.last_error.as_deref(), Some("415 unsupported type"));    assert_eq!(claim_due_media(&db, 10, T2).expect("claims"), vec![]);}#[test]fn mark_delete_pending_drops_the_local_path_and_starts_a_clean_job() {    let db = empty_file();    enqueue_media(&db, &entry()).expect("enqueues");    record_media_attempt(&db, &key(), "timeout", T0).expect("records");    mark_media_delete_pending(&db, &key(), T1).expect("marks");    let row = media_by_key(&db, &key()).expect("reads").expect("a row");    assert_eq!(row.state, "delete_pending");    // The file is already gone; a path to a deleted file is worse than none,    // because a renderer would hand it to the image loader.    assert_eq!(row.local_path, None);    assert_eq!(row.attempts, 0);    assert_eq!(row.last_error, None);    assert_eq!(row.next_attempt_at, None);}#[test]fn set_local_path_records_where_the_bytes_are_and_that_they_left() {    let db = empty_file();    enqueue_media(        &db,        &MediaEnqueueInput {            local_path: None,            state: Some("synced".to_string()),            ..entry()        },    )    .expect("enqueues");    set_media_local_path(&db, &key(), Some("/var/media/aa/aaa.jpg"), T1).expect("sets");    let row = media_by_key(&db, &key()).expect("reads").expect("a row");    assert_eq!(row.local_path.as_deref(), Some("/var/media/aa/aaa.jpg"));    assert_eq!(row.updated_at, T1);    set_media_local_path(&db, &key(), None, T2).expect("clears");    let row = media_by_key(&db, &key()).expect("reads").expect("a row");    assert_eq!(row.local_path, None);    assert_eq!(row.state, "synced");}#[test]fn a_mark_on_an_unknown_key_changes_nothing() {    let db = empty_file();    mark_media_synced(&db, "nope", T0).expect("marks");    mark_media_failed(&db, "nope", "x", T0).expect("marks");    mark_media_delete_pending(&db, "nope", T0).expect("marks");    set_media_local_path(&db, "nope", Some("/x"), T0).expect("sets");    assert_eq!(media_by_key(&db, "nope").expect("reads"), None);}// ─────────────────────────────────────────────────────────────────────────────// The delete sweep// ─────────────────────────────────────────────────────────────────────────────#[test]fn hands_back_delete_pending_rows_oldest_first_up_to_the_batch_size() {    let db = empty_file();    for (key, at) in [("k-1", T0), ("k-2", T1), ("k-3", T2)] {        seed(&db, key, T0);        mark_media_delete_pending(&db, key, at).expect("marks");    }    seed(&db, "k-live", T0);    assert_eq!(        keys(&claim_delete_pending(&db, 10).expect("claims")),        ["k-1", "k-2", "k-3"]    );    assert_eq!(        keys(&claim_delete_pending(&db, 2).expect("claims")),        ["k-1", "k-2"]    );}#[test]fn delete_media_row_forgets_the_row_once_the_object_is_gone() {    let db = empty_file();    enqueue_media(&db, &entry()).expect("enqueues");    mark_media_delete_pending(&db, &key(), T1).expect("marks");    delete_media_row(&db, &key()).expect("deletes");    assert_eq!(media_by_key(&db, &key()).expect("reads"), None);    assert_eq!(claim_delete_pending(&db, 10).expect("claims"), vec![]);}#[test]fn deleting_a_key_the_file_does_not_have_is_a_no_op() {    let db = empty_file();    delete_media_row(&db, "nope").expect("deletes nothing");}// ─────────────────────────────────────────────────────────────────────────────// media_counts and failed_media// ─────────────────────────────────────────────────────────────────────────────#[test]fn counts_each_state_for_one_workspace_and_ignores_every_other() {    let db = empty_file();    for key in ["k-p1", "k-p2"] {        seed(&db, key, T0);    }    enqueue_media(        &db,        &MediaEnqueueInput {            key: "k-s".to_string(),            state: Some("synced".to_string()),            ..entry()        },    )    .expect("enqueues");    seed(&db, "k-f", T0);    mark_media_failed(&db, "k-f", "413", T1).expect("marks");    seed(&db, "k-d", T0);    mark_media_delete_pending(&db, "k-d", T1).expect("marks");    enqueue_media(        &db,        &MediaEnqueueInput {            key: "k-other".to_string(),            workspace_id: OTHER_WS.to_string(),            ..entry()        },    )    .expect("enqueues");    assert_eq!(        media_counts(&db, WS).expect("counts"),        MediaCounts {            pending: 2,            // Two pending rows of the entry's 4096 bytes each: what is left to send.            pending_bytes: 8192,            synced: 1,            failed: 1,            delete_pending: 1,        }    );    assert_eq!(        media_counts(&db, OTHER_WS).expect("counts"),        MediaCounts {            pending: 1,            pending_bytes: 4096,            synced: 0,            failed: 0,            delete_pending: 0,        }    );}#[test]fn counts_are_all_zeroes_on_an_empty_file() {    let db = empty_file();    assert_eq!(        media_counts(&db, WS).expect("counts"),        MediaCounts::default()    );}#[test]fn failed_media_lists_the_one_state_nothing_retries_newest_first() {    let db = empty_file();    seed(&db, "k-old", T0);    mark_media_failed(&db, "k-old", "413 too large", T0).expect("marks");    seed(&db, "k-new", T0);    mark_media_failed(&db, "k-new", "415 wrong type", T1).expect("marks");    seed(&db, "k-fine", T0);    let failed = failed_media(&db, WS, 20).expect("reads");    assert_eq!(        failed,        vec![            FailedMedia {                key: "k-new".to_string(),                last_error: Some("415 wrong type".to_string()),                updated_at: T1.to_string(),            },            FailedMedia {                key: "k-old".to_string(),                last_error: Some("413 too large".to_string()),                updated_at: T0.to_string(),            },        ]    );    assert_eq!(failed_media(&db, WS, 1).expect("reads").len(), 1);    assert_eq!(failed_media(&db, OTHER_WS, 20).expect("reads"), vec![]);}#[test]fn survives_a_file_that_has_never_had_a_workspace_row() {    // `_media` has no foreign key to cotton_workspaces on purpose: it is device    // bookkeeping, and it must be writable the instant the file opens.    let db = empty_file();    enqueue_media(        &db,        &MediaEnqueueInput {            workspace_id: "unknown-workspace".to_string(),            ..entry()        },    )    .expect("the ledger is standalone");}#[test]fn an_after_trigger_and_the_row_it_names_round_trip_as_ledger_values() {    // The ledger CARRIES a post-upload trigger and the row it belongs to; it    // never interprets either. The drain is what reads them.    let db = empty_file();    enqueue_media(        &db,        &MediaEnqueueInput {            after: Some("document_process".to_string()),            after_row_id: Some("m1".to_string()),            ..entry()        },    )    .expect("enqueues");    let row = media_by_key(&db, &key()).expect("reads").expect("a row");    assert_eq!(row.after.as_deref(), Some("document_process"));    assert_eq!(row.after_row_id.as_deref(), Some("m1"));    assert_eq!(row.state, "pending");}// ─────────────────────────────────────────────────────────────────────────────// placement — the four columns the budgeted phone cache is decided from#[test]fn a_view_stamps_the_row_and_says_whether_there_was_one_to_stamp() {    let db = empty_file();    assert!(        !mark_viewed(&db, &key(), T1).expect("the stamp"),        "a key nothing has filed is not a view anything can order by"    );    enqueue_media(&db, &entry()).expect("enqueues");    assert!(mark_viewed(&db, &key(), T1).expect("the stamp"));    let row = media_by_key(&db, &key()).expect("reads").expect("a row");    assert_eq!(row.last_viewed_at.as_deref(), Some(T1));    // A second view MOVES it — the column is "last", not "first".    mark_viewed(&db, &key(), T2).expect("the stamp");    let row = media_by_key(&db, &key()).expect("reads").expect("a row");    assert_eq!(row.last_viewed_at.as_deref(), Some(T2));}#[test]fn a_pin_goes_both_ways_where_a_capture_mark_does_not() {    let db = empty_file();    enqueue_media(        &db,        &MediaEnqueueInput {            captured_here: true,            ..entry()        },    )    .expect("enqueues");    set_media_pinned(&db, &key(), true, T1).expect("the pin");    let row = media_by_key(&db, &key()).expect("reads").expect("a row");    assert!(row.captured_here);    assert!(row.pinned);    // The person can stop pinning. Nothing can un-say who took the picture:    // `enqueue_media` only ever ORs `captured_here`, and there is no writer    // that clears it — which is what makes a phone's own captures safe from    // the budget for ever.    set_media_pinned(&db, &key(), false, T2).expect("the unpin");    enqueue_media(&db, &entry()).expect("a re-enqueue that says nothing about it");    let row = media_by_key(&db, &key()).expect("reads").expect("a row");    assert!(!row.pinned);    assert!(row.captured_here, "sticky, and only ever one way");}// ─────────────────────────────────────────────────────────────────────────────// MEDIA DRAIN 1: where each file is, in a word the shell can act on// ─────────────────────────────────────────────────────────────────────────────/// RULE: A refusal is filed with its WORD (too_big, cotton_full, file_gone, refused), so a screen says which file could not be kept and why without reading a sentence; a wait (space_full, dead_key) keeps its word too and stays pending; an upload the object confirmed clears it.#[test]fn a_refusal_and_a_wait_are_filed_with_the_word_they_are_under() {    let db = empty_file();    seed(&db, "k-big", T0);    seed(&db, "k-full", T0);    seed(&db, "k-later", T0);    seed(&db, "k-dead", T0);    seed(&db, "k-up", T0);    mark_media_failed_as(&db, "k-big", codes::TOO_BIG, "a media file is at most 100 MB", T1).expect("marks");    record_media_attempt_as(&db, "k-full", Some(codes::SPACE_FULL), "Cotton is full", T1).expect("records");    record_media_attempt_as(&db, "k-later", None, "the object is having an afternoon", T1).expect("records");    park_until_exchange(&db, "k-dead", "dead", T1).expect("parks");    record_media_attempt_as(&db, "k-up", Some(codes::SPACE_FULL), "Cotton is full", T1).expect("records");    mark_media_synced(&db, "k-up", T2).expect("marks");    let rows = media_by_keys(&db, &["k-big", "k-full", "k-later", "k-dead", "k-up", "k-nobody"].map(str::to_string)).expect("reads");    let said: Vec<(String, String, Option<String>)> = rows.iter().map(|row| (row.key.clone(), row.state.clone(), row.last_code.clone())).collect();    assert_eq!(        said,        vec![            ("k-big".to_string(), "failed".to_string(), Some("too_big".to_string())),            ("k-full".to_string(), "pending".to_string(), Some("space_full".to_string())),            ("k-later".to_string(), "pending".to_string(), None),            ("k-dead".to_string(), "pending".to_string(), Some("dead_key".to_string())),            ("k-up".to_string(), "synced".to_string(), None),        ],        "in the order asked, a key the ledger never held left out"    );}/// RULE: Every row says under what network rule it goes and who asked: a file queued by an app that says nothing goes on any network, exactly as every app's uploads have always gone.#[test]fn a_file_nobody_ruled_on_goes_on_any_network() {    let db = empty_file();    let row = seed(&db, "k-any", T0);    assert_eq!(row.network, "any");    assert_eq!(row.asked_by, None);    assert_eq!(row.source, None);}/// A thousand keys read in one call: the read is cut into statements SQLite takes.#[test]fn many_keys_read_in_one_call() {    let db = empty_file();    let keys: Vec<String> = (0..1200).map(|n| format!("k-{n:04}")).collect();    for key in &keys {        seed(&db, key, T0);    }    assert_eq!(media_by_keys(&db, &keys).expect("reads").len(), 1200);}