//! The feedback engine: how the voices on an item are counted, and the title //! line a body yields, with vectors pinning the one string-valued function. use super::*; fn row(id: &str, person_id: Option<&str>, same_as: Option<&str>) -> FeedbackVoiceRow { FeedbackVoiceRow { id: id.to_string(), person_id: person_id.map(str::to_string), same_as: same_as.map(str::to_string), } } fn voices(rows: &[FeedbackVoiceRow], id: &str) -> Option { feedback_voices(rows).remove(id) } fn some(count: usize, people: &[&str]) -> Option { Some(FeedbackVoices { voices: count, people: people.iter().map(|p| (*p).to_string()).collect(), }) } #[test] fn counts_a_lone_report_as_one_voice() { let rows = [row("f1", Some("p1"), None)]; assert_eq!(voices(&rows, "f1"), some(1, &["p1"])); } #[test] fn adds_every_duplicates_reporter_to_the_item_it_repeats() { let rows = [ row("f1", Some("p1"), None), row("f2", Some("p2"), Some("f1")), row("f3", None, Some("f1")), ]; assert_eq!(voices(&rows, "f1"), some(3, &["p1", "p2"])); // A duplicate still speaks for itself — the weight sits on the canonical // item, which is the whole point of pointing at one. assert_eq!(voices(&rows, "f2"), some(1, &["p2"])); } #[test] fn counts_one_person_once_however_many_times_they_said_it() { let rows = [ row("f1", Some("p1"), None), row("f2", Some("p1"), Some("f1")), row("f3", Some("p1"), Some("f1")), ]; assert_eq!(voices(&rows, "f1"), some(1, &["p1"])); } /// Three reports from three strangers are three voices, not one /// "unknown" — a count that ignored them would under-weigh exactly the reports /// nobody has a row for yet. #[test] fn counts_each_unidentified_reporter_separately() { let rows = [ row("f1", None, None), row("f2", None, Some("f1")), row("f3", None, Some("f1")), ]; assert_eq!(voices(&rows, "f1"), some(3, &[])); } #[test] fn walks_no_chain_a_pointer_at_a_pointer_adds_its_voice_to_what_it_names() { let rows = [ row("f1", Some("p1"), None), row("f2", Some("p2"), Some("f1")), row("f3", Some("p3"), Some("f2")), ]; assert_eq!( voices(&rows, "f1").map(|v| v.people), Some(vec!["p1".to_string(), "p2".into()]) ); assert_eq!( voices(&rows, "f2").map(|v| v.people), Some(vec!["p2".to_string(), "p3".into()]) ); } #[test] fn counts_nothing_for_nothing() { assert!(feedback_voices(&[]).is_empty()); } /// The `people` list is "who agreed, in the order they were first seen", so the /// order is part of the answer and not an accident of iteration. #[test] fn keeps_the_people_in_first_seen_order() { let rows = [ row("f1", Some("zed"), None), row("f2", Some("amy"), Some("f1")), row("f3", Some("bob"), Some("f1")), ]; assert_eq!( voices(&rows, "f1").map(|v| v.people), Some(vec!["zed".to_string(), "amy".into(), "bob".into()]) ); } // ── feedbackLine ───────────────────────────────────────────────────────────── #[test] fn takes_the_first_line_as_the_title() { assert_eq!( feedback_line("The total is wrong.\nSteps: open the cart."), "The total is wrong." ); } #[test] fn falls_back_to_the_whole_body_when_it_opens_with_blank_lines() { assert_eq!( feedback_line("\n\nThe total is wrong."), "The total is wrong." ); } #[test] fn caps_a_long_line_with_an_ellipsis_rather_than_running_the_page() { let line = feedback_line(&"x".repeat(400)); assert_eq!(line.encode_utf16().count(), 120); assert!(line.ends_with('…')); } #[test] fn leaves_a_line_at_the_cap_alone() { let exact = "y".repeat(120); assert_eq!(feedback_line(&exact), exact); } /// Node vectors — the cut is at 119 UTF-16 code units, the tail is trimmed, and /// the ellipsis is one character. #[test] fn feedback_line_matches_the_typescript() { let vectors: &[(String, String)] = &[ ( "The total is wrong.\nSteps: open the cart.".into(), "The total is wrong.".into(), ), ( "\n\nThe total is wrong.".into(), "The total is wrong.".into(), ), ("".into(), "".into()), (" ".into(), "".into()), ("\n".into(), "".into()), (" padded \nsecond".into(), "padded".into()), ("y".repeat(120), "y".repeat(120)), ("x".repeat(400), format!("{}…", "x".repeat(119))), // The trim before the ellipsis: 118 chars, a space, then more. ( format!("{} {}", "z".repeat(118), "w".repeat(50)), format!("{}…", "z".repeat(118)), ), // An emoji is TWO code units, so the cap counts it as two — and the cut // can land INSIDE one, which is what `String.prototype.slice` does. // JavaScript keeps the lone high surrogate (`\ud83c`); a Rust `String` // cannot hold one at all, so `from_utf16_lossy` writes U+FFFD in its // place. That is the only divergence in this module, it is one // replacement character at the very end of a truncated line, and both // sides are 120 code units long. Making Rust match exactly would mean // carrying the whole line as `Vec` through the app. ("🎉".repeat(80), format!("{}\u{FFFD}…", "🎉".repeat(59))), ]; for (body, expected) in vectors { assert_eq!( &feedback_line(body), expected, "for a body of {} units", body.encode_utf16().count() ); } } /// RULE: Feedback list titles are plain previews of their body, so a backup notice never leaks code-span markers; the full body remains unchanged. #[test] fn feedback_previews_strip_markdown_before_clipping() { assert_eq!(feedback_line("The backup needs `settings` and [help](https://example.com)."), "The backup needs settings and help."); } /// RULE: An older system backup notice resolves its list title from typed context; developer instructions stay in its stored body and never become a person's list line. #[test] fn backup_notice_list_line_uses_context_instead_of_developer_body() { let context = serde_json::json!({"kind":"backup-failed", "machine":"Example Mac", "reason":"run `scripts/example-backup.sh`"}); let line = super::system_notice_line(&context).unwrap(); assert!(line.contains("Example Mac")); assert!(!line.contains("scripts/") && !line.contains('`')); assert_eq!(super::system_notice_line(&serde_json::json!({"kind":"report"})), None); }