usesuper::*;usecrate::test_support::{seeded_file, WORKSPACE};const MAC: &str = "device:mini";const SERVING: &str = "device:serving";fn receipt(db: &Driver, author: &str, hlc: &str) { db.run("INSERT INTO _writes(tbl,row_id,hlc,author,op_id,held,bytes) VALUES ('cotton_todos',?,?,?,?,0,X'7b7d')", &[SqlValue::Text(format!("todo-{hlc}")),SqlValue::Text(hlc.into()),SqlValue::Text(author.into()),SqlValue::Text(format!("op-{hlc}")), ], ) .unwrap();}fn mac_row(db: &Driver, device: &str) { db.run("INSERT INTO cotton_devices(id,workspace_id,device_id,name,platform,approved_at,approved_by_device,created_at,updated_at,scope,reported_state) VALUES (?,?,?,?, 'mac','now',?,'now','now','full','{}')", &[SqlValue::Text(format!("row-{device}")),SqlValue::Text(WORKSPACE.into()),SqlValue::Text(device.into()),SqlValue::Text(device.into()),SqlValue::Text(SERVING.into()), ], ) .unwrap();}fn holds(db: &Driver, device: &str, key: &str) { db.run("INSERT INTO cotton_media_holdings(id,workspace_id,object_key,device_id,tier,bytes,created_at,updated_at) VALUES (?,?,?,?,'original',10,'now','now')", &[SqlValue::Text(format!("{device}-{key}")),SqlValue::Text(WORKSPACE.into()),SqlValue::Text(key.into()),SqlValue::Text(device.into()), ], ) .unwrap();}fn files() -> (Driver, Driver) {let (mac, serving) = (seeded_file(MAC), seeded_file(SERVING));for db in [&mac, &serving] { mac_row(db, MAC); mac_row(db, SERVING); } (mac, serving)}/// RULE: The serving Mac confirms a Mac may drop its copy only when it holds every receipt that Mac authored (count and newest), the Mac has nothing queued, held or parked, and no original is held by that Mac alone — because a runner's dropped copy is gone, and anything only it held would be lost.#[test]fn the_serving_mac_confirms_only_what_it_holds() {let (mac, serving) = files();for hlc in ["0001", "0002"] { receipt(&mac, MAC, hlc); } receipt(&serving, MAC, "0001");let claim = claim_of(&mac, WORKSPACE, MAC).unwrap(); assert_eq!((claim.authored, claim.newest.as_deref(), claim.unsent), (2, Some("0002"), 0));let short = short_of(&serving, &claim, MAC).unwrap(); assert!(short.contains(&Short::Receipts { mac: 2, serving: 1 })); assert!(short.contains(&Short::Newest { hlc: "0002".into() })); receipt(&serving, MAC, "0002"); assert_eq!(short_of(&serving, &claim, MAC).unwrap(), vec![]);// An original only the Mac holds keeps the copy; once another keeping Mac// holds it too, it does not. holds(&serving, MAC, "photo-1"); assert_eq!(short_of(&serving, &claim, MAC).unwrap(), vec![Short::OnlyCopy { objects: 1 }]); holds(&serving, SERVING, "photo-1"); assert_eq!(short_of(&serving, &claim, MAC).unwrap(), vec![]);// Something unsent is never confirmed, whatever the serving Mac holds.let unsent = Claim { unsent: 3, ..claim }; assert_eq!(short_of(&serving, &unsent, MAC).unwrap(), vec![Short::Unsent { count: 3 }]);}/// RULE: Another device's receipts never stand in for the Mac's own — the count is by author, so a busy phone cannot make a Mac's missing changes look delivered.#[test]fn another_authors_receipts_do_not_count() {let (mac, serving) = files(); receipt(&mac, MAC, "0001"); receipt(&serving, "device:phone", "0001");let claim = claim_of(&mac, WORKSPACE, MAC).unwrap(); assert!(!short_of(&serving, &claim, MAC).unwrap().is_empty());}