//! Checks the generated asset set against its manifest. Run the converter first: //! `cargo run -p convert_frames -- --in frames --out assets/animations` use std::collections::BTreeSet; use std::path::PathBuf; use hamster_pet::core::chains::required_clips; use hamster_pet::core::manifest::{frame_file_name, MANIFEST_VERSION}; use hamster_pet::{Config, Engine, FrameStore, Manifest}; fn assets_root() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets/animations") } fn manifest() -> Manifest { Manifest::load(&assets_root().join("manifest.json")).unwrap_or_else(|err| { panic!("cannot read the generated manifest ({err}); run: cargo run -p convert_frames") }) } #[test] fn every_clip_has_exactly_the_frames_the_manifest_promises() { let manifest = manifest(); manifest.validate().expect("generated manifest is valid"); let store = FrameStore::new(assets_root(), manifest.clone(), 8); for (name, clip) in &manifest.clips { let dir = assets_root().join(name); let mut on_disk = BTreeSet::new(); for entry in std::fs::read_dir(&dir) .unwrap_or_else(|err| panic!("cannot list {}: {err}", dir.display())) { on_disk.insert(entry.unwrap().file_name().to_string_lossy().into_owned()); } let expected: BTreeSet = (0..clip.frames).map(frame_file_name).collect(); assert_eq!( on_disk, expected, "clip '{name}' does not match the manifest" ); // Decode the edges and the middle of each clip: dimensions must match the rect. for index in [0, clip.frames / 2, clip.frames - 1] { let frame = store .get(name, index) .unwrap_or_else(|err| panic!("{name} frame {index}: {err}")); assert_eq!( frame.dimensions(), (clip.rect.width, clip.rect.height), "clip '{name}' frame {index} size" ); } } } #[test] fn the_asset_set_covers_every_clip_the_engine_can_play() { let manifest = manifest(); for name in required_clips() { assert!(manifest.clips.contains_key(name), "missing clip '{name}'"); } } #[test] fn engine_starts_on_the_real_asset_set() { let manifest = manifest(); let (engine, warnings) = Engine::new(&manifest, Config::default(), 7, 0).expect("engine starts"); assert!(warnings.is_empty(), "{warnings:?}"); assert_eq!(engine.visible().0, "idle_main"); } #[test] fn resting_frame_keeps_dark_details_opaque() { // Golden pixels of idle_main frame 0: the background is transparent, the fur and the // very dark pixels under the belly stay fully opaque (luminance-as-alpha loses that). let manifest = manifest(); let store = FrameStore::new(assets_root(), manifest.clone(), 4); let frame = store.get("idle_main", 0).unwrap(); let (width, height) = frame.dimensions(); for corner in [ (0, 0), (width - 1, 0), (0, height - 1), (width - 1, height - 1), ] { assert_eq!( frame.get_pixel(corner.0, corner.1).0[3], 0, "corner {corner:?} is transparent" ); } for (x, y, expected) in [ (42, 50, [160, 148, 126, 255]), (40, 95, [107, 94, 85, 255]), (30, 60, [119, 80, 75, 255]), ] { assert_eq!(frame.get_pixel(x, y).0, expected, "pixel ({x},{y}) changed"); } } #[test] fn anchor_sits_on_the_bottom_of_the_resting_silhouette() { let manifest = manifest(); let store = FrameStore::new(assets_root(), manifest.clone(), 4); let frame = store.get("idle_main", 0).unwrap(); let clip = manifest.clip("idle_main").unwrap(); let last_opaque_row = (0..clip.rect.height) .rev() .find(|y| (0..clip.rect.width).any(|x| frame.get_pixel(x, *y).0[3] > 0)) .expect("idle_main is not empty"); assert_eq!( manifest.anchor.y as u32, clip.rect.y as u32 + last_opaque_row + 1, "anchor must sit on the bottom edge of the silhouette" ); assert!( manifest.anchor.x > clip.rect.x && manifest.anchor.x < clip.rect.x + clip.rect.width as i32 ); } #[test] fn manifest_uses_the_supported_version() { assert_eq!(manifest().version, MANIFEST_VERSION); }