1
//! Golden HTML fixtures of the browser tests.
2

            
3
const FIXTURES: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/frontend/tests/fixtures/");
4

            
5
/// What the pre-commit hook leaves of an html file: no trailing whitespace
6
/// and no run of blank lines.
7
36
fn hook_stable(html: &str) -> String {
8
36
    let mut out = String::new();
9
36
    let mut blanks = 0;
10
36
    for line in html.lines().map(str::trim_end) {
11
5556
        blanks = if line.is_empty() { blanks + 1 } else { 0 };
12
5556
        if blanks < 2 {
13
5390
            out.push_str(line);
14
5390
            out.push('\n');
15
166
        }
16
    }
17
36
    out
18
36
}
19

            
20
/// Asserts that the `<input` element containing each needle carries the
21
/// given `autocomplete` token.
22
10
pub(crate) fn assert_autocomplete(html: &str, expected: &[(&str, &str)]) {
23
10
    for (needle, token) in expected {
24
26
        let input = html
25
26
            .split("<input")
26
26
            .skip(1)
27
100
            .map(|chunk| chunk.split('>').next().unwrap_or_default())
28
100
            .find(|element| element.contains(needle));
29
26
        assert!(
30
26
            input.is_some_and(|e| e.contains(&format!("autocomplete=\"{token}\""))),
31
            "{needle}: {input:?}"
32
        );
33
    }
34
10
}
35

            
36
36
pub(crate) fn assert_fixture(name: &str, rendered: &str) {
37
36
    let path = format!("{FIXTURES}{name}");
38
36
    let html = hook_stable(rendered);
39
36
    if std::env::var_os("UPDATE_FIXTURES").is_some() {
40
        std::fs::write(&path, &html).unwrap();
41
36
    }
42
36
    assert_eq!(
43
36
        std::fs::read_to_string(&path).unwrap(),
44
36
        html,
45
        "regenerate with UPDATE_FIXTURES=1 cargo test -p web the_browser_fixture"
46
    );
47
36
}