Cotton code
56 lines · 3.2 KB · RustRaw
//! `notes.shared` over a synthetic Notes phone.use super::*;fn phone() -> (tempfile::TempDir, CottonHandle) {    let dir = tempfile::tempdir().expect("a temp container");    let manifest: Value = serde_json::from_str(include_str!("../../../../apps/cotton/App/Flavours/notes/flavour.json")).expect("a manifest");    let handle = CottonHandle::open(crate::handle::OpenConfig {        mode: crate::handle::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: cotton_core::flavor::Flavor::Real,        manifest: Some(manifest),    })    .expect("an open core");    crate::methods::dispatch(&handle, "workspace.startLocal", &json!({"deviceName": "An invented phone"})).expect("started");    (dir, handle)}fn note(handle: &CottonHandle, answer: &Value) -> Value {    let id = answer["lands"]["id"].as_str().unwrap_or_else(|| panic!("a landed note: {answer}"));    assert_eq!(answer["lands"]["table"], "note");    crate::methods::dispatch(handle, "records.get", &json!({"table": "note", "id": id})).expect("the note")["data"].clone()}// RULE: shared words become a note's body, the link after them where they do not hold it, the subject its title.#[test]fn shared_words_become_a_note() {    let (_dir, handle) = phone();    let answer = shared(&handle, &json!({"text": "An invented thought worth keeping.", "url": "https://example.invalid/a", "title": "Invented subject"})).unwrap();    let data = note(&handle, &answer);    assert_eq!(data["body"], "An invented thought worth keeping.\n\nhttps://example.invalid/a");    assert_eq!(data["title"], "Invented subject");    let answer = shared(&handle, &json!({"text": "Invented subject and more words", "title": "Invented subject"})).unwrap();    assert!(note(&handle, &answer)["title"].is_null(), "a subject the words begin with is no title");}// RULE: a shared text file is the note's words, titled by its name without the extension; a file that is no text writes nothing and says so.#[test]fn a_shared_text_file_becomes_a_note_and_a_picture_does_not() {    let (dir, handle) = phone();    let path = dir.path().join("staged.md");    std::fs::write(&path, "\u{feff}# Invented heading\n\nInvented words.\n").unwrap();    let answer = shared(&handle, &json!({"file": path.to_string_lossy(), "fileName": "Garden plan.md"})).unwrap();    let data = note(&handle, &answer);    assert_eq!(data["body"], "# Invented heading\n\nInvented words.");    assert_eq!(data["title"], "Garden plan");    let picture = dir.path().join("staged.png");    std::fs::write(&picture, [0x89u8, b'P', b'N', b'G', 0, 0, 0, 0]).unwrap();    let answer = shared(&handle, &json!({"file": picture.to_string_lossy(), "fileName": "invented.png"})).unwrap();    assert_eq!(answer["said"], "There are no words in this to keep, so no note was written.");    assert!(answer.get("lands").is_none());    assert_eq!(shared(&handle, &json!({"text": "   "})).unwrap()["said"], "There are no words in this to keep, so no note was written.");}