use super::super::fixtures_tests::*; use super::super::keys::blind_key; use super::*; fn bk() -> Key32 { blind_key(&dk(), DATABASE, "message").unwrap() } #[test] fn normalise_folds_case_width_and_latin_diacritics_but_keeps_devanagari() { assert_eq!(normalise("Café ABC"), "cafe abc"); assert_eq!(normalise("नमस्ते दुनिया"), "नमस्ते दुनिया"); assert_eq!(normalise("ÅNGSTRÖM"), "angstrom"); } #[test] fn words_drop_stopwords_single_letters_and_emoji() { assert_eq!(words("The ferry to Zanzibar is at 9 🚢 tomorrow"), ["ferry", "zanzibar", "tomorrow"]); assert_eq!(words("मैं कल दिल्ली में हूँ और तुम?"), ["मैं", "कल", "दिल्ली", "हूँ", "तुम"]); assert_eq!(words("Meeting @ 5pm, कल मिलते हैं 🙏"), ["meeting", "5pm", "कल", "मिलते"]); assert!(words("👍🎉🙏").is_empty()); } #[test] fn a_query_finds_a_whole_word_and_a_word_start() { let row = text_tokens(&bk(), "body", "Zanzibar ferry tickets"); for query in ["zanzibar", "Zanz", "ferry tick", "zanzibar ferry", "tickets"] { let q = query_tokens(&bk(), "body", query); assert!(!q.is_empty() && q.iter().all(|t| row.contains(t)), "{query}"); } // a word in the middle must be whole; another column never matches assert!(!row.contains(&query_tokens(&bk(), "body", "zanz ferry")[0])); assert!(query_tokens(&bk(), "preview", "ferry").iter().all(|t| !row.contains(t))); // a two-letter query is an exact word let short = text_tokens(&bk(), "body", "go home"); assert!(short.contains(&query_tokens(&bk(), "body", "go")[0])); assert!(query_tokens(&bk(), "body", "the a").is_empty()); } #[test] fn tokens_are_per_table_and_exact_tokens_are_exact() { let other = blind_key(&dk(), DATABASE, "email").unwrap(); assert_ne!(word_token(&bk(), "body", "ferry"), word_token(&other, "body", "ferry")); assert_eq!(exact_token(&bk(), "external_id", " wamid.ABC "), exact_token(&bk(), "external_id", "wamid.ABC")); assert_ne!(exact_token(&bk(), "external_id", "wamid.ABC"), exact_token(&bk(), "external_id", "wamid.abc")); assert_eq!(exact_token(&bk(), "external_id", " "), None); } #[test] fn row_tokens_cover_blind_and_exact_columns_only() { let spec = message_spec(); let tokens = row_tokens(&bk(), &spec, &message_row()); assert!(tokens.contains(&word_token(&bk(), "body", "zanzibar"))); assert!(tokens.contains(&exact_token(&bk(), "external_id", "wamid.HBgMOTE5ODc2NTQzMjEwFQIAEhgg").unwrap())); assert!(!tokens.contains(&word_token(&bk(), "channel", "whatsapp"))); assert!(!tokens.contains(&exact_token(&bk(), "sender_handle", "+919876543210").unwrap())); } /// RULE (plan §4, L7): a search by blind tokens, its hits held to `matches` /// once opened, finds exactly the rows `matches` finds over the clear text — /// the tokens never miss a row, and the plaintext check drops every hit that /// is not a word hit. Over a synthetic corpus built to share prefixes. #[test] fn tokens_then_the_plaintext_check_equal_the_clear_search() { let bk = bk(); let vocabulary = [ "conference", "conferred", "conferring", "confetti", "con", "cone", "conifer", "market", "marketing", "Market", "go", "ox", "the", "and", "a", "café", "cafe", "naïve", "बाज़ार", "किताब", "किताबें", "2026", "40", "pelican", "pen", ]; let mut seed = 7u64; let mut next = || { seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); (seed >> 33) as usize }; let texts: Vec = (0..80).map(|_| (0..(2 + next() % 7)).map(|_| vocabulary[next() % vocabulary.len()]).collect::>().join(" ")).collect(); let indexed: Vec> = texts.iter().map(|t| text_tokens(&bk, "body", t)).collect(); let mut queries: Vec = vocabulary.iter().map(|w| w.to_string()).collect(); for w in ["conference", "conferring", "marketing", "किताबें"] { for n in 1..=w.chars().count().min(8) { queries.push(w.chars().take(n).collect()); } } queries.extend(["conference conferred", "the market", "market con", "go pen", "x", "CAFE"].map(String::from)); let mut dropped = 0; for q in &queries { let tokens = query_tokens(&bk, "body", q); let by_tokens: Vec = (0..texts.len()).filter(|&i| !tokens.is_empty() && tokens.iter().all(|t| indexed[i].contains(t))).collect(); let clear: Vec = (0..texts.len()).filter(|&i| matches(&texts[i], q)).collect(); assert!(clear.iter().all(|i| by_tokens.contains(i)), "the tokens missed a row for {q:?}"); let held: Vec = by_tokens.iter().copied().filter(|&i| matches(&texts[i], q)).collect(); dropped += by_tokens.len() - held.len(); assert_eq!(held, clear, "{q:?}"); } assert!(dropped > 0, "the corpus gives the plaintext check something to drop"); } #[test] fn row_match_names_the_column_that_holds_every_word() { let spec = SealSpec { clear: vec![], blind: vec!["heading".into(), "body".into()], blind_exact: vec!["external_id".into()], blind_bytes: Default::default() }; let row = serde_json::json!({"heading": "Ferry", "body": ["Zanzibar", "tickets", 40], "external_id": "zanzibar"}); let row = row.as_object().unwrap(); assert_eq!(row_match(&spec, row, "zanzibar tick"), Some("body")); assert_eq!(row_match(&spec, row, "fer"), Some("heading")); assert_eq!(row_match(&spec, row, "40"), Some("body")); // the words of one query are held in one column, and an exact column is never searched by words assert_eq!(row_match(&spec, row, "ferry zanzibar"), None); assert_eq!(row_match(&spec, &serde_json::json!({"external_id": "zanzibar"}).as_object().unwrap().clone(), "zanzibar"), None); } #[test] fn a_keyed_id_is_uuid_shaped_the_same_for_the_same_value_and_names_nothing_of_it() { let id = keyed_id(&bk(), "id", "react:m1:919800000002"); assert_eq!(id, keyed_id(&bk(), "id", "react:m1:919800000002")); assert_ne!(id, keyed_id(&bk(), "id", "react:m1:919800000003")); assert_ne!(id, keyed_id(&bk(), "namespace", "react:m1:919800000002")); assert_ne!(id, keyed_id(&Key32::from([9u8; 32]), "id", "react:m1:919800000002")); assert_eq!(id.len(), 36); assert_eq!(&id[14..15], "4"); assert!(matches!(&id[19..20], "8" | "9" | "a" | "b")); assert!(!id.contains("9198")); } /// RULE (sealing plan Q12): a column `blind_bytes` caps is indexed from its /// start to that many bytes, cut back to a character boundary; a word past /// the cap makes no token, and a column it does not name is indexed whole. #[test] fn blind_bytes_indexes_only_the_head_of_a_long_column() { let mut spec = SealSpec { clear: vec![], blind: vec!["subject".into(), "body".into()], blind_exact: vec![], blind_bytes: Default::default() }; spec.blind_bytes.insert("body".into(), 2048); let body = format!("{} कोलकाता {} quokka", "lorem ".repeat(330), "ipsum ".repeat(400)); let row = serde_json::json!({"subject": "Harbour ferry", "body": body}); let tokens = row_tokens(&bk(), &spec, row.as_object().unwrap()); assert!(tokens.contains(&word_token(&bk(), "body", "कोलकाता")), "a word inside the head is indexed"); assert!(!tokens.contains(&word_token(&bk(), "body", "quokka")), "a word past 2 KB is not"); assert!(tokens.contains(&word_token(&bk(), "subject", "harbour"))); // A cut inside a multi-byte character steps back to its start. assert_eq!(spec.indexed("body", "aé"), "aé"); let mut tight = spec.clone(); tight.blind_bytes.insert("body".into(), 2); assert_eq!(tight.indexed("body", "aé"), "a"); assert_eq!(tight.indexed("subject", "aé"), "aé"); }