Cotton code
85 lines · 4.6 KB · RustRaw
//! A Photos copy's original leaves once the object holds it; nothing else does.use std::sync::Arc;use cotton_store::media_ledger::{enqueue_media, mark_media_synced, media_by_key, rule_media, MediaEnqueueInput};use cotton_store::media_holdings::{is_live_hold, record_holding, Tier};use cotton_store::mint::Held;use super::*;const NOW: &str = "2026-11-27T10:00:00.000Z";const NOW_MS: i64 = 1_795_773_600_000;/// Files `key` with its bytes at `name` in the store; `photos` marks it a copy of a Photos original.fn file(db: &Driver, layout: &Layout, workspace: &str, key: &str, name: &str, photos: bool, captured: bool, ids: &mut dyn IdSource) -> std::path::PathBuf {    let path = layout.client_media_dir().join(workspace).join(name);    std::fs::create_dir_all(path.parent().unwrap()).unwrap();    std::fs::write(&path, b"synthetic").unwrap();    enqueue_media(        db,        &MediaEnqueueInput {            key: key.to_string(),            workspace_id: workspace.to_string(),            sha256: "c".repeat(64),            content_type: "image/heic".to_string(),            size_bytes: 9,            local_path: Some(path.to_string_lossy().to_string()),            state: None,            after: None,            after_row_id: None,            tier: Some(Tier::Original),            captured_here: captured,            now: NOW.to_string(),        },    )    .unwrap();    if photos {        rule_media(db, key, None, Some("albums.keep"), Some(PHOTOS_SOURCE), true, NOW).unwrap();    }    db.run("INSERT OR IGNORE INTO cotton_devices (id,workspace_id,device_id,name,approved_at,approved_by_device,created_at,updated_at) VALUES ('phone',?,'phone','Phone','2026-01-01','phone','2026-01-01','2026-01-01')", &[rusqlite::types::Value::Text(workspace.into())]).unwrap();    record_holding(db, workspace, "phone", key, Tier::Original, 9, NOW_MS, ids, Held::Replica).unwrap();    path}/// RULE: A copy of a Photos original leaves this phone once the object has confirmed it, and not before; its row stays with no path and this phone's hold is dropped.#[test]fn a_photos_copy_leaves_once_the_object_holds_it() {    let (_dir, layout, replica) = crate::test_support::open_test_replica();    let (db, workspace) = (&replica.driver, replica.workspace_id.as_str());    let mut ids = crate::seams::op_ids(Arc::new(crate::seams::SeededRandom::default()));    let key = format!("{workspace}/photos/{}.heic", "c".repeat(64));    let path = file(db, &layout, workspace, &key, "kept.heic", true, false, &mut ids);    let early = release_photos_copies(db, workspace, "phone", &layout, &super::super::SystemEvictDisk, NOW_MS, &mut ids, Held::Replica).unwrap();    assert_eq!(early, Released::default(), "not confirmed: it stays");    assert!(path.exists());    mark_media_synced(db, &key, NOW).unwrap();    let released = release_photos_copies(db, workspace, "phone", &layout, &super::super::SystemEvictDisk, NOW_MS + 1, &mut ids, Held::Replica).unwrap();    assert_eq!(released, Released { files: 1, bytes: 9 });    assert!(!path.exists());    let row = media_by_key(db, &key).unwrap().unwrap();    assert_eq!((row.local_path, row.state.as_str(), row.source.as_deref()), (None, "synced", Some("photos")));    assert!(!is_live_hold(db, "phone", &key, Tier::Original).unwrap());    assert_eq!(release_photos_copies(db, workspace, "phone", &layout, &super::super::SystemEvictDisk, NOW_MS + 2, &mut ids, Held::Replica).unwrap(), Released::default(), "once");}/// RULE: A photo taken in Cotton never leaves by this rule, and neither does a Photos copy whose file a Cotton capture shares.#[test]fn a_capture_and_a_file_it_shares_stay() {    let (_dir, layout, replica) = crate::test_support::open_test_replica();    let (db, workspace) = (&replica.driver, replica.workspace_id.as_str());    let mut ids = crate::seams::op_ids(Arc::new(crate::seams::SeededRandom::default()));    let taken = format!("{workspace}/photos/{}.heic", "d".repeat(64));    let taken_path = file(db, &layout, workspace, &taken, "taken.heic", false, true, &mut ids);    mark_media_synced(db, &taken, NOW).unwrap();    // The same bytes under a second key, as a Photos copy: one file in the store.    let copy = format!("{workspace}/documents/{}.heic", "d".repeat(64));    file(db, &layout, workspace, &copy, "taken.heic", true, false, &mut ids);    mark_media_synced(db, &copy, NOW).unwrap();    let released = release_photos_copies(db, workspace, "phone", &layout, &super::super::SystemEvictDisk, NOW_MS, &mut ids, Held::Replica).unwrap();    assert_eq!(released, Released::default());    assert!(taken_path.exists(), "a photo this phone took is never released");}