Cotton code
89 lines · 4.1 KB · RustRaw
//! One page of the WhatsApp helper's outbox, read by cotton-client's reader//! from a fake helper on a Unix socket and sent through the sink to a fake//! object — the path the Mac importer runs, end to end.mod support;use std::sync::{Arc, Mutex};use cotton_client::ingest::sources::whatsapp_link::{WhatsappLinkSource, WhatsappLinkSourceInput};use cotton_client::ingest::{IngestBatch, IngestSource as _};use cotton_client::seal::HeldDataKeys;use cotton_mac::copy::Copy;use cotton_mac::object::ObjectClient;use cotton_mac::sink::ObjectSink;use cotton_store::file_identity::Namespace;use serde_json::{json, Value};const DB: &str = "00000000-0000-4000-8000-000000000001";const ACCOUNT: &str = "15550009999@s.whatsapp.net";const FRIEND: &str = "15550001111@s.whatsapp.net";use support::helper::{fake_helper, Helper};fn message(seq: i64, id: &str, extra: Value) -> Value {    let mut msg = json!({        "id": id, "chat": FRIEND, "sender": FRIEND, "from_me": false, "is_group": false,        "ts": 1_755_000_000 + seq, "kind": "text", "text": format!("synthetic {seq}"), "origin": "history"    });    if let (Value::Object(base), Value::Object(more)) = (&mut msg, extra) {        base.extend(more);    }    json!({ "seq": seq, "event": "message", "account": ACCOUNT, "message": msg })}// RULE (27 Oct 2026): a WhatsApp message whose media pointer fetches nothing// lands saying what it was, and its page lands with it. Old history carries// video and picture pointers with no direct path; the helper refused them// ("media pointer incomplete") and the reader failed the page on it, so the// whole outbox (15k events) waited behind six messages.#[test]fn a_page_with_an_unfetchable_pointer_lands_and_its_message_says_what_it_was() {    let dir = tempfile::tempdir().unwrap();    let no_path = json!({ "type": "video", "mimetype": "video/mp4", "direct_path": "", "media_key": "a2V5", "file_length": 16_051_529 });    let no_key = json!({ "type": "image", "mimetype": "image/jpeg", "direct_path": "", "file_length": 76_219 });    let state = Arc::new(Mutex::new(Helper {        outbox: vec![            message(20, "V1", json!({ "kind": "video", "text": "", "media": no_path })),            message(21, "P1", json!({ "kind": "image", "text": "", "media": no_key })),            message(22, "T1", json!({})),        ],        ..Helper::default()    }));    fake_helper(dir.path(), Arc::clone(&state));    let fake = support::object::FakeObject::start(        DB,        "+919800000099",        ["thread", "message", "thread_member", "message_reaction", "message_contact", "message_contact_handle", "person", "person_handle", "call", "seal_key"]            .into_iter()            .map(support::object::kit_table)            .collect(),    );    let server = &fake.server;    let object = ObjectClient::new(server.url.clone(), Some("device-token".to_string())).unwrap();    let copy = Copy::open(&dir.path().join("copy.db"), DB, HeldDataKeys::new()).unwrap();    let sink = ObjectSink::new(&object, DB).with_copy(&copy);    let source = WhatsappLinkSource::new(WhatsappLinkSourceInput {        workspace_id: DB.to_string(),        namespace: Namespace::of_database(DB),        device: "Test Mac".to_string(),        dir: dir.path().to_path_buf(),        media_dir: dir.path().join("media"),    });    let outcome = source        .read(Some("seq:19"), &mut |batch: IngestBatch| sink.send(batch).map(|_| ()).map_err(|error| error.to_string()))        .expect("the page lands");    assert_eq!(outcome.cursor, "seq:22");    assert_eq!(state.lock().unwrap().acks, vec![22]);    assert_eq!(state.lock().unwrap().media_asks, 2);    assert!(server.lines().iter().all(|line| !line.starts_with("POST /v1/import")), "{:?}", server.lines());    let held = fake.state.lock().unwrap().live("message");    let by_id = |id: &str| held.iter().find(|(_, h)| h.data["external_id"] == id).map(|(_, h)| h.data.clone()).unwrap();    assert_eq!(held.len(), 3);    assert_eq!(by_id("V1")["body"], "Video");    assert!(by_id("V1")["media"].as_array().is_none_or(Vec::is_empty));    assert_eq!(by_id("P1")["body"], "Photo");    assert_eq!(by_id("T1")["body"], "synthetic 22");}