use pomomon_garden::features::config::components::GameConfig; use std::fs; use std::io::Write; use uuid::Uuid; // Helper function to create a temporary file with content fn create_temp_file(content: &str) -> (std::path::PathBuf, String) { let filename = format!("test_config_{}.json", Uuid::new_v4()); let temp_dir = std::env::temp_dir(); let filepath = temp_dir.join(&filename); let mut file = fs::File::create(&filepath).expect("Could not create temp file"); file.write_all(content.as_bytes()) .expect("Could not write to temp file"); (filepath, filename) } #[test] fn test_load_valid_config() { let (filepath, _filename) = create_temp_file( r#"{ "grid_width": 10, "grid_height": 5, "pom_speed": 2.0, "shovel_base_price": 10, "shovel_rate": 0.2, "berry_seeds": [], "wonder_event_url": "wss://pomomon.farm/ws" }"#, ); let config = GameConfig::read_from_path(&filepath).expect("Failed to read valid config"); assert_eq!(config.grid_width, 10); assert_eq!(config.grid_height, 5); assert_eq!(config.pom_speed, 2.0); fs::remove_file(filepath).expect("Failed to delete temp file"); } #[test] fn test_load_invalid_config() { let (filepath, _filename) = create_temp_file(r#"this is not valid json"#); let config = GameConfig::read_from_path(&filepath); assert!(config.is_none(), "Expected invalid config to return None"); fs::remove_file(filepath).expect("Failed to delete temp file"); } #[test] fn test_load_missing_config() { let temp_dir = std::env::temp_dir(); let filepath = temp_dir.join("non_existent_config.json"); let config = GameConfig::read_from_path(&filepath); assert!(config.is_none(), "Expected missing config to return None"); }