Lines
97.92 %
Functions
100 %
Branches
use chrono::{DateTime, Utc};
use forms::REPORT_ACTIVITY;
use server::command::config::SetConfig;
use sqlx::PgPool;
use uuid::Uuid;
use super::test_query_seed::{Fixture, add_tx, add_tx_entered, fixture};
use super::test_seed::query_text;
use super::tests_query::{query, rejected, values};
use crate::forms::{ReportOutcome, typed_report_on};
async fn set_timezone(user_id: Uuid, zone: &str) -> anyhow::Result<()> {
SetConfig::new()
.user_id(user_id)
.name("timezone".to_string())
.value(zone.to_string())
.run()
.await?;
Ok(())
}
/// Ids among `all` whose instant lies inside the activity report's range for the same days.
async fn in_report(
f: &Fixture,
(from, to): (&str, &str),
all: &[(Uuid, &str)],
) -> anyhow::Result<Vec<String>> {
let range = values(&[("from", from), ("to", to)]);
let outcome = typed_report_on(f.user_id, &REPORT_ACTIVITY, &range, Utc::now()).await?;
let ReportOutcome::Activity(data) = outcome else {
anyhow::bail!("not an activity report");
};
let (start, end) = (data.meta.date_from, data.meta.date_to);
let mut ids = Vec::new();
for (id, at) in all {
let at = DateTime::parse_from_rfc3339(at)?.with_timezone(&Utc);
if start.is_none_or(|s| at >= s) && end.is_none_or(|e| at <= e) {
ids.push(id.to_string());
ids.sort();
Ok(ids)
/// The list and the report of the same days select the same instants; the first two
/// instants are inside the range, the last two just outside.
async fn zone_case(
pool: &PgPool,
zone: &str,
days: (&str, &str),
instants: [&'static str; 4],
) -> anyhow::Result<()> {
let f = fixture(pool).await?;
set_timezone(f.user_id, zone).await?;
let mut all = Vec::new();
for at in instants {
all.push((add_tx(&f, at, at).await?, at));
let expected = in_report(&f, days, &all).await?;
let mut listed = query(&f, &[("from", days.0), ("to", days.1)]).await?.0;
listed.sort();
assert_eq!(listed, expected, "{zone} {days:?}");
let inside: Vec<String> = all.iter().take(2).map(|(id, _)| id.to_string()).collect();
assert_eq!(listed.len(), inside.len(), "{zone}: {listed:?}");
assert!(inside.iter().all(|id| listed.contains(id)), "{zone}");
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn typed_days_match_the_report_in_utc(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let instants = [
"2026-12-01T00:00:00Z",
"2026-12-02T23:59:59.999999Z",
"2026-11-30T23:59:59Z",
"2026-12-03T00:00:00Z",
];
zone_case(&pool, "UTC", ("2026-12-01", "2026-12-02"), instants).await
async fn typed_days_match_the_report_in_tokyo(pool: PgPool) -> anyhow::Result<()> {
"2026-11-30T15:00:00Z",
"2026-12-02T14:59:59.999999Z",
"2026-11-30T14:59:59Z",
"2026-12-02T15:00:00Z",
zone_case(&pool, "Asia/Tokyo", ("2026-12-01", "2026-12-02"), instants).await
async fn typed_days_match_the_report_in_new_york(pool: PgPool) -> anyhow::Result<()> {
"2026-12-01T05:00:00Z",
"2026-12-03T04:59:59.999999Z",
"2026-12-01T04:59:59Z",
"2026-12-03T05:00:00Z",
let days = ("2026-12-01", "2026-12-02");
zone_case(&pool, "America/New_York", days, instants).await
async fn a_fall_back_day_lasts_twenty_five_hours(pool: PgPool) -> anyhow::Result<()> {
"2026-11-01T04:00:00Z",
"2026-11-02T04:59:59.999999Z",
"2026-11-01T03:59:59Z",
"2026-11-02T05:00:00Z",
let days = ("2026-11-01", "2026-11-01");
async fn a_datetime_in_a_day_filter_is_refused(pool: PgPool) -> anyhow::Result<()> {
let f = fixture(&pool).await?;
for (key, value) in [
("from", "2026-12-01T10:00"),
("to", "2026-12-01T10:00:00Z"),
("from", "2026-12-01 10:00"),
] {
let detail = rejected(&f, &[(key, value)]).await?;
let expected = format!("(:FIELD \"{key}\" :MESSAGE \"'{value}' is not a day");
assert!(detail.contains(&expected), "{detail}");
async fn rows_print_the_date_in_the_user_zone(pool: PgPool) -> anyhow::Result<()> {
set_timezone(f.user_id, "Asia/Tokyo").await?;
add_tx_entered(&f, ("2026-11-30T17:00:00Z", "2026-11-29T01:00:00Z"), "late").await?;
let text = query_text(f.user_id, &values(&[])).await?;
assert!(
text.contains(":DATE \"2026-11-30T17:00:00Z\" :DATE-TEXT \"2026-12-01 02:00\""),
"{text}"
);