//! A reader opens its copy; a runner keeps none (architect's ruling, 18 Oct //! 2026, 298's first row). Every name and row is synthetic. use base64::engine::general_purpose::URL_SAFE_NO_PAD; use base64::Engine; use serde_json::{json, Value}; use crate::handle::{CottonHandle, OpenConfig, OpenMode}; /// A phone joined with `scope`, its first copy already down. fn a_phone(scope: &str) -> (tempfile::TempDir, CottonHandle) { let dir = tempfile::tempdir().expect("a temp container"); let layout = cotton_client::layout::Layout::new(dir.path()); std::fs::create_dir_all(layout.client_dir()).expect("the client folder"); std::fs::write( layout.connection_json(), serde_json::to_vec_pretty(&json!({ "mode": "remote", "baseUrl": "https://server.invalid", "workspaceId": crate::fixture::WORKSPACE, "ownerId": crate::fixture::OWNER, "connectedAt": "2026-10-18T09:00:00.000Z", "deviceScope": scope, })) .expect("json"), ) .expect("the connection record"); let replica = layout.replica(crate::fixture::WORKSPACE); std::fs::create_dir_all(replica.parent().expect("a parent")).expect("the replica folder"); drop(crate::fixture::seeded(&replica)); 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(URL_SAFE_NO_PAD.encode([9u8; 32])), vault_secret: None, flavor: cotton_core::flavor::Flavor::Real, manifest: None, }) .expect("an open core"); // The pull after approval brought this device's own row, at its scope. if scope == "reader" { let slot = handle.client().receipt_signer_slot(); let signer = slot.get().expect("a receipt signer"); handle .client() .with_writer(|file| { crate::fixture::device_row(file.driver, &signer.device_key()); file.driver .run( "UPDATE cotton_devices SET scope = 'reader' WHERE device_id = ?", &[rusqlite::types::Value::Text(signer.device_key())], ) .map(|_| ()) .map_err(|error| error.to_string()) }) .expect("the reader's row"); } (dir, handle) } fn ask(handle: &CottonHandle, method: &str, request: Value) -> Result { crate::methods::dispatch(handle, method, &request) } /// RULE: A reader launch refreshes its package catalogue without writing or a tool_package_refused error; walker298, 18 Oct 2026 rule 6. #[test] fn a_reader_package_refresh_reads_without_writing() { let (_dir, handle) = a_phone("reader"); let counts = || handle.client().with_file(|file| { file.driver.get("SELECT (SELECT COUNT(*) FROM _outbox) AS queued, (SELECT COUNT(*) FROM cotton_changes) AS receipts", &[]) .map_err(|error| error.to_string()) }).unwrap().unwrap(); let before = counts(); let packages = ask(&handle, "tools.list", json!({})).expect("reader refresh never refuses"); assert!(packages["tools"].is_array()); assert!(packages["table_definitions"].is_array()); let after = counts(); assert_eq!(before.int("queued"), after.int("queued")); assert_eq!(before.int("receipts"), after.int("receipts")); } /// RULE: A reader opens its copy and reaches Home; only a runner keeps no copy. /// /// WHY: 18 Oct 2026, walker296 — the copy open refused every device that was /// not full with the runner's sentence, and every simulator joins as a reader /// under shape 139, so the sim downloaded its 3,984 MB copy and Home drew /// "this device runs jobs and takes placements; it keeps no database copy". #[test] fn a_reader_opens_its_copy_and_reaches_home() { let (_dir, handle) = a_phone("reader"); for method in ["read.boot", "read.home"] { ask(&handle, method, json!({})) .unwrap_or_else(|error| panic!("{method}: {}: {}", error.code, error.message)); } } /// RULE: Every write a reader asks for is refused at the store with the typed refusal, and the person reads "This device reads only." — never "try again". /// /// WHY: the reader's copy is open now (18 Oct 2026), so the store's 139 /// refusal is what stands between a reader and the household's file; a retry /// sentence would invite a write that can never be kept. #[test] fn a_readers_write_is_refused_at_the_store_and_says_it_reads_only() { let (_dir, handle) = a_phone("reader"); { let (tool, args) = ("create_todo", json!({"title": "Written by a reader"})); let refused = ask(&handle, "write", json!({"tool": tool, "args": args})).expect_err("a reader never writes"); assert!( refused.message.contains(cotton_core::device::scope::READER_WRITE_REFUSAL), "{tool}: {}: {}", refused.code, refused.message ); let (said, logged) = refused.for_person(); assert_eq!(said.message, cotton_core::failure::READS_ONLY, "{tool}"); assert!(logged.is_some(), "the raw refusal goes to the log"); } let queued = handle .client() .with_writer(|file| { file.driver.get("SELECT COUNT(*) AS n FROM _outbox", &[]).map_err(|error| error.to_string()) }) .expect("a read") .and_then(|row| row.int("n")); assert_eq!(queued, Some(0), "nothing a reader asked for is queued"); } /// RULE: A reader is offered no write act: role resolves the definition before /// it reaches a drawing surface, because walker298 saw Ahead offer "Add what /// is ahead" on a reader copy on 18 Oct 2026. #[test] fn a_reader_definition_offers_no_write_act() { let (_dir, reader) = a_phone("reader"); for method in ["definitions.tables", "definitions.catalogue"] { let answer = ask(&reader, method, json!({})).unwrap_or_else(|error| panic!("{method}: {}: {}", error.code, error.message)); let tables = answer[if method == "definitions.tables" { "definitions" } else { "tables" }].as_array().expect("tables"); for table in tables { assert!(table["commands"].as_array().into_iter().flatten().all(|command| { !matches!(command["kind"].as_str(), Some("create" | "update" | "delete")) }), "{method} offered a write command: {table}"); } } } /// RULE: A runner still keeps no database copy; its open is refused with the runner's sentence. #[test] fn a_runner_still_keeps_no_copy() { let (_dir, handle) = a_phone("runner"); let refused = ask(&handle, "read.home", json!({})).expect_err("a runner opens no copy"); assert!(refused.message.contains("keeps no database copy"), "{}: {}", refused.code, refused.message); }