//! A note's attached files (CONTRACT.md section 15), on a phone that never //! signed in. Every note and file is invented. use serde_json::{json, Value}; use crate::handle::{CottonHandle, OpenConfig, OpenMode}; use crate::methods::dispatch; use cotton_tools::mcp::handlers::common::insert_row; fn phone(dir: &tempfile::TempDir) -> CottonHandle { phone_as(dir, cotton_core::flavor::Flavor::Real) } fn phone_as(dir: &tempfile::TempDir, flavor: cotton_core::flavor::Flavor) -> CottonHandle { let manifest = json!({ "genesis": "personal", "flavour": "notes", "name": "Cotton Words", "databases": "single", "database": "personal", "business": { "server": { "real": "https://cottondb.com", "dev": "https://dev.example.invalid" } }, "fan": { "home": false, "tools": false, "opens": "notebook" }, "kit": "notes", "ground": "blank" }); let handle = CottonHandle::open(OpenConfig { mode: OpenMode::Phone { container_path: dir.path().to_string_lossy().to_string(), previous_container_path: None }, app_version: None, time_zone: Some("Asia/Kolkata".to_string()), device_secret: Some(crate::fixture::paired_secret()), vault_secret: None, flavor, manifest: Some(manifest), }) .expect("an open core"); dispatch(&handle, "workspace.startLocal", &json!({ "deviceName": "An invented phone" })).expect("started on the phone"); handle } fn ask(handle: &CottonHandle, method: &str, request: Value) -> Value { dispatch(handle, method, &request).unwrap_or_else(|error| panic!("{method} {request}: {error:?}")) } /// An invented document in this copy, with a file key. fn document(handle: &CottonHandle, id: &str, title: &str, name: &str, mime: &str, bytes: i64) { super::super::notes::with_write(handle, "test", |ctx| { let key = cotton_core::media::keys::document_original_key(ctx.file.id(), id, name.rsplit_once('.').map_or("", |(_, ending)| ending)); let row = json!({ "title": title, "original_name": name, "mime": mime, "byte_size": bytes, "sha256": format!("{:0>64}", bytes), "storage_path": key, "status": "ready" }); insert_row(ctx, "cotton_documents", id, row.as_object().cloned().unwrap()).map_err(super::super::notes::tool)?; ctx.commit().map_err(super::super::notes::tool)?; Ok(json!({})) }) .unwrap(); } /// A note an assistant wrote, attaching `files` (documents of this copy) in order. fn note_with(handle: &CottonHandle, files: &[&str]) -> String { let attachments: Vec = files.iter().map(|id| json!({ "kind": "document", "id": id })).collect(); let made = super::super::notes::with_write(handle, "mcp", |ctx| { let args = json!({ "title": "Invented trip", "body": "What we planned", "attachments": attachments }).as_object().cloned().unwrap(); Ok(cotton_tools::mcp::handlers::records_1::notes::create_note(ctx, &args).unwrap()) }) .unwrap(); made["id"].as_str().unwrap().to_string() } /// RULE (Words' attached files, 22 Nov 2026): a note's documents are answered with the note, in its order, each /// saying its name, what it is and how it opens: a page sealed, Markdown as a note, a PDF's pages, a picture whole, /// anything else in another app. Until this they were dropped and the note showed nothing of them. #[test] fn a_notes_files_are_answered_in_its_order_each_with_how_it_opens() { let dir = tempfile::tempdir().unwrap(); let phone = phone(&dir); document(&phone, "00000000-0000-4000-8000-000000000001", "Invented itinerary", "itinerary.html", "text/html", 2048); document(&phone, "00000000-0000-4000-8000-000000000002", "Packing list", "packing.md", "text/markdown", 300); document(&phone, "00000000-0000-4000-8000-000000000003", "Invented tickets", "tickets.pdf", "application/pdf", 1_500_000); document(&phone, "00000000-0000-4000-8000-000000000004", "Invented map", "map.png", "image/png", 40_000); document(&phone, "00000000-0000-4000-8000-000000000005", "Invented letter", "letter.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 9000); let id = note_with(&phone, &["00000000-0000-4000-8000-000000000001", "00000000-0000-4000-8000-000000000002", "00000000-0000-4000-8000-000000000003", "00000000-0000-4000-8000-000000000004", "00000000-0000-4000-8000-000000000005"]); let opened = ask(&phone, "notes.open", json!({ "id": id })); let files = opened["files"].as_array().expect("files"); let seen: Vec<(&str, &str, &str)> = files.iter().map(|f| (f["id"].as_str().unwrap(), f["name"].as_str().unwrap(), f["opens"].as_str().unwrap())).collect(); assert_eq!(seen, [ ("00000000-0000-4000-8000-000000000001", "Invented itinerary", "page"), ("00000000-0000-4000-8000-000000000002", "Packing list", "note"), ("00000000-0000-4000-8000-000000000003", "Invented tickets", "pdf"), ("00000000-0000-4000-8000-000000000004", "Invented map", "picture"), ("00000000-0000-4000-8000-000000000005", "Invented letter", "elsewhere"), ]); assert_eq!(files[2]["about"], "PDF ยท 1.5 MB"); assert_eq!(files[1]["format"], "Markdown"); assert_eq!(files[0]["label"], "Open Invented itinerary"); assert!(files.iter().all(|f| f["here"] == true && f["key"].is_string() && f["refused"].is_null()), "{files:?}"); // The text holds none of them: a file is not a picture. assert_eq!(opened["text"], "# Invented trip\n\nWhat we planned", "{}", opened["text"]); // notes.file answers a file this copy holds as files does, without asking the object. let one = ask(&phone, "notes.file", json!({ "note": id, "id": "00000000-0000-4000-8000-000000000003" })); assert_eq!(one, files[2]); } /// RULE: a file a note attaches that this copy does not hold (Words pulls only web pages of `document`) still /// says plainly what it is, and opening it asks the object for its row; with no object to ask it is refused in /// Words' words, never a dead tap. A file the note does not attach is never fetched. #[test] fn a_file_not_in_this_copy_says_what_it_is_and_asks_the_object() { let dir = tempfile::tempdir().unwrap(); let phone = phone(&dir); document(&phone, "00000000-0000-4000-8000-000000000002", "Packing list", "packing.md", "text/markdown", 300); let id = note_with(&phone, &["00000000-0000-4000-8000-000000000002"]); // A PDF attached on the Mac, its document never pulled here. super::super::notes::with_write(&phone, "test", |ctx| { let row = json!({ "note_id": id, "record_kind": "document", "record_id": "00000000-0000-4000-8000-000000000006", "position": 1, "caption": null, "created_at": "2026-11-22T10:00:00Z", "updated_at": "2026-11-22T10:00:00Z" }); let key = cotton_store::protocol::derived_id::note_attachment_id(&id, "document", "00000000-0000-4000-8000-000000000006"); insert_row(ctx, "cotton_note_attachments", &key, row.as_object().cloned().unwrap()).map_err(super::super::notes::tool)?; ctx.commit().map_err(super::super::notes::tool)?; Ok(json!({})) }) .unwrap(); let files = ask(&phone, "notes.open", json!({ "id": id }))["files"].clone(); let away = &files[1]; assert_eq!(away["id"], "00000000-0000-4000-8000-000000000006"); assert_eq!(away["name"], "Attached file"); assert_eq!(away["here"], false); assert!(away["opens"].is_null() && away["key"].is_null(), "{away}"); let refused = dispatch(&phone, "notes.file", &json!({ "note": id, "id": "00000000-0000-4000-8000-000000000006" })).expect_err("no object to ask"); assert_eq!(refused.code, "file_offline"); assert_eq!(refused.message, "This file is not on this phone yet. It opens when you are online."); let stranger = dispatch(&phone, "notes.file", &json!({ "note": id, "id": "00000000-0000-4000-8000-000000000007" })).expect_err("not attached"); assert_eq!(stranger.code, "not_found"); } /// RULE (Cotton Dev only): `developer.inventNoteFiles` makes a note with invented files attached, kept as documents of /// this copy, for a walk on an emulator; the real Cotton (the live server's app) refuses it by name and writes nothing. #[test] fn invented_note_files_land_in_cotton_dev_only() { let staged = tempfile::tempdir().unwrap(); let md = staged.path().join("packing.md"); std::fs::write(&md, "# Packing\n\n- Passport\n").unwrap(); let ask_for = json!({ "title": "Invented trip", "body": "What we planned", "files": [{ "path": md.to_string_lossy(), "mime": "text/plain", "name": "packing.md" }], "away": ["00000000-0000-4000-8000-000000000009"] }); let dir = tempfile::tempdir().unwrap(); let real = phone_as(&dir, cotton_core::flavor::Flavor::Real); let refused = dispatch(&real, "developer.inventNoteFiles", &ask_for).expect_err("the real Cotton refuses it"); assert_eq!(refused.code, "not_dev"); assert!(ask(&real, "notes.list", json!({ "view": "all" }))["rows"].as_array().is_none_or(Vec::is_empty), "nothing written"); drop(real); let dir = tempfile::tempdir().unwrap(); let dev = phone_as(&dir, cotton_core::flavor::Flavor::Dev); let made = ask(&dev, "developer.inventNoteFiles", ask_for); let files = ask(&dev, "notes.open", json!({ "id": made["id"] }))["files"].clone(); assert_eq!((files[0]["opens"].as_str(), files[0]["format"].as_str()), (Some("note"), Some("Markdown")), "{files}"); assert_eq!(files[0]["here"], true); assert!(files[0]["key"].is_string()); assert_eq!((files[1]["here"].as_bool(), files[1]["name"].as_str()), (Some(false), Some("Attached file")), "{files}"); }