Skip to main content

max / goingson

5.4 KB · 164 lines History Blame Raw
1 //! Integration tests for dashboard stats.
2
3 use chrono::{Duration, Utc};
4 use goingson_core::{DashboardStats, HighUrgencyTask, NewEvent, NewTask, Priority};
5
6 use crate::commands::stats::DashboardStatsResponse;
7 use crate::test_utils::{create_test_project, setup_test_state};
8
9 /// Computes the same UTC day windows the `get_dashboard_stats` command derives.
10 fn windows() -> (
11 chrono::DateTime<Utc>,
12 chrono::DateTime<Utc>,
13 chrono::DateTime<Utc>,
14 chrono::DateTime<Utc>,
15 ) {
16 let now = Utc::now();
17 let today = now.date_naive();
18 let midnight = |d: chrono::NaiveDate| d.and_hms_opt(0, 0, 0).unwrap().and_utc();
19 let today_start = midnight(today);
20 let tomorrow_start = midnight(today + Duration::days(1));
21 let week_end = midnight(today + Duration::days(8));
22 (now, today_start, tomorrow_start, week_end)
23 }
24
25 #[tokio::test]
26 async fn dashboard_counts_tasks_by_due_window() {
27 let (state, user_id) = setup_test_state().await;
28 let (now, today_start, tomorrow_start, week_end) = windows();
29
30 // Overdue: due 3 days ago.
31 let overdue = NewTask::builder("Overdue task")
32 .priority(Priority::High)
33 .due(now - Duration::days(3))
34 .build();
35 state.tasks.create(user_id, overdue).await.unwrap();
36
37 // Due this week (but not today): 3 days out.
38 let this_week = NewTask::builder("This week task")
39 .priority(Priority::Medium)
40 .due(now + Duration::days(3))
41 .build();
42 state.tasks.create(user_id, this_week).await.unwrap();
43
44 let stats = state
45 .stats
46 .get_dashboard_stats(user_id, now, today_start, tomorrow_start, week_end)
47 .await
48 .unwrap();
49
50 assert_eq!(stats.overdue_count, 1);
51 assert_eq!(stats.tasks_due_today, 0);
52 assert_eq!(stats.tasks_due_this_week, 1);
53 // No emails seeded.
54 assert_eq!(stats.unread_emails, 0);
55 }
56
57 #[tokio::test]
58 async fn dashboard_counts_projects_and_events() {
59 let (state, user_id) = setup_test_state().await;
60 let (now, today_start, tomorrow_start, week_end) = windows();
61
62 // create_test_project makes one Active project.
63 create_test_project(&state, user_id).await;
64
65 // Event within the 7-day window and one beyond it.
66 let soon = NewEvent::builder("Soon", now + Duration::days(2)).build();
67 state.events.create(user_id, soon).await.unwrap();
68 let far = NewEvent::builder("Far", now + Duration::days(30)).build();
69 state.events.create(user_id, far).await.unwrap();
70
71 let stats = state
72 .stats
73 .get_dashboard_stats(user_id, now, today_start, tomorrow_start, week_end)
74 .await
75 .unwrap();
76
77 assert_eq!(stats.active_projects, 1);
78 assert_eq!(stats.upcoming_events, 1);
79 }
80
81 #[tokio::test]
82 #[allow(
83 clippy::float_cmp,
84 reason = "asserting exact, directly-set f64 urgency values"
85 )]
86 async fn dashboard_high_urgency_sorted_and_excludes_completed() {
87 let (state, user_id) = setup_test_state().await;
88 let (now, today_start, tomorrow_start, week_end) = windows();
89
90 for (desc, urgency) in [("low", 1.0), ("high", 9.0), ("mid", 5.0)] {
91 let t = NewTask::builder(desc)
92 .priority(Priority::Medium)
93 .urgency(urgency)
94 .build();
95 state.tasks.create(user_id, t).await.unwrap();
96 }
97 // A very urgent but completed task must not appear.
98 let done = NewTask::builder("done")
99 .priority(Priority::High)
100 .urgency(100.0)
101 .build();
102 let done = state.tasks.create(user_id, done).await.unwrap();
103 state.tasks.complete(done.id, user_id).await.unwrap();
104
105 let stats = state
106 .stats
107 .get_dashboard_stats(user_id, now, today_start, tomorrow_start, week_end)
108 .await
109 .unwrap();
110
111 assert_eq!(stats.high_urgency_tasks.len(), 3);
112 assert_eq!(stats.high_urgency_tasks[0].description, "high");
113 assert_eq!(stats.high_urgency_tasks[0].urgency, 9.0);
114 assert_eq!(stats.high_urgency_tasks[2].description, "low");
115 assert!(
116 stats
117 .high_urgency_tasks
118 .iter()
119 .all(|t| t.description != "done")
120 );
121 }
122
123 /// The response type renames fields (tasks_due_today -> dueTodayCount, etc.), so the
124 /// `From` mapping is where a wiring regression would hide.
125 #[tokio::test]
126 #[allow(
127 clippy::float_cmp,
128 reason = "asserting an exact f64 urgency round-trips through the response conversion"
129 )]
130 async fn response_conversion_maps_fields() {
131 let stats = DashboardStats {
132 tasks_due_today: 2,
133 tasks_due_this_week: 5,
134 overdue_count: 3,
135 unread_emails: 7,
136 upcoming_events: 4,
137 active_projects: 6,
138 high_urgency_tasks: vec![HighUrgencyTask {
139 id: "abc".to_string(),
140 description: "Ship it".to_string(),
141 urgency: 8.5,
142 status: "Pending".to_string(),
143 due: Some("2026-07-04T00:00:00Z".to_string()),
144 }],
145 };
146
147 let resp = DashboardStatsResponse::from(stats);
148
149 assert_eq!(resp.due_today_count, 2);
150 assert_eq!(resp.due_this_week_count, 5);
151 assert_eq!(resp.overdue_count, 3);
152 assert_eq!(resp.unread_emails, 7);
153 assert_eq!(resp.upcoming_events, 4);
154 assert_eq!(resp.active_projects, 6);
155 assert_eq!(resp.high_urgency_tasks.len(), 1);
156 assert_eq!(resp.high_urgency_tasks[0].id, "abc");
157 assert_eq!(resp.high_urgency_tasks[0].description, "Ship it");
158 assert_eq!(resp.high_urgency_tasks[0].urgency, 8.5);
159 assert_eq!(
160 resp.high_urgency_tasks[0].due.as_deref(),
161 Some("2026-07-04T00:00:00Z")
162 );
163 }
164