1194 lines · 37.0 KB · RustRaw
//! `apps/cotton/__tests__/ingest/messagesSource.test.ts`, over a synthetic//! pair of files.//!//! It asserts the DECISIONS, not the SQL: which channel a service label is,//! which conversation a row belongs to, what a tapback becomes, and which of//! two files wins when both hold the same message.use super::*;use crate::ingest::sources::messages_fixture::{ attributed_body_for, write_messages_fixture, FixtureAttachment, FixtureChat, FixtureMessage,};const WORKSPACE: &str = "11111111-1111-4111-8111-111111111111";const DEVICE: &str = "Mac (test)";fn temp_home() -> (tempfile::TempDir, Layout) { let dir = tempfile::tempdir().expect("a temp home"); let layout = Layout::new(dir.path()); (dir, layout)}fn mac_file(path: &std::path::Path) -> MessagesFile { MessagesFile { id: MessagesFileId::Mac, path: path.to_path_buf(), }}struct Pass { batches: Vec<ImportBatch>, media: Vec<IngestMediaFile>, cursor: String, notes: IngestNotes,}impl Pass { fn messages(&self) -> Vec<&EnvelopeMessage> { self.batches .iter() .flat_map(|batch| batch.messages.iter()) .collect() } fn ids(&self) -> Vec<String> { let mut ids: Vec<String> = self .messages() .iter() .map(|message| message.external_id.clone()) .collect(); ids.sort(); ids }}fn source_for(home: &std::path::Path, layout: &Layout, files: Vec<MessagesFile>) -> MessagesSource { MessagesSource::new(MessagesSourceInput { workspace_id: WORKSPACE.to_string(), namespace: crate::test_support::namespace().clone(), layout: layout.clone(), device: DEVICE.to_string(), home: home.to_path_buf(), country_code: None, files: Some(files), })}fn read_all( home: &std::path::Path, layout: &Layout, files: Vec<MessagesFile>, cursor: Option<&str>,) -> Pass { let source = source_for(home, layout, files); let mut batches: Vec<ImportBatch> = Vec::new(); let mut media: Vec<IngestMediaFile> = Vec::new(); let outcome = source .read(cursor, &mut |batch: IngestBatch| { if let IngestWork::Messages { request, media: files, } = batch.work { batches.push(*request); media.extend(files); } Ok(()) }) .expect("a pass"); Pass { batches, media, cursor: outcome.cursor, notes: outcome.notes, }}/// A real file under this test's fake home, named as Apple names it.fn write_attachment(home: &std::path::Path, relative: &str) -> String { let path = home.join(relative); std::fs::create_dir_all(path.parent().expect("a folder")).expect("the folder"); std::fs::write(&path, [9u8; 24]).expect("the bytes"); format!("~/{relative}")}fn attachment(filename: &str, mime: &str, name: &str) -> FixtureAttachment { FixtureAttachment { filename: filename.to_string(), mime_type: Some(mime.to_string()), transfer_name: Some(name.to_string()), }}fn service_of(message: &EnvelopeMessage) -> String { message.meta.as_ref().expect("provenance")["source"]["service"] .as_str() .expect("a label") .to_string()}#[test]fn it_reads_a_one_to_one_imessage_conversation_both_directions_into_one_dm_batch() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000001", "+919000000001", "iMessage", )], &[ FixtureMessage::new( "GUID-IN-1", "iMessage", "2026-08-01T09:00:00.000Z", "iMessage;-;+919000000001", ) .text("are you free at four") .handle("+919000000001"), FixtureMessage::new( "GUID-OUT-1", "iMessage", "2026-08-01T09:01:00.000Z", "iMessage;-;+919000000001", ) .text("four works") .outbound(), ], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert_eq!(pass.batches.len(), 1); let batch = &pass.batches[0]; assert_eq!(batch.channel, "imessage"); assert_eq!( batch.dm.as_ref().expect("a counterpart").phone.as_deref(), Some("919000000001") ); assert!(batch.thread.is_none()); assert_eq!( batch .messages .iter() .map(|message| message.direction) .collect::<Vec<_>>(), vec![Direction::In, Direction::Out] ); assert_eq!( batch .messages .iter() .map(|message| message.body.as_str()) .collect::<Vec<_>>(), vec!["are you free at four", "four works"] ); assert_eq!(batch.messages[0].external_id, "GUID-IN-1");}#[test]fn sms_and_imessage_from_one_person_are_two_batches_both_naming_that_person() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[ FixtureChat::dm("iMessage;-;+919000000002", "+919000000002", "iMessage"), FixtureChat::dm("SMS;-;+919000000002", "+919000000002", "SMS"), ], &[ FixtureMessage::new( "G-IM", "iMessage", "2026-08-02T09:00:00.000Z", "iMessage;-;+919000000002", ) .text("over imessage") .handle("+919000000002"), FixtureMessage::new( "G-SMS", "SMS", "2026-08-02T09:05:00.000Z", "SMS;-;+919000000002", ) .text("over sms") .handle("+919000000002"), ], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); let mut channels: Vec<&str> = pass .batches .iter() .map(|batch| batch.channel.as_str()) .collect(); channels.sort_unstable(); assert_eq!(channels, vec!["imessage", "sms"]); // ONE person, so both batches reserve the SAME thread id — a contact has // one dm thread whatever they message over. let threads: std::collections::BTreeSet<&String> = pass .batches .iter() .filter_map(|batch| batch.new_thread_id.as_ref()) .collect(); assert_eq!(threads.len(), 1); assert!(pass .batches .iter() .all(|batch| batch.dm.as_ref().and_then(|dm| dm.phone.as_deref()) == Some("919000000002")));}#[test]fn mms_and_rcs_are_the_sms_channel_and_the_label_survives_in_provenance() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "SMS;-;+919000000003", "+919000000003", "SMS", )], &[ FixtureMessage::new( "G-MMS", "MMS", "2026-08-03T09:00:00.000Z", "SMS;-;+919000000003", ) .text("a picture caption") .handle("+919000000003"), FixtureMessage::new( "G-RCS", "RCS", "2026-08-03T09:01:00.000Z", "SMS;-;+919000000003", ) .text("and a follow-up") .handle("+919000000003"), ], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert_eq!(pass.batches.len(), 1); assert_eq!(pass.batches[0].channel, "sms"); let mut services: Vec<String> = pass.messages().iter().map(|m| service_of(m)).collect(); services.sort(); assert_eq!(services, vec!["MMS".to_string(), "RCS".to_string()]);}#[test]fn every_message_carries_its_service_device_external_id_and_which_file_it_came_from() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000004", "+919000000004", "iMessage", )], &[FixtureMessage::new( "G-PROV", "iMessage", "2026-08-04T09:00:00.000Z", "iMessage;-;+919000000004", ) .text("hello") .handle("+919000000004")], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); let meta = pass.messages()[0].meta.as_ref().expect("provenance"); assert_eq!( serde_json::Value::Object(meta.clone()), serde_json::json!({ "source": { "service": "iMessage", "device": DEVICE, "externalId": "G-PROV", "file": "mac", "senderHandle": {"kind": "phone", "value": "919000000004"}, } }) );}#[test]fn it_reads_the_body_out_of_the_blob_when_the_text_column_is_null() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000005", "+919000000005", "iMessage", )], &[FixtureMessage::new( "G-ATTR", "iMessage", "2026-08-05T09:00:00.000Z", "iMessage;-;+919000000005", ) .blob(attributed_body_for("the body was only ever in the blob")) .handle("+919000000005")], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert_eq!( pass.messages()[0].body, "the body was only ever in the blob" );}#[test]fn a_group_becomes_a_thread_block_with_its_members_and_per_message_sender_labels() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::group( "iMessage;+;chat900001", "chat900001", "iMessage", &["+919000000006", "+919000000007"], ) .named("Saturday plan")], &[ FixtureMessage::new( "G-GRP-1", "iMessage", "2026-08-06T09:00:00.000Z", "iMessage;+;chat900001", ) .text("who is driving") .handle("+919000000006"), FixtureMessage::new( "G-GRP-2", "iMessage", "2026-08-06T09:02:00.000Z", "iMessage;+;chat900001", ) .text("I can") .outbound(), ], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert_eq!(pass.batches.len(), 1); let thread = pass.batches[0].thread.as_ref().expect("a group block"); assert_eq!(thread.kind, "group"); assert_eq!(thread.title, "Saturday plan"); assert_eq!(thread.external_key, "iMessage;+;chat900001"); let mut phones: Vec<&str> = thread .participants .iter() .filter_map(|participant| participant.phone.as_deref()) .collect(); phones.sort_unstable(); assert_eq!(phones, vec!["919000000006", "919000000007"]); // The owner's own message names no sender: the engine reads a null sender // as "you", and inventing a label for it would make the owner a member row. assert_eq!( pass.messages() .iter() .map(|message| message.sender_label.as_deref()) .collect::<Vec<_>>(), vec![Some("+919000000006"), None] );}/// RULE: A text that arrived carries the number it came from on its own row (meta.source.senderHandle), so a group's sender gets a seat and a text keeps its sender whatever names the chat; a word sender id or the owner's own text carries none.#[test]fn a_text_that_arrived_carries_the_number_it_came_from() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[ FixtureChat::group("SMS;+;chat900002", "chat900002", "SMS", &["+919000000016", "+919000000017"]), FixtureChat::dm("SMS;-;AX-INVENT", "AX-INVENT", "SMS"), ], &[ FixtureMessage::new("G-SMS-1", "SMS", "2026-08-06T09:00:00.000Z", "SMS;+;chat900002") .text("on my way") .handle("+919000000016"), FixtureMessage::new("G-SMS-2", "SMS", "2026-08-06T09:01:00.000Z", "SMS;+;chat900002") .text("see you") .outbound(), FixtureMessage::new("G-SMS-3", "SMS", "2026-08-06T09:02:00.000Z", "SMS;-;AX-INVENT") .text("Your parcel is out for delivery") .handle("AX-INVENT"), ], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); let sender = |guid: &str| { pass.messages() .iter() .find(|message| message.external_id == guid) .and_then(|message| message.meta.as_ref()) .and_then(|meta| meta.get("source")) .and_then(|source| source.get("senderHandle")) .cloned() }; assert_eq!(sender("G-SMS-1"), Some(serde_json::json!({"kind": "phone", "value": "919000000016"}))); assert_eq!(sender("G-SMS-2"), None, "the owner's own text names no sender"); assert_eq!(sender("G-SMS-3"), None, "a sender id that is a word is nobody's number"); // The handle is one the import engine reads back as the sender's. let seat: cotton_core::import::envelope::EnvelopeHandle = serde_json::from_value(sender("G-SMS-1").unwrap()).expect("an envelope handle"); assert_eq!(seat.value, "919000000016");}#[test]fn an_unnamed_group_is_titled_by_its_members() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::group( "iMessage;+;chat900002", "chat900002", "iMessage", &["+919000000008", "+919000000009"], )], &[FixtureMessage::new( "G-GRP-3", "iMessage", "2026-08-07T09:00:00.000Z", "iMessage;+;chat900002", ) .text("still on?") .handle("+919000000008")], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert_eq!( pass.batches[0].thread.as_ref().expect("a group").title, "+919000000008, +919000000009" );}#[test]fn an_apple_id_handle_is_an_email_identity_and_never_an_unknown() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;someone@example.com", "someone@example.com", "iMessage", )], &[FixtureMessage::new( "G-EMAIL", "iMessage", "2026-08-08T09:00:00.000Z", "iMessage;-;someone@example.com", ) .text("sent from a mac") .handle("someone@example.com")], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); let dm = pass.batches[0].dm.as_ref().expect("a counterpart"); assert_eq!(dm.label, "someone@example.com"); assert_eq!(dm.email.as_deref(), Some("someone@example.com")); assert_eq!(dm.phone, None);}#[test]fn an_alphanumeric_sender_id_stays_a_label_with_no_phone_and_no_email() { // Most handles in a real file are shaped like this: a bank, an OTP sender, // a delivery service. Forcing one into a phone-shaped contact would file a // bank under a number that is not a number. let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm("SMS;-;AD-EXAMPLE", "AD-EXAMPLE", "SMS")], &[FixtureMessage::new( "G-SHORT", "SMS", "2026-08-09T09:00:00.000Z", "SMS;-;AD-EXAMPLE", ) .text("your code is 000000") .handle("AD-EXAMPLE")], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); let dm = pass.batches[0].dm.as_ref().expect("a counterpart"); assert_eq!(dm.label, "AD-EXAMPLE"); assert_eq!(dm.phone, None); assert_eq!(dm.email, None); // And it reserves a stable id, so a second pass lands in the same thread // rather than making a new one for a counterpart nobody can be. let thread_id = pass.batches[0] .new_thread_id .as_deref() .expect("a reserved id"); assert_eq!(thread_id.len(), 36); assert!(thread_id.chars().all(|c| c.is_ascii_hexdigit() || c == '-'));}#[test]fn a_tapback_becomes_a_reaction_on_the_message_it_points_at_never_a_message() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000010", "+919000000010", "iMessage", )], &[ FixtureMessage::new( "G-TARGET", "iMessage", "2026-08-10T09:00:00.000Z", "iMessage;-;+919000000010", ) .text("shipped it") .outbound(), FixtureMessage::new( "G-TAPBACK", "iMessage", "2026-08-10T09:00:30.000Z", "iMessage;-;+919000000010", ) .handle("+919000000010") .tapback(2000, "p:0/G-TARGET"), ], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert_eq!(pass.batches[0].messages.len(), 1); assert_eq!(pass.batches[0].messages[0].external_id, "G-TARGET"); assert_eq!( pass.batches[0].messages[0].reactions.as_deref(), Some( [EnvelopeReaction { emoji: "❤️".to_string(), actor: "919000000010".to_string(), at: Some("2026-08-10T09:00:30.000Z".to_string()), }] .as_slice() ) );}#[test]fn a_tapback_removal_is_counted_and_writes_nothing_because_the_engine_has_no_delete() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000011", "+919000000011", "iMessage", )], &[ FixtureMessage::new( "G-T2", "iMessage", "2026-08-11T09:00:00.000Z", "iMessage;-;+919000000011", ) .text("done") .outbound(), FixtureMessage::new( "G-UNLIKE", "iMessage", "2026-08-11T09:01:00.000Z", "iMessage;-;+919000000011", ) .handle("+919000000011") .tapback(3001, "p:0/G-T2"), ], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert!(pass.batches[0].messages[0].reactions.is_none()); assert_eq!(pass.notes.get("tapbackRemoval"), Some(&1));}#[test]fn a_reply_travels_as_its_targets_id_with_the_part_prefix_stripped() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000012", "+919000000012", "iMessage", )], &[ FixtureMessage::new( "G-ORIGIN", "iMessage", "2026-08-12T09:00:00.000Z", "iMessage;-;+919000000012", ) .text("three options attached") .handle("+919000000012"), FixtureMessage::new( "G-REPLY", "iMessage", "2026-08-12T09:05:00.000Z", "iMessage;-;+919000000012", ) .text("the second one") .outbound() .reply_to("p:0/G-ORIGIN"), ], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); let reply = pass .messages() .into_iter() .find(|message| message.external_id == "G-REPLY") .expect("the reply"); assert_eq!(reply.reply_to_external_id.as_deref(), Some("G-ORIGIN"));}#[test]fn a_group_event_row_is_dropped_and_counted_rather_than_written_as_a_sentence_nobody_typed() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::group( "iMessage;+;chat900003", "chat900003", "iMessage", &["+919000000013"], )], &[ FixtureMessage::new( "G-RENAME", "iMessage", "2026-08-13T09:00:00.000Z", "iMessage;+;chat900003", ) .handle("+919000000013") .item_type(2), FixtureMessage::new( "G-AFTER", "iMessage", "2026-08-13T09:01:00.000Z", "iMessage;+;chat900003", ) .text("renamed it") .handle("+919000000013"), ], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert_eq!(pass.ids(), vec!["G-AFTER".to_string()]); assert_eq!(pass.notes.get("groupEvent"), Some(&1));}#[test]fn an_attachments_real_path_reaches_the_queue_with_the_type_apple_recorded() { let (dir, layout) = temp_home(); let filename = write_attachment( dir.path(), "Library/Messages/Attachments/ab/01/GUID/lake.jpeg", ); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000021", "+919000000021", "iMessage", )], &[FixtureMessage::new( "A-1", "iMessage", "2026-08-14T09:00:00.000Z", "iMessage;-;+919000000021", ) .text("look at this") .handle("+919000000021") .attach(attachment(&filename, "image/jpeg", "lake.jpeg"))], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert_eq!( pass.media, vec![IngestMediaFile { external_id: "A-1".to_string(), path: dir .path() .join("Library/Messages/Attachments/ab/01/GUID/lake.jpeg"), name: Some("lake.jpeg".to_string()), content_type: Some("image/jpeg".to_string()), }] );}#[test]fn a_message_that_is_only_an_attachment_is_kept_which_text_only_used_to_drop() { let (dir, layout) = temp_home(); let filename = write_attachment( dir.path(), "Library/Messages/Attachments/cd/02/GUID/clip.mov", ); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000022", "+919000000022", "iMessage", )], &[FixtureMessage::new( "A-2", "iMessage", "2026-08-14T09:01:00.000Z", "iMessage;-;+919000000022", ) .handle("+919000000022") .attach(attachment(&filename, "video/quicktime", "clip.mov"))], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert_eq!(pass.messages().len(), 1); assert_eq!(pass.messages()[0].external_id, "A-2"); assert_eq!(pass.messages()[0].body, ""); assert_eq!(pass.media.len(), 1); assert_eq!(pass.notes.get("noBody"), None);}#[test]fn an_attachment_this_mac_no_longer_holds_is_counted_and_its_message_still_lands() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000023", "+919000000023", "iMessage", )], &[FixtureMessage::new( "A-3", "iMessage", "2026-08-14T09:02:00.000Z", "iMessage;-;+919000000023", ) .text("sent you the file") .handle("+919000000023") .attach(FixtureAttachment { filename: "~/Library/Messages/Attachments/ff/09/GUID/never-here.pdf".to_string(), mime_type: Some("application/pdf".to_string()), transfer_name: None, })], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert!(pass.media.is_empty()); assert_eq!(pass.notes.get("media:notOnThisMac"), Some(&1)); assert_eq!(pass.ids(), vec!["A-3".to_string()]);}#[test]fn a_row_with_no_body_no_decodable_blob_and_no_file_is_dropped_and_says_which_it_was() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000014", "+919000000014", "iMessage", )], &[ FixtureMessage::new( "G-EMPTY", "iMessage", "2026-08-14T09:00:00.000Z", "iMessage;-;+919000000014", ) .handle("+919000000014"), FixtureMessage::new( "G-JUNK", "iMessage", "2026-08-14T09:01:00.000Z", "iMessage;-;+919000000014", ) .blob(vec![1, 2, 3, 4]) .handle("+919000000014"), ], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert!(pass.batches.is_empty()); assert_eq!(pass.notes.get("noBody"), Some(&1)); assert_eq!(pass.notes.get("bodyUndecodable"), Some(&1));}#[test]fn it_resumes_from_its_cursor_and_a_second_pass_over_an_unchanged_file_reads_nothing() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000016", "+919000000016", "iMessage", )], &[ FixtureMessage::new( "G-C1", "iMessage", "2026-08-16T09:00:00.000Z", "iMessage;-;+919000000016", ) .text("one") .handle("+919000000016"), FixtureMessage::new( "G-C2", "iMessage", "2026-08-16T09:01:00.000Z", "iMessage;-;+919000000016", ) .text("two") .handle("+919000000016"), ], ); let first = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert_eq!(first.messages().len(), 2); assert_eq!(parse_messages_cursor(Some(&first.cursor)).mac, 2); let second = read_all( dir.path(), &layout, vec![mac_file(&path)], Some(&first.cursor), ); assert!(second.batches.is_empty()); assert_eq!(parse_messages_cursor(Some(&second.cursor)).mac, 2);}#[test]fn it_reads_only_what_arrived_after_the_cursor() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000017", "+919000000017", "iMessage", )], &[ FixtureMessage::new( "G-OLD", "iMessage", "2026-08-17T09:00:00.000Z", "iMessage;-;+919000000017", ) .text("before") .handle("+919000000017"), FixtureMessage::new( "G-NEW", "iMessage", "2026-08-17T09:05:00.000Z", "iMessage;-;+919000000017", ) .text("after") .handle("+919000000017"), ], ); let pass = read_all( dir.path(), &layout, vec![mac_file(&path)], Some(r#"{"mac":1,"phone-backup":0,"backupUdid":null}"#), ); assert_eq!(pass.ids(), vec!["G-NEW".to_string()]);}#[test]fn a_cursor_that_will_not_parse_re_reads_everything_rather_than_guessing() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000018", "+919000000018", "iMessage", )], &[FixtureMessage::new( "G-ONLY", "iMessage", "2026-08-18T09:00:00.000Z", "iMessage;-;+919000000018", ) .text("still here") .handle("+919000000018")], ); let pass = read_all( dir.path(), &layout, vec![mac_file(&path)], Some("not json at all"), ); assert_eq!(pass.messages().len(), 1);}#[test]fn it_probes_ready_and_counts_the_rows_it_can_see() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000019", "+919000000019", "iMessage", )], &[ FixtureMessage::new( "G-P1", "iMessage", "2026-08-19T09:00:00.000Z", "iMessage;-;+919000000019", ) .text("a") .handle("+919000000019"), FixtureMessage::new( "G-P2", "iMessage", "2026-08-19T09:01:00.000Z", "iMessage;-;+919000000019", ) .text("b") .handle("+919000000019"), ], ); let source = source_for(dir.path(), &layout, vec![mac_file(&path)]); assert_eq!(source.probe(), SourceStatus::ready(2));}/* ---- what the move to Rust earned, beyond the TypeScript's own cases ---- */#[test]fn a_mac_with_no_messages_database_is_not_found_rather_than_an_error() { let (dir, layout) = temp_home(); let source = MessagesSource::new(MessagesSourceInput { workspace_id: WORKSPACE.to_string(), namespace: crate::test_support::namespace().clone(), layout, device: DEVICE.to_string(), home: dir.path().to_path_buf(), country_code: None, files: None, }); let status = source.probe(); assert_eq!(status.state, crate::ingest::SourceState::NotFound); assert!(status .detail .expect("a sentence") .contains("Sign in to Messages"));}#[test]fn the_reader_watches_its_database_whose_wal_is_what_moves_when_a_message_arrives() { let (dir, layout) = temp_home(); let source = MessagesSource::new(MessagesSourceInput { workspace_id: WORKSPACE.to_string(), namespace: crate::test_support::namespace().clone(), layout, device: DEVICE.to_string(), home: dir.path().to_path_buf(), country_code: None, files: None, }); let triggers = source.triggers(); // The watch names the database; its folder stream wakes on the `-wal`. assert_eq!(triggers.watch, vec![mac_messages_path(dir.path())]);}#[test]fn a_pass_reads_its_own_copy_and_never_the_file_messages_holds_open() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000025", "+919000000025", "iMessage", )], &[FixtureMessage::new( "G-COPY", "iMessage", "2026-08-21T09:00:00.000Z", "iMessage;-;+919000000025", ) .text("copied before it was read") .handle("+919000000025")], ); read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert!(layout.snapshots_dir().join("messages-mac.db").exists());}#[test]fn a_conversation_whose_style_this_reader_does_not_know_is_counted_not_guessed_at() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); let mut chat = FixtureChat::dm("iMessage;-;+919000000026", "+919000000026", "iMessage"); chat.style = 99; write_messages_fixture( &path, &[chat], &[FixtureMessage::new( "G-STYLE", "iMessage", "2026-08-22T09:00:00.000Z", "iMessage;-;+919000000026", ) .text("in a shape nobody knows") .handle("+919000000026")], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert!(pass.batches.is_empty()); assert_eq!(pass.notes.get("unknownChatStyle"), Some(&1));}#[test]fn a_service_label_no_channel_claims_is_counted_rather_than_filed_under_a_guess() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "Carrier;-;+919000000027", "+919000000027", "Carrier-Branded-Thing", )], &[FixtureMessage::new( "G-SERVICE", "Carrier-Branded-Thing", "2026-08-23T09:00:00.000Z", "Carrier;-;+919000000027", ) .text("over something else entirely") .handle("+919000000027")], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert!(pass.batches.is_empty()); assert_eq!(pass.notes.get("unknownService"), Some(&1));}#[test]fn a_tapback_whose_target_this_pass_never_saw_is_counted_rather_than_dropped_in_silence() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000028", "+919000000028", "iMessage", )], &[ FixtureMessage::new( "G-ANCHOR", "iMessage", "2026-08-24T09:00:00.000Z", "iMessage;-;+919000000028", ) .text("so the conversation exists") .handle("+919000000028"), FixtureMessage::new( "G-ORPHAN", "iMessage", "2026-08-24T09:01:00.000Z", "iMessage;-;+919000000028", ) .handle("+919000000028") .tapback(2001, "p:0/A-MESSAGE-FROM-LAST-YEAR"), ], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert_eq!(pass.notes.get("tapbackWithoutTarget"), Some(&1)); assert_eq!(pass.ids(), vec!["G-ANCHOR".to_string()]);}#[test]fn a_run_says_how_many_rows_each_file_it_opened_was_asked_for() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); write_messages_fixture( &path, &[FixtureChat::dm( "iMessage;-;+919000000029", "+919000000029", "iMessage", )], &[FixtureMessage::new( "G-READ", "iMessage", "2026-08-25T09:00:00.000Z", "iMessage;-;+919000000029", ) .text("one row") .handle("+919000000029")], ); let pass = read_all(dir.path(), &layout, vec![mac_file(&path)], None); assert_eq!(pass.notes.get("read:mac"), Some(&1)); // `read:` is a count of rows LOOKED at, never a drop. assert_eq!(crate::ingest::dropped_from(&pass.notes), 0);}/// RULE (coordinator, 24 Nov 2026: SMS through the Mac stopped on 26 Oct): a first read on a Mac whose texts Cotton/// already holds up to an instant starts there. What was sent before it is passed over and counted (never re-sent/// with its attachments); what came after lands; and the cursor then moves past every row read, so the next pass/// reads only what arrives.#[test]fn a_first_read_starts_where_cottons_texts_end_and_then_follows_the_file() { let (dir, layout) = temp_home(); let path = dir.path().join("chat.db"); let chat = "SMS;-;+919000000041"; write_messages_fixture( &path, &[FixtureChat::dm(chat, "+919000000041", "SMS")], &[ FixtureMessage::new("G-HELD", "SMS", "2026-10-26T11:08:00.000Z", chat).text("already in Cotton").handle("+919000000041"), FixtureMessage::new("G-NOV", "SMS", "2026-11-23T09:00:00.000Z", chat).text("arrived since").handle("+919000000041"), ], ); let source = source_for(dir.path(), &layout, vec![mac_file(&path)]).since(Some("2026-10-26T11:08:01.000Z".to_string())); let mut ids = Vec::new(); let outcome = source .read(None, &mut |batch: IngestBatch| { if let IngestWork::Messages { request, .. } = batch.work { ids.extend(request.messages.iter().map(|m| m.external_id.clone())); } Ok(()) }) .unwrap(); assert_eq!(ids, vec!["G-NOV".to_string()]); assert_eq!(outcome.notes.get("beforeSince"), Some(&1)); assert_eq!(parse_messages_cursor(Some(&outcome.cursor)).mac, 2, "past every row read"); let again = read_all(dir.path(), &layout, vec![mac_file(&path)], Some(&outcome.cursor)); assert!(again.ids().is_empty(), "the next pass reads only what arrives");}