1
//! Each report command's parameter contract, pinned as golden text before the
2
//! reports move onto form descriptors (plan 0016 M3k). Set
3
//! `REPORT_FIXTURES_BLESS=1` to rewrite the fixtures after a deliberate change.
4

            
5
mod activity;
6
mod balance;
7
mod breakdown;
8
mod damage;
9

            
10
use tokio::sync::OnceCell;
11

            
12
use super::super::{CmdError, CmdResult, FilterEntity, ReportFilter};
13
use super::fixture::render::outcome;
14
use super::fixture::seed::{self, Ledger};
15
use crate::db::install_test_pool;
16

            
17
static CONTEXT: OnceCell<()> = OnceCell::const_new();
18

            
19
4
async fn setup() {
20
4
    CONTEXT
21
8
        .get_or_init(|| async { crate::telemetry::init_test_logging() })
22
4
        .await;
23
4
}
24

            
25
/// One report's cases, rendered in order and compared with its fixture.
26
struct Golden {
27
    file: &'static str,
28
    text: String,
29
}
30

            
31
impl Golden {
32
3
    fn new(file: &'static str) -> Self {
33
3
        Self {
34
3
            file,
35
3
            text: String::new(),
36
3
        }
37
3
    }
38

            
39
61
    fn case(&mut self, ledger: &Ledger, title: &str, result: Result<Option<CmdResult>, CmdError>) {
40
61
        if !self.text.is_empty() {
41
58
            self.text.push('\n');
42
3
        }
43
61
        self.text.push_str(&format!("== {title}\n"));
44
61
        self.text.push_str(&outcome(ledger, result));
45
61
    }
46

            
47
3
    fn check(self) -> anyhow::Result<()> {
48
3
        let path = format!(
49
            "{}/tests/fixtures/reports/{}",
50
            env!("CARGO_MANIFEST_DIR"),
51
3
            self.file
52
        );
53
3
        if std::env::var_os("REPORT_FIXTURES_BLESS").is_some() {
54
            std::fs::write(&path, &self.text)?;
55
            return Ok(());
56
3
        }
57
3
        assert_eq!(std::fs::read_to_string(&path)?, self.text, "{path}");
58
3
        Ok(())
59
3
    }
60
}
61

            
62
4
async fn ledger() -> anyhow::Result<Ledger> {
63
4
    let user = crate::user::User {
64
4
        id: sqlx::types::Uuid::new_v4(),
65
4
    };
66
4
    user.commit().await?;
67
4
    seed::seed(user).await
68
4
}
69

            
70
23
fn tag(entity: FilterEntity, name: &str, value: &str) -> ReportFilter {
71
23
    ReportFilter::Tag {
72
23
        entity,
73
23
        name: name.to_owned(),
74
23
        value: value.to_owned(),
75
23
    }
76
23
}
77

            
78
16
fn account_type(value: &str) -> ReportFilter {
79
16
    tag(FilterEntity::Account, "type", value)
80
16
}
81

            
82
6
fn project_home() -> ReportFilter {
83
6
    tag(FilterEntity::Transaction, "project", "home")
84
6
}