test(axum): use dom-aware html assertions

Harden axum response and root fingerprint tests with scraper-based HTML parsing instead of raw rendered-string equality.

req: test/005
This commit is contained in:
slhx agent
2026-05-25 23:19:13 +02:00
parent 355fc99888
commit 227a408985
4 changed files with 47 additions and 13 deletions
+24 -8
View File
@@ -1,5 +1,6 @@
use axum::http::{header, HeaderMap};
use axum::response::IntoResponse;
use scraper::{Html, Selector};
use slhx_axum::{
runtime_js, DispatchRejection, EffectResponse, HandlerRegistry, InteractionForm,
InteractionFormRejection, PageMode, PageRequest, PageResponse, SLHX_CONTENT_TYPE,
@@ -7,6 +8,10 @@ use slhx_axum::{
};
use slhx_core::{push, BuildFingerprint, Slot};
fn selector(value: &str) -> Selector {
Selector::parse(value).expect("test selector parses")
}
#[test]
fn page_mode_detects_partial_header() {
let mut headers = HeaderMap::new();
@@ -18,17 +23,28 @@ fn page_mode_detects_partial_header() {
#[test]
fn page_request_wraps_full_pages_and_leaves_partials_unwrapped() {
let full = PageRequest { mode: PageMode::Full }.page("<main>Docs</main>", |content| {
format!("<html>{content}</html>")
});
// req: test/005
let full = PageRequest { mode: PageMode::Full }.page(
"<main data-page=\"docs\">Docs</main>",
|content| format!("<html><body data-shell=\"docs\">{content}</body></html>"),
);
assert_eq!(full.mode, PageMode::Full);
assert_eq!(full.html, "<html><main>Docs</main></html>");
let full_document = Html::parse_document(&full.html);
assert_eq!(
full_document
.select(&selector("body[data-shell=\"docs\"] main[data-page=\"docs\"]"))
.count(),
1
);
let partial = PageRequest { mode: PageMode::Partial }.page("<main>Docs</main>", |content| {
format!("<html>{content}</html>")
});
let partial = PageRequest { mode: PageMode::Partial }.page(
"<main data-page=\"docs\">Docs</main>",
|content| format!("<html><body data-shell=\"docs\">{content}</body></html>"),
);
assert_eq!(partial.mode, PageMode::Partial);
assert_eq!(partial.html, "<main>Docs</main>");
let partial_fragment = Html::parse_fragment(&partial.html);
assert_eq!(partial_fragment.select(&selector("main[data-page=\"docs\"]")).count(), 1);
assert_eq!(partial_fragment.select(&selector("body[data-shell=\"docs\"]")).count(), 0);
}
#[test]