1
//! The template source box is sized for code on both template pages.
2

            
3
use anyhow::{Result, ensure};
4
use serde_json::json;
5

            
6
use super::harness::{Harness, with_harness};
7
use super::page::{login, open, wait_for};
8

            
9
const SOURCE: &str = "(set-draft-note \"Groceries\")";
10

            
11
const SOURCE_BOX: &str = r#"
12
const box = document.querySelector('textarea[name="source"]');
13
const form = box.closest('form').getBoundingClientRect();
14
const style = getComputedStyle(box);
15
return {
16
  ratio: box.getBoundingClientRect().width / form.width,
17
  whiteSpace: style.whiteSpace,
18
  font: style.fontFamily,
19
  resize: style.resize,
20
  rows: box.getBoundingClientRect().height / parseFloat(style.lineHeight),
21
};
22
"#;
23

            
24
2
async fn check_source_box(h: &Harness, path: &str) -> Result<()> {
25
2
    open(h, path).await?;
26
2
    let found = h.driver.run(SOURCE_BOX, vec![]).await?;
27
2
    ensure!(
28
2
        found["ratio"].as_f64().is_some_and(|r| r >= 0.8),
29
        "{path}: the box is not the form width: {found}"
30
    );
31
2
    ensure!(
32
2
        found["whiteSpace"] == json!("pre"),
33
        "{path}: the box wraps: {found}"
34
    );
35
2
    ensure!(
36
2
        found["font"]
37
2
            .as_str()
38
2
            .is_some_and(|f| f.contains("monospace")),
39
        "{path}: the box is not monospace: {found}"
40
    );
41
2
    ensure!(
42
2
        found["resize"] == json!("vertical"),
43
        "{path}: the box does not resize vertically: {found}"
44
    );
45
2
    ensure!(
46
2
        found["rows"].as_f64().is_some_and(|r| r >= 15.0),
47
        "{path}: the box is too short: {found}"
48
    );
49
2
    Ok(())
50
2
}
51

            
52
1
async fn create_template(h: &Harness) -> Result<String> {
53
1
    server::user::User { id: h.tenant.id }
54
1
        .create_template(SOURCE.to_string(), Some("e2e".to_string()))
55
1
        .await?;
56
1
    open(h, "/template/list").await?;
57
    // The rows arrive by an htmx load after the page is ready.
58
1
    let href = wait_for(
59
1
        &h.driver,
60
1
        "const a = document.querySelector('a[href^=\"/template/edit/\"]'); \
61
1
         return a && a.getAttribute('href');",
62
1
        vec![],
63
1
        "an edit link on the template list",
64
1
    )
65
1
    .await?;
66
1
    href.as_str()
67
1
        .map(str::to_owned)
68
        .ok_or_else(|| anyhow::anyhow!("no edit link on the template list"))
69
1
}
70

            
71
1
async fn editors(h: &Harness) -> Result<()> {
72
1
    login(h).await?;
73
1
    check_source_box(h, "/template/create").await?;
74
1
    let edit = create_template(h).await?;
75
1
    check_source_box(h, &edit).await
76
1
}
77

            
78
#[supp_macro::shared_runtime_test]
79
1
async fn template_source_box_is_sized_for_code() -> Result<()> {
80
1
    with_harness(|h| Box::pin(editors(h))).await
81
}