1
use super::*;
2
use axum::http::Request;
3

            
4
14
fn request(uri: &str, current: Option<&str>) -> Request<Body> {
5
14
    let mut builder = Request::builder().uri(uri).header("hx-request", "true");
6
14
    if let Some(current) = current {
7
12
        builder = builder.header("hx-current-url", current);
8
2
    }
9
14
    builder.body(Body::empty()).expect("request")
10
14
}
11

            
12
#[test]
13
2
fn the_content_url_carries_the_shell_query() {
14
2
    assert_eq!(
15
2
        content_url("/api/report/balance", None),
16
2
        "/api/report/balance"
17
    );
18
2
    assert_eq!(
19
2
        content_url("/api/report/balance", Some("")),
20
2
        "/api/report/balance"
21
    );
22
2
    assert_eq!(
23
2
        content_url(
24
2
            "/api/report/balance",
25
2
            Some("as-of=2026-01-01&sort_order=name_asc")
26
2
        ),
27
2
        "/api/report/balance?as-of=2026-01-01&sort_order=name_asc"
28
    );
29
2
}
30

            
31
#[test]
32
2
fn a_changed_query_pushes_the_page_url() {
33
2
    let target = push_target(&request(
34
2
        "/api/report/activity?grouping=month&chart_kind=line",
35
2
        Some("http://host/report/activity"),
36
2
    ));
37
2
    assert_eq!(
38
2
        target.as_deref(),
39
2
        Some("/report/activity?grouping=month&chart_kind=line")
40
    );
41
2
}
42

            
43
#[test]
44
2
fn clearing_pushes_the_bare_page_url() {
45
2
    let target = push_target(&request(
46
2
        "/api/report/category-breakdown",
47
2
        Some("http://host/report/category-breakdown?sort=name_asc"),
48
2
    ));
49
2
    assert_eq!(target.as_deref(), Some("/report/category-breakdown"));
50
2
}
51

            
52
#[test]
53
2
fn the_shells_own_load_and_a_repeat_push_nothing() {
54
2
    for (uri, current) in [
55
2
        (
56
2
            "/api/report/balance?a=1&b=2",
57
2
            "http://host/report/balance?b=2&a=1",
58
2
        ),
59
2
        ("/api/report/balance", "http://host/report/balance"),
60
2
    ] {
61
4
        assert_eq!(push_target(&request(uri, Some(current))), None, "{uri}");
62
    }
63
2
}
64

            
65
#[test]
66
2
fn only_htmx_requests_for_a_report_table_push() {
67
2
    let plain = Request::builder()
68
2
        .uri("/api/report/balance?a=1")
69
2
        .header("hx-current-url", "http://host/report/balance")
70
2
        .body(Body::empty())
71
2
        .expect("request");
72
2
    assert_eq!(push_target(&plain), None);
73
2
    for uri in [
74
2
        "/api/report/balance/chart.svg?a=1",
75
2
        "/api/report/category-breakdown/export.csv?a=1",
76
2
    ] {
77
4
        let chart = request(uri, Some("http://host/report/balance"));
78
4
        assert_eq!(push_target(&chart), None, "{uri}");
79
    }
80
2
    assert_eq!(push_target(&request("/api/report/balance?a=1", None)), None);
81
2
}