use axum::http::{header, HeaderMap}; use axum::response::IntoResponse; use slhx_axum::{ runtime_js, DispatchRejection, EffectResponse, HandlerRegistry, InteractionForm, InteractionFormRejection, PageMode, PageRequest, PageResponse, SLHX_CONTENT_TYPE, SLHX_FINGERPRINT_HEADER, SLHX_PARTIAL_HEADER, SLHX_RUNTIME_CONTENT_TYPE, SLHX_TITLE_HEADER, }; use slhx_core::{push, BuildFingerprint, Slot}; #[test] fn page_mode_detects_partial_header() { let mut headers = HeaderMap::new(); assert_eq!(PageMode::from_headers(&headers), PageMode::Full); headers.insert(SLHX_PARTIAL_HEADER, "true".parse().unwrap()); assert_eq!(PageMode::from_headers(&headers), PageMode::Partial); } #[test] fn page_request_wraps_full_pages_and_leaves_partials_unwrapped() { let full = PageRequest { mode: PageMode::Full }.page("
Docs
", |content| { format!("{content}") }); assert_eq!(full.mode, PageMode::Full); assert_eq!(full.html, "
Docs
"); let partial = PageRequest { mode: PageMode::Partial }.page("
Docs
", |content| { format!("{content}") }); assert_eq!(partial.mode, PageMode::Partial); assert_eq!(partial.html, "
Docs
"); } #[test] fn partial_page_response_sets_partial_and_title_headers() { let response = PageResponse::partial("
Docs
") .title("Docs") .into_response(); assert_eq!(response.headers()[header::CONTENT_TYPE], "text/html; charset=utf-8"); assert_eq!(response.headers()[SLHX_PARTIAL_HEADER], "true"); assert_eq!(response.headers()[SLHX_TITLE_HEADER], "Docs"); } #[test] fn effect_response_is_wire_batch_with_fingerprint_header() { let response = EffectResponse::new(push("/docs"), BuildFingerprint(99)).into_response(); assert_eq!(response.headers()[header::CONTENT_TYPE], SLHX_CONTENT_TYPE); assert_eq!(response.headers()[SLHX_FINGERPRINT_HEADER], "99"); } #[test] fn interaction_form_parses_handle_and_fields() { let form = InteractionForm::parse_urlencoded(b"__h=42&title=Hello+World&tag=a&tag=b%2Fc") .unwrap(); assert_eq!(form.handle_id, 42); assert_eq!(form.value("title"), Some("Hello World")); assert_eq!(form.values("tag").collect::>(), ["a", "b/c"]); } #[test] fn interaction_form_requires_numeric_handle() { assert_eq!( InteractionForm::parse_urlencoded(b"title=Hello").unwrap_err(), InteractionFormRejection::MissingHandle ); assert_eq!( InteractionForm::parse_urlencoded(b"__h=nope").unwrap_err(), InteractionFormRejection::InvalidHandle ); } #[test] fn handler_registry_dispatches_by_numeric_handle_id() { let title = Slot::::new(7); let registry = HandlerRegistry::new(BuildFingerprint(123)).register(42, move |form| { title.text(form.value("title").unwrap_or("")) }); let response = registry .dispatch(InteractionForm::new(42, [("title".into(), "Hello".into())])) .unwrap(); assert_eq!(response.batch.fingerprint, BuildFingerprint(123)); assert_eq!(response.batch.ops, vec![title.text("Hello")]); } #[test] fn handler_registry_rejects_unknown_handle_ids() { let registry = HandlerRegistry::new(BuildFingerprint(123)); assert_eq!( registry.dispatch(InteractionForm::new(9, [])).unwrap_err(), DispatchRejection::UnknownHandle(9) ); } #[test] fn runtime_js_response_serves_embedded_runtime() { let response = runtime_js().into_response(); assert_eq!(response.headers()[header::CONTENT_TYPE], SLHX_RUNTIME_CONTENT_TYPE); assert!(response.headers().contains_key(header::CACHE_CONTROL)); }