| 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 | /// Position of a Google calendar in the calendar list; Outlook is 0. |
| 229 | pub fn google_calendar_index( |
| 230 | accounts: &store::CalendarAccounts, |
| 231 | account_id: &str, |
| 232 | calendar_id: &str, |
| 233 | ) -> Option<usize> { |
| 234 | accounts |
| 235 | .google_accounts |
| 236 | .iter() |
| 237 | .flat_map(|account| { |
| 238 | account |
| 239 | .calendars |
| 240 | .iter() |
| 241 | .map(move |calendar| (account.id.as_str(), calendar.id.as_str())) |
| 242 | }) |
| 243 | .position(|(account, calendar)| account == account_id && calendar == calendar_id) |
| 244 | .map(|position| position + 1) |
| 245 | } |
| 246 | |
| 247 | /// Calendar list position that colours an event (merged events keep Outlook's colour). |
| 248 | pub fn calendar_index(event: &CalendarEvent, accounts: &store::CalendarAccounts) -> usize { |
| 249 | match event.source { |
| 250 | chatsvc::CalendarSource::Outlook => 0, |
| 251 | chatsvc::CalendarSource::Google => event |
| 252 | .account_id |
| 253 | .as_deref() |
| 254 | .zip(event.calendar_id.as_deref()) |
| 255 | .and_then(|(account, calendar)| google_calendar_index(accounts, account, calendar)) |
| 256 | .unwrap_or(1), |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | pub fn shown_in_calendar(event: &CalendarEvent) -> bool { |
| 261 | event.response != ResponseType::Declined |
| 262 | } |
| 263 | |
| 264 | /// True all-day events, plus timed blocks that cover whole local days |
| 265 | /// (Google holidays/week markers as 00:00→00:00, or multi-day 00:00→23:59). |
| 266 | pub fn is_effective_all_day(event: &CalendarEvent, offset: FixedOffset) -> bool { |
| 267 | if event.all_day { |
| 268 | return true; |
| 269 | } |
| 270 | let start = event.start.with_timezone(&offset); |
| 271 | let end = event.end.with_timezone(&offset); |
| 272 | if start.time() != NaiveTime::MIN || end <= start { |
| 273 | return false; |
| 274 | } |
| 275 | if end.time() == NaiveTime::MIN { |
| 276 | return true; |
| 277 | } |
| 278 | // Multi-day blocks that run from midnight through end-of-day (e.g. 23:59). |
| 279 | end.date_naive() > start.date_naive() |
| 280 | && end.time() >= NaiveTime::from_hms_opt(23, 59, 0).unwrap() |
| 281 | } |
| 282 | |
| 283 | pub fn week_days( |
| 284 | events: &[CalendarEvent], |
| 285 | dates: &[NaiveDate], |
| 286 | offset: FixedOffset, |
| 287 | ) -> Vec<WeekDay> { |
| 288 | dates |
| 289 | .iter() |
| 290 | .map(|&date| { |
| 291 | let (day_start, day_end) = week_range(date, 1, offset); |
| 292 | let mut all_day = Vec::new(); |
| 293 | let mut spans = Vec::new(); |
| 294 | for (index, event) in events.iter().enumerate() { |
| 295 | if !shown_in_calendar(event) || event.end <= day_start || event.start >= day_end { |
| 296 | continue; |
| 297 | } |
| 298 | if is_effective_all_day(event, offset) { |
| 299 | all_day.push(index); |
| 300 | continue; |
| 301 | } |
| 302 | let start_minute = (event.start.max(day_start) - day_start).num_minutes(); |
| 303 | let end_minute = (event.end.min(day_end) - day_start).num_minutes(); |
| 304 | spans.push((index, start_minute, end_minute.max(start_minute + 1))); |
| 305 | } |
| 306 | WeekDay { |
| 307 | date, |
| 308 | all_day, |
| 309 | blocks: lay_out(spans), |
| 310 | } |
| 311 | }) |
| 312 | .collect() |
| 313 | } |
| 314 | |
| 315 | fn lay_out(mut spans: Vec<(usize, i64, i64)>) -> Vec<WeekBlock> { |
| 316 | spans.sort_by_key(|&(index, start, end)| (start, std::cmp::Reverse(end), index)); |
| 317 | let mut blocks: Vec<WeekBlock> = Vec::with_capacity(spans.len()); |
| 318 | let mut cluster_start = 0; |
| 319 | let mut cluster_end = i64::MIN; |
| 320 | let mut lane_ends: Vec<i64> = Vec::new(); |
| 321 | for (index, start, end) in spans { |
| 322 | if start >= cluster_end { |
| 323 | close_cluster(&mut blocks[cluster_start..], lane_ends.len()); |
| 324 | cluster_start = blocks.len(); |
| 325 | lane_ends.clear(); |
| 326 | } |
| 327 | let lane = match lane_ends.iter().position(|&lane_end| lane_end <= start) { |
| 328 | Some(free) => { |
| 329 | lane_ends[free] = end; |
| 330 | free |
| 331 | } |
| 332 | None => { |
| 333 | lane_ends.push(end); |
| 334 | lane_ends.len() - 1 |
| 335 | } |
| 336 | }; |
| 337 | cluster_end = cluster_end.max(end); |
| 338 | blocks.push(WeekBlock { |
| 339 | event_index: index, |
| 340 | start_minute: start, |
| 341 | end_minute: end, |
| 342 | lane, |
| 343 | lanes: 1, |
| 344 | }); |
| 345 | } |
| 346 | close_cluster(&mut blocks[cluster_start..], lane_ends.len()); |
| 347 | blocks |
| 348 | } |
| 349 | |
| 350 | fn close_cluster(blocks: &mut [WeekBlock], lanes: usize) { |
| 351 | for block in blocks { |
| 352 | block.lanes = lanes.max(1); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 357 | pub enum EventLook { |
| 358 | Busy, |
| 359 | Free, |
| 360 | Tentative, |
| 361 | } |
| 362 | |
| 363 | pub fn event_look(event: &CalendarEvent) -> EventLook { |
| 364 | match event.show_as { |
| 365 | ShowAs::Free => EventLook::Free, |
| 366 | ShowAs::Tentative => EventLook::Tentative, |
| 367 | ShowAs::Busy | ShowAs::OutOfOffice | ShowAs::WorkingElsewhere => EventLook::Busy, |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | pub fn hour_label(hour: u32, twelve_hour: bool) -> String { |
| 372 | if !twelve_hour { |
| 373 | return format!("{hour:02}:00"); |
| 374 | } |
| 375 | match hour { |
| 376 | 0 => "12 AM".to_owned(), |
| 377 | 12 => "12 PM".to_owned(), |
| 378 | hour if hour < 12 => format!("{hour} AM"), |
| 379 | hour => format!("{} PM", hour - 12), |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | #[cfg(test)] |
| 384 | mod tests { |
| 385 | use chrono::TimeZone; |
| 386 | |
| 387 | use super::*; |
| 388 | |
| 389 | fn event(start_hour: u32, start_minute: u32, minutes: i64) -> CalendarEvent { |
| 390 | let start = Utc |
| 391 | .with_ymd_and_hms(2026, 11, 9, start_hour, start_minute, 0) |
| 392 | .unwrap(); |
| 393 | CalendarEvent { |
| 394 | object_id: format!("{start_hour}:{start_minute}"), |
| 395 | subject: "Sync".into(), |
| 396 | start, |
| 397 | end: start + Duration::minutes(minutes), |
| 398 | all_day: false, |
| 399 | cancelled: false, |
| 400 | online: true, |
| 401 | join_url: None, |
| 402 | thread_id: None, |
| 403 | organizer_name: None, |
| 404 | organizer_address: None, |
| 405 | ical_uid: None, |
| 406 | show_as: Default::default(), |
| 407 | dial_in: None, |
| 408 | response: ResponseType::Accepted, |
| 409 | ..Default::default() |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | fn utc() -> FixedOffset { |
| 414 | FixedOffset::east_opt(0).unwrap() |
| 415 | } |
| 416 | |
| 417 | fn run(start: NaiveDate, days: i64) -> Vec<NaiveDate> { |
| 418 | (0..days).map(|day| start + Duration::days(day)).collect() |
| 419 | } |
| 420 | |
| 421 | fn nov(day: u32) -> NaiveDate { |
| 422 | NaiveDate::from_ymd_opt(2026, 11, day).unwrap() |
| 423 | } |
| 424 | |
| 425 | fn layout() -> WeekLayout { |
| 426 | WeekLayout::default() |
| 427 | } |
| 428 | |
| 429 | fn sunday_first() -> WeekLayout { |
| 430 | WeekLayout { |
| 431 | first_day: Weekday::Sun, |
| 432 | ..WeekLayout::default() |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | #[test] |
| 437 | fn month_view_steps_whole_months_from_a_monday_grid() { |
| 438 | let layout = layout(); |
| 439 | let start = CalendarSpan::Month.start_for(nov(18), &layout); |
| 440 | assert_eq!(start, NaiveDate::from_ymd_opt(2026, 10, 26).unwrap()); |
| 441 | assert_eq!(month_shown(start), nov(1)); |
| 442 | let next = CalendarSpan::Month.shifted(start, 1, &layout); |
| 443 | assert_eq!(next, nov(30)); |
| 444 | let back = CalendarSpan::Month.shifted(start, -11, &layout); |
| 445 | assert_eq!( |
| 446 | month_shown(back), |
| 447 | NaiveDate::from_ymd_opt(2025, 12, 1).unwrap() |
| 448 | ); |
| 449 | assert_eq!( |
| 450 | CalendarSpan::Week.shifted(nov(2), -1, &layout), |
| 451 | nov(2) - Duration::weeks(1) |
| 452 | ); |
| 453 | } |
| 454 | |
| 455 | #[test] |
| 456 | fn a_sunday_first_week_and_month_start_on_sunday() { |
| 457 | let layout = sunday_first(); |
| 458 | assert_eq!(CalendarSpan::Week.start_for(nov(18), &layout), nov(15)); |
| 459 | assert_eq!( |
| 460 | visible_dates(CalendarSpan::Week, nov(15), &layout), |
| 461 | run(nov(15), 7) |
| 462 | ); |
| 463 | assert_eq!( |
| 464 | CalendarSpan::Month.start_for(nov(18), &layout), |
| 465 | NaiveDate::from_ymd_opt(2026, 11, 1).unwrap() |
| 466 | ); |
| 467 | assert_eq!(layout.weekdays()[0], Weekday::Sun); |
| 468 | assert_eq!(layout.weekdays()[6], Weekday::Sat); |
| 469 | } |
| 470 | |
| 471 | #[test] |
| 472 | fn a_saturday_first_week_starts_on_saturday() { |
| 473 | let layout = WeekLayout { |
| 474 | first_day: Weekday::Sat, |
| 475 | ..WeekLayout::default() |
| 476 | }; |
| 477 | assert_eq!(CalendarSpan::Week.start_for(nov(18), &layout), nov(14)); |
| 478 | assert_eq!(week_start(nov(14), Weekday::Sat), nov(14)); |
| 479 | } |
| 480 | |
| 481 | #[test] |
| 482 | fn work_week_shows_only_the_work_days_even_with_gaps() { |
| 483 | let layout = WeekLayout { |
| 484 | work_days: vec![Weekday::Mon, Weekday::Tue, Weekday::Thu], |
| 485 | ..WeekLayout::default() |
| 486 | }; |
| 487 | let start = CalendarSpan::WorkWeek.start_for(nov(18), &layout); |
| 488 | assert_eq!( |
| 489 | visible_dates(CalendarSpan::WorkWeek, start, &layout), |
| 490 | [nov(16), nov(17), nov(19)] |
| 491 | ); |
| 492 | } |
| 493 | |
| 494 | #[test] |
| 495 | fn on_sunday_work_week_shows_next_or_this_week() { |
| 496 | let next = layout(); |
| 497 | assert_eq!(CalendarSpan::WorkWeek.start_for(nov(15), &next), nov(16)); |
| 498 | let this = WeekLayout { |
| 499 | weekend: WeekendWorkWeek::ThisWeek, |
| 500 | ..WeekLayout::default() |
| 501 | }; |
| 502 | assert_eq!(CalendarSpan::WorkWeek.start_for(nov(15), &this), nov(9)); |
| 503 | // Sunday-first weeks still have work days ahead on Sunday. |
| 504 | assert_eq!( |
| 505 | CalendarSpan::WorkWeek.start_for(nov(15), &sunday_first()), |
| 506 | nov(15) |
| 507 | ); |
| 508 | assert_eq!( |
| 509 | visible_dates(CalendarSpan::WorkWeek, nov(15), &sunday_first()), |
| 510 | run(nov(16), 5) |
| 511 | ); |
| 512 | } |
| 513 | |
| 514 | #[test] |
| 515 | fn the_week_starts_on_monday() { |
| 516 | let sunday = nov(15); |
| 517 | assert_eq!(week_start(sunday, Weekday::Mon), nov(9)); |
| 518 | assert_eq!( |
| 519 | week_start(week_start(sunday, Weekday::Mon), Weekday::Mon), |
| 520 | nov(9) |
| 521 | ); |
| 522 | assert_eq!(work_week_for(sunday, &layout()), nov(16)); |
| 523 | assert_eq!(work_week_for(nov(13), &layout()), nov(9)); |
| 524 | } |
| 525 | |
| 526 | #[test] |
| 527 | fn overlapping_meetings_share_the_column_in_lanes() { |
| 528 | let events = vec![ |
| 529 | event(9, 0, 60), |
| 530 | event(9, 30, 60), |
| 531 | event(10, 0, 30), |
| 532 | event(13, 0, 30), |
| 533 | ]; |
| 534 | let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(); |
| 535 | let days = week_days(&events, &run(monday, 5), utc()); |
| 536 | assert_eq!(days.len(), 5); |
| 537 | let lanes: Vec<(usize, usize, usize)> = days[0] |
| 538 | .blocks |
| 539 | .iter() |
| 540 | .map(|block| (block.event_index, block.lane, block.lanes)) |
| 541 | .collect(); |
| 542 | assert_eq!(lanes, [(0, 0, 2), (1, 1, 2), (2, 0, 2), (3, 0, 1)]); |
| 543 | assert_eq!(days[0].blocks[0].start_minute, 9 * 60); |
| 544 | assert!(days[1].blocks.is_empty()); |
| 545 | } |
| 546 | |
| 547 | #[test] |
| 548 | fn declined_and_other_days_are_left_out_and_all_day_is_separate() { |
| 549 | let mut declined = event(9, 0, 30); |
| 550 | declined.response = ResponseType::Declined; |
| 551 | let mut all_day = event(0, 0, 24 * 60); |
| 552 | all_day.all_day = true; |
| 553 | let late = event(23, 30, 60); |
| 554 | let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(); |
| 555 | let days = week_days(&[declined, all_day, late], &run(monday, 2), utc()); |
| 556 | assert!(days[0].blocks.iter().all(|block| block.event_index != 0)); |
| 557 | assert_eq!(days[0].all_day, [1]); |
| 558 | assert_eq!(days[0].blocks[0].end_minute, MINUTES_PER_DAY); |
| 559 | assert_eq!( |
| 560 | (days[1].blocks[0].start_minute, days[1].blocks[0].end_minute), |
| 561 | (0, 30) |
| 562 | ); |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn midnight_to_midnight_timed_events_sit_in_the_all_day_row() { |
| 567 | // Google holidays / week markers often arrive as dateTime 00:00→00:00. |
| 568 | let holiday = event(0, 0, 24 * 60); |
| 569 | let meeting = event(9, 30, 60); |
| 570 | let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(); |
| 571 | assert!(is_effective_all_day(&holiday, utc())); |
| 572 | assert!(!is_effective_all_day(&meeting, utc())); |
| 573 | let days = week_days(&[holiday, meeting], &run(monday, 1), utc()); |
| 574 | assert_eq!(days[0].all_day, [0]); |
| 575 | assert_eq!(days[0].blocks.len(), 1); |
| 576 | assert_eq!(days[0].blocks[0].event_index, 1); |
| 577 | } |
| 578 | |
| 579 | #[test] |
| 580 | fn multi_day_midnight_to_end_of_day_is_all_day() { |
| 581 | // e.g. KubeCon as 00:00 Mon → 23:59 Thu. |
| 582 | let conference = event(0, 0, 4 * 24 * 60 - 1); |
| 583 | assert!(is_effective_all_day(&conference, utc())); |
| 584 | let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(); |
| 585 | let days = week_days(&[conference], &run(monday, 5), utc()); |
| 586 | assert!(days.iter().all(|day| day.blocks.is_empty())); |
| 587 | assert_eq!(days[0].all_day, [0]); |
| 588 | assert_eq!(days[3].all_day, [0]); |
| 589 | } |
| 590 | |
| 591 | #[test] |
| 592 | fn spans_start_and_step_like_teams() { |
| 593 | let layout = layout(); |
| 594 | let (wednesday, monday) = (nov(11), nov(9)); |
| 595 | assert_eq!(CalendarSpan::Day.start_for(wednesday, &layout), wednesday); |
| 596 | assert_eq!(CalendarSpan::WorkWeek.start_for(wednesday, &layout), monday); |
| 597 | assert_eq!(CalendarSpan::Week.start_for(wednesday, &layout), monday); |
| 598 | assert_eq!(CalendarSpan::Week.start_for(nov(15), &layout), monday); |
| 599 | assert_eq!( |
| 600 | CalendarSpan::Day.shifted(wednesday, 1, &layout), |
| 601 | wednesday + Duration::days(1) |
| 602 | ); |
| 603 | assert_eq!(visible_dates(CalendarSpan::Week, monday, &layout).len(), 7); |
| 604 | assert_eq!( |
| 605 | visible_dates(CalendarSpan::WorkWeek, monday, &layout).len(), |
| 606 | 5 |
| 607 | ); |
| 608 | assert_eq!( |
| 609 | dates_range(&run(monday, 5), utc()), |
| 610 | ( |
| 611 | local_midnight(monday, utc()), |
| 612 | local_midnight(nov(14), utc()) |
| 613 | ) |
| 614 | ); |
| 615 | } |
| 616 | |
| 617 | #[test] |
| 618 | fn events_take_the_colour_index_of_their_calendar() { |
| 619 | use store::{CalendarAccounts, GoogleAccount, GoogleCalendarInfo, GoogleTokenSet}; |
| 620 | let calendar = |id: &str| GoogleCalendarInfo { |
| 621 | id: id.into(), |
| 622 | summary: id.into(), |
| 623 | selected: true, |
| 624 | primary: false, |
| 625 | }; |
| 626 | let accounts = CalendarAccounts { |
| 627 | google_accounts: vec![ |
| 628 | GoogleAccount { |
| 629 | id: "a".into(), |
| 630 | email: "a@example.com".into(), |
| 631 | calendars: vec![calendar("primary"), calendar("family")], |
| 632 | tokens: GoogleTokenSet::default(), |
| 633 | }, |
| 634 | GoogleAccount { |
| 635 | id: "b".into(), |
| 636 | email: "b@example.com".into(), |
| 637 | calendars: vec![calendar("work")], |
| 638 | tokens: GoogleTokenSet::default(), |
| 639 | }, |
| 640 | ], |
| 641 | ..CalendarAccounts::default() |
| 642 | }; |
| 643 | let mut google = event(9, 0, 30); |
| 644 | google.source = chatsvc::CalendarSource::Google; |
| 645 | google.account_id = Some("b".into()); |
| 646 | google.calendar_id = Some("work".into()); |
| 647 | assert_eq!(calendar_index(&google, &accounts), 3); |
| 648 | google.account_id = Some("a".into()); |
| 649 | google.calendar_id = Some("family".into()); |
| 650 | assert_eq!(calendar_index(&google, &accounts), 2); |
| 651 | assert_eq!(calendar_index(&event(9, 0, 30), &accounts), 0); |
| 652 | } |
| 653 | |
| 654 | #[test] |
| 655 | fn labels_read_like_teams() { |
| 656 | assert_eq!(hour_label(0, true), "12 AM"); |
| 657 | assert_eq!(hour_label(13, true), "1 PM"); |
| 658 | assert_eq!(hour_label(13, false), "13:00"); |
| 659 | assert_eq!(hour_label(7, false), "07:00"); |
| 660 | } |
| 661 | |
| 662 | #[test] |
| 663 | fn free_and_tentative_meetings_look_like_teams() { |
| 664 | let mut following = event(9, 0, 30); |
| 665 | following.show_as = ShowAs::Free; |
| 666 | following.response = ResponseType::Tentative; |
| 667 | let mut tentative = event(9, 0, 30); |
| 668 | tentative.show_as = ShowAs::Tentative; |
| 669 | let mut away = event(9, 0, 30); |
| 670 | away.show_as = ShowAs::OutOfOffice; |
| 671 | assert_eq!(event_look(&following), EventLook::Free); |
| 672 | assert_eq!(event_look(&tentative), EventLook::Tentative); |
| 673 | assert_eq!(event_look(&away), EventLook::Busy); |
| 674 | assert_eq!(event_look(&event(9, 0, 30)), EventLook::Busy); |
| 675 | } |
| 676 | } |