//! Comms' Sites (`sites.*`, SITES.md; chat CONTRACT.md "Sites: the core's `sites.*`"). Every address is invented.use serde_json::{json, Value};usecrate::handle::CottonHandle;usecrate::phone::passwords::tests::phone;const KEY: &str = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8";fn ask(handle: &CottonHandle, method: &str, mut request: Value) -> Value { request["key"] = json!(KEY);crate::methods::dispatch(handle, method, &request).unwrap_or_else(|e| panic!("{method}: {} {}", e.code, e.message))}fn refused(handle: &CottonHandle, method: &str, mut request: Value) -> (String, String) { request["key"] = json!(KEY);let e = crate::methods::dispatch(handle, method, &request).expect_err(method); (e.code, e.message)}fn you_ids(handle: &CottonHandle) -> Vec<String> {let you = crate::methods::dispatch(handle, "chat.you.now", &json!({})).unwrap(); you["rows"].as_array().unwrap().iter().map(|r| r["id"].as_str().unwrap().to_string()).collect()}/// Two accounts on one site are two rows, each with its own store; the list answers its words; Sites moves from/// under You to the pill with the first row and back with the last (a development build: everyone but the owner).#[test]fn rows_are_added_listed_and_sites_becomes_a_place() {let dir = tempfile::tempdir().unwrap();let handle = crate::phone::chat_app::mail::tests::comms(&dir, cotton_core::flavor::Flavor::Dev);let empty = ask(&handle, "sites.list", json!({})); assert_eq!(empty["title"], "Sites"); assert_eq!(empty["place"], "you"); assert_eq!(empty["rows"], json!([])); assert_eq!(empty["private"], json!({ "label": "Private", "line": "Nothing is kept" })); assert_eq!(empty["hidden"]["label"], "Hidden"); assert_eq!(empty["hidden"]["locked"], true); assert_eq!(empty["paste"]["label"], "Open copied link here"); assert_eq!(empty["words"]["delete_ask"], "Remove {label}? Its sign-in on this phone is deleted too."); assert!(!you_ids(&handle).contains(&"sites".to_string()), "Sites is Cotton Sites' own app now (COMMS WITHOUT MAIL): never under Comms' You");let work = ask(&handle, "sites.add", json!({ "label": "Work Claude", "home": "claude.ai" })); assert_eq!(work["said"], "Work Claude added");let work = &work["row"];let home = &ask(&handle, "sites.add", json!({ "label": "Home Claude", "home": "https://claude.ai", "stay": false }))["row"]; assert_eq!(work["home"], "https://claude.ai/"); assert_eq!(work["site"], "claude.ai"); assert_eq!(work["line"], "claude.ai"); assert_eq!(work["stay"], true); assert_eq!(home["line"], "claude.ai · Signed out when closed"); assert_ne!(work["store"], home["store"]);let id = work["id"].as_str().unwrap(); assert_eq!(work["store"], format!("site-{id}")); assert_eq!(work["store_uuid"], id, "the row's id is a UUID, and names its store on iPhone");let list = ask(&handle, "sites.list", json!({})); assert_eq!(list["place"], "tab");let labels: Vec<&str> = list["rows"].as_array().unwrap().iter().map(|r| r["label"].as_str().unwrap()).collect(); assert_eq!(labels, ["Work Claude", "Home Claude"]); assert!(!you_ids(&handle).contains(&"sites".to_string()), "a place on the pill now");let gone = ask(&handle, "sites.delete", json!({ "id": id })); assert_eq!(gone["store"], format!("site-{id}")); assert_eq!(gone["said"], "Work Claude removed"); ask(&handle, "sites.delete", json!({ "id": home["id"] })); assert_eq!(ask(&handle, "sites.list", json!({}))["place"], "you"); assert!(!you_ids(&handle).contains(&"sites".to_string()), "and still never under Comms' You");}/// A hidden row is never answered until the device lock has said yes (`unlocked`), nor offered by a link.#[test]fn hidden_rows_wait_for_the_unlock() {let (_dir, handle) = phone(); ask(&handle, "sites.add", json!({ "label": "Bank", "home": "bank.example", "hidden": true })); ask(&handle, "sites.add", json!({ "label": "News", "home": "news.example" }));let locked = ask(&handle, "sites.list", json!({})); assert_eq!(locked["rows"].as_array().unwrap().len(), 1); assert_eq!(locked["rows"][0]["label"], "News"); assert_eq!(locked["hidden"]["locked"], true); assert_eq!(locked["hidden"]["count"], 1); assert!(locked["hidden"].get("rows").is_none());let open = ask(&handle, "sites.list", json!({ "unlocked": true })); assert_eq!(open["hidden"]["locked"], false); assert_eq!(open["hidden"]["rows"][0]["label"], "Bank"); assert_eq!(open["rows"].as_array().unwrap().len(), 1, "the hidden rows stand apart");let link = ask(&handle, "sites.link", json!({ "url": "https://club.example/in?t=1" })); assert_eq!(link["open_in"]["rows"].as_array().unwrap().len(), 1); assert_eq!(link["open_in"]["label"], "Open in a session");}/// A link opens in Private, in a store made for this once; Open in a session opens it in a row's store.#[test]fn a_link_opens_private_then_in_a_row() {let (_dir, handle) = phone();let club = ask(&handle, "sites.add", json!({ "label": "Club", "home": "club.example" }))["row"].clone();let link = ask(&handle, "sites.link", json!({ "url": "http://localhost:8765/in?t=walk-link" })); assert_eq!(link["private"], true); assert_eq!(link["url"], "http://localhost:8765/in?t=walk-link"); assert!(link["store"].as_str().unwrap().starts_with("private-")); assert_eq!(link["title"], "Private");let again = ask(&handle, "sites.link", json!({ "url": "https://club.example/" })); assert_ne!(again["store"], link["store"], "each Private opening has a store of its own");let inside = ask(&handle, "sites.open", json!({ "id": club["id"], "url": "http://localhost:8765/in?t=walk-link" })); assert_eq!(inside["private"], false); assert_eq!(inside["store"], club["store"]); assert_eq!(inside["store_uuid"], club["store_uuid"]); assert_eq!(inside["url"], "http://localhost:8765/in?t=walk-link"); assert_eq!(inside["title"], "Club"); assert_eq!(ask(&handle, "sites.open", json!({ "id": club["id"] }))["url"], "https://club.example/", "its home by default"); assert_eq!(refused(&handle, "sites.link", json!({ "url": "javascript:alert(1)" })).0, "not_web");}/// Rename, Change home and Stay signed in change only what is given; a refusal is said and changes nothing.#[test]fn edits_and_refusals_are_in_words() {let (_dir, handle) = phone();let id = ask(&handle, "sites.add", json!({ "label": "Mail", "home": "mail.example" }))["row"]["id"].clone();let renamed = ask(&handle, "sites.edit", json!({ "id": id, "label": " Work mail " })); assert_eq!(renamed["row"]["label"], "Work mail"); assert_eq!(renamed["said"], "Saved");let moved = &ask(&handle, "sites.edit", json!({ "id": id, "home": "www.mail.example/inbox", "stay": false }))["row"]; assert_eq!(moved["home"], "https://www.mail.example/inbox"); assert_eq!(moved["line"], "mail.example · Signed out when closed"); assert_eq!(refused(&handle, "sites.edit", json!({ "id": id, "home": "best pizza near me" })), ("not_web".into(), "That isn't a web address.".into())); assert_eq!(ask(&handle, "sites.add", json!({ "label": "", "home": "a.example" }))["row"]["label"], "a.example", "an empty label takes the domain"); assert_eq!(refused(&handle, "sites.delete", json!({ "id": "gone" })).1, "That site is no longer here."); assert_eq!(ask(&handle, "sites.list", json!({}))["rows"][0]["home"], "https://www.mail.example/inbox");}/// The book is one sealed file on this phone: no label or address in the clear, and another key reads nothing/// (the file is set aside, never written over).#[test]fn the_book_is_sealed_on_the_phone() {let (_dir, handle) = phone(); ask(&handle, "sites.add", json!({ "label": "Secret club", "home": "club.example" }));let file = handle.client().layout().client_dir().join("sites.c1");let bytes = std::fs::read(&file).unwrap(); assert!(!bytes.windows(6).any(|w| w == b"Secret")); assert!(!bytes.windows(4).any(|w| w == b"club"));let other = json!({ "key": "Hx4dHBsaGRgXFhUUExIREA8ODQwLCgkIBwYFBAMCAQA" });let read = crate::methods::dispatch(&handle, "sites.list", &other).unwrap(); assert_eq!(read["rows"], json!([])); assert!(file.with_extension("c1.unreadable").exists()); assert!(crate::methods::dispatch(&handle, "sites.list", &json!({})).is_err(), "no key, no rows");// Android sends standard base64 with padding: the same key, the same book.let padded = json!({ "key": "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=" }); assert_eq!(crate::methods::dispatch(&handle, "sites.list", &padded).unwrap()["rows"], json!([]));}/// On CottonDB's own build (the owner's household) Sites is a place on the pill from the start, never a row under You.#[test]fn the_owners_build_has_sites_as_a_place_at_once() {let (_dir, handle) = phone(); assert_eq!(ask(&handle, "sites.list", json!({}))["place"], "tab"); assert!(!you_ids(&handle).contains(&"sites".to_string()));}/// SITES 4: a row reopens where it was left, sealed like the rest of the book; one address, replaced; Stay signed in/// off and Private keep nothing; Sign out (`url` null) sends it home again.#[test]fn a_row_reopens_where_it_was_left() {let (_dir, handle) = phone();let club = ask(&handle, "sites.add", json!({ "label": "Club", "home": "club.example" }))["row"]["id"].as_str().unwrap().to_string();let bank = ask(&handle, "sites.add", json!({ "label": "Bank", "home": "bank.example", "stay": false }))["row"]["id"].as_str().unwrap().to_string();let open = |id: &str| ask(&handle, "sites.open", json!({ "id": id }))["url"].as_str().unwrap().to_string();let listed = |id: &str| ask(&handle, "sites.list", json!({}))["rows"].as_array().unwrap().iter().find(|r| r["id"] == id).unwrap()["open"].clone(); assert_eq!(open(&club), "https://club.example/"); ask(&handle, "sites.left", json!({ "id": club, "url": "https://club.example/code?wait=yes" })); ask(&handle, "sites.left", json!({ "id": club, "url": "https://club.example/verify" })); assert_eq!(open(&club), "https://club.example/verify", "the last one, replaced"); assert_eq!(listed(&club), json!("https://club.example/verify"));let bytes = std::fs::read(handle.client().layout().client_dir().join("sites.c1")).unwrap(); assert!(!bytes.windows(6).any(|w| w == b"verify"), "sealed"); ask(&handle, "sites.left", json!({ "id": bank, "url": "https://bank.example/accounts" })); assert_eq!(open(&bank), "https://bank.example/", "Stay signed in off keeps nothing"); assert_eq!(refused(&handle, "sites.left", json!({ "url": "https://club.example/" })).0, "no_row", "Private has no row to keep it in"); assert_eq!(refused(&handle, "sites.left", json!({ "id": club, "url": "about:blank" })).0, "not_web"); ask(&handle, "sites.left", json!({ "id": club, "url": null })); assert_eq!(open(&club), "https://club.example/", "signed out: home");}