use std::cell::Cell; use std::sync::Arc; use cotton_client::seams::Clock; use rusqlite::types::Value as Sql; use serde_json::{json, Value}; use crate::methods::{could_not_read, MethodError, COULD_NOT_READ}; use crate::shell::client::ClientController; /// A paired phone whose copy names `synthetic-serving` its serving Mac, with /// the sync line's own observations: the Mac answered `answered_ago` minutes /// ago and, when given, a pass failed `failed_ago` minutes ago. Synthetic. fn phone(answered_ago: i64, failed_ago: Option) -> (tempfile::TempDir, Arc, String) { let (dir, client, clock) = crate::shell::client::phone_tests::attached_on_a_clock(); let now = clock.now(); let mut line = cotton_client::sync::line::HomeLine::default(); let answered = now - chrono::Duration::minutes(answered_ago); line.answered(Default::default(), answered.timestamp_millis()); if let Some(ago) = failed_ago { line.unanswered(None, (now - chrono::Duration::minutes(ago)).timestamp_millis()); } cotton_client::sync::line::store( &client.layout().home_lines_json(), &[(crate::fixture::WORKSPACE.to_string(), line)].into_iter().collect(), ); drop(client); // The lines are read when the controller is made, as on a relaunch. let client = Arc::new( ClientController::new( cotton_client::layout::Layout::new(dir.path()), Arc::new(crate::events::EventBus::new()), Some("UTC".into()), None, ) .with_seams(clock.clone(), Arc::new(cotton_client::seams::SeededRandom::default())), ); client.set_self_device_id(Some(crate::fixture::SELF_DEVICE_ID.into())); client .with_writer(|file| { file.driver .run( "INSERT INTO cotton_devices (id,workspace_id,device_id,name,platform,approved_at,approved_by_device,created_at,updated_at) \ VALUES ('synthetic-serving',?,'synthetic-serving','Synthetic Mac','mac','2026-09-01','self','2026-09-01','2026-09-01')", &[Sql::Text(file.workspace_id.into())], ) .map_err(|e| e.to_string())?; file.driver .run( "INSERT INTO cotton_serving_machine (id,workspace_id,epoch,device_credential_id,author_id,since) \ VALUES ('synthetic-serving-fact',?,1,'synthetic-serving','synthetic-author','2026-09-01')", &[Sql::Text(file.workspace_id.into())], ) .map_err(|e| e.to_string())?; Ok(()) }) .unwrap(); (dir, client, cotton_client::seams::iso(answered)) } /// RULE: AI usage never waits on a Mac this device does not hear (295 rule 15, walker294): with the serving Mac not heard now — a pass failed after its last contact, the same fact as `heard_now` — a `records.list` of a metering table answers at once, typed `could_not_read` with `why: on_the_mac`, the table and the Mac's `heard_at` instant, and the transport is never called; the database definition says the line, so nothing here is a sentence the page shows. With the Mac heard, the read still asks the Mac. #[test] fn a_metering_read_says_on_the_mac_at_once_when_the_mac_is_not_heard() { // One phone each way, every metering table read on it: a phone is the // costly part (four of them ran past the fast profile's 300 s on the box). let (_unheard_dir, unheard, heard_at) = phone(3, Some(1)); let (_heard_dir, heard, _) = phone(1, None); for table in cotton_core::definitions::METERING_READS { let client = &unheard; let asked = Cell::new(0); let refused = super::answer(client, "records.list", table, || { asked.set(asked.get() + 1); Ok(json!({ "records": [] })) }) .expect_err("not heard: refused at once"); assert_eq!(asked.get(), 0, "{table}: no request to a Mac this device does not hear"); // The dispatcher's own typing keeps it whole. let refused = could_not_read("records.list", refused); assert_eq!(refused.code, COULD_NOT_READ, "{table}"); assert_eq!(refused.details["why"], super::ON_THE_MAC, "{table}"); assert_eq!(refused.details["table"], *table); assert_eq!(refused.details["heard_at"], Value::from(heard_at.as_str()), "{table}: the Mac's last contact"); let client = &heard; let asked = Cell::new(0); let answer = super::answer(client, "records.list", table, || { asked.set(asked.get() + 1); Ok(json!({ "records": [{ "id": "synthetic-call" }] })) }) .expect("heard: the Mac answers"); assert_eq!(asked.get(), 1, "{table}: a heard Mac is asked"); assert_eq!(answer["records"][0]["id"], "synthetic-call"); } } /// RULE: the Mac's refusal of a heard read keeps its own path — `answer` adds no typing to what the Mac said (295 rule 15). #[test] fn a_heard_macs_refusal_is_its_own() { let (_dir, client, _) = phone(1, None); let refused = super::answer(&client, "records.list", "ai_calls", || Err(MethodError::new("server_refused", "synthetic"))) .expect_err("the Mac refused"); assert_eq!(refused.code, "server_refused"); }