| 1 | //! Ported behavior parity tests mapping back to Python's unit test suites. |
| 2 | |
| 3 | use std::collections::{BTreeMap, BTreeSet}; |
| 4 | |
| 5 | use last30days_core::cjk; |
| 6 | use last30days_core::cluster::{ClusterCandidate, ClusterPlan, WindowState, cluster_candidates}; |
| 7 | use last30days_core::dedupe::{ |
| 8 | self, DedupeEntry, DedupeMode, dedupe_kept_indices, hybrid_similarity, jaccard_similarity, |
| 9 | ngrams, normalize_text, token_jaccard, |
| 10 | }; |
| 11 | use last30days_core::entity::{entity_overlap, extract_text_entities, has_anchor_signal}; |
| 12 | use last30days_core::markdown::{ |
| 13 | self, MarkdownBatchItem, PRIVATE_CORPUS_END, PRIVATE_CORPUS_START, PUBLIC_CLUSTERS_MARKER, |
| 14 | PUBLIC_DRILL_MARKER, PUBLIC_FRESHNESS_MARKER, |
| 15 | }; |
| 16 | use last30days_core::relevance::{PreparedQuery, token_overlap_relevance, tokenize}; |
| 17 | use last30days_core::signals::{ |
| 18 | DateKind, FreshnessMode, GithubItemKind, GroundingPolicy, SignalItem, annotate_stream, |
| 19 | engagement_raw, engagement_score, parse_ymd_days, recency_score_from_days, |
| 20 | }; |
| 21 | |
| 22 | fn str_set(items: &[&str]) -> BTreeSet<String> { |
| 23 | items.iter().map(|s| (*s).to_owned()).collect() |
| 24 | } |
| 25 | |
| 26 | fn approx_eq(a: f64, b: f64, eps: f64) { |
| 27 | assert!( |
| 28 | (a - b).abs() <= eps, |
| 29 | "expected {a} to be within {eps} of {b}" |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | // --------------------------------------------------------------------------- |
| 34 | // Ported from tests/test_cjk.py |
| 35 | // --------------------------------------------------------------------------- |
| 36 | |
| 37 | #[test] |
| 38 | fn cjk_has_cjk_matches_python() { |
| 39 | assert!(cjk::has_cjk("国产大模型"), "Chinese text has CJK"); |
| 40 | assert!(cjk::has_cjk("GPT4很强"), "mixed text has CJK"); |
| 41 | assert!(!cjk::has_cjk("hello world"), "ASCII text has no CJK"); |
| 42 | assert!(!cjk::has_cjk(""), "empty string has no CJK"); |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | fn cjk_segment_ascii_chinese_mixed_and_single() { |
| 47 | assert_eq!( |
| 48 | cjk::segment("best react hooks"), |
| 49 | vec!["best", "react", "hooks"], |
| 50 | "ASCII segmentation" |
| 51 | ); |
| 52 | let zh = cjk::segment("大模型"); |
| 53 | assert!(zh.contains(&"大模".to_owned()), "missing bigram 大模"); |
| 54 | assert!(zh.contains(&"模型".to_owned()), "missing bigram 模型"); |
| 55 | |
| 56 | let mixed = cjk::segment("GPT4很强 react"); |
| 57 | assert!(mixed.contains(&"gpt4".to_owned()), "missing gpt4"); |
| 58 | assert!(mixed.contains(&"react".to_owned()), "missing react"); |
| 59 | assert!(mixed.contains(&"很强".to_owned()), "missing 很强"); |
| 60 | |
| 61 | assert_eq!(cjk::segment("中"), vec!["中"], "single CJK char"); |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn cjk_chinese_relevance_and_phrase_bonus() { |
| 66 | let q = PreparedQuery::new("国产大模型 测评"); |
| 67 | let score = token_overlap_relevance(&q, "这是国产大模型的最新测评", &[]); |
| 68 | assert!(score > 0.5, "expected score > 0.5, got {score}"); |
| 69 | |
| 70 | let unrelated = token_overlap_relevance(&q, "今天天气很好适合出门散步", &[]); |
| 71 | approx_eq(unrelated, 0.0, 1e-9); |
| 72 | |
| 73 | let contiguous = token_overlap_relevance(&q, "国产大模型测评合集", &[]); |
| 74 | let scattered = token_overlap_relevance(&q, "测评了很多东西也聊到国产大模型", &[]); |
| 75 | assert!( |
| 76 | contiguous > scattered, |
| 77 | "contiguous {contiguous} should beat scattered {scattered}" |
| 78 | ); |
| 79 | |
| 80 | let en_q = PreparedQuery::new("react hooks"); |
| 81 | approx_eq( |
| 82 | token_overlap_relevance(&en_q, "react myreacthooks bundle", &[]), |
| 83 | 0.58, |
| 84 | 1e-9, |
| 85 | ); |
| 86 | approx_eq( |
| 87 | token_overlap_relevance(&en_q, "react unrelated bundle", &[]), |
| 88 | 0.58, |
| 89 | 1e-9, |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn cjk_chinese_dedupe() { |
| 95 | let near = hybrid_similarity("国产大模型最新测评对比", "国产大模型测评对比最新"); |
| 96 | assert!(near > 0.5, "reordered Chinese should be near-dup: {near}"); |
| 97 | let distinct = hybrid_similarity("国产大模型测评", "今天天气很好出门散步"); |
| 98 | assert!( |
| 99 | distinct < 0.3, |
| 100 | "distinct Chinese should have low sim: {distinct}" |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | // --------------------------------------------------------------------------- |
| 105 | // Ported from tests/test_dedupe_v3.py |
| 106 | // --------------------------------------------------------------------------- |
| 107 | |
| 108 | #[test] |
| 109 | fn dedupe_normalize_text() { |
| 110 | assert_eq!(normalize_text("Hello World"), "hello world", "lowercases"); |
| 111 | assert_eq!( |
| 112 | normalize_text("it's a test!"), |
| 113 | "it s a test", |
| 114 | "strips punctuation" |
| 115 | ); |
| 116 | assert_eq!(normalize_text("a b\t\nc"), "a b c", "collapses ws"); |
| 117 | assert_eq!(normalize_text(""), "", "empty"); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn dedupe_ngrams() { |
| 122 | assert_eq!( |
| 123 | ngrams("abcde", 3).unwrap(), |
| 124 | str_set(&["abc", "bcd", "cde"]), |
| 125 | "simple trigrams" |
| 126 | ); |
| 127 | assert_eq!(ngrams("ab", 3).unwrap(), str_set(&["ab"]), "short text"); |
| 128 | assert!(ngrams("", 3).unwrap().is_empty(), "empty text"); |
| 129 | assert_eq!( |
| 130 | ngrams("A!B", 3).unwrap(), |
| 131 | str_set(&["a b"]), |
| 132 | "normalizes before ngrams" |
| 133 | ); |
| 134 | assert!(ngrams("abc", 0).is_err(), "zero n-gram rejected"); |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn dedupe_jaccard_and_token_jaccard() { |
| 139 | approx_eq( |
| 140 | jaccard_similarity(&str_set(&["a", "b"]), &str_set(&["a", "b"])), |
| 141 | 1.0, |
| 142 | 1e-9, |
| 143 | ); |
| 144 | approx_eq( |
| 145 | jaccard_similarity(&str_set(&["a"]), &str_set(&["b"])), |
| 146 | 0.0, |
| 147 | 1e-9, |
| 148 | ); |
| 149 | approx_eq( |
| 150 | jaccard_similarity(&str_set(&["a", "b", "c"]), &str_set(&["b", "c", "d"])), |
| 151 | 0.5, |
| 152 | 1e-9, |
| 153 | ); |
| 154 | approx_eq( |
| 155 | jaccard_similarity(&BTreeSet::new(), &str_set(&["a"])), |
| 156 | 0.0, |
| 157 | 1e-9, |
| 158 | ); |
| 159 | approx_eq(token_jaccard("hello world", "hello world"), 1.0, 1e-9); |
| 160 | approx_eq(token_jaccard("alpha beta", "gamma delta"), 0.0, 1e-9); |
| 161 | approx_eq(token_jaccard("the big cat", "a big dog"), 1.0 / 3.0, 1e-9); |
| 162 | approx_eq( |
| 163 | token_jaccard("x shared alpha", "x shared beta"), |
| 164 | 1.0 / 3.0, |
| 165 | 1e-9, |
| 166 | ); |
| 167 | approx_eq(token_jaccard("x alpha", "x beta"), 0.0, 1e-9); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn dedupe_kept_indices_behavior() { |
| 172 | let entries = vec![ |
| 173 | DedupeEntry { |
| 174 | mode: DedupeMode::Fuzzy, |
| 175 | text: "OpenClaw vs NanoClaw comparison review".to_owned(), |
| 176 | }, |
| 177 | DedupeEntry { |
| 178 | mode: DedupeMode::Fuzzy, |
| 179 | text: "OpenClaw vs NanoClaw comparison review thread".to_owned(), |
| 180 | }, |
| 181 | DedupeEntry { |
| 182 | mode: DedupeMode::UrlOnly, |
| 183 | text: "OpenClaw vs NanoClaw comparison review".to_owned(), |
| 184 | }, |
| 185 | DedupeEntry { |
| 186 | mode: DedupeMode::Fuzzy, |
| 187 | text: String::new(), |
| 188 | }, |
| 189 | ]; |
| 190 | let kept = dedupe_kept_indices(&entries, 0.7).unwrap(); |
| 191 | assert_eq!( |
| 192 | kept, |
| 193 | vec![0, 2, 3], |
| 194 | "fuzzy dup dropped, url_only & empty kept" |
| 195 | ); |
| 196 | |
| 197 | let analysis = vec![ |
| 198 | DedupeEntry { |
| 199 | mode: DedupeMode::Fuzzy, |
| 200 | text: "OpenClaw security analysis".to_owned(), |
| 201 | }, |
| 202 | DedupeEntry { |
| 203 | mode: DedupeMode::Fuzzy, |
| 204 | text: "OpenClaw security review".to_owned(), |
| 205 | }, |
| 206 | ]; |
| 207 | assert_eq!( |
| 208 | dedupe_kept_indices(&analysis, 1.0).unwrap(), |
| 209 | vec![0, 1], |
| 210 | "threshold 1.0 keeps non-exact" |
| 211 | ); |
| 212 | assert_eq!( |
| 213 | dedupe_kept_indices(&analysis, 0.3).unwrap(), |
| 214 | vec![0], |
| 215 | "threshold 0.3 collapses similar" |
| 216 | ); |
| 217 | assert!( |
| 218 | dedupe_kept_indices(&analysis, 1.5).is_err(), |
| 219 | "out-of-range threshold rejected" |
| 220 | ); |
| 221 | // Verify hybrid_similarity matches max(ngram, token). |
| 222 | let a = "OpenClaw security issues discussion"; |
| 223 | let b = "OpenClaw security issues thread"; |
| 224 | let expected = |
| 225 | jaccard_similarity(&ngrams(a, 3).unwrap(), &ngrams(b, 3).unwrap()).max(token_jaccard(a, b)); |
| 226 | approx_eq(dedupe::hybrid_similarity(a, b), expected, 1e-9); |
| 227 | } |
| 228 | |
| 229 | // --------------------------------------------------------------------------- |
| 230 | // Ported from tests/test_relevance.py |
| 231 | // --------------------------------------------------------------------------- |
| 232 | |
| 233 | #[test] |
| 234 | fn relevance_tokenize_and_synonyms() { |
| 235 | let tokens = tokenize("how to use the AI tools"); |
| 236 | assert!(!tokens.contains("how"), "stopword removed"); |
| 237 | assert!(!tokens.contains("the"), "stopword removed"); |
| 238 | assert!(tokens.contains("ai"), "ai kept"); |
| 239 | assert!(tokens.contains("artificial"), "ai synonym expanded"); |
| 240 | assert!(tokens.contains("intelligence"), "ai synonym expanded"); |
| 241 | assert!( |
| 242 | tokenize("js framework").contains("javascript"), |
| 243 | "js synonym" |
| 244 | ); |
| 245 | assert!( |
| 246 | tokenize("svelte app").contains("sveltejs"), |
| 247 | "svelte synonym" |
| 248 | ); |
| 249 | assert!(tokenize("vue components").contains("vuejs"), "vue synonym"); |
| 250 | assert!( |
| 251 | !tokenize("a b c python").contains("a"), |
| 252 | "single char dropped" |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | #[test] |
| 257 | fn relevance_scoring_and_hashtags() { |
| 258 | let q_claude = PreparedQuery::new("claude code"); |
| 259 | assert!( |
| 260 | token_overlap_relevance(&q_claude, "Claude Code tricks and tips", &[]) > 0.7, |
| 261 | "exact match high" |
| 262 | ); |
| 263 | let q_empty = PreparedQuery::new(""); |
| 264 | approx_eq( |
| 265 | token_overlap_relevance(&q_empty, "Some video", &[]), |
| 266 | 0.5, |
| 267 | 1e-9, |
| 268 | ); |
| 269 | let q_stop = PreparedQuery::new("the a is"); |
| 270 | approx_eq( |
| 271 | token_overlap_relevance(&q_stop, "some content", &[]), |
| 272 | 0.5, |
| 273 | 1e-9, |
| 274 | ); |
| 275 | |
| 276 | let q_py = PreparedQuery::new("python tutorial"); |
| 277 | approx_eq( |
| 278 | token_overlap_relevance(&q_py, "Python Tutorial for Beginners", &[]), |
| 279 | 1.0, |
| 280 | 1e-9, |
| 281 | ); |
| 282 | |
| 283 | let q_odds = PreparedQuery::new("anthropic odds"); |
| 284 | let generic = token_overlap_relevance(&q_odds, "Republican house odds update", &[]); |
| 285 | let informative = token_overlap_relevance(&q_odds, "Anthropic valuation market", &[]); |
| 286 | assert!(generic < 0.1, "generic-only capped below floor: {generic}"); |
| 287 | assert!( |
| 288 | informative > generic, |
| 289 | "informative {informative} > generic {generic}" |
| 290 | ); |
| 291 | |
| 292 | let tags = vec!["claudecode".to_owned(), "ai".to_owned()]; |
| 293 | let no_hash = token_overlap_relevance(&q_claude, "random video about stuff", &[]); |
| 294 | let with_hash = token_overlap_relevance(&q_claude, "random video about stuff", &tags); |
| 295 | assert!( |
| 296 | with_hash > no_hash, |
| 297 | "hashtags boost relevance: {with_hash} > {no_hash}" |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | // --------------------------------------------------------------------------- |
| 302 | // Ported from tests/test_signals_v3.py |
| 303 | // --------------------------------------------------------------------------- |
| 304 | |
| 305 | fn make_signal_item(source: &str, title: &str, body: &str) -> SignalItem { |
| 306 | SignalItem { |
| 307 | source: source.to_owned(), |
| 308 | title: title.to_owned(), |
| 309 | body: body.to_owned(), |
| 310 | snippet: String::new(), |
| 311 | published_at: Some("2026-03-15".to_owned()), |
| 312 | date_kind: DateKind::Event, |
| 313 | hashtags: Vec::new(), |
| 314 | labels: Vec::new(), |
| 315 | grounding: GroundingPolicy::Standard, |
| 316 | github_kind: GithubItemKind::Standard, |
| 317 | engagement: BTreeMap::new(), |
| 318 | has_stars_key: false, |
| 319 | top_comment_score: None, |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | #[test] |
| 324 | fn signals_engagement_formulas() { |
| 325 | let mut reddit = make_signal_item("reddit", "Title", "Body"); |
| 326 | reddit.engagement.insert("score".to_owned(), 99.0); |
| 327 | reddit.engagement.insert("num_comments".to_owned(), 20.0); |
| 328 | reddit.engagement.insert("upvote_ratio".to_owned(), 0.8); |
| 329 | reddit.top_comment_score = Some(10.0); |
| 330 | let expected = 0.50 * 99.0_f64.ln_1p() |
| 331 | + 0.35 * 20.0_f64.ln_1p() |
| 332 | + 0.05 * (0.8 * 10.0) |
| 333 | + 0.10 * 10.0_f64.ln_1p(); |
| 334 | approx_eq(engagement_raw(&reddit).unwrap(), expected, 1e-9); |
| 335 | |
| 336 | let mut yt = make_signal_item("youtube", "Title", "Body"); |
| 337 | yt.engagement.insert("views".to_owned(), 10_000.0); |
| 338 | yt.engagement.insert("likes".to_owned(), 500.0); |
| 339 | yt.engagement.insert("comments".to_owned(), 30.0); |
| 340 | yt.top_comment_score = Some(500.0); |
| 341 | let expected_yt = 0.45 * 10_000.0_f64.ln_1p() |
| 342 | + 0.32 * 500.0_f64.ln_1p() |
| 343 | + 0.13 * 30.0_f64.ln_1p() |
| 344 | + 0.10 * 500.0_f64.ln_1p(); |
| 345 | approx_eq(engagement_raw(&yt).unwrap(), expected_yt, 1e-6); |
| 346 | |
| 347 | let mut hn = make_signal_item("hackernews", "Show HN", "Desc"); |
| 348 | hn.engagement.insert("points".to_owned(), 150.0); |
| 349 | hn.engagement.insert("comments".to_owned(), 45.0); |
| 350 | let expected_hn = 0.55 * 150.0_f64.ln_1p() + 0.45 * 45.0_f64.ln_1p(); |
| 351 | approx_eq(engagement_raw(&hn).unwrap(), expected_hn, 1e-9); |
| 352 | |
| 353 | let mut pm = make_signal_item("polymarket", "Title", "Body"); |
| 354 | pm.engagement.insert("volume".to_owned(), 1000.0); |
| 355 | pm.engagement.insert("liquidity".to_owned(), 250.0); |
| 356 | let expected_pm = 0.60 * 1000.0_f64.ln_1p() + 0.40 * 250.0_f64.ln_1p(); |
| 357 | approx_eq(engagement_raw(&pm).unwrap(), expected_pm, 1e-9); |
| 358 | |
| 359 | // GitHub repo snapshot returns None for engagement_score |
| 360 | let mut gh_repo = make_signal_item("github", "Repo", "Desc"); |
| 361 | gh_repo.engagement.insert("stars".to_owned(), 5000.0); |
| 362 | gh_repo.has_stars_key = true; |
| 363 | assert_eq!( |
| 364 | engagement_score(&gh_repo), |
| 365 | None, |
| 366 | "repo snapshot has no engagement score" |
| 367 | ); |
| 368 | } |
| 369 | |
| 370 | #[test] |
| 371 | fn signals_parse_ymd_days_rejects_noncanonical_dates() { |
| 372 | for raw in [ |
| 373 | "2026-+1-01", |
| 374 | "+026-01-01", |
| 375 | "2026-01-+1", |
| 376 | "0000-01-01", |
| 377 | "2026-00-01", |
| 378 | "2026-01-00", |
| 379 | "2023-02-29", |
| 380 | ] { |
| 381 | assert!(parse_ymd_days(raw).is_err(), "accepted {raw}"); |
| 382 | } |
| 383 | assert!(parse_ymd_days("2024-02-29").is_ok(), "leap day rejected"); |
| 384 | |
| 385 | let reference = parse_ymd_days("2026-01-10").expect("reference date"); |
| 386 | assert_eq!( |
| 387 | recency_score_from_days(Some("2026-+1-03"), 30, reference), |
| 388 | 0, |
| 389 | "signed month should not score as fresh" |
| 390 | ); |
| 391 | } |
| 392 | |
| 393 | #[test] |
| 394 | fn signals_annotate_stream_orders_relevant_above_noise() { |
| 395 | let mut relevant = make_signal_item( |
| 396 | "reddit", |
| 397 | "Deploy to Fly.io with MCP in 60 seconds", |
| 398 | "Deploy to Fly.io guide with concrete steps.", |
| 399 | ); |
| 400 | relevant.engagement.insert("score".to_owned(), 2.0); |
| 401 | relevant.engagement.insert("upvote_ratio".to_owned(), 0.8); |
| 402 | |
| 403 | let mut noisy = make_signal_item( |
| 404 | "reddit", |
| 405 | "BATTLEFIELD 6 GAME UPDATE 1.2.2.0", |
| 406 | "Patch notes and gameplay discussion.", |
| 407 | ); |
| 408 | noisy.engagement.insert("score".to_owned(), 5000.0); |
| 409 | noisy.engagement.insert("num_comments".to_owned(), 1200.0); |
| 410 | noisy.engagement.insert("upvote_ratio".to_owned(), 0.95); |
| 411 | noisy.top_comment_score = Some(400.0); |
| 412 | |
| 413 | let ranked = annotate_stream( |
| 414 | &[noisy, relevant], |
| 415 | "How do I deploy on Fly.io?", |
| 416 | FreshnessMode::EvergreenOk, |
| 417 | "2026-03-16", |
| 418 | 30, |
| 419 | ) |
| 420 | .unwrap(); |
| 421 | assert_eq!(ranked[0].index, 1, "relevant item must rank first"); |
| 422 | } |
| 423 | |
| 424 | // --------------------------------------------------------------------------- |
| 425 | // Ported from tests/test_cluster_v3.py & test_entity_extract.py |
| 426 | // --------------------------------------------------------------------------- |
| 427 | |
| 428 | fn make_cand(id: &str, source: &str, title: &str, snippet: &str, score: f64) -> ClusterCandidate { |
| 429 | ClusterCandidate { |
| 430 | candidate_id: id.to_owned(), |
| 431 | title: title.to_owned(), |
| 432 | snippet: snippet.to_owned(), |
| 433 | sources: vec![source.to_owned()], |
| 434 | final_score: score, |
| 435 | window_state: WindowState::InWindow, |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | #[test] |
| 440 | fn entity_anchor_and_overlap() { |
| 441 | assert!(has_anchor_signal("Rust"), "leading capital"); |
| 442 | assert!(has_anchor_signal("GPT4"), "all caps + digit"); |
| 443 | assert!(has_anchor_signal("v2"), "digit"); |
| 444 | assert!(!has_anchor_signal("plain"), "lowercase no digit"); |
| 445 | let a = extract_text_entities("Kanye West Wireless Festival 2026"); |
| 446 | let b = extract_text_entities("BREAKING: Kanye West UK comeback Wireless Festival"); |
| 447 | assert!(entity_overlap(&a, &b) >= 0.45, "shared event entities"); |
| 448 | } |
| 449 | |
| 450 | #[test] |
| 451 | fn cluster_cross_source_merge_and_polymarket_isolation() { |
| 452 | let plan = ClusterPlan { |
| 453 | intent: "breaking_news".to_owned(), |
| 454 | cluster_mode: "story".to_owned(), |
| 455 | raw_topic: "test".to_owned(), |
| 456 | }; |
| 457 | |
| 458 | let cands = vec![ |
| 459 | make_cand( |
| 460 | "c1", |
| 461 | "reddit", |
| 462 | "Kanye West to headline all three nights of Wireless Festival 2026", |
| 463 | "Big announcement for Wireless.", |
| 464 | 80.0, |
| 465 | ), |
| 466 | make_cand( |
| 467 | "c2", |
| 468 | "x", |
| 469 | "BREAKING: Kanye West is making his massive UK comeback at Wireless Festival this July", |
| 470 | "Ye returns to UK.", |
| 471 | 75.0, |
| 472 | ), |
| 473 | make_cand( |
| 474 | "c3", |
| 475 | "youtube", |
| 476 | "Kanye West BULLY Album Review - Knox Hill Reacts", |
| 477 | "Full album reaction and breakdown.", |
| 478 | 70.0, |
| 479 | ), |
| 480 | ]; |
| 481 | let clusters = cluster_candidates(&cands, &plan); |
| 482 | assert_eq!(clusters.len(), 2, "c1 and c2 merge, c3 stays separate"); |
| 483 | let wireless = clusters |
| 484 | .iter() |
| 485 | .find(|cl| cl.candidate_ids.len() == 2) |
| 486 | .expect("merged wireless cluster"); |
| 487 | assert_eq!( |
| 488 | wireless.sources, |
| 489 | vec!["reddit", "x"], |
| 490 | "sorted merged sources" |
| 491 | ); |
| 492 | assert_ne!( |
| 493 | wireless.uncertainty.as_deref(), |
| 494 | Some("single-source"), |
| 495 | "multi-source cluster is not single-source" |
| 496 | ); |
| 497 | |
| 498 | // Polymarket isolation |
| 499 | let poly_cands = vec![ |
| 500 | make_cand( |
| 501 | "p1", |
| 502 | "reddit", |
| 503 | "Sam Altman personal rivalry with Elon Musk escalates", |
| 504 | "The feud between Sam Altman and Elon Musk continues.", |
| 505 | 80.0, |
| 506 | ), |
| 507 | make_cand( |
| 508 | "p2", |
| 509 | "polymarket", |
| 510 | "Sam Altman equity stake in OpenAI valued at $500M", |
| 511 | "Will Sam Altman receive equity in OpenAI restructuring?", |
| 512 | 75.0, |
| 513 | ), |
| 514 | ]; |
| 515 | let poly_clusters = cluster_candidates(&poly_cands, &plan); |
| 516 | assert_eq!( |
| 517 | poly_clusters.len(), |
| 518 | 2, |
| 519 | "Polymarket cluster must not merge with news cluster" |
| 520 | ); |
| 521 | } |
| 522 | |
| 523 | #[test] |
| 524 | fn cluster_stale_candidate_demoted_below_fresh() { |
| 525 | let plan = ClusterPlan { |
| 526 | intent: "breaking_news".to_owned(), |
| 527 | cluster_mode: "story".to_owned(), |
| 528 | raw_topic: "test".to_owned(), |
| 529 | }; |
| 530 | let mut stale = make_cand( |
| 531 | "stale", |
| 532 | "reddit", |
| 533 | "Model launch reactions discussion", |
| 534 | "Model launch reactions discussion", |
| 535 | 95.0, |
| 536 | ); |
| 537 | stale.window_state = WindowState::OutOfWindow; |
| 538 | let fresh = make_cand( |
| 539 | "fresh", |
| 540 | "x", |
| 541 | "Model launch reactions update", |
| 542 | "Model launch reactions update", |
| 543 | 50.0, |
| 544 | ); |
| 545 | let clusters = cluster_candidates(&[stale, fresh], &plan); |
| 546 | assert_eq!(clusters.len(), 1, "similar items cluster together"); |
| 547 | assert_eq!( |
| 548 | clusters[0].representative_ids[0], "fresh", |
| 549 | "fresh leads cluster" |
| 550 | ); |
| 551 | assert_eq!( |
| 552 | clusters[0].title, "Model launch reactions update", |
| 553 | "fresh supplies title" |
| 554 | ); |
| 555 | } |
| 556 | |
| 557 | // --------------------------------------------------------------------------- |
| 558 | // Ported from tests/test_library_feed.py & test_library_search.py |
| 559 | // --------------------------------------------------------------------------- |
| 560 | |
| 561 | #[test] |
| 562 | fn markdown_redacts_private_blocks_and_drills_respecting_fences() { |
| 563 | let md = format!( |
| 564 | "# last30days v4.6.0: Test Topic\n\n\ |
| 565 | {PRIVATE_CORPUS_START}\n## From your files\nSecret note.\n{PRIVATE_CORPUS_END}\n\n\ |
| 566 | ## Public Section\n\n\ |
| 567 | ```markdown\n## From your files\nCode fence example stays intact.\n```\n\n\ |
| 568 | {PUBLIC_DRILL_MARKER}\n## Drill Follow-up\n\nPublic drill stays.\n\n\ |
| 569 | ## Drill Follow-up\n\nLegacy unmarked drill gets stripped.\n\n\ |
| 570 | ### Deeper\n\n- 3 new items after dedupe\n- Re-researched sources: Reddit\n\n\ |
| 571 | {PUBLIC_FRESHNESS_MARKER}\n## Freshness Verification\n\n\ |
| 572 | | Verdict | Claim | Evidence | Checked |\n\ |
| 573 | | --- | --- | --- | --- |\n\ |
| 574 | | **supported** | Public claim | https://example.com | 2026-11-20 |\n\ |
| 575 | | **contradicted** | Private claim | [notes](corpus://secret.md) | 2026-11-20 |\n\n\ |
| 576 | {PUBLIC_CLUSTERS_MARKER}\n## Ranked Evidence Clusters\n\n\ |
| 577 | ### 1. Public headline (score 80, 1 item, sources: Reddit)\n" |
| 578 | ); |
| 579 | |
| 580 | let cleaned = markdown::public_markdown(&md); |
| 581 | assert!(!cleaned.contains("Secret note."), "private block stripped"); |
| 582 | assert!( |
| 583 | cleaned.contains("Code fence example stays intact."), |
| 584 | "fenced heading preserved" |
| 585 | ); |
| 586 | assert!( |
| 587 | cleaned.contains("Public drill stays."), |
| 588 | "marked public drill preserved" |
| 589 | ); |
| 590 | assert!( |
| 591 | !cleaned.contains("Legacy unmarked drill gets stripped."), |
| 592 | "unmarked drill stripped" |
| 593 | ); |
| 594 | assert!( |
| 595 | cleaned.contains("Public claim"), |
| 596 | "public freshness row preserved" |
| 597 | ); |
| 598 | assert!( |
| 599 | !cleaned.contains("corpus://secret.md"), |
| 600 | "private freshness row stripped" |
| 601 | ); |
| 602 | } |
| 603 | |
| 604 | #[test] |
| 605 | fn markdown_unbounded_private_brief_excluded() { |
| 606 | let brief = "🌐 last30days · Secret Topic · Nov 20, 2026 · v4.6.0\n\n\ |
| 607 | Based on Your files, here is the private summary.\n"; |
| 608 | assert!( |
| 609 | markdown::is_unbounded_private_brief(brief, Some("finished-brief")), |
| 610 | "unbounded private finished brief detected" |
| 611 | ); |
| 612 | assert_eq!( |
| 613 | markdown::public_markdown(brief), |
| 614 | "", |
| 615 | "unbounded private brief redacts to empty" |
| 616 | ); |
| 617 | } |
| 618 | |
| 619 | #[test] |
| 620 | fn markdown_batch_and_meta_parse() { |
| 621 | let doc = "# last30days v4.6.0: OpenClaw\n\n\ |
| 622 | - Date range: 2026-06-10 to 2026-07-01\n\n\ |
| 623 | ## Ranked Evidence Clusters\n\n\ |
| 624 | ### 1. MCP servers need permission boundaries (score 42, 2 items, sources: Reddit)\n\ |
| 625 | 1. [reddit] A useful thread\n\ |
| 626 | - URL: https://example.com/openclaw\n\ |
| 627 | - Evidence: MCP servers should isolate tools and credentials.\n"; |
| 628 | let meta = markdown::parse_markdown_meta( |
| 629 | doc, |
| 630 | "/tmp/openclaw-raw.md", |
| 631 | "openclaw-raw", |
| 632 | "markdown", |
| 633 | "2026-07-01", |
| 634 | ) |
| 635 | .unwrap(); |
| 636 | assert_eq!(meta.topic, "OpenClaw", "topic"); |
| 637 | assert_eq!(meta.slug, "openclaw", "slug"); |
| 638 | assert_eq!(meta.published_date, "2026-07-01", "date"); |
| 639 | assert_eq!( |
| 640 | meta.headline, "MCP servers need permission boundaries", |
| 641 | "headline" |
| 642 | ); |
| 643 | assert_eq!( |
| 644 | meta.summary, "MCP servers should isolate tools and credentials.", |
| 645 | "summary" |
| 646 | ); |
| 647 | assert!(!meta.unbounded_private, "public report"); |
| 648 | assert_eq!(meta.indexable_fingerprint.len(), 64, "sha256 hex length"); |
| 649 | |
| 650 | let batch = markdown::sanitize_markdown_batch(&[MarkdownBatchItem { |
| 651 | content: doc.to_owned(), |
| 652 | source_format: Some("markdown".to_owned()), |
| 653 | }]); |
| 654 | assert_eq!(batch.len(), 1, "batch length"); |
| 655 | assert_eq!( |
| 656 | batch[0].indexable_fingerprint, meta.indexable_fingerprint, |
| 657 | "fingerprint matches" |
| 658 | ); |
| 659 | } |
| 660 | |
| 661 | #[test] |
| 662 | fn markdown_nested_inline_emphasis_matches_python_clean_inline() { |
| 663 | let cases = [ |
| 664 | ("# **__Rust__**\n", "_Rust_", "rust"), |
| 665 | ("# __**Rust**__\n", "*Rust*", "rust"), |
| 666 | ( |
| 667 | "# **bold** and __bold2__\n", |
| 668 | "bold and bold2", |
| 669 | "bold-and-bold2", |
| 670 | ), |
| 671 | ("# snake_case_name\n", "snake_case_name", "snake-case-name"), |
| 672 | ( |
| 673 | "# **__nested__** and *italic* and `code`\n", |
| 674 | "_nested_ and italic and code", |
| 675 | "nested-and-italic-and-code", |
| 676 | ), |
| 677 | ]; |
| 678 | for (doc, expected_topic, expected_slug) in cases { |
| 679 | let meta = markdown::parse_markdown_meta( |
| 680 | doc, |
| 681 | "/tmp/sample.md", |
| 682 | "sample", |
| 683 | "markdown", |
| 684 | "2026-11-26", |
| 685 | ) |
| 686 | .unwrap(); |
| 687 | assert_eq!(meta.topic, expected_topic, "topic mismatch for {doc:?}"); |
| 688 | assert_eq!(meta.slug, expected_slug, "slug mismatch for {doc:?}"); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | #[test] |
| 693 | fn markdown_unicode_line_breaks_redact_private_sections() { |
| 694 | let breaks = [ |
| 695 | "\u{2028}", "\u{2029}", "\u{0085}", "\x0b", "\x0c", "\x1c", "\x1d", "\x1e", |
| 696 | ]; |
| 697 | for brk in breaks { |
| 698 | let doc = format!("Intro{brk}## From your files\nSecret note\n## Public\nVisible\n"); |
| 699 | let redacted = markdown::public_markdown(&doc); |
| 700 | assert!( |
| 701 | !redacted.contains("Secret note"), |
| 702 | "private section leaked after break {brk:?}: {redacted:?}" |
| 703 | ); |
| 704 | assert!( |
| 705 | redacted.contains("## Public\nVisible\n"), |
| 706 | "public section lost after break {brk:?}: {redacted:?}" |
| 707 | ); |
| 708 | assert_eq!(redacted, format!("Intro{brk}## Public\nVisible\n")); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | #[test] |
| 713 | fn markdown_nbsp_from_your_files_label_in_legacy_cluster_is_redacted() { |
| 714 | let doc = "## Ranked Evidence Clusters\n### 1. Notes\n\u{00a0}**From your files**\nSecret\n"; |
| 715 | let redacted = markdown::public_markdown(doc); |
| 716 | assert!( |
| 717 | !redacted.contains("Secret"), |
| 718 | "private note leaked when preceded by NBSP: {redacted:?}" |
| 719 | ); |
| 720 | assert!( |
| 721 | !redacted.contains("From your files"), |
| 722 | "private label leaked when preceded by NBSP: {redacted:?}" |
| 723 | ); |
| 724 | } |
| 725 | |
| 726 | #[test] |
| 727 | fn markdown_unicode_case_variants_from_your_files_are_redacted() { |
| 728 | for heading in [ |
| 729 | "## From your f\u{0131}les", |
| 730 | "## From your file\u{017f}", |
| 731 | "### From your f\u{0131}les", |
| 732 | "**From your f\u{0131}les**", |
| 733 | ] { |
| 734 | let doc = format!("{heading}\nSecret\n## Public\nVisible\n"); |
| 735 | let redacted = markdown::public_markdown(&doc); |
| 736 | assert!( |
| 737 | !redacted.contains("Secret"), |
| 738 | "private note leaked for {heading:?}: {redacted:?}" |
| 739 | ); |
| 740 | assert_eq!(redacted, "## Public\nVisible\n", "mismatch for {heading:?}"); |
| 741 | } |
| 742 | } |
| 743 |