BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-call-model / src / week_grid.rs

week_grid.rs

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