Lines
96.55 %
Functions
100 %
Branches
use chrono::{DateTime, TimeZone, Utc};
use forms::{FormValues, REPORT_ACTIVITY, REPORT_BALANCE};
use server::command::config::SetConfig;
use server::timezone::{Tz, local_day};
use sqlx::PgPool;
use uuid::Uuid;
use super::test_report::ledger;
use super::test_seed::seed_user;
use crate::forms::user_zone;
use crate::forms::{ReportOutcome, apply_report_defaults, format_balance, typed_report_on};
use crate::natives::report::run_balance_report;
/// 2026-12-01T02:00 in Tokyo, still the evening of November 30 in UTC.
fn now() -> DateTime<Utc> {
Utc.with_ymd_and_hms(2026, 11, 30, 17, 0, 0).unwrap()
}
fn instant(y: i32, mo: u32, d: u32, h: u32) -> DateTime<Utc> {
Utc.with_ymd_and_hms(y, mo, d, h, 0, 0).unwrap()
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(())
fn values(pairs: &[(&str, &str)]) -> FormValues {
let mut values = FormValues::default();
for (key, value) in pairs {
values.set(key, value);
values
async fn balance(user_id: Uuid, values: &FormValues) -> anyhow::Result<String> {
match typed_report_on(user_id, &REPORT_BALANCE, values, now()).await? {
ReportOutcome::Balance(data) => Ok(format_balance(&data)),
_ => anyhow::bail!("not a balance report"),
/// The test pool holds one config table for every user, so a user "with no timezone" is the
/// same user before the zone is set.
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn today_is_the_local_day_of_the_user(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let user = seed_user(&pool).await?;
let zone = user_zone(user).await?;
assert_eq!(zone, Tz::UTC);
assert_eq!(local_day(now(), zone).to_string(), "2026-11-30");
set_timezone(user, "Asia/Tokyo").await?;
assert_eq!(local_day(now(), zone).to_string(), "2026-12-01");
async fn month_to_date_defaults_follow_the_user_zone(pool: PgPool) -> anyhow::Result<()> {
for (zone, from, to) in [
(None, "2026-11-01", "2026-11-30"),
(Some("Asia/Tokyo"), "2026-12-01", "2026-12-01"),
] {
if let Some(zone) = zone {
set_timezone(user, zone).await?;
let mut defaulted = FormValues::default();
let today = local_day(now(), user_zone(user).await?);
apply_report_defaults(&REPORT_ACTIVITY, &mut defaulted, today);
assert_eq!(defaulted.text("from"), Some(from));
assert_eq!(defaulted.text("to"), Some(to));
async fn an_as_of_default_ends_the_local_day_in_utc(pool: PgPool) -> anyhow::Result<()> {
for (zone, end) in [
(None, "2026-11-30T23:59:59.999999+00:00"),
(Some("Asia/Tokyo"), "2026-12-01T14:59:59.999999+00:00"),
let outcome = typed_report_on(user, &REPORT_BALANCE, &FormValues::default(), now()).await?;
let ReportOutcome::Balance(data) = outcome else {
anyhow::bail!("not a balance report");
};
assert_eq!(
data.meta.date_to.map(|to| to.to_rfc3339()).as_deref(),
Some(end)
);
async fn a_typed_range_is_the_local_days_in_each_zone(pool: PgPool) -> anyhow::Result<()> {
let range = values(&[("from", "2026-12-01"), ("to", "2026-12-02")]);
(
"Asia/Tokyo",
"2026-11-30T15:00:00+00:00",
"2026-12-02T14:59:59.999999+00:00",
),
"America/New_York",
"2026-12-01T05:00:00+00:00",
"2026-12-03T04:59:59.999999+00:00",
let outcome = typed_report_on(user, &REPORT_ACTIVITY, &range, now()).await?;
let ReportOutcome::Activity(data) = outcome else {
anyhow::bail!("not an activity report");
data.meta.date_from.map(|d| d.to_rfc3339()).as_deref(),
Some(from),
"{zone}"
data.meta.date_to.map(|d| d.to_rfc3339()).as_deref(),
Some(to),
async fn the_balance_native_and_the_form_agree_under_a_timezone(
pool: PgPool,
) -> anyhow::Result<()> {
let l = ledger(&pool).await?;
let id = l.user.id;
l.post(
instant(2026, 12, 1, 10),
&[("Checking", "USD", 777, 1), ("Salary", "USD", -777, 1)],
)
set_timezone(id, "Asia/Tokyo").await?;
let form = balance(id, &FormValues::default()).await?;
assert_eq!(form, run_balance_report(id, now()).await?);
let before_the_entry = balance(id, &values(&[("as-of", "2026-11-30")])).await?;
assert_ne!(form, before_the_entry);