From 2f0a3f50b8866d785575af9536edaafa4aeb0c5d Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Tue, 26 Aug 2025 21:24:49 -0700 Subject: [PATCH] server: move tests to the bottom of the file --- server/src/email_extract.rs | 183 ++++++++++++++++++------------------ 1 file changed, 92 insertions(+), 91 deletions(-) diff --git a/server/src/email_extract.rs b/server/src/email_extract.rs index 94e4f2b..c374c98 100644 --- a/server/src/email_extract.rs +++ b/server/src/email_extract.rs @@ -1,93 +1,3 @@ -// --- TESTS --- -#[cfg(test)] -mod tests { - use super::*; - #[test] - fn google_calendar_email_renders_ical_summary() { - use mailparse::parse_mail; - let raw_email = include_str!("../../server/testdata/google-calendar-example.eml"); - let parsed = parse_mail(raw_email.as_bytes()).expect("parse_mail"); - let mut part_addr = vec![]; - let body = extract_body(&parsed, &mut part_addr).expect("extract_body"); - let meta = extract_calendar_metadata_from_mail(&parsed, &body); - // Assert detection as Google Calendar - assert!(meta.is_google_calendar_event); - // Assert metadata extraction - assert_eq!(meta.summary, Some("Tamara and Scout in Alaska".to_string())); - assert_eq!(meta.organizer, Some("tconvertino@gmail.com".to_string())); - assert_eq!(meta.start_date, Some("20250624".to_string())); - assert_eq!(meta.end_date, Some("20250701".to_string())); - // Debug: print the rendered HTML for inspection - if let Some(ref html) = meta.body_html { - println!("Rendered HTML: {}", html); - } else { - println!("No body_html rendered"); - } - // Assert ical summary is rendered and prepended (look for 'ical-flex' class) - assert!(meta - .body_html - .as_ref() - .map(|h| h.contains("ical-flex")) - .unwrap_or(false)); - } - - #[test] - fn google_calendar_email_2_renders_ical_summary() { - use mailparse::parse_mail; - let raw_email = include_str!("../../server/testdata/google-calendar-example-2.eml"); - let parsed = parse_mail(raw_email.as_bytes()).expect("parse_mail"); - let mut part_addr = vec![]; - let body = extract_body(&parsed, &mut part_addr).expect("extract_body"); - let meta = extract_calendar_metadata_from_mail(&parsed, &body); - // Assert detection as Google Calendar - assert!(meta.is_google_calendar_event); - // Assert metadata extraction (update these values to match the new .eml) - assert_eq!(meta.summary, Some("McClure BLT".to_string())); - // Organizer: from From header, extract email address - assert_eq!( - meta.organizer, - Some("calendar-notification@google.com".to_string()) - ); - // Dates: from subject, Thu Sep 11 to Fri Jan 30, 2026 - let current_year = chrono::Local::now().year(); - assert_eq!(meta.start_date, Some(format!("{}0911", current_year))); - assert_eq!(meta.end_date, Some("20260131".to_string())); - // Debug: print the rendered HTML for inspection - if let Some(ref html) = meta.body_html { - println!("Rendered HTML: {}", html); - } else { - println!("No body_html rendered"); - } - // Assert ical summary is rendered and prepended (look for 'ical-flex' class) - assert!(meta - .body_html - .as_ref() - .map(|h| h.contains("ical-flex")) - .unwrap_or(false)); - } - - #[test] - fn recurring_event_rrule_metadata_and_highlight() { - use super::render_ical_summary; - // This .ics should contain an RRULE for recurrence - let ical = include_str!("../../server/testdata/ical-straddle.ics"); - let html = render_ical_summary(&ical).expect("render ical summary"); - // Should mention recurrence in the display - assert!(html.contains("Repeats") || html.contains("recurr") || html.contains("RRULE")); - - // Should only highlight the correct days (not all days between start and end) - // For a weekly event, check that only the correct weekday cells are highlighted - // (e.g., if event is every Monday, only Mondays are highlighted) - // This is a weak test: just check that not all days in the range are highlighted - let highlighted = html.matches("background:#ffd700").count(); - let total_days = html.matches(" 0, "Should highlight at least one day"); - assert!( - highlighted < total_days / 2, - "Should not highlight all days" - ); - } -} #[derive(Debug, PartialEq)] pub struct ExtractedCalendarMetadata { pub is_google_calendar_event: bool, @@ -2095,6 +2005,7 @@ pub fn render_ical_summary(ical_data: &str) -> Result { added += 1; } } + (fmt_start, fmt_end, days, recurrence_display) } else { // No RRULE: just add all days between start and end let d = start.date_naive(); @@ -2112,8 +2023,8 @@ pub fn render_ical_summary(ical_data: &str) -> Result { days.push(day_iter); day_iter = day_iter.succ_opt().unwrap(); } + (fmt_start, fmt_end, days, recurrence_display) } - (fmt_start, fmt_end, days, recurrence_display) } else { (String::new(), String::new(), vec![], String::new()) }; @@ -2260,3 +2171,93 @@ fn parse_ical_datetime_tz(dt: &str, tz: Tz) -> Option> { None } } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn google_calendar_email_renders_ical_summary() { + use mailparse::parse_mail; + let raw_email = include_str!("../../server/testdata/google-calendar-example.eml"); + let parsed = parse_mail(raw_email.as_bytes()).expect("parse_mail"); + let mut part_addr = vec![]; + let body = extract_body(&parsed, &mut part_addr).expect("extract_body"); + let meta = extract_calendar_metadata_from_mail(&parsed, &body); + // Assert detection as Google Calendar + assert!(meta.is_google_calendar_event); + // Assert metadata extraction + assert_eq!(meta.summary, Some("Tamara and Scout in Alaska".to_string())); + assert_eq!(meta.organizer, Some("tconvertino@gmail.com".to_string())); + assert_eq!(meta.start_date, Some("20250624".to_string())); + assert_eq!(meta.end_date, Some("20250701".to_string())); + // Debug: print the rendered HTML for inspection + if let Some(ref html) = meta.body_html { + println!("Rendered HTML: {}", html); + } else { + println!("No body_html rendered"); + } + // Assert ical summary is rendered and prepended (look for 'ical-flex' class) + assert!(meta + .body_html + .as_ref() + .map(|h| h.contains("ical-flex")) + .unwrap_or(false)); + } + + #[test] + fn google_calendar_email_2_renders_ical_summary() { + use mailparse::parse_mail; + let raw_email = include_str!("../../server/testdata/google-calendar-example-2.eml"); + let parsed = parse_mail(raw_email.as_bytes()).expect("parse_mail"); + let mut part_addr = vec![]; + let body = extract_body(&parsed, &mut part_addr).expect("extract_body"); + let meta = extract_calendar_metadata_from_mail(&parsed, &body); + // Assert detection as Google Calendar + assert!(meta.is_google_calendar_event); + // Assert metadata extraction (update these values to match the new .eml) + assert_eq!(meta.summary, Some("McClure BLT".to_string())); + // Organizer: from From header, extract email address + assert_eq!( + meta.organizer, + Some("calendar-notification@google.com".to_string()) + ); + // Dates: from subject, Thu Sep 11 to Fri Jan 30, 2026 + let current_year = chrono::Local::now().year(); + assert_eq!(meta.start_date, Some(format!("{}0911", current_year))); + assert_eq!(meta.end_date, Some("20260131".to_string())); + // Debug: print the rendered HTML for inspection + if let Some(ref html) = meta.body_html { + println!("Rendered HTML: {}", html); + } else { + println!("No body_html rendered"); + } + // Assert ical summary is rendered and prepended (look for 'ical-flex' class) + assert!(meta + .body_html + .as_ref() + .map(|h| h.contains("ical-flex")) + .unwrap_or(false)); + } + + #[test] + fn recurring_event_rrule_metadata_and_highlight() { + use super::render_ical_summary; + // This .ics should contain an RRULE for recurrence + let ical = include_str!("../../server/testdata/ical-straddle.ics"); + let html = render_ical_summary(&ical).expect("render ical summary"); + // Should mention recurrence in the display + assert!(html.contains("Repeats") || html.contains("recurr") || html.contains("RRULE")); + + // Should only highlight the correct days (not all days between start and end) + // For a weekly event, check that only the correct weekday cells are highlighted + // (e.g., if event is every Monday, only Mondays are highlighted) + // This is a weak test: just check that not all days in the range are highlighted + let highlighted = html.matches("background:#ffd700").count(); + let total_days = html.matches(" 0, "Should highlight at least one day"); + assert!( + highlighted < total_days / 2, + "Should not highlight all days" + ); + } +}