//! A media-only message's body is never words: the kit's label is resolved at //! read (`messages::media_only::media_only_words`), and nothing is written //! into such a body. Since schema 145 dropped the message table's CHECK, a //! message whose bytes were left behind keeps an empty body too: no importer //! writes the old token (`conversation.preview.`) into one. use std::path::{Path, PathBuf}; use cotton_core::messages::media_only::LEFT_BEHIND_LABELS; fn sources(dir: &Path, out: &mut Vec) { let Ok(entries) = std::fs::read_dir(dir) else { return }; for entry in entries.flatten() { let path = entry.path(); let name = path.file_name().and_then(|name| name.to_str()).unwrap_or(""); if path.is_dir() { if name != "target" && name != "tests" { sources(&path, out); } } else if name.ends_with(".rs") && !name.ends_with("_tests.rs") { out.push(path); } } } /// RULE: No code writes a media-only label's words, or the old token, into a message body. #[test] fn no_label_is_written_into_a_body() { // The crates directory, read at run time (manifest-dir-at-run-time) and // never above it (rust-crate-local-reads); paths are named from `crates/`. let crates = cotton_store::manifest_dir().parent().expect("cotton-store sits in the crates directory").to_path_buf(); let mut files = Vec::new(); sources(&crates, &mut files); assert!(!files.is_empty(), "the media-only body scan found no source files under {}", crates.display()); let mut found = Vec::new(); for file in files { let rel = format!("crates/{}", file.strip_prefix(&crates).unwrap_or(&file).to_string_lossy().replace('\\', "/")); let text = std::fs::read_to_string(&file).unwrap_or_default(); for (at, line) in text.lines().enumerate() { let writes_body = line.contains("body =") || line.contains("\"body\""); if !writes_body { continue; } let words = line.contains("media_only_words(") || line.contains("LEFT_BEHIND_LABELS") || LEFT_BEHIND_LABELS.iter().any(|label| line.contains(&format!("body = \"{label}\""))); let token = line.contains("left_behind_placeholder(") || line.contains("body = \"conversation.preview."); if words || token { found.push(format!("{rel}:{}: {}", at + 1, line.trim())); } } } assert!(found.is_empty(), "a media-only label written into a body:\n{}", found.join("\n")); }