//! `shell.reading` over a synthetic Notes phone: what a note's reading page//! reads, how it cuts its pages and the face it wears.usesuper::*;usecrate::handle::CottonHandle;const ZONE: &str = "Asia/Kolkata";fn phone() -> (tempfile::TempDir, CottonHandle) { opened(include_str!("../../../../apps/cotton/App/Flavours/notes/flavour.json"))}fn opened(manifest: &str) -> (tempfile::TempDir, CottonHandle) {let dir = tempfile::tempdir().expect("a temp container");let manifest: Value = serde_json::from_str(manifest).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(ZONE.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 ask(handle: &CottonHandle, method: &str, request: Value) -> Result<Value, MethodError> {crate::methods::dispatch(handle, method, &request)}fn note(handle: &CottonHandle, data: Value) -> String {let made = ask(handle, "records.create", json!({"table": "note", "data": data})).expect("a note"); made["id"].as_str().or_else(|| made["record"]["id"].as_str()).expect("its id").to_string()}// RULE: a note's reading page is headed by its title, its markdown let go, and reads its body as one page where the shell measured nothing; the body is written here.#[test]fn a_notes_reading_page_reads_its_title_and_its_body() {let (_dir, handle) = phone();let id = note(&handle, json!({"title": "# Invented *lake*", "body": "One invented line.\n\nAnd another."}));let page = ask(&handle, "shell.reading", json!({"table": "note", "id": id})).expect("a reading page"); assert_eq!(page["title"], "Invented lake"); assert_eq!(page["bodies"], json!([{"column": "body", "markdown": true, "writes": true}])); assert_eq!(page["pages"], json!([{"body": 0, "text": "One invented line.\n\nAnd another."}])); assert_eq!(page["words_per_page"], Value::Null);}// RULE: an untitled note's reading page is headed by its first sentence, as its torn page is.#[test]fn an_untitled_note_is_headed_by_its_first_sentence() {let (_dir, handle) = phone();let id = note(&handle, json!({"body": "Buy the invented ink. Then the paper."}));let page = ask(&handle, "shell.reading", json!({"table": "note", "id": id})).expect("a reading page"); assert_eq!(page["title"], "Buy the invented ink");}// RULE: measured, the page holds half the words its block takes, and a long body turns between its paragraphs and sentences.#[test]fn a_measured_page_cuts_the_body_by_its_words() {let (_dir, handle) = phone();let id = note(&handle, json!({"title": "Invented", "body": "One two three. Four five six.\n\nSeven eight nine ten."}));// 23 columns at 5.7 letters a word is 4.03 words a line; 3 lines at half depth is 6 words a page.let page = ask(&handle, "shell.reading", json!({"table": "note", "id": id, "columns": 23, "lines": 3})).expect("a reading page"); assert_eq!(page["words_per_page"], 6);let texts: Vec<&str> = page["pages"].as_array().unwrap().iter().map(|page| page["text"].as_str().unwrap()).collect(); assert_eq!(texts, ["One two three. Four five six.", "Seven eight nine ten."]);}// RULE: the reading page wears the reader's face — Paper until one is chosen, the one chosen after — and deals Paper, Sepia and Night as its faces.#[test]fn the_reading_page_wears_the_readers_face() {let (_dir, handle) = phone();let id = note(&handle, json!({"title": "Invented", "body": "Words."}));let page = ask(&handle, "shell.reading", json!({"table": "note", "id": id})).expect("a reading page"); assert_eq!(page["settings"]["face"], "paper");let faces: Vec<&str> = page["faces"]["cards"].as_array().unwrap().iter().map(|card| card["id"].as_str().unwrap()).collect(); assert_eq!(faces, ["face-paper", "face-sepia", "face-night"]); assert!(page["faces"]["cards"].as_array().unwrap().iter().all(|card| card["label"].as_str().is_some_and(|label| !label.is_empty())), "each face says its name"); ask(&handle, "reader.settings", json!({"face": "night"})).expect("Night chosen");let page = ask(&handle, "shell.reading", json!({"table": "note", "id": id})).expect("a reading page"); assert_eq!(page["settings"]["worn"], "night"); assert_eq!(page["faces"]["raised"], "face-night");}// RULE: a table whose card is not its own words opens no reading page (a todo, an event); a note's does, its body read.#[test]fn only_a_card_of_its_own_words_opens_a_reading_page() {let (_whole, cotton) = opened(include_str!("../../../../apps/cotton/App/CottonManifest/flavour.json"));for table in ["todo", "event"] {let definition = crate::phone::card_actions::read_definition(&cotton.client(), table, "test").expect("a definition"); assert!(reading_page::slots(&definition).is_none(), "{table} opens no reading page"); }let (_dir, handle) = phone();let client = handle.client();let note = crate::phone::card_actions::read_definition(&client, "note", "test").expect("the note");let slots = reading_page::slots(¬e).expect("a note reads"); assert_eq!(slots.title.as_deref(), Some("title")); assert_eq!(slots.bodies, ["body"]);}