1
use chrono::{DateTime, Utc};
2
use forms::REPORT_ACTIVITY;
3
use server::command::config::SetConfig;
4
use sqlx::PgPool;
5
use uuid::Uuid;
6

            
7
use super::test_query_seed::{Fixture, add_tx, add_tx_entered, fixture};
8
use super::test_seed::query_text;
9
use super::tests_query::{query, rejected, values};
10
use crate::forms::{ReportOutcome, typed_report_on};
11

            
12
5
async fn set_timezone(user_id: Uuid, zone: &str) -> anyhow::Result<()> {
13
5
    SetConfig::new()
14
5
        .user_id(user_id)
15
5
        .name("timezone".to_string())
16
5
        .value(zone.to_string())
17
5
        .run()
18
5
        .await?;
19
5
    Ok(())
20
5
}
21

            
22
/// Ids among `all` whose instant lies inside the activity report's range for the same days.
23
4
async fn in_report(
24
4
    f: &Fixture,
25
4
    (from, to): (&str, &str),
26
4
    all: &[(Uuid, &str)],
27
4
) -> anyhow::Result<Vec<String>> {
28
4
    let range = values(&[("from", from), ("to", to)]);
29
4
    let outcome = typed_report_on(f.user_id, &REPORT_ACTIVITY, &range, Utc::now()).await?;
30
4
    let ReportOutcome::Activity(data) = outcome else {
31
        anyhow::bail!("not an activity report");
32
    };
33
4
    let (start, end) = (data.meta.date_from, data.meta.date_to);
34
4
    let mut ids = Vec::new();
35
4
    for (id, at) in all {
36
16
        let at = DateTime::parse_from_rfc3339(at)?.with_timezone(&Utc);
37
16
        if start.is_none_or(|s| at >= s) && end.is_none_or(|e| at <= e) {
38
8
            ids.push(id.to_string());
39
8
        }
40
    }
41
4
    ids.sort();
42
4
    Ok(ids)
43
4
}
44

            
45
/// The list and the report of the same days select the same instants; the first two
46
/// instants are inside the range, the last two just outside.
47
4
async fn zone_case(
48
4
    pool: &PgPool,
49
4
    zone: &str,
50
4
    days: (&str, &str),
51
4
    instants: [&'static str; 4],
52
4
) -> anyhow::Result<()> {
53
4
    let f = fixture(pool).await?;
54
4
    set_timezone(f.user_id, zone).await?;
55
4
    let mut all = Vec::new();
56
4
    for at in instants {
57
16
        all.push((add_tx(&f, at, at).await?, at));
58
    }
59
4
    let expected = in_report(&f, days, &all).await?;
60
4
    let mut listed = query(&f, &[("from", days.0), ("to", days.1)]).await?.0;
61
4
    listed.sort();
62
4
    assert_eq!(listed, expected, "{zone} {days:?}");
63
8
    let inside: Vec<String> = all.iter().take(2).map(|(id, _)| id.to_string()).collect();
64
4
    assert_eq!(listed.len(), inside.len(), "{zone}: {listed:?}");
65
8
    assert!(inside.iter().all(|id| listed.contains(id)), "{zone}");
66
4
    Ok(())
67
4
}
68

            
69
#[sqlx::test(migrator = "server::db::MIGRATOR")]
70
async fn typed_days_match_the_report_in_utc(pool: PgPool) -> anyhow::Result<()> {
71
    let _guard = server::db::install_test_pool(pool.clone());
72
    let instants = [
73
        "2026-12-01T00:00:00Z",
74
        "2026-12-02T23:59:59.999999Z",
75
        "2026-11-30T23:59:59Z",
76
        "2026-12-03T00:00:00Z",
77
    ];
78
    zone_case(&pool, "UTC", ("2026-12-01", "2026-12-02"), instants).await
79
}
80

            
81
#[sqlx::test(migrator = "server::db::MIGRATOR")]
82
async fn typed_days_match_the_report_in_tokyo(pool: PgPool) -> anyhow::Result<()> {
83
    let _guard = server::db::install_test_pool(pool.clone());
84
    let instants = [
85
        "2026-11-30T15:00:00Z",
86
        "2026-12-02T14:59:59.999999Z",
87
        "2026-11-30T14:59:59Z",
88
        "2026-12-02T15:00:00Z",
89
    ];
90
    zone_case(&pool, "Asia/Tokyo", ("2026-12-01", "2026-12-02"), instants).await
91
}
92

            
93
#[sqlx::test(migrator = "server::db::MIGRATOR")]
94
async fn typed_days_match_the_report_in_new_york(pool: PgPool) -> anyhow::Result<()> {
95
    let _guard = server::db::install_test_pool(pool.clone());
96
    let instants = [
97
        "2026-12-01T05:00:00Z",
98
        "2026-12-03T04:59:59.999999Z",
99
        "2026-12-01T04:59:59Z",
100
        "2026-12-03T05:00:00Z",
101
    ];
102
    let days = ("2026-12-01", "2026-12-02");
103
    zone_case(&pool, "America/New_York", days, instants).await
104
}
105

            
106
#[sqlx::test(migrator = "server::db::MIGRATOR")]
107
async fn a_fall_back_day_lasts_twenty_five_hours(pool: PgPool) -> anyhow::Result<()> {
108
    let _guard = server::db::install_test_pool(pool.clone());
109
    let instants = [
110
        "2026-11-01T04:00:00Z",
111
        "2026-11-02T04:59:59.999999Z",
112
        "2026-11-01T03:59:59Z",
113
        "2026-11-02T05:00:00Z",
114
    ];
115
    let days = ("2026-11-01", "2026-11-01");
116
    zone_case(&pool, "America/New_York", days, instants).await
117
}
118

            
119
#[sqlx::test(migrator = "server::db::MIGRATOR")]
120
async fn a_datetime_in_a_day_filter_is_refused(pool: PgPool) -> anyhow::Result<()> {
121
    let _guard = server::db::install_test_pool(pool.clone());
122
    let f = fixture(&pool).await?;
123
    for (key, value) in [
124
        ("from", "2026-12-01T10:00"),
125
        ("to", "2026-12-01T10:00:00Z"),
126
        ("from", "2026-12-01 10:00"),
127
    ] {
128
        let detail = rejected(&f, &[(key, value)]).await?;
129
        let expected = format!("(:FIELD \"{key}\" :MESSAGE \"'{value}' is not a day");
130
        assert!(detail.contains(&expected), "{detail}");
131
    }
132
    Ok(())
133
}
134

            
135
#[sqlx::test(migrator = "server::db::MIGRATOR")]
136
async fn rows_print_the_date_in_the_user_zone(pool: PgPool) -> anyhow::Result<()> {
137
    let _guard = server::db::install_test_pool(pool.clone());
138
    let f = fixture(&pool).await?;
139
    set_timezone(f.user_id, "Asia/Tokyo").await?;
140
    add_tx_entered(&f, ("2026-11-30T17:00:00Z", "2026-11-29T01:00:00Z"), "late").await?;
141

            
142
    let text = query_text(f.user_id, &values(&[])).await?;
143

            
144
    assert!(
145
        text.contains(":DATE \"2026-11-30T17:00:00Z\" :DATE-TEXT \"2026-12-01 02:00\""),
146
        "{text}"
147
    );
148
    Ok(())
149
}