Cotton code
102 lines · 4.6 KB · RustRaw
//! Where each file is: the ledger and the watch, as one answer.use cotton_store::media_ledger::{    enqueue_media, mark_media_failed_as, mark_media_synced, media_by_keys, park_until_exchange, record_media_attempt_as,    MediaEnqueueInput,};use super::*;use crate::test_support::{open_test_replica, WORKSPACE};const T0: &str = "2026-11-27T10:00:00.000Z";const T1: &str = "2026-11-27T10:01:00.000Z";fn queue(replica: &crate::Replica, key: &str, size: i64) {    enqueue_media(        &replica.driver,        &MediaEnqueueInput {            key: key.to_string(),            workspace_id: WORKSPACE.to_string(),            sha256: "b".repeat(64),            content_type: "image/jpeg".to_string(),            size_bytes: size,            local_path: Some("/nowhere/b.jpg".to_string()),            state: None,            after: None,            after_row_id: None,            tier: None,            captured_here: false,            now: T0.to_string(),        },    )    .expect("a ledger row");}/// RULE: Each file answers one phase — queued, waiting (with the word it waits for), uploading (bytes sent of total), retrying, uploaded (the object answered), failed (with the word the person acts on), or unknown — and the counts add up to the files asked.#[test]fn each_file_answers_where_it_is() {    let (_dir, _layout, replica) = open_test_replica();    for key in ["queued", "flying", "busy", "full", "dead", "big", "gone", "up"] {        queue(&replica, key, 1000);    }    let db = &replica.driver;    record_media_attempt_as(db, "busy", None, "the object is having an afternoon", T1).expect("records");    record_media_attempt_as(db, "full", Some(codes::SPACE_FULL), "Cotton is full", T1).expect("records");    park_until_exchange(db, "dead", "an erase names this", T1).expect("parks");    mark_media_failed_as(db, "big", codes::TOO_BIG, "a media file is at most 100 MB", T1).expect("marks");    mark_media_failed_as(db, "gone", codes::FILE_GONE, "these bytes are not on this device any more", T1).expect("marks");    mark_media_synced(db, "up", T1).expect("marks");    let watch = UploadWatch::new();    watch.begin("flying", 1000);    watch.progress("flying", 400, 1040);    let keys: Vec<String> = ["queued", "flying", "busy", "full", "dead", "big", "gone", "up", "never"].map(str::to_string).to_vec();    let rows = media_by_keys(db, &keys).expect("reads");    let uploads = uploads_of(&keys, &rows, &watch);    let said: Vec<(&str, Phase, Option<u64>, u64, Option<&str>)> = uploads        .files        .iter()        .map(|file| (file.key.as_str(), file.phase, file.sent, file.total, file.code.as_deref()))        .collect();    assert_eq!(        said,        vec![            ("queued", Phase::Queued, None, 1000, None),            ("flying", Phase::Uploading, Some(400), 1040, None),            ("busy", Phase::Retrying, None, 1000, None),            ("full", Phase::Waiting, None, 1000, Some("space_full")),            ("dead", Phase::Waiting, None, 1000, Some("dead_key")),            ("big", Phase::Failed, None, 1000, Some("too_big")),            ("gone", Phase::Failed, None, 1000, Some("file_gone")),            ("up", Phase::Uploaded, None, 1000, None),            ("never", Phase::Unknown, None, 0, None),        ]    );    assert_eq!(        uploads.counts,        UploadCounts { queued: 1, waiting: 2, uploading: 1, retrying: 1, uploaded: 1, failed: 2, removing: 0, unknown: 1 }    );    assert_eq!(uploads.files[2].line.as_deref(), Some("the object is having an afternoon"));    assert!(uploads.files[2].next_try_at.is_some(), "a retry says when");}/// RULE: Uploaded is the object's answer and nothing else: a file whose upload is in the air, or whose queue emptied because it was refused, is never uploaded.#[test]fn a_file_in_the_air_or_refused_is_not_uploaded() {    let (_dir, _layout, replica) = open_test_replica();    queue(&replica, "flying", 10);    queue(&replica, "refused", 10);    mark_media_failed_as(&replica.driver, "refused", codes::REFUSED, "a key this object will not take", T1).expect("marks");    let watch = UploadWatch::new();    watch.begin("flying", 10);    let keys = vec!["flying".to_string(), "refused".to_string()];    let uploads = uploads_of(&keys, &media_by_keys(&replica.driver, &keys).expect("reads"), &watch);    assert_eq!(uploads.counts.uploaded, 0);    assert_eq!(uploads.counts.uploading, 1);    assert_eq!(uploads.counts.failed, 1);    // An upload that ended leaves the air whichever way it went.    watch.end("flying");    assert_eq!(watch.in_the_air(), 0);    watch.progress("flying", 5, 10);    assert_eq!(watch.flight("flying"), None, "a count after the end is not put back");}