@@ -14,14 +14,14 @@ fn utc_time_to_api_time(time: &UtcTime) -> String {
1414 time. to_rfc3339_opts ( chrono:: SecondsFormat :: Secs , use_z)
1515}
1616
17- #[ derive( Copy , Clone , Debug , Default ) ]
17+ #[ derive( Copy , Clone , Debug , Default , PartialEq , Eq ) ]
1818pub enum CommunityEventType {
1919 #[ default]
2020 OfficeHours ,
2121 SyncUp ,
2222}
2323
24- #[ derive( Clone , Debug , Default ) ]
24+ #[ derive( Clone , Debug , Default , PartialEq , Eq ) ]
2525pub struct CommunityEventDescriptionData {
2626 /// Type of the event, as specified by the user.
2727 pub event_type : CommunityEventType ,
@@ -423,9 +423,106 @@ pub async fn fetch_near_llvm_calendar_office_hour_events(
423423
424424#[ cfg( test) ]
425425mod test {
426+ use super :: * ;
427+
426428 #[ test]
427429 fn test_reqwest_url_parses ( ) {
428430 // This has an internal `unwrap`. If it doesn't crash, we're all good.
429431 let _ = super :: get_baseline_llvm_calendar_url ( ) ;
430432 }
433+
434+ #[ test]
435+ fn test_parse_plain_text_basic ( ) {
436+ // Minimal valid plain-text description.
437+ let basic_plain_text = "\
438+ discord-bot-event-type: office-hours
439+ discord-bot-channels-to-mention: #llvm-officehours
440+ discord-bot-mention: @alice, @bob
441+ " ;
442+ assert_eq ! (
443+ parse_event_description_data( "My Event" , basic_plain_text) ,
444+ Some ( CommunityEventDescriptionData {
445+ event_type: CommunityEventType :: OfficeHours ,
446+ mention_channels: vec![ "llvm-officehours" . into( ) ] ,
447+ mention_users: vec![ "alice" . into( ) , "bob" . into( ) ] ,
448+ ping_duration_before_start_mins: 30 ,
449+ extra_message: None ,
450+ } ) ,
451+ ) ;
452+ }
453+
454+ #[ test]
455+ fn test_parse_plain_text_sync_up_and_all_fields ( ) {
456+ let desc = "\
457+ discord-bot-event-type: sync-up
458+ discord-bot-channels-to-mention: #general, #announcements
459+ discord-bot-mention: @carol
460+ discord-bot-reminder-time-before-start: 15
461+ discord-bot-message: Don't forget!
462+ " ;
463+ assert_eq ! (
464+ parse_event_description_data( "Sync" , desc) ,
465+ Some ( CommunityEventDescriptionData {
466+ event_type: CommunityEventType :: SyncUp ,
467+ mention_channels: vec![ "general" . into( ) , "announcements" . into( ) ] ,
468+ mention_users: vec![ "carol" . into( ) ] ,
469+ ping_duration_before_start_mins: 15 ,
470+ extra_message: Some ( "Don't forget!" . into( ) ) ,
471+ } ) ,
472+ ) ;
473+ }
474+
475+ #[ test]
476+ fn test_parse_no_event_type_returns_none ( ) {
477+ let desc = "discord-bot-channels-to-mention: #llvm-officehours\n " ;
478+ assert ! ( parse_event_description_data( "My Event" , desc) . is_none( ) ) ;
479+ }
480+
481+ #[ test]
482+ fn test_parse_empty_description_returns_none ( ) {
483+ assert ! ( parse_event_description_data( "My Event" , "" ) . is_none( ) ) ;
484+ }
485+
486+ #[ test]
487+ fn test_parse_invalid_event_type_returns_none ( ) {
488+ let desc = "discord-bot-event-type: totally-made-up\n " ;
489+ assert ! ( parse_event_description_data( "My Event" , desc) . is_none( ) ) ;
490+ }
491+
492+ #[ test]
493+ fn test_parse_html_description ( ) {
494+ // Same content as basic_plain_text but wrapped in HTML with <br> line breaks.
495+ let html = "\
496+ discord-bot-event-type: office-hours <br>\
497+ discord-bot-channels-to-mention: #llvm-officehours<br>\
498+ discord-bot-mention: @alice<br>";
499+ assert_eq ! (
500+ parse_event_description_data( "My Event" , html) ,
501+ Some ( CommunityEventDescriptionData {
502+ event_type: CommunityEventType :: OfficeHours ,
503+ mention_channels: vec![ "llvm-officehours" . into( ) ] ,
504+ mention_users: vec![ "alice" . into( ) ] ,
505+ ping_duration_before_start_mins: 30 ,
506+ extra_message: None ,
507+ } ) ,
508+ ) ;
509+ }
510+
511+ #[ test]
512+ fn test_parse_comments_stripped ( ) {
513+ let desc = "\
514+ discord-bot-event-type: office-hours // this is a comment
515+ discord-bot-channels-to-mention: #llvm-officehours // another comment
516+ " ;
517+ assert_eq ! (
518+ parse_event_description_data( "My Event" , desc) ,
519+ Some ( CommunityEventDescriptionData {
520+ event_type: CommunityEventType :: OfficeHours ,
521+ mention_channels: vec![ "llvm-officehours" . into( ) ] ,
522+ mention_users: vec![ ] ,
523+ ping_duration_before_start_mins: 30 ,
524+ extra_message: None ,
525+ } ) ,
526+ ) ;
527+ }
431528}
0 commit comments