//! Integration tests for dashboard stats. use chrono::{Duration, Utc}; use goingson_core::{DashboardStats, HighUrgencyTask, NewEvent, NewTask, Priority}; use crate::commands::stats::DashboardStatsResponse; use crate::test_utils::{create_test_project, setup_test_state}; /// Computes the same UTC day windows the `get_dashboard_stats` command derives. fn windows() -> ( chrono::DateTime, chrono::DateTime, chrono::DateTime, chrono::DateTime, ) { let now = Utc::now(); let today = now.date_naive(); let midnight = |d: chrono::NaiveDate| d.and_hms_opt(0, 0, 0).unwrap().and_utc(); let today_start = midnight(today); let tomorrow_start = midnight(today + Duration::days(1)); let week_end = midnight(today + Duration::days(8)); (now, today_start, tomorrow_start, week_end) } #[tokio::test] async fn dashboard_counts_tasks_by_due_window() { let (state, user_id) = setup_test_state().await; let (now, today_start, tomorrow_start, week_end) = windows(); // Overdue: due 3 days ago. let overdue = NewTask::builder("Overdue task") .priority(Priority::High) .due(now - Duration::days(3)) .build(); state.tasks.create(user_id, overdue).await.unwrap(); // Due this week (but not today): 3 days out. let this_week = NewTask::builder("This week task") .priority(Priority::Medium) .due(now + Duration::days(3)) .build(); state.tasks.create(user_id, this_week).await.unwrap(); let stats = state .stats .get_dashboard_stats(user_id, now, today_start, tomorrow_start, week_end) .await .unwrap(); assert_eq!(stats.overdue_count, 1); assert_eq!(stats.tasks_due_today, 0); assert_eq!(stats.tasks_due_this_week, 1); // No emails seeded. assert_eq!(stats.unread_emails, 0); } #[tokio::test] async fn dashboard_counts_projects_and_events() { let (state, user_id) = setup_test_state().await; let (now, today_start, tomorrow_start, week_end) = windows(); // create_test_project makes one Active project. create_test_project(&state, user_id).await; // Event within the 7-day window and one beyond it. let soon = NewEvent::builder("Soon", now + Duration::days(2)).build(); state.events.create(user_id, soon).await.unwrap(); let far = NewEvent::builder("Far", now + Duration::days(30)).build(); state.events.create(user_id, far).await.unwrap(); let stats = state .stats .get_dashboard_stats(user_id, now, today_start, tomorrow_start, week_end) .await .unwrap(); assert_eq!(stats.active_projects, 1); assert_eq!(stats.upcoming_events, 1); } #[tokio::test] #[allow( clippy::float_cmp, reason = "asserting exact, directly-set f64 urgency values" )] async fn dashboard_high_urgency_sorted_and_excludes_completed() { let (state, user_id) = setup_test_state().await; let (now, today_start, tomorrow_start, week_end) = windows(); for (desc, urgency) in [("low", 1.0), ("high", 9.0), ("mid", 5.0)] { let t = NewTask::builder(desc) .priority(Priority::Medium) .urgency(urgency) .build(); state.tasks.create(user_id, t).await.unwrap(); } // A very urgent but completed task must not appear. let done = NewTask::builder("done") .priority(Priority::High) .urgency(100.0) .build(); let done = state.tasks.create(user_id, done).await.unwrap(); state.tasks.complete(done.id, user_id).await.unwrap(); let stats = state .stats .get_dashboard_stats(user_id, now, today_start, tomorrow_start, week_end) .await .unwrap(); assert_eq!(stats.high_urgency_tasks.len(), 3); assert_eq!(stats.high_urgency_tasks[0].description, "high"); assert_eq!(stats.high_urgency_tasks[0].urgency, 9.0); assert_eq!(stats.high_urgency_tasks[2].description, "low"); assert!( stats .high_urgency_tasks .iter() .all(|t| t.description != "done") ); } /// The response type renames fields (tasks_due_today -> dueTodayCount, etc.), so the /// `From` mapping is where a wiring regression would hide. #[tokio::test] #[allow( clippy::float_cmp, reason = "asserting an exact f64 urgency round-trips through the response conversion" )] async fn response_conversion_maps_fields() { let stats = DashboardStats { tasks_due_today: 2, tasks_due_this_week: 5, overdue_count: 3, unread_emails: 7, upcoming_events: 4, active_projects: 6, high_urgency_tasks: vec![HighUrgencyTask { id: "abc".to_string(), description: "Ship it".to_string(), urgency: 8.5, status: "Pending".to_string(), due: Some("2026-07-04T00:00:00Z".to_string()), }], }; let resp = DashboardStatsResponse::from(stats); assert_eq!(resp.due_today_count, 2); assert_eq!(resp.due_this_week_count, 5); assert_eq!(resp.overdue_count, 3); assert_eq!(resp.unread_emails, 7); assert_eq!(resp.upcoming_events, 4); assert_eq!(resp.active_projects, 6); assert_eq!(resp.high_urgency_tasks.len(), 1); assert_eq!(resp.high_urgency_tasks[0].id, "abc"); assert_eq!(resp.high_urgency_tasks[0].description, "Ship it"); assert_eq!(resp.high_urgency_tasks[0].urgency, 8.5); assert_eq!( resp.high_urgency_tasks[0].due.as_deref(), Some("2026-07-04T00:00:00Z") ); }