1
//! `submit-form` answers with a real value that a script reads directly (plan 0016 VF1).
2

            
3
use forms::escape_str;
4
use sqlx::PgPool;
5

            
6
use super::test_query_seed::{add_tx, fixture};
7
use crate::ctx::ScriptCtx;
8
use crate::session::Session;
9

            
10
11
async fn value_of(session: &mut Session, form: &str) -> String {
11
11
    session.handle_form(&format!("(:id 1 :form {form})")).await
12
11
}
13

            
14
4
fn query(payload: &str) -> String {
15
4
    format!(
16
        "(submit-form \"transaction-query\" {})",
17
4
        escape_str(payload)
18
    )
19
4
}
20

            
21
#[sqlx::test(migrator = "server::db::MIGRATOR")]
22
async fn a_script_reads_the_page_plist_cells(pool: PgPool) -> anyhow::Result<()> {
23
    let _guard = server::db::install_test_pool(pool.clone());
24
    let f = fixture(&pool).await?;
25
    add_tx(&f, "2024-03-01T10:00:00Z", "a").await?;
26
    let newest = add_tx(&f, "2024-03-02T10:00:00Z", "b").await?;
27
    let mut session = Session::new(ScriptCtx::new(f.user_id))?;
28
    let one = query("(:limit \"1\")");
29
    let all = query("(:limit \"5\")");
30

            
31
    let cases = [
32
        (format!("(car {one})"), ":ROWS".to_string()),
33
        (format!("(car (cdr (cdr (cdr {one}))))"), "t".to_string()),
34
        (format!("(car (cdr (cdr (cdr {all}))))"), "nil".to_string()),
35
        (
36
            format!("(car (cdr (cdr (cdr (cdr (cdr {all}))))))"),
37
            "2".to_string(),
38
        ),
39
        (format!("(car (car (car (cdr {one}))))"), ":ID".to_string()),
40
        (
41
            format!("(car (cdr (car (car (cdr {one})))))"),
42
            format!("\"{newest}\""),
43
        ),
44
        (
45
            query("(:from \"2030-01-01\")"),
46
            "(:ROWS nil :HAS-MORE nil :TOTAL 0)".to_string(),
47
        ),
48
    ];
49
    for (form, expected) in cases {
50
        assert_eq!(
51
            value_of(&mut session, &form).await,
52
            format!("(:id 1 :value {expected})"),
53
            "{form}"
54
        );
55
    }
56
    Ok(())
57
}
58

            
59
#[sqlx::test(migrator = "server::db::MIGRATOR")]
60
async fn a_mutation_still_answers_its_uuid_string(pool: PgPool) -> anyhow::Result<()> {
61
    let _guard = server::db::install_test_pool(pool.clone());
62
    let f = fixture(&pool).await?;
63
    let mut session = Session::new(ScriptCtx::new(f.user_id))?;
64
    let form = "(submit-form \"account-create\" \"(:name \\\"Cafe\\\")\")";
65
    let wire = value_of(&mut session, form).await;
66
    let id = wire
67
        .strip_prefix("(:id 1 :value \"")
68
1
        .and_then(|rest| rest.strip_suffix("\")"))
69
        .ok_or_else(|| anyhow::anyhow!("{wire}"))?;
70
    uuid::Uuid::parse_str(id)?;
71
    Ok(())
72
}
73

            
74
#[sqlx::test(migrator = "server::db::MIGRATOR")]
75
async fn a_script_still_matches_the_upper_case_keyword(pool: PgPool) -> anyhow::Result<()> {
76
    let _guard = server::db::install_test_pool(pool.clone());
77
    let f = fixture(&pool).await?;
78
    add_tx(&f, "2024-03-01T10:00:00Z", "a").await?;
79
    let mut session = Session::new(ScriptCtx::new(f.user_id))?;
80
    let page = query("(:limit \"5\")");
81
    let cases = [
82
        (format!("(equal (car {page}) :rows)"), "t"),
83
        (format!("(equal (car (cdr (cdr {page}))) :has-more)"), "t"),
84
        (format!("(equal (car {page}) :ROWS)"), "t"),
85
    ];
86
    for (form, expected) in cases {
87
        assert_eq!(
88
            value_of(&mut session, &form).await,
89
            format!("(:id 1 :value {expected})"),
90
            "{form}"
91
        );
92
    }
93
    Ok(())
94
}