//! `chat.export` over invented chats: nobody here is real. use std::io::Read as _; use super::*; /// The zip the answer names: each entry's name and its bytes, in order. fn entries(answer: &Value) -> Vec<(String, Vec)> { let file = std::fs::File::open(answer["path"].as_str().expect("a path")).expect("the zip"); let mut zip = zip::ZipArchive::new(file).expect("a zip"); (0..zip.len()) .map(|i| { let mut entry = zip.by_index(i).unwrap(); let mut bytes = Vec::new(); entry.read_to_end(&mut bytes).unwrap(); (entry.name().unwrap().into_owned(), bytes) }) .collect() } fn text_of(entries: &[(String, Vec)], name: &str) -> String { let (_, bytes) = entries.iter().find(|(n, _)| n == name).unwrap_or_else(|| panic!("{name} in {:?}", entries.iter().map(|(n, _)| n).collect::>())); String::from_utf8(bytes.clone()).unwrap() } /// `2026-11-23 14:05 · ` taken off each message line, the rest kept. fn said(text: &str) -> Vec { text.lines().skip(3).map(|line| line.split_once(" · ").map_or(line.to_string(), |(_, rest)| rest.to_string())).collect() } // RULE (owner, 23 Nov 2026; CONTRACT.md "Export"): one chat goes as `.zip` holding `.txt`, each message a line // with its day, clock and speaker ("You" for the person's own), and `files/` with the files on this phone; a file not on // this phone is said in its line and never fetched. #[test] fn one_chat_goes_as_its_text_and_the_files_this_phone_holds() { let (_dir, handle) = phone(); imported_group(&handle); message(&handle, "m-g8", WEEKEND, true, "whatsapp", "imported", "See you all at six", &iso(5), 0, Some("s-me")); let fetched = std::sync::Arc::new(OwnMedia::default()); handle.client().set_media_fetch(Some(fetched.clone())); // The view from the top was opened once, so its file is on this phone; the screenshot never was. let out = handle.client().layout().exports_dir().join("opened"); answer(&handle, "chat.file", json!({ "message": "m-g7", "index": 0, "dir": out.to_string_lossy() })); let asked = fetched.0.lock().unwrap().len(); let exported = answer(&handle, "chat.export", json!({ "thread": WEEKEND })); assert_eq!((exported["file_name"].as_str(), exported["mime"].as_str(), exported["count"].as_i64()), (Some("Hill trip.zip"), Some("application/zip"), Some(1)), "{exported}"); assert_eq!((exported["files"].as_i64(), exported["missing"].as_i64()), (Some(1), Some(1))); assert_eq!(fetched.0.lock().unwrap().len(), asked, "an export never fetches a file"); let zip = entries(&exported); let names: Vec<&str> = zip.iter().map(|(n, _)| n.as_str()).collect(); assert_eq!(names, ["files/00000012-PHOTO-2026-11-03-10-25-00.jpg", "Hill trip.txt"]); assert_eq!(zip[0].1, b"an invented picture's bytes"); let text = text_of(&zip, "Hill trip.txt"); assert!(text.starts_with("Hill trip\nExported from Cotton Comms on "), "{text}"); let lines = said(&text); assert_eq!(lines[0], "Bimal Example: Who has the tent?", "{text}"); assert!(lines.iter().any(|l| l == "Chitra Example: The view from the top " || l == "Chitra Example: "), "{text}"); assert!(lines.iter().any(|l| l.starts_with("Chitra Example:") && l.ends_with("")), "{text}"); assert_eq!(lines.last().map(String::as_str), Some("You: See you all at six")); // Each line carries its day and its clock. assert!(text.lines().nth(3).is_some_and(|l| l.len() > 19 && l.as_bytes()[4] == b'-' && l.as_bytes()[13] == b':'), "{text}"); } // RULE: with no chat named, every chat the list holds goes as `Comms chats.zip`, a folder per chat, the newest first; // a hidden message, one before the chat was cleared, and Cotton's own Downloads chat stay out. #[test] fn every_chat_goes_as_a_folder_each() { let (_dir, handle) = phone(); three_chats(&handle); sql(&handle, "UPDATE cotton_msg_messages SET hidden_at = '2026-01-02T00:00:00.000Z' WHERE id = 'm-a2'", &[]); let exported = answer(&handle, "chat.export", json!({})); assert_eq!(exported["file_name"], "Comms chats.zip"); assert_eq!(exported["count"], 3, "{exported}"); let zip = entries(&exported); let texts: Vec<&str> = zip.iter().map(|(n, _)| n.as_str()).filter(|n| n.ends_with(".txt")).collect(); assert_eq!(texts.len(), 3, "{texts:?}"); let asha = texts.iter().find(|n| n.starts_with("Asha Example/")).expect("Asha's folder"); let lines = said(&text_of(&zip, asha)); assert_eq!(lines, ["You: Are we still on?", "Asha Example: Bring the kite"], "the hidden message stays out"); assert!(texts.iter().any(|n| *n == "Weekend plans/Weekend plans.txt"), "{texts:?}"); assert!(texts.iter().any(|n| *n == "Old number/Old number.txt"), "an archived chat is still a chat: {texts:?}"); assert!(!zip.iter().any(|(n, _)| n.contains("Downloads"))); } #[test] fn a_chat_not_here_is_refused_and_no_chat_at_all_is_said() { let (_dir, handle) = phone(); assert_eq!(refused(&handle, "chat.export", json!({})), "refused"); assert_eq!(refused(&handle, "chat.export", json!({ "thread": "11111111-1111-4111-8111-00000000ffff" })), "not_found"); }