#[cfg(test)] mod tests { use crate::error::WebBuilderError; use crate::parser_hjson::parse_site_config; use std::fs; use std::path::PathBuf; use tempfile::TempDir; fn create_test_site(temp_dir: &TempDir) -> PathBuf { let site_dir = temp_dir.path().join("site"); fs::create_dir(&site_dir).unwrap(); // Create main.hjson let main_hjson = r#"{ # Main configuration "name": "test", "title": "Test Site", "description": "A test site", "url": "https://example.com", "favicon": "favicon.ico", "keywords": [ "demo", "test", "example" ] }"#; fs::write(site_dir.join("main.hjson"), main_hjson).unwrap(); // Create header.hjson let header_hjson = r#"{ # Header configuration "title": "Test Site", "logo": { "src": "logo.png", "alt": "Logo" }, "menu": [ { "label": "Home", "link": "/" }, { "label": "About", "link": "/about" } ] }"#; fs::write(site_dir.join("header.hjson"), header_hjson).unwrap(); // Create footer.hjson let footer_hjson = r#"{ # Footer configuration "title": "Footer", "copyright": "© 2023 Test", "sections": [ { "title": "Links", "links": [ { "label": "Home", "href": "/" }, { "label": "About", "href": "/about" } ] } ] }"#; fs::write(site_dir.join("footer.hjson"), footer_hjson).unwrap(); // Create collection.hjson let collection_hjson = r#"[ { # First collection "name": "test", "url": "https://git.ourworld.tf/tfgrid/home.git", "description": "A test collection", "scan": true }, { # Second collection "name": "test2", "url": "https://git.example.com/src/branch/main/test2", "description": "Another test collection" } ]"#; fs::write(site_dir.join("collection.hjson"), collection_hjson).unwrap(); // Create pages directory let pages_dir = site_dir.join("pages"); fs::create_dir(&pages_dir).unwrap(); // Create pages/pages.hjson let pages_hjson = r#"[ { # Home page "name": "home", "title": "Home", "description": "Home page", "navpath": "/", "collection": "test", "draft": false }, { # About page "name": "about", "title": "About", "description": "About page", "navpath": "/about", "collection": "test" } ]"#; fs::write(pages_dir.join("pages.hjson"), pages_hjson).unwrap(); site_dir } #[test] fn test_parse_site_config() { let temp_dir = TempDir::new().unwrap(); let site_dir = create_test_site(&temp_dir); let config = parse_site_config(&site_dir).unwrap(); // Check basic site info assert_eq!(config.name, "test"); assert_eq!(config.title, "Test Site"); assert_eq!(config.description, Some("A test site".to_string())); assert_eq!(config.url, Some("https://example.com".to_string())); assert_eq!(config.favicon, Some("favicon.ico".to_string())); assert_eq!( config.keywords, Some(vec![ "demo".to_string(), "test".to_string(), "example".to_string() ]) ); // Check header assert!(config.header.is_some()); let header = config.header.as_ref().unwrap(); assert_eq!(header.title, Some("Test Site".to_string())); assert!(header.logo.is_some()); let logo = header.logo.as_ref().unwrap(); assert_eq!(logo.src, "logo.png"); assert_eq!(logo.alt, Some("Logo".to_string())); assert!(header.menu.is_some()); let menu = header.menu.as_ref().unwrap(); assert_eq!(menu.len(), 2); assert_eq!(menu[0].label, "Home"); assert_eq!(menu[0].link, "/"); // Check footer assert!(config.footer.is_some()); let footer = config.footer.as_ref().unwrap(); assert_eq!(footer.title, Some("Footer".to_string())); assert_eq!(footer.copyright, Some("© 2023 Test".to_string())); assert!(footer.sections.is_some()); let sections = footer.sections.as_ref().unwrap(); assert_eq!(sections.len(), 1); assert_eq!(sections[0].title, "Links"); assert_eq!(sections[0].links.len(), 2); assert_eq!(sections[0].links[0].label, "Home"); assert_eq!(sections[0].links[0].href, "/"); // Check collections assert_eq!(config.collections.len(), 2); // First collection assert_eq!(config.collections[0].name, Some("test".to_string())); assert_eq!( config.collections[0].url, Some("https://git.ourworld.tf/tfgrid/home.git".to_string()) ); assert_eq!( config.collections[0].description, Some("A test collection".to_string()) ); assert_eq!(config.collections[0].scan, Some(true)); // Second collection (with URL conversion) assert_eq!(config.collections[1].name, Some("test2".to_string())); assert_eq!( config.collections[1].url, Some("https://git.example.com.git".to_string()) ); assert_eq!( config.collections[1].description, Some("Another test collection".to_string()) ); assert_eq!(config.collections[1].scan, None); // Check pages assert_eq!(config.pages.len(), 2); // First page assert_eq!(config.pages[0].name, "home"); assert_eq!(config.pages[0].title, "Home"); assert_eq!(config.pages[0].description, Some("Home page".to_string())); assert_eq!(config.pages[0].navpath, "/"); assert_eq!(config.pages[0].collection, "test"); assert_eq!(config.pages[0].draft, Some(false)); // Second page assert_eq!(config.pages[1].name, "about"); assert_eq!(config.pages[1].title, "About"); assert_eq!(config.pages[1].description, Some("About page".to_string())); assert_eq!(config.pages[1].navpath, "/about"); assert_eq!(config.pages[1].collection, "test"); assert_eq!(config.pages[1].draft, None); } #[test] fn test_parse_site_config_missing_directory() { let result = parse_site_config("/nonexistent/directory"); assert!(matches!(result, Err(WebBuilderError::MissingDirectory(_)))); } #[test] fn test_parse_site_config_not_a_directory() { let temp_dir = TempDir::new().unwrap(); let file_path = temp_dir.path().join("file.txt"); fs::write(&file_path, "not a directory").unwrap(); let result = parse_site_config(&file_path); assert!(matches!( result, Err(WebBuilderError::InvalidConfiguration(_)) )); } #[test] fn test_parse_site_config_minimal() { let temp_dir = TempDir::new().unwrap(); let site_dir = temp_dir.path().join("site"); fs::create_dir(&site_dir).unwrap(); // Create minimal main.hjson let main_hjson = r#"{ "name": "minimal", "title": "Minimal Site" }"#; fs::write(site_dir.join("main.hjson"), main_hjson).unwrap(); let config = parse_site_config(&site_dir).unwrap(); assert_eq!(config.name, "minimal"); assert_eq!(config.title, "Minimal Site"); assert_eq!(config.description, None); assert_eq!(config.url, None); assert_eq!(config.favicon, None); assert!(config.header.is_none()); assert!(config.footer.is_none()); assert!(config.collections.is_empty()); assert!(config.pages.is_empty()); } #[test] fn test_parse_site_config_empty() { let temp_dir = TempDir::new().unwrap(); let site_dir = temp_dir.path().join("site"); fs::create_dir(&site_dir).unwrap(); let config = parse_site_config(&site_dir).unwrap(); assert_eq!(config.name, "default"); assert_eq!(config.title, ""); assert_eq!(config.description, None); assert_eq!(config.url, None); assert_eq!(config.favicon, None); assert!(config.header.is_none()); assert!(config.footer.is_none()); assert!(config.collections.is_empty()); assert!(config.pages.is_empty()); } #[test] fn test_parse_site_config_invalid_hjson() { let temp_dir = TempDir::new().unwrap(); let site_dir = temp_dir.path().join("site"); fs::create_dir(&site_dir).unwrap(); // Create invalid main.hjson let main_hjson = r#"{ name: "test, title: "Test Site" }"#; // Missing closing quote fs::write(site_dir.join("main.hjson"), main_hjson).unwrap(); let result = parse_site_config(&site_dir); assert!(matches!(result, Err(WebBuilderError::HjsonError(_)))); } }