632 lines · 28.1 KB · RustRaw
use super::*;use crate::mcp::dispatch::ToolServices;use crate::mcp::handlers::common::insert_row;use crate::mcp::writer::job;use crate::random::SystemRandom;use chrono::Utc;use cotton_store::file_identity::read_file_identity;use cotton_store::meta::Meta;use rusqlite::types::Value as SqlValue;use std::sync::Arc;/// RULE: The shared mover refuses an unadmitted chat before queuing any member of a mixed batch, so neither the generic verb nor owner proposal approval can bypass the database's participant boundary.#[test]fn an_unadmitted_chat_refuses_the_whole_batch_before_any_write(){ let (sdb,sf,so)=file("00000000-0000-4000-8000-000000000801"); let (ddb,df,do_)=file("00000000-0000-4000-8000-000000000802"); let services=ToolServices::none(); let mut source=owner_context(&sdb,&sf,&so,&services); let mut destination=context(&ddb,&df,&do_,&services); insert_row(&mut source,"cotton_notes","note",serde_json::from_value(json!({"title":"Keep this note here"})).unwrap()).unwrap(); insert_row(&mut source,"cotton_msg_contacts","contact",serde_json::from_value(json!({"display_name":"Invented Unadmitted Sender","phone_e164":"14155550123"})).unwrap()).unwrap(); insert_row(&mut source,"cotton_msg_threads","thread",serde_json::from_value(json!({"kind":"dm","contact_id":"contact"})).unwrap()).unwrap(); source.commit().unwrap(); let before=sdb.all("SELECT * FROM cotton_changes",&[]).unwrap(); let error=queue_batch(&mut source,&mut destination,&["note".into(),"thread".into()],"Project").unwrap_err(); assert!(error.to_string().contains("Invented Unadmitted Sender")); assert_eq!(source.writer().unwrap().pending(),0);assert_eq!(destination.writer().unwrap().pending(),0); assert_eq!(sdb.all("SELECT * FROM cotton_changes",&[]).unwrap(),before); assert!(read_row(&sdb,"cotton_notes","note").unwrap().is_some()); assert!(read_row(&ddb,"cotton_notes","note").unwrap().is_none());}const AT: &str = "2026-10-14T10:00:00.000Z";fn no_audit(_: &str, _: &str) {}/// RULE: Rehoming a repository keeps its ID and carries its owned rows through the same filing verb, because pulls and bundles still name the original repository.#[test]fn a_repository_moves_with_its_owned_rows_and_keeps_its_id() { let (sdb, sf, so) = file("00000000-0000-4000-8000-000000000701"); let (ddb, df, do_) = file("00000000-0000-4000-8000-000000000702"); let services = ToolServices::none(); let mut source = context(&sdb, &sf, &so, &services); let mut destination = context(&ddb, &df, &do_, &services); insert_row(&mut source, "cotton_repos", "stable-repository", serde_json::from_value(json!({ "key":"git.example.test/team/example", "host":"git.example.test", "owner":"team", "name":"example", "kind":"forgejo", "web_url":"https://git.example.test/team/example", "held":true, "created_at":AT, "updated_at":AT, })).unwrap()).unwrap(); insert_row(&mut source, "cotton_repo_issues", "stable-issue", serde_json::from_value(json!({ "repo_id":"stable-repository", "number":1, "title":"A synthetic issue", "body":"Keep these words", "state":"open", "created_at":AT, "updated_at":AT, })).unwrap()).unwrap(); source.commit().unwrap(); move_batch(&mut source, &mut destination, &["stable-repository".into()], "Project").unwrap(); assert!(read_row(&sdb, "cotton_repos", "stable-repository").unwrap().is_none()); assert!(read_row(&sdb, "cotton_repo_issues", "stable-issue").unwrap().is_none()); let repo = read_row(&ddb, "cotton_repos", "stable-repository").unwrap().unwrap(); assert_eq!(repo["id"], "stable-repository"); assert_eq!(repo["workspace_id"], df.id().as_str()); let issue = read_row(&ddb, "cotton_repo_issues", "stable-issue").unwrap().unwrap(); assert_eq!(issue["repo_id"], "stable-repository"); assert_eq!(issue["body"], "Keep these words");}fn file(id: &str) -> (Driver, cotton_store::file_identity::FileIdentity, String) { let owner = "00000000-0000-4000-8000-0000000000aa".to_owned(); let db = cotton_store::test_template::create_workspace_in_memory_for_test(&Meta { workspace_id: id.to_owned(), owner_id: owner.clone(), device_id: format!("device:{id}"), last_server_seq: 0, last_hlc: None, }) .unwrap(); db.run( "INSERT INTO cotton_workspaces (id,name,owner_user_id,created_at,_hlc) VALUES (?,?,?,?,'')", &[ SqlValue::Text(id.to_owned()), SqlValue::Text(id.to_owned()), SqlValue::Text(owner.clone()), SqlValue::Text(AT.to_owned()), ], ) .unwrap(); db.run( "UPDATE cotton_workspaces SET kind = ? WHERE id = ?", &[ SqlValue::Text( if id.ends_with('1') { "personal" } else { "shared" } .into(), ), SqlValue::Text(id.into()), ], ) .unwrap(); let identity = read_file_identity(&db).unwrap().unwrap(); (db, identity, owner)}fn context<'a>( db: &'a Driver, file: &'a cotton_store::file_identity::FileIdentity, owner: &'a str, services: &'a ToolServices,) -> Ctx<'a> { Ctx::for_worker( db, file, owner, job("synthetic"), "job:synthetic", chrono::DateTime::parse_from_rfc3339(AT) .unwrap() .with_timezone(&Utc), Arc::new(SystemRandom), services, &no_audit, )}/// RULE: A cross-file move retains the record's identity and provenance plus its comments, links and person reference, because every one remains a reader of the original id after filing.#[test]fn move_keeps_note_comments_links_and_people_reference() { let (source_db, source_file, source_owner) = file("00000000-0000-4000-8000-000000000101"); let (dest_db, dest_file, dest_owner) = file("00000000-0000-4000-8000-000000000102"); let services = ToolServices::none(); let mut source = context(&source_db, &source_file, &source_owner, &services); insert_row( &mut source, "cotton_people", "person-1", Map::from_iter([("full_name".into(), json!("Maya"))]), ) .unwrap(); insert_row( &mut source, "cotton_notes", "note-1", Map::from_iter([ ("title".into(), json!("Keep")), ("body".into(), json!("body")), ("author_label".into(), json!("Maya")), ("author_kind".into(), json!("agent")), ("created_at".into(), json!("2020-01-01T00:00:00.000Z")), ("updated_at".into(), json!(AT)), ]), ) .unwrap(); insert_row( &mut source, "cotton_comments", "comment-1", Map::from_iter([ ("record_kind".into(), json!("note")), ("record_id".into(), json!("note-1")), ("body".into(), json!("yes")), ("author_kind".into(), json!("agent")), ("author_label".into(), json!("Maya")), ]), ) .unwrap(); insert_row( &mut source, "cotton_links", "link-1", Map::from_iter([ ("from_kind".into(), json!("note")), ("from_id".into(), json!("note-1")), ("to_kind".into(), json!("person")), ("to_id".into(), json!("person-1")), ("created_at".into(), json!(AT)), ]), ) .unwrap(); source.commit().unwrap(); let mut dest = context(&dest_db, &dest_file, &dest_owner, &services); let answer = move_batch(&mut source, &mut dest, &["note-1".into()], "Project").unwrap(); assert_eq!(answer["changes"][0]["undoable"], true); let note = read_row(&dest_db, "cotton_notes", "note-1") .unwrap() .unwrap(); assert_eq!(note["author_label"], "Maya"); assert_eq!(note["created_at"], "2020-01-01T00:00:00.000Z"); assert!( read_row(&dest_db, "cotton_comments", "comment-1") .unwrap() .is_some() ); assert!( read_row(&dest_db, "cotton_links", "link-1") .unwrap() .is_some() ); assert_eq!( lookup_moved(&source_db, "note-1").unwrap().unwrap()["to_name"], "Project" ); let change_id = answer["changes"][0]["id"].as_str().unwrap().to_owned(); queue_undo(&mut source, &mut dest, &change_id).unwrap(); dest.commit().unwrap(); source.commit().unwrap(); assert!( read_row(&source_db, "cotton_notes", "note-1") .unwrap() .is_some() ); assert!(lookup_moved(&source_db, "note-1").unwrap().is_none());}/// RULE: A destination collision refuses the whole batch before either writer queues a receipt, because partial filing would split one record's readers.#[test]fn batch_collision_writes_neither_home() { let (source_db, source_file, source_owner) = file("00000000-0000-4000-8000-000000000201"); let (dest_db, dest_file, dest_owner) = file("00000000-0000-4000-8000-000000000202"); let services = ToolServices::none(); let mut source = context(&source_db, &source_file, &source_owner, &services); let mut dest = context(&dest_db, &dest_file, &dest_owner, &services); for id in ["note-a", "note-b"] { insert_row( &mut source, "cotton_notes", id, Map::from_iter([("title".into(), json!(id)), ("body".into(), json!(id))]), ) .unwrap(); } source.commit().unwrap(); insert_row( &mut dest, "cotton_notes", "note-b", Map::from_iter([ ("title".into(), json!("there")), ("body".into(), json!("there")), ]), ) .unwrap(); dest.commit().unwrap(); assert!( move_batch( &mut source, &mut dest, &["note-a".into(), "note-b".into()], "Project" ) .is_err() ); assert!( read_row(&source_db, "cotton_notes", "note-a") .unwrap() .is_some() ); assert!( read_row(&dest_db, "cotton_notes", "note-a") .unwrap() .is_none() );}/// RULE: A batch has one Undo that returns every member, and later moves keep the first origin, because callers must not split a filing action or lose its provenance.#[test]fn batch_undo_and_following_move_keep_the_original_home() { let (a, af, ao) = file("00000000-0000-4000-8000-000000000501"); let (b, bf, bo) = file("00000000-0000-4000-8000-000000000502"); let (c, cf, co) = file("00000000-0000-4000-8000-000000000503"); let services = ToolServices::none(); let mut source = context(&a, &af, &ao, &services); let mut destination = context(&b, &bf, &bo, &services); let mut last = context(&c, &cf, &co, &services); for id in ["batch-a", "batch-b"] { insert_row( &mut source, "cotton_notes", id, Map::from_iter([("title".into(), json!(id)), ("body".into(), json!(id))]), ) .unwrap(); } source.commit().unwrap(); let moved = move_batch( &mut source, &mut destination, &["batch-a".into(), "batch-b".into()], "B", ) .unwrap(); queue_undo( &mut source, &mut destination, moved["change"]["id"].as_str().unwrap(), ) .unwrap(); source.commit().unwrap(); destination.commit().unwrap(); for id in ["batch-a", "batch-b"] { assert!(read_row(&a, "cotton_notes", id).unwrap().is_some()); assert!(read_row(&b, "cotton_notes", id).unwrap().is_none()); } move_batch(&mut source, &mut destination, &["batch-a".into()], "B").unwrap(); move_batch(&mut destination, &mut last, &["batch-a".into()], "C").unwrap(); let redirect = lookup_moved(&b, "batch-a").unwrap().unwrap(); assert_eq!(redirect["to"], cf.id().as_str()); assert_eq!(redirect["origin"]["database"], af.id().as_str());}/// RULE: A moved thread carries its required contact context without taking the contact away from other source threads; the destination must materialize the whole record.#[test]fn thread_keeps_required_contact_and_all_messages() { let (a, af, ao) = file("00000000-0000-4000-8000-000000000601"); let (b, bf, bo) = file("00000000-0000-4000-8000-000000000602"); let services = ToolServices::none(); let mut source = owner_context(&a, &af, &ao, &services); let mut destination = context(&b, &bf, &bo, &services); insert_row( &mut source, "cotton_msg_contacts", "contact", Map::from_iter([("display_name".into(), json!("Invented contact")), ("phone_e164".into(), json!("14155550123"))]), ) .unwrap(); insert_row( &mut source, "cotton_msg_threads", "thread", Map::from_iter([ ("contact_id".into(), json!("contact")), ("title".into(), json!("Conversation")), ]), ) .unwrap(); insert_row( &mut source, "cotton_msg_messages", "message", Map::from_iter([ ("thread_id".into(), json!("thread")), ("direction".into(), json!("in")), ("channel".into(), json!("whatsapp")), ("origin".into(), json!("imported")), ("body".into(), json!("An invented message")), ("sent_at".into(), json!(AT)), ]), ) .unwrap(); source.commit().unwrap(); admit_contact(&b, bf.id().as_str(), "14155550123"); move_batch(&mut source, &mut destination, &["thread".into()], "B").unwrap(); assert!( read_row(&a, "cotton_msg_messages", "message") .unwrap() .is_none() ); assert_eq!( read_row(&b, "cotton_msg_messages", "message") .unwrap() .unwrap()["body"], "An invented message" ); assert!( read_row(&a, "cotton_msg_threads", "thread") .unwrap() .is_none() ); assert!( read_row(&a, "cotton_msg_contacts", "contact") .unwrap() .is_some() ); assert_eq!( read_row(&b, "cotton_msg_threads", "thread") .unwrap() .unwrap()["contact_id"], "contact" ); assert!( read_row(&b, "cotton_msg_contacts", "contact") .unwrap() .is_some() ); // RULE: A repeat import appends to the already-filed thread without replacing // its root or earlier messages; ordinary moves still refuse that collision. let original = read_row(&b, "cotton_msg_threads", "thread") .unwrap() .unwrap(); a.run("INSERT INTO cotton_msg_threads (id,workspace_id,contact_id,title,created_at,updated_at,_hlc) VALUES ('thread','00000000-0000-4000-8000-000000000601','contact','Reimported',?,?,'')", &[SqlValue::Text(AT.into()), SqlValue::Text(AT.into())]).unwrap(); a.run("INSERT INTO cotton_msg_messages (id,workspace_id,thread_id,direction,channel,origin,body,sent_at,created_at,_hlc) VALUES ('later','00000000-0000-4000-8000-000000000601','thread','in','whatsapp','imported','Later arrival',?,?,'')", &[SqlValue::Text(AT.into()),SqlValue::Text(AT.into())]).unwrap(); assert!(queue_batch(&mut source, &mut destination, &["thread".into()], "B").is_err()); let answer = queue_arrival(&mut source, &mut destination, "thread", "B").unwrap(); destination.commit().unwrap(); source.commit().unwrap(); assert_eq!( read_row(&b, "cotton_msg_threads", "thread") .unwrap() .unwrap(), original ); assert!( read_row(&b, "cotton_msg_messages", "message") .unwrap() .is_some() ); assert_eq!( read_row(&b, "cotton_msg_messages", "later") .unwrap() .unwrap()["body"], "Later arrival" ); assert!( read_row(&a, "cotton_msg_threads", "thread") .unwrap() .is_none() ); assert!( queue_undo( &mut source, &mut destination, answer["change"]["id"].as_str().unwrap() ) .is_err() );}fn admit_contact(db: &Driver, database: &str, phone: &str) { use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use cotton_core::keys::{statements, PersonKey}; let owner = PersonKey::from_seed([47; 32].into()); let key = owner.public_key(); let text = |s: &str| SqlValue::Text(s.into()); let admission = URL_SAFE_NO_PAD.encode(owner.sign(&statements::admission_bytes(database, &key, "owner", None, AT))); db.run("INSERT INTO cotton_admissions(id,workspace_id,person_key,person_name,rights,admitted_by_person,p_sig,admitted_at,_hlc) VALUES('synthetic-owner',?,?,'Invented contact','owner',?,?,?,'1700000000000:0001:serving')", &[text(database),text(&key),text(&key),text(&admission),text(AT)]).unwrap(); db.run("INSERT INTO cotton_people(id,workspace_id,full_name,created_at,updated_at) VALUES('admitted-person',?,'Invented contact',?,?)", &[text(database),text(AT),text(AT)]).unwrap(); db.run("INSERT INTO cotton_person_handles(id,workspace_id,person_id,kind,value,normalised,source,created_at,updated_at) VALUES('admitted-handle',?,'admitted-person','phone',?,?,'manual',?,?)", &[text(database),text(phone),text(phone),text(AT),text(AT)]).unwrap(); let claim = cotton_store::member_person::claim_bytes(database,"synthetic-owner",&key,"admitted-person",AT); let signature = URL_SAFE_NO_PAD.encode(owner.sign(&claim)); db.run("INSERT INTO cotton_member_person_claims(id,workspace_id,admission_id,person_key,person_id,p_sig,claimed_at,_hlc) VALUES('synthetic-claim',?,'synthetic-owner',?,'admitted-person',?,?,'1700000000000:0002:serving')", &[text(database),text(&key),text(&signature),text(AT)]).unwrap();}fn owner_context<'a>(db: &'a Driver, file: &'a cotton_store::file_identity::FileIdentity, owner: &'a str, services: &'a ToolServices) -> Ctx<'a> { use cotton_store::actor::{Actor, DeviceId}; let text = |s: &str| SqlValue::Text(s.into()); db.run("INSERT OR IGNORE INTO cotton_devices(id,workspace_id,device_id,name,approved_at,approved_by_device,created_at,updated_at) VALUES('synthetic-phone',?,'synthetic-phone','Invented Phone',?,'synthetic-phone',?,?)", &[text(file.id().as_str()),text(AT),text(AT),text(AT)]).unwrap(); Ctx::for_worker(db,file,owner,Actor::owner(DeviceId::in_file(db,"synthetic-phone").unwrap()),"user",chrono::DateTime::parse_from_rfc3339(AT).unwrap().with_timezone(&Utc),Arc::new(SystemRandom),services,&no_audit)}/// RULE: A job cannot file a chat even when every participant is admitted; only an owner act can move it, while a departure may return only its proven Personal-origin records.#[test]fn jobs_need_owner_approval_and_departures_prove_the_origin() { let (sdb,sf,so)=file("00000000-0000-4000-8000-000000000811"); let (ddb,df,do_)=file("00000000-0000-4000-8000-000000000812"); let services=ToolServices::none(); let mut source=context(&sdb,&sf,&so,&services); let mut dest=context(&ddb,&df,&do_,&services); insert_row(&mut source,"cotton_msg_contacts","contact",serde_json::from_value(json!({"display_name":"Invented Sender","phone_e164":"14155550123"})).unwrap()).unwrap(); insert_row(&mut source,"cotton_msg_threads","thread",serde_json::from_value(json!({"kind":"dm","contact_id":"contact"})).unwrap()).unwrap(); source.commit().unwrap(); admit_contact(&ddb,df.id().as_str(),"14155550123"); assert!(queue_batch(&mut source,&mut dest,&["thread".into()],"Project").unwrap_err().to_string().contains("confirmation")); assert_eq!(source.writer().unwrap().pending(),0);assert_eq!(dest.writer().unwrap().pending(),0); source=owner_context(&sdb,&sf,&so,&services); let moved=move_batch(&mut source,&mut dest,&["thread".into()],"Project").unwrap(); source=context(&sdb,&sf,&so,&services); assert!(queue_undo(&mut source,&mut dest,moved["changes"][0]["id"].as_str().or_else(||moved["change"]["id"].as_str()).unwrap()).unwrap_err().to_string().contains("confirmation")); queue_return(&mut dest,&mut source,&["thread".into()],"Personal").unwrap(); source.commit().unwrap();dest.commit().unwrap(); assert!(read_row(&sdb,"cotton_msg_threads","thread").unwrap().is_some()); assert!(read_row(&ddb,"cotton_msg_threads","thread").unwrap().is_none()); insert_row(&mut dest,"cotton_notes","native-project-note",serde_json::from_value(json!({"title":"Born here"})).unwrap()).unwrap();dest.commit().unwrap(); assert!(queue_return(&mut dest,&mut source,&["native-project-note".into()],"Personal").is_err()); assert_eq!(source.writer().unwrap().pending(),0);assert_eq!(dest.writer().unwrap().pending(),0);}/// RULE: A photo requires the owner's explicit filing act even without participants; a face tag never grants a job permission to choose its home.#[test]fn a_photo_cannot_be_filed_by_a_job() { let (sdb,sf,so)=file("00000000-0000-4000-8000-000000000821"); let (ddb,df,do_)=file("00000000-0000-4000-8000-000000000822"); let services=ToolServices::none(); let mut source=context(&sdb,&sf,&so,&services); let mut dest=context(&ddb,&df,&do_,&services); let sha="1".repeat(64); let photo=format!("photo:{sha}"); insert_row(&mut source,"cotton_photos",&photo,serde_json::from_value(json!({"key":format!("{}/photos/{sha}.jpg",sf.id().as_str()),"sha256":sha,"content_type":"image/jpeg","size_bytes":0,"taken_at":AT,"caption":"A whiteboard without people"})).unwrap()).unwrap(); source.commit().unwrap(); let error=queue_batch(&mut source,&mut dest,&[photo],"Project").unwrap_err(); assert!(error.to_string().contains("confirmation"),"{error}"); assert_eq!(source.writer().unwrap().pending(),0);assert_eq!(dest.writer().unwrap().pending(),0);}/// RULE: Filing is an act on the whole chat, never an individual message, so choosing a message id cannot bypass the participant admission check or split the conversation's history.#[test]fn even_the_owner_files_the_chat_rather_than_one_message() { let (sdb,sf,so)=file("00000000-0000-4000-8000-000000000831"); let (ddb,df,do_)=file("00000000-0000-4000-8000-000000000832"); let services=ToolServices::none(); let mut source=owner_context(&sdb,&sf,&so,&services); let mut dest=context(&ddb,&df,&do_,&services); insert_row(&mut source,"cotton_msg_threads","thread",serde_json::from_value(json!({"kind":"group","title":"Whole chat"})).unwrap()).unwrap(); insert_row(&mut source,"cotton_msg_messages","message",serde_json::from_value(json!({"thread_id":"thread","direction":"in","channel":"whatsapp","origin":"imported","body":"Keep this in its chat","sent_at":AT})).unwrap()).unwrap(); source.commit().unwrap(); assert!(queue_batch(&mut source,&mut dest,&["message".into()],"Project").unwrap_err().to_string().contains("whole chat")); assert_eq!(source.writer().unwrap().pending(),0);assert_eq!(dest.writer().unwrap().pending(),0); assert!(read_row(&sdb,"cotton_msg_messages","message").unwrap().is_some()); assert!(read_row(&ddb,"cotton_msg_messages","message").unwrap().is_none());}/// RULE: A connector resolves a filed chat through Personal's retained move history before reserving a new root; an older thread id stays its identity even when the connection supplies a different reservation.#[test]fn a_connector_resolves_a_filed_counterpart_before_reserving_a_second_chat() { let (sdb, sf, so) = file("00000000-0000-4000-8000-000000000871"); let (ddb, df, do_) = file("00000000-0000-4000-8000-000000000872"); let services = ToolServices::none(); let mut source = owner_context(&sdb, &sf, &so, &services); let mut dest = context(&ddb, &df, &do_, &services); insert_row( &mut source, "cotton_msg_contacts", "contact", serde_json::from_value( json!({"display_name":"Invented Sender","phone_e164":"14155550123"}), ) .unwrap(), ) .unwrap(); insert_row( &mut source, "cotton_msg_threads", "older-thread", serde_json::from_value(json!({"kind":"dm","contact_id":"contact"})).unwrap(), ) .unwrap(); source.commit().unwrap(); admit_contact(&ddb, df.id().as_str(), "14155550123"); let batch=serde_json::from_value(json!({"workspaceId":sf.id().as_str(),"channel":"imessage","newThreadId":"connection-reservation","dm":{"label":"Invented Sender","phone":"14155550123"},"messages":[{"direction":"in","phone":"14155550123","externalId":"alias-arrival","sentAt":AT,"body":"The same chat, now filed"}]})).unwrap(); assert_eq!( cotton_client::messages_import::resolve_thread(&sdb, &batch) .unwrap() .as_deref(), Some("older-thread") ); move_batch(&mut source, &mut dest, &["older-thread".into()], "Project").unwrap(); assert_eq!( cotton_client::messages_import::resolve_thread(&sdb, &batch) .unwrap() .as_deref(), Some("older-thread") ); assert!(read_row(&sdb, "cotton_msg_threads", "older-thread") .unwrap() .is_none()); let clock = cotton_client::seams::FixedClock::new( chrono::DateTime::parse_from_rfc3339(AT) .unwrap() .with_timezone(&Utc), ); let random = cotton_client::seams::SeededRandom::default(); let mut ids = cotton_store::protocol::ulid::Monotonic::new(|bytes: &mut [u8]| bytes.fill(7)); let refused = cotton_client::messages_import::import_messages_as( &sdb, sf.namespace(), &batch, &clock, &random, &mut ids, cotton_store::mint::Held::Served, ) .unwrap_err(); assert!(refused.to_string().contains("already filed"), "{refused}"); assert!(sdb .get( "SELECT id FROM cotton_msg_messages WHERE external_id='alias-arrival'", &[] ) .unwrap() .is_none()); insert_row( &mut source, "cotton_msg_threads", "conflicting-personal-thread", serde_json::from_value(json!({"kind":"dm","contact_id":"contact"})).unwrap(), ) .unwrap(); source.commit().unwrap(); assert!(cotton_client::messages_import::resolve_thread(&sdb, &batch) .unwrap_err() .contains("more than one chat home"));}/// RULE: A filed group keeps its original root id when a connector names its source key with a new reservation; resolving that key must not resurrect the group in Personal.#[test]fn a_connector_resolves_a_filed_group_by_its_existing_source_key() { let (sdb, sf, so) = file("00000000-0000-4000-8000-000000000881"); let (ddb, df, do_) = file("00000000-0000-4000-8000-000000000882"); let services = ToolServices::none(); let mut source = owner_context(&sdb, &sf, &so, &services); let mut dest = context(&ddb, &df, &do_, &services); insert_row( &mut source, "cotton_msg_contacts", "contact", serde_json::from_value( json!({"display_name":"Invented Sender","phone_e164":"14155550123"}), ) .unwrap(), ) .unwrap(); insert_row(&mut source,"cotton_msg_threads","older-group",serde_json::from_value(json!({"kind":"group","title":"Invented Group","external_key":"wa:group:invented@g.us"})).unwrap()).unwrap(); insert_row(&mut source,"cotton_msg_thread_participants","seat",serde_json::from_value(json!({"thread_id":"older-group","contact_id":"contact","display_name":"Invented Sender"})).unwrap()).unwrap(); source.commit().unwrap(); admit_contact(&ddb, df.id().as_str(), "14155550123"); let batch=serde_json::from_value(json!({"workspaceId":sf.id().as_str(),"channel":"whatsapp","newThreadId":"connection-reservation","thread":{"kind":"group","title":"Invented Group","externalKey":"wa:group:invented@g.us","participants":[{"label":"Invented Sender","phone":"14155550123"}]},"messages":[{"direction":"in","senderLabel":"Invented Sender","phone":"14155550123","externalId":"group-alias-arrival","sentAt":AT,"body":"The group remains filed"}]})).unwrap(); assert_eq!( cotton_client::messages_import::resolve_thread(&sdb, &batch) .unwrap() .as_deref(), Some("older-group") ); move_batch(&mut source, &mut dest, &["older-group".into()], "Project").unwrap(); assert_eq!( cotton_client::messages_import::resolve_thread(&sdb, &batch) .unwrap() .as_deref(), Some("older-group") ); assert!(read_row(&sdb, "cotton_msg_threads", "older-group") .unwrap() .is_none());}