use pixelsrc::config::{load_config, PxlConfig};
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use tempfile::TempDir;
fn basic_toml() -> &'static str {
include_str!("../../../examples/demos/build/basic.toml")
}
fn full_toml() -> &'static str {
include_str!("../../../examples/demos/build/full.toml")
}
#[test]
fn test_basic_config_minimal() {
let toml_content = basic_toml();
let config: PxlConfig = toml::from_str(toml_content).expect("Should parse basic config");
assert_eq!(config.project.name, "basic-demo", "Project name should match");
assert_eq!(config.project.version, "0.1.0", "Default version should be 0.1.0");
assert_eq!(config.project.src, PathBuf::from("src/pxl"), "Default src should be src/pxl");
assert_eq!(config.project.out, PathBuf::from("build"), "Default out should be build");
assert_eq!(config.defaults.scale, 1, "Default scale should be 1");
assert_eq!(config.defaults.padding, 1, "Default padding should be 1");
let errors = config.validate();
assert!(errors.is_empty(), "Basic config should have no validation errors");
}
#[test]
fn test_basic_config_full() {
let toml_content = full_toml();
let config: PxlConfig = toml::from_str(toml_content).expect("Should parse full config");
assert_eq!(config.project.name, "full-demo");
assert_eq!(config.project.version, "1.0.0");
assert_eq!(config.project.src, PathBuf::from("assets/pxl"));
assert_eq!(config.project.out, PathBuf::from("dist"));
assert_eq!(config.defaults.scale, 2);
assert_eq!(config.defaults.padding, 4);
assert!(config.atlases.contains_key("characters"), "Should have characters atlas");
assert!(config.atlases.contains_key("ui"), "Should have ui atlas");
let chars = config.atlases.get("characters").unwrap();
assert_eq!(chars.sources.len(), 2);
assert!(chars.sources.contains(&"sprites/player/**".to_string()));
assert!(chars.sources.contains(&"sprites/enemies/**".to_string()));
assert_eq!(chars.max_size, [2048, 2048]);
assert_eq!(chars.padding, Some(2));
assert!(chars.power_of_two);
assert!(config.animations.preview);
assert_eq!(config.animations.preview_scale, 4);
assert!(config.exports.godot.enabled);
assert_eq!(config.exports.godot.resource_path, "res://sprites");
assert!(config.exports.unity.enabled);
assert_eq!(config.exports.unity.pixels_per_unit, 32);
assert!(!config.exports.libgdx.enabled);
assert!(config.validate.strict);
assert_eq!(config.watch.debounce_ms, 200);
assert!(!config.watch.clear_screen);
let errors = config.validate();
assert!(errors.is_empty(), "Full config should have no validation errors");
}
#[test]
fn test_basic_config_load_file() {
let temp = TempDir::new().expect("Should create temp dir");
let config_path = temp.path().join("pxl.toml");
let mut file = fs::File::create(&config_path).expect("Should create file");
file.write_all(basic_toml().as_bytes()).expect("Should write file");
let config = load_config(Some(&config_path)).expect("Should load config from file");
assert_eq!(config.project.name, "basic-demo");
assert!(config.is_valid(), "Loaded config should be valid");
}
#[test]
fn test_basic_config_validation_errors() {
let invalid_name = r#"
[project]
name = ""
"#;
let config: PxlConfig = toml::from_str(invalid_name).expect("Should parse");
let errors = config.validate();
assert!(!errors.is_empty(), "Empty name should produce error");
assert!(errors.iter().any(|e| e.field == "project.name"), "Should have project.name error");
let invalid_scale = r#"
[project]
name = "test"
[defaults]
scale = 0
"#;
let config: PxlConfig = toml::from_str(invalid_scale).expect("Should parse");
let errors = config.validate();
assert!(errors.iter().any(|e| e.field == "defaults.scale"), "Should have defaults.scale error");
let invalid_atlas = r#"
[project]
name = "test"
[atlases.empty]
sources = []
"#;
let config: PxlConfig = toml::from_str(invalid_atlas).expect("Should parse");
let errors = config.validate();
assert!(
errors.iter().any(|e| e.field.contains("atlases.empty.sources")),
"Should have atlas sources error"
);
}
#[test]
fn test_basic_config_defaults_applied() {
let minimal = r#"
[project]
name = "minimal-test"
"#;
let config: PxlConfig = toml::from_str(minimal).expect("Should parse");
assert_eq!(config.project.version, "0.1.0");
assert_eq!(config.project.src, PathBuf::from("src/pxl"));
assert_eq!(config.project.out, PathBuf::from("build"));
assert_eq!(config.defaults.scale, 1);
assert_eq!(config.defaults.padding, 1);
assert!(config.atlases.is_empty());
assert!(!config.animations.preview);
assert_eq!(config.animations.preview_scale, 1);
assert!(config.exports.generic.enabled);
assert!(!config.exports.godot.enabled);
assert!(!config.exports.unity.enabled);
assert!(!config.exports.libgdx.enabled);
assert!(!config.validate.strict);
assert_eq!(config.watch.debounce_ms, 100);
assert!(config.watch.clear_screen);
}