use std::path::Path;

use gltf_level::LoadedLevel;

#[test]
fn one_mesh_per_material_primitive() {
    let level = LoadedLevel::<()>::load(Path::new("tests/fixtures/multi_material.glb")).unwrap();

    assert_eq!(level.meshes.len(), 2, "two materials must yield two meshes");

    let mut names: Vec<&str> = level
        .meshes
        .iter()
        .filter_map(|mesh| {
            mesh.material
                .as_ref()
                .map(|material| material.name.as_str())
        })
        .collect();
    names.sort_unstable();
    assert_eq!(names, ["MaterialA", "MaterialB"]);

    for mesh in &level.meshes {
        assert!(!mesh.vertices.is_empty());
        assert!(!mesh.indices.is_empty());
        let in_range = mesh
            .indices
            .iter()
            .all(|&index| (index as usize) < mesh.vertices.len());
        assert!(in_range, "indices must address own vertex list");
    }
}

#[test]
fn load_from_slice_matches_load() {
    let bytes = include_bytes!("fixtures/multi_material.glb");
    let from_slice = LoadedLevel::<()>::load_from_slice(bytes).unwrap();
    let from_path =
        LoadedLevel::<()>::load(Path::new("tests/fixtures/multi_material.glb")).unwrap();

    assert_eq!(from_slice.meshes.len(), from_path.meshes.len());
    for (sliced, pathed) in from_slice.meshes.iter().zip(&from_path.meshes) {
        assert_eq!(sliced.vertices.len(), pathed.vertices.len());
        assert_eq!(sliced.indices, pathed.indices);
    }
}
