//! Tests for el-platform backends. #[cfg(test)] mod tests { use crate::{ backend_for, backends::{server::ServerBackend, web::WebBackend}, config::{PlatformConfig, PlatformTarget}, node::PlatformNode, PlatformBackend, }; // ── Test 1: PlatformTarget::from_str parses all targets ────────────────── #[test] fn test_platform_target_from_str() { assert_eq!(PlatformTarget::from_str("web"), Some(PlatformTarget::Web)); assert_eq!(PlatformTarget::from_str("server"), Some(PlatformTarget::Server)); assert_eq!(PlatformTarget::from_str("ios"), Some(PlatformTarget::Ios)); assert_eq!(PlatformTarget::from_str("android"), Some(PlatformTarget::Android)); assert_eq!(PlatformTarget::from_str("macos"), Some(PlatformTarget::Macos)); assert_eq!(PlatformTarget::from_str("linux"), Some(PlatformTarget::Linux)); assert_eq!(PlatformTarget::from_str("windows"), Some(PlatformTarget::Windows)); assert_eq!(PlatformTarget::from_str("unknown"), None); } // ── Test 2: PlatformTarget::as_str round-trips ─────────────────────────── #[test] fn test_platform_target_as_str() { assert_eq!(PlatformTarget::Web.as_str(), "web"); assert_eq!(PlatformTarget::Server.as_str(), "server"); assert_eq!(PlatformTarget::Ios.as_str(), "ios"); } // ── Test 3: is_native() identifies native targets ──────────────────────── #[test] fn test_is_native() { assert!(!PlatformTarget::Web.is_native()); assert!(!PlatformTarget::Server.is_native()); assert!(PlatformTarget::Ios.is_native()); assert!(PlatformTarget::Android.is_native()); assert!(PlatformTarget::Macos.is_native()); assert!(PlatformTarget::Linux.is_native()); assert!(PlatformTarget::Windows.is_native()); } // ── Test 4: PlatformConfig defaults to web ─────────────────────────────── #[test] fn test_platform_config_default() { let cfg = PlatformConfig::default(); assert_eq!(cfg.target, PlatformTarget::Web); assert!(!cfg.ssr); } // ── Test 5: PlatformConfig with_ssr ────────────────────────────────────── #[test] fn test_platform_config_with_ssr() { let cfg = PlatformConfig::new(PlatformTarget::Server).with_ssr(true); assert_eq!(cfg.target, PlatformTarget::Server); assert!(cfg.ssr); } // ── Test 6: WebBackend renders element to HTML ─────────────────────────── #[test] fn test_web_backend_renders_element() { let backend = WebBackend::new(); let mut div = backend.create_element("div").unwrap(); backend.set_attribute(&mut div, "class", "container").unwrap(); backend.append_child(&mut div, backend.create_text("Hello").unwrap()).unwrap(); let html = backend.render_to_string(&div).unwrap(); assert!(html.contains(""), "should close div tag"); } // ── Test 7: ServerBackend supports SSR ─────────────────────────────────── #[test] fn test_server_backend_supports_ssr() { let backend = ServerBackend::new(); assert!(backend.supports_ssr()); } // ── Test 8: ServerBackend renders full HTML page ───────────────────────── #[test] fn test_server_backend_render_page() { let backend = ServerBackend::new(); let root = PlatformNode::element("div") .with_attr("id", "content") .with_child(PlatformNode::text("Hello World")); let page = backend.render_page(&root, "My App", "/app.js").unwrap(); assert!(page.contains(""), "should be full HTML doc"); assert!(page.contains("My App"), "should include title"); assert!(page.contains("Hello World"), "should include content"); assert!(page.contains("/app.js"), "should include script src"); } // ── Test 9: PlatformNode::to_html renders nested tree ──────────────────── #[test] fn test_platform_node_to_html_nested() { let node = PlatformNode::element("ul") .with_child(PlatformNode::element("li").with_child(PlatformNode::text("item 1"))) .with_child(PlatformNode::element("li").with_child(PlatformNode::text("item 2"))); let html = node.to_html(); assert!(html.contains("")); } // ── Test 10: PlatformNode::to_html escapes text content ────────────────── #[test] fn test_html_escaping() { let node = PlatformNode::element("span") .with_child(PlatformNode::text("")); let html = node.to_html(); assert!(!html.contains("