| 1 | use chatsvc::{CalendarEvent, ResponseType, ShowAs}; |
| 2 | |
| 3 | pub use crate::calendar_prefs::WeekendWorkWeek; |
| 4 | use chrono::{ |
| 5 | DateTime, Datelike, Duration, FixedOffset, Months, NaiveDate, NaiveTime, Utc, Weekday, |
| 6 | }; |
| 7 | |
| 8 | pub const MINUTES_PER_DAY: i64 = 24 * 60; |
| 9 | pub const MONTH_GRID_DAYS: i64 = 42; |
| 10 | |
| 11 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 12 | pub struct WeekBlock { |
| 13 | pub event_index: usize, |
| 14 | pub start_minute: i64, |
| 15 | pub end_minute: i64, |
| 16 | pub lane: usize, |
| 17 | pub lanes: usize, |
| 18 | } |
| 19 | |
| 20 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 21 | pub struct WeekDay { |
| 22 | pub date: NaiveDate, |
| 23 | pub all_day: Vec<usize>, |
| 24 | pub blocks: Vec<WeekBlock>, |
| 25 | } |
| 26 | |
| 27 | /// How weeks are laid out: first day, work days, and what Work week shows on a day off. |
| 28 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 29 | pub struct WeekLayout { |
| 30 | pub first_day: Weekday, |
| 31 | pub work_days: Vec<Weekday>, |
| 32 | pub weekend: WeekendWorkWeek, |
| 33 | } |
| 34 | |
| 35 | impl Default for WeekLayout { |
| 36 | fn default() -> Self { |
| 37 | WeekLayout { |
| 38 | first_day: Weekday::Mon, |
| 39 | work_days: vec![ |
| 40 | Weekday::Mon, |
| 41 | Weekday::Tue, |
| 42 | Weekday::Wed, |
| 43 | Weekday::Thu, |
| 44 | Weekday::Fri, |
| 45 | ], |
| 46 | weekend: WeekendWorkWeek::NextWeek, |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl WeekLayout { |
| 52 | pub fn is_work_day(&self, day: Weekday) -> bool { |
| 53 | self.work_days.contains(&day) |
| 54 | } |
| 55 | |
| 56 | /// Work days, or Monday to Friday when none are set (an empty work week shows nothing). |
| 57 | fn effective_work_days(&self) -> Vec<Weekday> { |
| 58 | if self.work_days.is_empty() { |
| 59 | WeekLayout::default().work_days |
| 60 | } else { |
| 61 | self.work_days.clone() |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /// The seven weekdays in order from the first day. |
| 66 | pub fn weekdays(&self) -> [Weekday; 7] { |
| 67 | let mut day = self.first_day; |
| 68 | std::array::from_fn(|_| { |
| 69 | let current = day; |
| 70 | day = day.succ(); |
| 71 | current |
| 72 | }) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | pub fn week_start(date: NaiveDate, first_day: Weekday) -> NaiveDate { |
| 77 | let back = (7 + date.weekday().num_days_from_monday() - first_day.num_days_from_monday()) % 7; |
| 78 | date - Duration::days(i64::from(back)) |
| 79 | } |
| 80 | |
| 81 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)] |
| 82 | #[serde(rename_all = "snake_case")] |
| 83 | pub enum CalendarSpan { |
| 84 | Day, |
| 85 | #[default] |
| 86 | WorkWeek, |
| 87 | Week, |
| 88 | Month, |
| 89 | } |
| 90 | |
| 91 | impl CalendarSpan { |
| 92 | pub const ALL: [CalendarSpan; 4] = [ |
| 93 | CalendarSpan::Day, |
| 94 | CalendarSpan::WorkWeek, |
| 95 | CalendarSpan::Week, |
| 96 | CalendarSpan::Month, |
| 97 | ]; |
| 98 | |
| 99 | /// Locale key of the span's name (`calendar.view.span_*`). |
| 100 | pub fn label_key(self) -> &'static str { |
| 101 | match self { |
| 102 | CalendarSpan::Day => "calendar.view.span_day", |
| 103 | CalendarSpan::WorkWeek => "calendar.view.span_work_week", |
| 104 | CalendarSpan::Week => "calendar.view.span_week", |
| 105 | CalendarSpan::Month => "calendar.view.span_month", |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | pub fn label(self) -> String { |
| 110 | app_base::ui_locale::tr(self.label_key()) |
| 111 | } |
| 112 | |
| 113 | /// First date of the range that contains `date`. |
| 114 | pub fn start_for(self, date: NaiveDate, layout: &WeekLayout) -> NaiveDate { |
| 115 | match self { |
| 116 | CalendarSpan::Day => date, |
| 117 | CalendarSpan::WorkWeek => work_week_for(date, layout), |
| 118 | CalendarSpan::Week => week_start(date, layout.first_day), |
| 119 | CalendarSpan::Month => month_grid_start(date, layout.first_day), |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | pub fn shifted(self, start: NaiveDate, steps: i32, layout: &WeekLayout) -> NaiveDate { |
| 124 | match self { |
| 125 | CalendarSpan::Day => start + Duration::days(i64::from(steps)), |
| 126 | CalendarSpan::WorkWeek | CalendarSpan::Week => { |
| 127 | start + Duration::weeks(i64::from(steps)) |
| 128 | } |
| 129 | CalendarSpan::Month => { |
| 130 | let month = month_shown(start); |
| 131 | let moved = if steps >= 0 { |
| 132 | month.checked_add_months(Months::new(steps.unsigned_abs())) |
| 133 | } else { |
| 134 | month.checked_sub_months(Months::new(steps.unsigned_abs())) |
| 135 | }; |
| 136 | month_grid_start(moved.unwrap_or(month), layout.first_day) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | pub fn title(self, start: NaiveDate, layout: &WeekLayout) -> String { |
| 142 | match self { |
| 143 | CalendarSpan::Month => app_base::format::month_year_label(month_shown(start)), |
| 144 | _ => { |
| 145 | let dates = visible_dates(self, start, layout); |
| 146 | let first = dates.first().copied().unwrap_or(start); |
| 147 | let last = dates.last().copied().unwrap_or(start); |
| 148 | app_base::format::calendar_range_label(first, last) |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /// The dates a span draws, in column order. Work week keeps only work days. |
| 155 | pub fn visible_dates(span: CalendarSpan, start: NaiveDate, layout: &WeekLayout) -> Vec<NaiveDate> { |
| 156 | let run = |days: i64| (0..days).map(|day| start + Duration::days(day)).collect(); |
| 157 | match span { |
| 158 | CalendarSpan::Day => vec![start], |
| 159 | CalendarSpan::Week => run(7), |
| 160 | CalendarSpan::Month => run(MONTH_GRID_DAYS), |
| 161 | CalendarSpan::WorkWeek => { |
| 162 | let work_days = layout.effective_work_days(); |
| 163 | (0..7) |
| 164 | .map(|day| start + Duration::days(day)) |
| 165 | .filter(|date| work_days.contains(&date.weekday())) |
| 166 | .collect() |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | pub fn first_of_month(date: NaiveDate) -> NaiveDate { |
| 172 | date.with_day(1).unwrap_or(date) |
| 173 | } |
| 174 | |
| 175 | pub fn month_grid_start(date: NaiveDate, first_day: Weekday) -> NaiveDate { |
| 176 | week_start(first_of_month(date), first_day) |
| 177 | } |
| 178 | |
| 179 | /// The grid starts up to six days before the 1st, so a week in is always inside the shown month. |
| 180 | pub fn month_shown(grid_start: NaiveDate) -> NaiveDate { |
| 181 | first_of_month(grid_start + Duration::weeks(1)) |
| 182 | } |
| 183 | |
| 184 | /// Start of the week Work week shows for `today`. |
| 185 | /// |
| 186 | /// On a day off with no work day left in this week, Outlook and Teams show the |
| 187 | /// coming week; `WeekendWorkWeek::ThisWeek` keeps the current one. |
| 188 | pub fn work_week_for(today: NaiveDate, layout: &WeekLayout) -> NaiveDate { |
| 189 | let week = week_start(today, layout.first_day); |
| 190 | let work_days = layout.effective_work_days(); |
| 191 | let work_left = (0..7) |
| 192 | .map(|day| week + Duration::days(day)) |
| 193 | .any(|date| date >= today && work_days.contains(&date.weekday())); |
| 194 | match layout.weekend { |
| 195 | WeekendWorkWeek::NextWeek if !work_left => week + Duration::weeks(1), |
| 196 | _ => week, |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | pub fn local_midnight(date: NaiveDate, offset: FixedOffset) -> DateTime<Utc> { |
| 201 | date.and_hms_opt(0, 0, 0) |
| 202 | .and_then(|local| local.and_local_timezone(offset).single()) |
| 203 | .map(|local| local.with_timezone(&Utc)) |
| 204 | .unwrap_or_else(|| date.and_hms_opt(0, 0, 0).unwrap_or_default().and_utc()) |
| 205 | } |
| 206 | |
| 207 | /// UTC range from the first date's midnight to the midnight after the last date. |
| 208 | pub fn dates_range(dates: &[NaiveDate], offset: FixedOffset) -> (DateTime<Utc>, DateTime<Utc>) { |
| 209 | let first = dates.first().copied().unwrap_or_default(); |
| 210 | let last = dates.last().copied().unwrap_or(first); |
| 211 | ( |
| 212 | local_midnight(first, offset), |
| 213 | local_midnight(last + Duration::days(1), offset), |
| 214 | ) |
| 215 | } |
| 216 | |
| 217 | pub fn week_range( |
| 218 | start: NaiveDate, |
| 219 | days: i64, |
| 220 | offset: FixedOffset, |
| 221 | ) -> (DateTime<Utc>, DateTime<Utc>) { |
| 222 | ( |
| 223 | local_midnight(start, offset), |
| 224 | local_midnight(start + Duration::days(days), offset), |
| 225 | ) |
| 226 | } |
| 227 | |
| 228 | pub fn shown_in_calendar(event: &CalendarEvent) -> bool { |
| 229 | event.response != ResponseType::Declined |
| 230 | } |
| 231 | |
| 232 | /// True all-day events, plus timed blocks that cover whole local days |
| 233 | /// (Google holidays/week markers as 00:00→00:00, or multi-day 00:00→23:59). |
| 234 | pub fn is_effective_all_day(event: &CalendarEvent, offset: FixedOffset) -> bool { |
| 235 | if event.all_day { |
| 236 | return true; |
| 237 | } |
| 238 | let start = event.start.with_timezone(&offset); |
| 239 | let end = event.end.with_timezone(&offset); |
| 240 | if start.time() != NaiveTime::MIN || end <= start { |
| 241 | return false; |
| 242 | } |
| 243 | if end.time() == NaiveTime::MIN { |
| 244 | return true; |
| 245 | } |
| 246 | // Multi-day blocks that run from midnight through end-of-day (e.g. 23:59). |
| 247 | end.date_naive() > start.date_naive() |
| 248 | && end.time() >= NaiveTime::from_hms_opt(23, 59, 0).unwrap() |
| 249 | } |
| 250 | |
| 251 | pub fn week_days( |
| 252 | events: &[CalendarEvent], |
| 253 | dates: &[NaiveDate], |
| 254 | offset: FixedOffset, |
| 255 | ) -> Vec<WeekDay> { |
| 256 | dates |
| 257 | .iter() |
| 258 | .map(|&date| { |
| 259 | let (day_start, day_end) = week_range(date, 1, offset); |
| 260 | let mut all_day = Vec::new(); |
| 261 | let mut spans = Vec::new(); |
| 262 | for (index, event) in events.iter().enumerate() { |
| 263 | if !shown_in_calendar(event) || event.end <= day_start || event.start >= day_end { |
| 264 | continue; |
| 265 | } |
| 266 | if is_effective_all_day(event, offset) { |
| 267 | all_day.push(index); |
| 268 | continue; |
| 269 | } |
| 270 | let start_minute = (event.start.max(day_start) - day_start).num_minutes(); |
| 271 | let end_minute = (event.end.min(day_end) - day_start).num_minutes(); |
| 272 | spans.push((index, start_minute, end_minute.max(start_minute + 1))); |
| 273 | } |
| 274 | WeekDay { |
| 275 | date, |
| 276 | all_day, |
| 277 | blocks: lay_out(spans), |
| 278 | } |
| 279 | }) |
| 280 | .collect() |
| 281 | } |
| 282 | |
| 283 | fn lay_out(mut spans: Vec<(usize, i64, i64)>) -> Vec<WeekBlock> { |
| 284 | spans.sort_by_key(|&(index, start, end)| (start, std::cmp::Reverse(end), index)); |
| 285 | let mut blocks: Vec<WeekBlock> = Vec::with_capacity(spans.len()); |
| 286 | let mut cluster_start = 0; |
| 287 | let mut cluster_end = i64::MIN; |
| 288 | let mut lane_ends: Vec<i64> = Vec::new(); |
| 289 | for (index, start, end) in spans { |
| 290 | if start >= cluster_end { |
| 291 | close_cluster(&mut blocks[cluster_start..], lane_ends.len()); |
| 292 | cluster_start = blocks.len(); |
| 293 | lane_ends.clear(); |
| 294 | } |
| 295 | let lane = match lane_ends.iter().position(|&lane_end| lane_end <= start) { |
| 296 | Some(free) => { |
| 297 | lane_ends[free] = end; |
| 298 | free |
| 299 | } |
| 300 | None => { |
| 301 | lane_ends.push(end); |
| 302 | lane_ends.len() - 1 |
| 303 | } |
| 304 | }; |
| 305 | cluster_end = cluster_end.max(end); |
| 306 | blocks.push(WeekBlock { |
| 307 | event_index: index, |
| 308 | start_minute: start, |
| 309 | end_minute: end, |
| 310 | lane, |
| 311 | lanes: 1, |
| 312 | }); |
| 313 | } |
| 314 | close_cluster(&mut blocks[cluster_start..], lane_ends.len()); |
| 315 | blocks |
| 316 | } |
| 317 | |
| 318 | fn close_cluster(blocks: &mut [WeekBlock], lanes: usize) { |
| 319 | for block in blocks { |
| 320 | block.lanes = lanes.max(1); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 325 | pub enum EventLook { |
| 326 | Busy, |
| 327 | Free, |
| 328 | Tentative, |
| 329 | } |
| 330 | |
| 331 | pub fn event_look(event: &CalendarEvent) -> EventLook { |
| 332 | match event.show_as { |
| 333 | ShowAs::Free => EventLook::Free, |
| 334 | ShowAs::Tentative => EventLook::Tentative, |
| 335 | ShowAs::Busy | ShowAs::OutOfOffice | ShowAs::WorkingElsewhere => EventLook::Busy, |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | pub fn hour_label(hour: u32, twelve_hour: bool) -> String { |
| 340 | if !twelve_hour { |
| 341 | return hour.to_string(); |
| 342 | } |
| 343 | match hour { |
| 344 | 0 => "12 AM".to_owned(), |
| 345 | 12 => "12 PM".to_owned(), |
| 346 | hour if hour < 12 => format!("{hour} AM"), |
| 347 | hour => format!("{} PM", hour - 12), |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | #[cfg(test)] |
| 352 | mod tests { |
| 353 | use chrono::TimeZone; |
| 354 | |
| 355 | use super::*; |
| 356 | |
| 357 | fn event(start_hour: u32, start_minute: u32, minutes: i64) -> CalendarEvent { |
| 358 | let start = Utc |
| 359 | .with_ymd_and_hms(2026, 11, 9, start_hour, start_minute, 0) |
| 360 | .unwrap(); |
| 361 | CalendarEvent { |
| 362 | object_id: format!("{start_hour}:{start_minute}"), |
| 363 | subject: "Sync".into(), |
| 364 | start, |
| 365 | end: start + Duration::minutes(minutes), |
| 366 | all_day: false, |
| 367 | cancelled: false, |
| 368 | online: true, |
| 369 | join_url: None, |
| 370 | thread_id: None, |
| 371 | organizer_name: None, |
| 372 | organizer_address: None, |
| 373 | ical_uid: None, |
| 374 | show_as: Default::default(), |
| 375 | dial_in: None, |
| 376 | response: ResponseType::Accepted, |
| 377 | ..Default::default() |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | fn utc() -> FixedOffset { |
| 382 | FixedOffset::east_opt(0).unwrap() |
| 383 | } |
| 384 | |
| 385 | fn run(start: NaiveDate, days: i64) -> Vec<NaiveDate> { |
| 386 | (0..days).map(|day| start + Duration::days(day)).collect() |
| 387 | } |
| 388 | |
| 389 | fn nov(day: u32) -> NaiveDate { |
| 390 | NaiveDate::from_ymd_opt(2026, 11, day).unwrap() |
| 391 | } |
| 392 | |
| 393 | fn layout() -> WeekLayout { |
| 394 | WeekLayout::default() |
| 395 | } |
| 396 | |
| 397 | fn sunday_first() -> WeekLayout { |
| 398 | WeekLayout { |
| 399 | first_day: Weekday::Sun, |
| 400 | ..WeekLayout::default() |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | #[test] |
| 405 | fn month_view_steps_whole_months_from_a_monday_grid() { |
| 406 | let layout = layout(); |
| 407 | let start = CalendarSpan::Month.start_for(nov(18), &layout); |
| 408 | assert_eq!(start, NaiveDate::from_ymd_opt(2026, 10, 26).unwrap()); |
| 409 | assert_eq!(month_shown(start), nov(1)); |
| 410 | let next = CalendarSpan::Month.shifted(start, 1, &layout); |
| 411 | assert_eq!(next, nov(30)); |
| 412 | let back = CalendarSpan::Month.shifted(start, -11, &layout); |
| 413 | assert_eq!( |
| 414 | month_shown(back), |
| 415 | NaiveDate::from_ymd_opt(2025, 12, 1).unwrap() |
| 416 | ); |
| 417 | assert_eq!( |
| 418 | CalendarSpan::Week.shifted(nov(2), -1, &layout), |
| 419 | nov(2) - Duration::weeks(1) |
| 420 | ); |
| 421 | } |
| 422 | |
| 423 | #[test] |
| 424 | fn a_sunday_first_week_and_month_start_on_sunday() { |
| 425 | let layout = sunday_first(); |
| 426 | assert_eq!(CalendarSpan::Week.start_for(nov(18), &layout), nov(15)); |
| 427 | assert_eq!( |
| 428 | visible_dates(CalendarSpan::Week, nov(15), &layout), |
| 429 | run(nov(15), 7) |
| 430 | ); |
| 431 | assert_eq!( |
| 432 | CalendarSpan::Month.start_for(nov(18), &layout), |
| 433 | NaiveDate::from_ymd_opt(2026, 11, 1).unwrap() |
| 434 | ); |
| 435 | assert_eq!(layout.weekdays()[0], Weekday::Sun); |
| 436 | assert_eq!(layout.weekdays()[6], Weekday::Sat); |
| 437 | } |
| 438 | |
| 439 | #[test] |
| 440 | fn a_saturday_first_week_starts_on_saturday() { |
| 441 | let layout = WeekLayout { |
| 442 | first_day: Weekday::Sat, |
| 443 | ..WeekLayout::default() |
| 444 | }; |
| 445 | assert_eq!(CalendarSpan::Week.start_for(nov(18), &layout), nov(14)); |
| 446 | assert_eq!(week_start(nov(14), Weekday::Sat), nov(14)); |
| 447 | } |
| 448 | |
| 449 | #[test] |
| 450 | fn work_week_shows_only_the_work_days_even_with_gaps() { |
| 451 | let layout = WeekLayout { |
| 452 | work_days: vec![Weekday::Mon, Weekday::Tue, Weekday::Thu], |
| 453 | ..WeekLayout::default() |
| 454 | }; |
| 455 | let start = CalendarSpan::WorkWeek.start_for(nov(18), &layout); |
| 456 | assert_eq!( |
| 457 | visible_dates(CalendarSpan::WorkWeek, start, &layout), |
| 458 | [nov(16), nov(17), nov(19)] |
| 459 | ); |
| 460 | } |
| 461 | |
| 462 | #[test] |
| 463 | fn on_sunday_work_week_shows_next_or_this_week() { |
| 464 | let next = layout(); |
| 465 | assert_eq!(CalendarSpan::WorkWeek.start_for(nov(15), &next), nov(16)); |
| 466 | let this = WeekLayout { |
| 467 | weekend: WeekendWorkWeek::ThisWeek, |
| 468 | ..WeekLayout::default() |
| 469 | }; |
| 470 | assert_eq!(CalendarSpan::WorkWeek.start_for(nov(15), &this), nov(9)); |
| 471 | // Sunday-first weeks still have work days ahead on Sunday. |
| 472 | assert_eq!( |
| 473 | CalendarSpan::WorkWeek.start_for(nov(15), &sunday_first()), |
| 474 | nov(15) |
| 475 | ); |
| 476 | assert_eq!( |
| 477 | visible_dates(CalendarSpan::WorkWeek, nov(15), &sunday_first()), |
| 478 | run(nov(16), 5) |
| 479 | ); |
| 480 | } |
| 481 | |
| 482 | #[test] |
| 483 | fn the_week_starts_on_monday() { |
| 484 | let sunday = nov(15); |
| 485 | assert_eq!(week_start(sunday, Weekday::Mon), nov(9)); |
| 486 | assert_eq!( |
| 487 | week_start(week_start(sunday, Weekday::Mon), Weekday::Mon), |
| 488 | nov(9) |
| 489 | ); |
| 490 | assert_eq!(work_week_for(sunday, &layout()), nov(16)); |
| 491 | assert_eq!(work_week_for(nov(13), &layout()), nov(9)); |
| 492 | } |
| 493 | |
| 494 | #[test] |
| 495 | fn overlapping_meetings_share_the_column_in_lanes() { |
| 496 | let events = vec![ |
| 497 | event(9, 0, 60), |
| 498 | event(9, 30, 60), |
| 499 | event(10, 0, 30), |
| 500 | event(13, 0, 30), |
| 501 | ]; |
| 502 | let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(); |
| 503 | let days = week_days(&events, &run(monday, 5), utc()); |
| 504 | assert_eq!(days.len(), 5); |
| 505 | let lanes: Vec<(usize, usize, usize)> = days[0] |
| 506 | .blocks |
| 507 | .iter() |
| 508 | .map(|block| (block.event_index, block.lane, block.lanes)) |
| 509 | .collect(); |
| 510 | assert_eq!(lanes, [(0, 0, 2), (1, 1, 2), (2, 0, 2), (3, 0, 1)]); |
| 511 | assert_eq!(days[0].blocks[0].start_minute, 9 * 60); |
| 512 | assert!(days[1].blocks.is_empty()); |
| 513 | } |
| 514 | |
| 515 | #[test] |
| 516 | fn declined_and_other_days_are_left_out_and_all_day_is_separate() { |
| 517 | let mut declined = event(9, 0, 30); |
| 518 | declined.response = ResponseType::Declined; |
| 519 | let mut all_day = event(0, 0, 24 * 60); |
| 520 | all_day.all_day = true; |
| 521 | let late = event(23, 30, 60); |
| 522 | let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(); |
| 523 | let days = week_days(&[declined, all_day, late], &run(monday, 2), utc()); |
| 524 | assert!(days[0].blocks.iter().all(|block| block.event_index != 0)); |
| 525 | assert_eq!(days[0].all_day, [1]); |
| 526 | assert_eq!(days[0].blocks[0].end_minute, MINUTES_PER_DAY); |
| 527 | assert_eq!( |
| 528 | (days[1].blocks[0].start_minute, days[1].blocks[0].end_minute), |
| 529 | (0, 30) |
| 530 | ); |
| 531 | } |
| 532 | |
| 533 | #[test] |
| 534 | fn midnight_to_midnight_timed_events_sit_in_the_all_day_row() { |
| 535 | // Google holidays / week markers often arrive as dateTime 00:00→00:00. |
| 536 | let holiday = event(0, 0, 24 * 60); |
| 537 | let meeting = event(9, 30, 60); |
| 538 | let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(); |
| 539 | assert!(is_effective_all_day(&holiday, utc())); |
| 540 | assert!(!is_effective_all_day(&meeting, utc())); |
| 541 | let days = week_days(&[holiday, meeting], &run(monday, 1), utc()); |
| 542 | assert_eq!(days[0].all_day, [0]); |
| 543 | assert_eq!(days[0].blocks.len(), 1); |
| 544 | assert_eq!(days[0].blocks[0].event_index, 1); |
| 545 | } |
| 546 | |
| 547 | #[test] |
| 548 | fn multi_day_midnight_to_end_of_day_is_all_day() { |
| 549 | // e.g. KubeCon as 00:00 Mon → 23:59 Thu. |
| 550 | let conference = event(0, 0, 4 * 24 * 60 - 1); |
| 551 | assert!(is_effective_all_day(&conference, utc())); |
| 552 | let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(); |
| 553 | let days = week_days(&[conference], &run(monday, 5), utc()); |
| 554 | assert!(days.iter().all(|day| day.blocks.is_empty())); |
| 555 | assert_eq!(days[0].all_day, [0]); |
| 556 | assert_eq!(days[3].all_day, [0]); |
| 557 | } |
| 558 | |
| 559 | #[test] |
| 560 | fn spans_start_and_step_like_teams() { |
| 561 | let layout = layout(); |
| 562 | let (wednesday, monday) = (nov(11), nov(9)); |
| 563 | assert_eq!(CalendarSpan::Day.start_for(wednesday, &layout), wednesday); |
| 564 | assert_eq!(CalendarSpan::WorkWeek.start_for(wednesday, &layout), monday); |
| 565 | assert_eq!(CalendarSpan::Week.start_for(wednesday, &layout), monday); |
| 566 | assert_eq!(CalendarSpan::Week.start_for(nov(15), &layout), monday); |
| 567 | assert_eq!( |
| 568 | CalendarSpan::Day.shifted(wednesday, 1, &layout), |
| 569 | wednesday + Duration::days(1) |
| 570 | ); |
| 571 | assert_eq!(visible_dates(CalendarSpan::Week, monday, &layout).len(), 7); |
| 572 | assert_eq!( |
| 573 | visible_dates(CalendarSpan::WorkWeek, monday, &layout).len(), |
| 574 | 5 |
| 575 | ); |
| 576 | assert_eq!( |
| 577 | dates_range(&run(monday, 5), utc()), |
| 578 | ( |
| 579 | local_midnight(monday, utc()), |
| 580 | local_midnight(nov(14), utc()) |
| 581 | ) |
| 582 | ); |
| 583 | } |
| 584 | |
| 585 | #[test] |
| 586 | fn labels_read_like_teams() { |
| 587 | assert_eq!(hour_label(0, true), "12 AM"); |
| 588 | assert_eq!(hour_label(13, true), "1 PM"); |
| 589 | assert_eq!(hour_label(13, false), "13"); |
| 590 | } |
| 591 | |
| 592 | #[test] |
| 593 | fn free_and_tentative_meetings_look_like_teams() { |
| 594 | let mut following = event(9, 0, 30); |
| 595 | following.show_as = ShowAs::Free; |
| 596 | following.response = ResponseType::Tentative; |
| 597 | let mut tentative = event(9, 0, 30); |
| 598 | tentative.show_as = ShowAs::Tentative; |
| 599 | let mut away = event(9, 0, 30); |
| 600 | away.show_as = ShowAs::OutOfOffice; |
| 601 | assert_eq!(event_look(&following), EventLook::Free); |
| 602 | assert_eq!(event_look(&tentative), EventLook::Tentative); |
| 603 | assert_eq!(event_look(&away), EventLook::Busy); |
| 604 | assert_eq!(event_look(&event(9, 0, 30)), EventLook::Busy); |
| 605 | } |
| 606 | } |