1
use anyhow::{Result, anyhow};
2
use axum::http::{HeaderMap, HeaderValue, StatusCode};
3
use axum::response::Response;
4
use forms::REPORT_BALANCE;
5

            
6
use super::{SHELL_ID, report_invalid};
7
use crate::response::WebError;
8

            
9
6
fn shown() -> Vec<(String, String)> {
10
6
    vec![("sort_order".into(), "name_asc".into())]
11
6
}
12

            
13
6
fn refused(trigger: Option<&str>) -> Result<Response> {
14
6
    let mut headers = HeaderMap::new();
15
6
    if let Some(trigger) = trigger {
16
4
        headers.insert("hx-trigger", HeaderValue::from_str(trigger)?);
17
2
    }
18
6
    let editor = |oob: bool| Ok(format!("<editor oob=\"{oob}\"/>"));
19
6
    report_invalid(
20
6
        &headers,
21
6
        &REPORT_BALANCE,
22
6
        "<form id=\"nf-report-balance\"></form>".into(),
23
6
        &shown(),
24
6
        Some(&editor),
25
6
    )
26
    .map_err(|e| anyhow!("{e:?}"))
27
6
}
28

            
29
6
async fn body(response: Response) -> Result<String> {
30
6
    let bytes = axum::body::to_bytes(response.into_body(), usize::MAX).await?;
31
6
    Ok(String::from_utf8(bytes.to_vec())?)
32
6
}
33

            
34
#[tokio::test]
35
2
async fn the_shell_gets_the_form_editor_and_controls_inside_the_filters_wrapper() -> Result<()> {
36
2
    let response = refused(Some(SHELL_ID))?;
37
2
    assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
38
2
    assert!(response.headers().get("hx-retarget").is_none());
39
2
    let html = body(response).await?;
40
2
    assert!(html.contains("class=\"report-filters\""), "{html}");
41
2
    assert!(html.contains("hx-boost=\"true\""), "{html}");
42
2
    assert!(html.contains("<form id=\"nf-report-balance\">"), "{html}");
43
2
    assert!(html.contains("<editor oob=\"false\"/>"), "{html}");
44
2
    assert!(
45
2
        html.contains("form=\"nf-report-balance\" name=\"sort_order\" value=\"name_asc\""),
46
        "{html}"
47
    );
48
4
    Ok(())
49
2
}
50

            
51
#[tokio::test]
52
2
async fn a_loaded_table_swaps_the_form_in_place() -> Result<()> {
53
2
    for trigger in [None, Some("nf-report-balance")] {
54
4
        let response = refused(trigger)?;
55
4
        assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
56
4
        assert_eq!(
57
4
            response.headers().get("hx-retarget"),
58
4
            Some(&HeaderValue::from_static("#nf-report-balance"))
59
2
        );
60
4
        let html = body(response).await?;
61
4
        assert!(!html.contains("report-filters"), "{html}");
62
4
        assert!(html.contains("<editor oob=\"true\"/>"), "{html}");
63
2
    }
64
2
    Ok(())
65
2
}
66

            
67
#[test]
68
2
fn no_editor_renders_nothing_beside_the_form() -> Result<()> {
69
2
    let response = report_invalid(
70
2
        &HeaderMap::new(),
71
2
        &REPORT_BALANCE,
72
2
        "<form></form>".into(),
73
2
        &[],
74
2
        None,
75
2
    )
76
    .map_err(|e| anyhow!("{e:?}"))?;
77
2
    assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
78
2
    Ok(())
79
2
}
80

            
81
#[test]
82
2
fn an_editor_failure_is_the_error() {
83
2
    let broken = |_: bool| Err(WebError::internal("editor", "broken"));
84
2
    let failed = report_invalid(
85
2
        &HeaderMap::new(),
86
2
        &REPORT_BALANCE,
87
2
        String::new(),
88
2
        &[],
89
2
        Some(&broken),
90
2
    );
91
2
    assert!(failed.is_err());
92
2
}