use super::*; use crate::schema::ddl::{LINKS_TABLE, PROJECT_LINKS_TABLE}; use crate::schema::table_specs::TABLE_NAMES; /// RULE: Every Mine-bound kind is one its table's vocabulary admits, and every Mine-bound table is a synced table. /// So the rule is never asked about a kind or a parent that a rename left behind. #[test] fn every_mine_bound_kind_and_table_is_one_the_file_has() { for kind in MINE_BOUND_LINK_KINDS { assert!( LINKS_TABLE.contains(&format!("'{kind}'")), "{kind} is not in cotton_links' vocabulary" ); } for kind in MINE_BOUND_PROJECT_LINK_KINDS { assert!( PROJECT_LINKS_TABLE.contains(&format!("'{kind}'")), "{kind} is not in cotton_project_links' vocabulary" ); } for table in MINE_BOUND_TABLES { assert!(TABLE_NAMES.contains(&table), "{table} is not a synced table"); } // A Messages table is in a shared database when a chat is filed there, so // it is never also a table a shared database may not name. for table in FILED_CHAT_TABLES { assert!(TABLE_NAMES.contains(&table), "{table} is not a synced table"); assert!(!MINE_BOUND_TABLES.contains(&table), "{table} is both filed and Mine-bound"); } } /// RULE: Usage never enters another person's database; local writes and exchange both use the same link check to keep private habits personal. #[test] fn usage_is_refused_in_shared_databases() { let op = CottonOp { op_id: "invented-open".into(), device_id: "device:invented".into(), kind: OpKind::Upsert, table: "cotton_usage".into(), row_id: "invented-open".into(), patch: None, hlc: String::new(), schema_version: 136, actor: "user".into(), receipt: String::new(), restamps: None, sig: None, }; let mut file = ThisFile { id: Some("invented-database".into()), kind: FileKind::Shared }; assert!(check(&file, &op, Some(&[])).unwrap().contains("personal")); file.kind = FileKind::Personal; assert!(check(&file, &op, Some(&[])).is_none()); } /// RULE: Arrival screen context and undecided proposals are the owner's private queue; peer writes cannot publish them into a project. #[test] fn arrivals_and_proposals_are_personal_at_the_fold() { for table in ["cotton_arrivals","cotton_filing_proposals"] { let op=CottonOp{op_id:"fixture".into(),device_id:"fixture".into(),kind:OpKind::Upsert,table:table.into(),row_id:"fixture".into(),patch:None,hlc:String::new(),schema_version:crate::schema::ddl::SCHEMA_VERSION,actor:"user".into(),receipt:String::new(),restamps:None,sig:None}; let file=ThisFile{id:Some("project".into()),kind:FileKind::Shared}; assert!(check(&file,&op,Some(&[])).is_some()); let personal=ThisFile{id:Some("personal".into()),kind:FileKind::Personal}; assert!(check(&personal,&op,Some(&[])).is_none()); } } /// RULE: A newer move into the same home supersedes its old move tombstone for both root and owned rows; moving away again restores the write refusal. A return cannot grant another database writes. #[test] fn a_return_supersedes_the_old_move_refusal(){ let db=Driver::open_in_memory().unwrap(); db.exec(r#"CREATE TABLE cotton_changes(id TEXT,table_id TEXT,record_id TEXT,verb TEXT,before TEXT,after TEXT,_hlc TEXT); CREATE TABLE cotton_undos(change_id TEXT); CREATE TABLE _tombstones(tbl TEXT,row_id TEXT); INSERT INTO _tombstones VALUES('cotton_notes','root'),('cotton_comments','child'); INSERT INTO cotton_changes VALUES('out','cotton_notes','root','move','{"related":[{"table":"cotton_comments","id":"child"}]}','{"to":"project","to_name":"Project"}','001');"#).unwrap(); let file=ThisFile{id:Some("personal".into()),kind:FileKind::Personal}; let mut op=CottonOp{op_id:"fixture".into(),device_id:"fixture".into(),kind:OpKind::Upsert,table:"cotton_notes".into(),row_id:"root".into(),patch:None,hlc:String::new(),schema_version:crate::schema::ddl::SCHEMA_VERSION,actor:"user".into(),receipt:String::new(),restamps:None,sig:None}; assert!(check_on(&db,&file,&op,Some(&[])).unwrap().is_some()); db.exec(r#"INSERT INTO cotton_changes VALUES('foreign','cotton_notes','root','move_in','{}','{"to":"other"}','002');"#).unwrap(); assert!(check_on(&db,&file,&op,Some(&[])).unwrap().is_some()); db.exec(r#"INSERT INTO cotton_changes VALUES('back','cotton_notes','root','move_in','{}','{"to":"personal"}','003');"#).unwrap(); assert!(check_on(&db,&file,&op,Some(&[])).unwrap().is_none()); op.table="cotton_comments".into();op.row_id="child".into(); assert!(check_on(&db,&file,&op,Some(&[])).unwrap().is_none()); db.exec(r#"INSERT INTO cotton_changes VALUES('out-again','cotton_notes','root','move','{"related":[{"table":"cotton_comments","id":"child"}]}','{"to":"third"}','004');"#).unwrap(); assert!(check_on(&db,&file,&op,Some(&[])).unwrap().is_some()); op.table="cotton_notes".into();op.row_id="root".into(); assert!(check_on(&db,&file,&op,Some(&[])).unwrap().is_some()); }