104 lines · 5.4 KB · RustRaw
use rusqlite::types::Value as Sql;use serde_json::json;use super::*;use crate::shell::client::phone_tests::attached;const AT: &str = "2026-11-12T09:00:00.000Z";fn seed(client: &ClientController, sql: &str, params: &[Sql]) { client .with_writer(|file| { file.driver.run(sql, params).map_err(|error| error.to_string())?; crate::fixture::receipted(file.driver, sql); Ok(()) }) .expect("seeded");}fn workspace(client: &ClientController) -> String { client.with_writer(|file| Ok(file.workspace_id.to_string())).expect("the workspace")}/// A photo (or a film, its poster beside it), its row in the library.fn picture(client: &ClientController, kind: &str, digest: char) -> (String, String) { let ws = workspace(client); let hash = digest.to_string().repeat(64); let id = format!("{kind}:{hash}"); let key = format!("{ws}/{kind}s/{hash}.{}", if kind == "photo" { "jpg" } else { "mov" }); if kind == "photo" { seed(client, "INSERT INTO cotton_photos (id,workspace_id,key,sha256,content_type,size_bytes,taken_at,created_at,updated_at) VALUES (?,?,?,?,'image/jpeg',8,?,?,?)", &[Sql::Text(id.clone()), Sql::Text(ws), Sql::Text(key.clone()), Sql::Text(hash), Sql::Text(AT.into()), Sql::Text(AT.into()), Sql::Text(AT.into())]); } else { seed(client, "INSERT INTO cotton_videos (id,workspace_id,key,sha256,content_type,size_bytes,taken_at,poster_key,created_at,updated_at) VALUES (?,?,?,?,'video/quicktime',8,?,?,?,?)", &[Sql::Text(id.clone()), Sql::Text(ws.clone()), Sql::Text(key.clone()), Sql::Text(hash.clone()), Sql::Text(AT.into()), Sql::Text(format!("{ws}/posters/{hash}.jpg")), Sql::Text(AT.into()), Sql::Text(AT.into())]); } (id, key)}/// The picture's bytes in this device's media store, and its ledger row.fn on_this_phone(client: &ClientController, key: &str) -> std::path::PathBuf { let path = client.layout().client_media_dir().join(crate::fixture::WORKSPACE).join(key.rsplit('/').next().expect("a name")); std::fs::create_dir_all(path.parent().expect("a folder")).expect("the media folder"); std::fs::write(&path, b"invented").expect("the picture's bytes"); client .with_writer(|replica| { cotton_store::media_ledger::enqueue_media( replica.driver, &cotton_store::media_ledger::MediaEnqueueInput { key: key.to_string(), workspace_id: replica.workspace_id.to_string(), sha256: "c".repeat(64), content_type: "image/jpeg".to_string(), size_bytes: 8, local_path: Some(path.to_string_lossy().to_string()), state: Some("pending".to_string()), after: None, after_row_id: None, tier: None, captured_here: true, now: AT.to_string(), }, ) .map(|_| ()) .map_err(|error| error.to_string()) }) .expect("the ledger row"); path}fn listed(client: &ClientController, on_phone: Value) -> Vec<String> { let answer = crate::phone::records::call(client, "records.list", &json!({"table": "media", "where": {"on_phone": on_phone}})).expect("a list"); answer["records"].as_array().expect("records").iter().map(|row| row["id"].as_str().expect("an id").to_string()).collect()}// RULE (Cotton Photos, 12 Nov 2026): a picture is On this phone when its own bytes are in this device's store (a film's, never only its poster), else absent; the library's records say so, and its On this phone view lists exactly those, kept ones first gone when their file is.#[test]fn the_library_says_and_lists_what_this_phone_keeps() { let (_dir, client) = attached(); let (kept_photo, kept_key) = picture(&client, "photo", 'a'); let (elsewhere, _) = picture(&client, "photo", 'b'); let (kept_film, film_key) = picture(&client, "video", 'd'); assert_eq!(listed(&client, json!(["kept"])), Vec::<String>::new(), "nothing is kept yet"); let path = on_this_phone(&client, &kept_key); on_this_phone(&client, &film_key); let all = crate::phone::records::call(&client, "records.list", &json!({"table": "media"})).expect("the library"); let said: BTreeMap<String, Value> = all["records"].as_array().unwrap().iter().map(|row| (row["id"].as_str().unwrap().to_string(), row["data"][ON_PHONE].clone())).collect(); assert_eq!(said[&kept_photo], json!(KEPT)); assert_eq!(said[&kept_film], json!(KEPT), "a film by its own bytes"); assert_eq!(said[&elsewhere], json!(ABSENT)); let mut here = listed(&client, json!(["kept"])); here.sort(); assert_eq!(here, [kept_photo.clone(), kept_film.clone()]); assert_eq!(listed(&client, json!("absent")), [elsewhere.clone()]); assert_eq!(crate::phone::records::call(&client, "records.list", &json!({"table": "media", "where": {"on_phone": "lost"}})).unwrap_err().code, "bad_request"); std::fs::remove_file(path).expect("the file goes"); assert_eq!(listed(&client, json!(["kept"])), [kept_film], "a ledger row whose file has gone keeps nothing"); let mut other = json!({"table": "book", "where": {"on_phone": "kept"}}).as_object().unwrap().clone(); narrow_where(&client, &mut other).expect("another table passes"); assert_eq!(other["where"], json!({"on_phone": "kept"}));}