3.7 KiB · text History 7f67051
//! Smoke test: every page template loads from the embedded environment and
//! renders with a plausible context. Templates are data, not code, so a typo
//! in one is invisible to `cargo check` — this is what catches it.
use minijinja::context;
use recourse::build_environment;

fn user() -> minijinja::Value {
    context! {
        username => "alice",
        email => "alice@example.com",
        is_active => true,
        created_at => "2026-08-03T00:00:00Z",
        last_login_at => "2026-08-03T12:00:00Z",
    }
}

fn note(processed: bool) -> minijinja::Value {
    context! {
        id => "0198f0aa-0000-7000-8000-000000000000",
        title => "A note",
        body => "Body text",
        created_at => "2026-08-03T00:00:00Z",
        processed_at => if processed { Some("2026-08-03T00:01:00Z") } else { None },
    }
}

/// Every full page renders and carries the app name from `APP_NAME`.
#[test]
fn renders_every_page() {
    let env = build_environment();
    let cases: Vec<(&str, minijinja::Value)> = vec![
        ("index.html", context! { title => "Home", user => user(), registration_open => false }),
        ("index.html", context! { title => "Home", registration_open => true }),
        ("auth/login.html", context! { title => "Login" }),
        (
            "auth/register.html",
            context! { title => "Register", invite_token => "tok-123", registration_open => false },
        ),
        ("auth/profile.html", context! { title => "Profile", user => user() }),
        (
            "auth/invites.html",
            context! {
                title => "Invites",
                user => user(),
                now => "2026-08-03T00:00:00Z",
                invites => vec![context! {
                    token => "tok-123",
                    email => "friend@example.com",
                    expires_at => "2026-09-03T00:00:00Z",
                    is_used => false,
                }],
            },
        ),
        (
            "notes/list.html",
            context! { title => "Notes", user => user(), notes => vec![note(false), note(true)] },
        ),
        ("notes/show.html", context! { title => "A note", user => user(), note => note(false) }),
    ];

    for (name, ctx) in cases {
        let tmpl = env.get_template(name).unwrap_or_else(|e| panic!("load {name}: {e}"));
        let html = tmpl.render(ctx).unwrap_or_else(|e| panic!("render {name}: {e}"));
        assert!(
            html.contains(recourse::APP_NAME),
            "{name} did not render the app name"
        );
    }
}

/// The empty-collection branches of the list pages.
#[test]
fn renders_empty_lists() {
    let env = build_environment();
    let pages = [
        ("auth/invites.html", "invites"),
        ("notes/list.html", "notes"),
    ];
    for (name, key) in pages {
        let ctx = context! { user => user(), now => "2026-08-03T00:00:00Z", ..
            minijinja::Value::from_iter([(key, Vec::<minijinja::Value>::new())]) };
        let tmpl = env.get_template(name).unwrap();
        tmpl.render(ctx).unwrap_or_else(|e| panic!("render empty {name}: {e}"));
    }
}

/// The status fragment drives the htmx poll: it must carry `hx-get` while the
/// note is pending and drop it once processed, or the page polls forever.
#[test]
fn status_fragment_stops_polling_when_processed() {
    let env = build_environment();
    let tmpl = env.get_template("notes/_status.html").unwrap();

    let pending = tmpl.render(context! { note => note(false) }).unwrap();
    assert!(pending.contains("hx-get"), "pending status should keep polling");

    let done = tmpl.render(context! { note => note(true) }).unwrap();
    assert!(!done.contains("hx-get"), "processed status should stop polling");
}