From cc0dde106a7d1a2a719de174aa20104f81875d65 Mon Sep 17 00:00:00 2001 From: objz Date: Tue, 26 May 2026 21:11:38 +0200 Subject: [PATCH] refactored: move public-API filesystem tests to tests/ Per Phase C of the test overhaul: tests that touch the filesystem and exercise the public API surface (scan_screenshots, scan_shaders, scan_worlds, scan_resource_packs, log_files) move from inline mod tests into tests/. They now consume rmcl as an external crate, mirroring how real callers would. AccountStore::store_save_and_reload stays inline because the helper it needs (constructing AccountStore directly with a tempdir path) requires reaching into a private field; exposing that just for tests would leak implementation detail. The other accounts tests are in-memory and stay inline regardless. The extract_description_* tests in resource_packs stay inline since they are pure logic on serde_json::Value (no filesystem). image is added to dev-dependencies so integration tests can fabricate 1x1 PNG fixtures (already a regular dep of the lib). --- Cargo.toml | 1 + src/instance/content/resource_packs.rs | 61 -------- src/instance/content/shaders.rs | 61 -------- src/instance/content/worlds.rs | 76 ---------- src/instance/log_files.rs | 69 --------- src/instance/screenshots.rs | 84 ----------- src/main.rs | 2 - tests/content_scanners.rs | 187 +++++++++++++++++++++++++ tests/log_files.rs | 76 ++++++++++ tests/screenshots.rs | 85 +++++++++++ 10 files changed, 349 insertions(+), 353 deletions(-) create mode 100644 tests/content_scanners.rs create mode 100644 tests/log_files.rs create mode 100644 tests/screenshots.rs diff --git a/Cargo.toml b/Cargo.toml index c4cb89c..ed11178 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -76,3 +76,4 @@ tempfile = "3" insta = "1" wiremock = "0.6" rstest = "0.23" +image = { version = "0.25", default-features = false, features = ["png"] } diff --git a/src/instance/content/resource_packs.rs b/src/instance/content/resource_packs.rs index 4586fc1..cbaaa0e 100644 --- a/src/instance/content/resource_packs.rs +++ b/src/instance/content/resource_packs.rs @@ -201,65 +201,4 @@ mod tests { let val = serde_json::json!(true); assert_eq!(extract_description(&val), ""); } - - fn setup_packs_dir(tmp: &std::path::Path, instance: &str) -> std::path::PathBuf { - let dir = tmp.join(instance).join(".minecraft").join("resourcepacks"); - std::fs::create_dir_all(&dir).unwrap(); - dir - } - - #[test] - fn scan_resource_packs_empty_dir() { - let tmp = tempfile::tempdir().unwrap(); - setup_packs_dir(tmp.path(), "inst"); - let packs = scan_resource_packs(tmp.path(), "inst"); - assert!(packs.is_empty()); - } - - #[test] - fn scan_resource_packs_missing_dir_returns_empty() { - let tmp = tempfile::tempdir().unwrap(); - let packs = scan_resource_packs(tmp.path(), "ghost"); - assert!(packs.is_empty()); - } - - #[test] - fn scan_resource_packs_finds_zips_and_dirs() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_packs_dir(tmp.path(), "inst"); - std::fs::write(dir.join("pack-a.zip"), b"PK\x03\x04").unwrap(); - std::fs::create_dir(dir.join("pack-b")).unwrap(); - let packs = scan_resource_packs(tmp.path(), "inst"); - assert_eq!(packs.len(), 2); - } - - #[test] - fn scan_resource_packs_disabled_variants() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_packs_dir(tmp.path(), "inst"); - std::fs::write(dir.join("on.zip"), b"PK\x03\x04").unwrap(); - std::fs::write(dir.join("off.zip.disabled"), b"PK\x03\x04").unwrap(); - std::fs::create_dir(dir.join("diron")).unwrap(); - std::fs::create_dir(dir.join("diroff.disabled")).unwrap(); - let packs = scan_resource_packs(tmp.path(), "inst"); - assert_eq!(packs.len(), 4); - let on_zip = packs.iter().find(|p| p.file_stem == "on").unwrap(); - let off_zip = packs.iter().find(|p| p.file_stem == "off").unwrap(); - let on_dir = packs.iter().find(|p| p.file_stem == "diron").unwrap(); - let off_dir = packs.iter().find(|p| p.file_stem == "diroff").unwrap(); - assert!(on_zip.enabled); - assert!(!off_zip.enabled); - assert!(on_dir.enabled); - assert!(!off_dir.enabled); - } - - #[test] - fn scan_resource_packs_ignores_non_pack_files() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_packs_dir(tmp.path(), "inst"); - std::fs::write(dir.join("notes.txt"), "not a pack").unwrap(); - std::fs::write(dir.join("valid.zip"), b"PK\x03\x04").unwrap(); - let packs = scan_resource_packs(tmp.path(), "inst"); - assert_eq!(packs.len(), 1); - } } diff --git a/src/instance/content/shaders.rs b/src/instance/content/shaders.rs index 9a46f08..775f8d7 100644 --- a/src/instance/content/shaders.rs +++ b/src/instance/content/shaders.rs @@ -90,64 +90,3 @@ fn read_shader_metadata_from_dir(dir: &Path) -> (String, Option>) { (description, icon_bytes) } -#[cfg(test)] -mod tests { - use super::*; - - fn setup_shaders_dir(tmp: &std::path::Path, instance: &str) -> std::path::PathBuf { - let dir = tmp.join(instance).join(".minecraft").join("shaderpacks"); - std::fs::create_dir_all(&dir).unwrap(); - dir - } - - #[test] - fn scan_shaders_empty_dir() { - let tmp = tempfile::tempdir().unwrap(); - setup_shaders_dir(tmp.path(), "inst"); - let shaders = scan_shaders(tmp.path(), "inst"); - assert!(shaders.is_empty()); - } - - #[test] - fn scan_shaders_missing_dir_returns_empty() { - let tmp = tempfile::tempdir().unwrap(); - let shaders = scan_shaders(tmp.path(), "ghost"); - assert!(shaders.is_empty()); - } - - #[test] - fn scan_shaders_finds_zip_and_dir() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_shaders_dir(tmp.path(), "inst"); - std::fs::write(dir.join("shader-a.zip"), b"PK\x03\x04").unwrap(); - std::fs::create_dir(dir.join("shader-b")).unwrap(); - let shaders = scan_shaders(tmp.path(), "inst"); - assert_eq!(shaders.len(), 2); - } - - #[test] - fn scan_shaders_disabled_variants() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_shaders_dir(tmp.path(), "inst"); - std::fs::write(dir.join("active.zip"), b"PK\x03\x04").unwrap(); - std::fs::write(dir.join("off.zip.disabled"), b"PK\x03\x04").unwrap(); - std::fs::create_dir(dir.join("dirshader.disabled")).unwrap(); - let shaders = scan_shaders(tmp.path(), "inst"); - let active = shaders.iter().find(|s| s.file_stem == "active").unwrap(); - let off = shaders.iter().find(|s| s.file_stem == "off").unwrap(); - let diroff = shaders.iter().find(|s| s.file_stem == "dirshader").unwrap(); - assert!(active.enabled); - assert!(!off.enabled); - assert!(!diroff.enabled); - } - - #[test] - fn scan_shaders_ignores_non_shader_files() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_shaders_dir(tmp.path(), "inst"); - std::fs::write(dir.join("readme.txt"), "not a shader").unwrap(); - std::fs::write(dir.join("valid.zip"), b"PK\x03\x04").unwrap(); - let shaders = scan_shaders(tmp.path(), "inst"); - assert_eq!(shaders.len(), 1); - } -} diff --git a/src/instance/content/worlds.rs b/src/instance/content/worlds.rs index fd11df5..fce0aa9 100644 --- a/src/instance/content/worlds.rs +++ b/src/instance/content/worlds.rs @@ -135,79 +135,3 @@ fn format_size(bytes: u64) -> String { } } -#[cfg(test)] -mod tests { - use super::*; - - fn setup_saves_dir(tmp: &Path, instance: &str) -> std::path::PathBuf { - let dir = tmp.join(instance).join(".minecraft").join("saves"); - std::fs::create_dir_all(&dir).unwrap(); - dir - } - - #[test] - fn scan_worlds_empty_dir() { - let tmp = tempfile::tempdir().unwrap(); - setup_saves_dir(tmp.path(), "inst"); - let worlds = scan_worlds(tmp.path(), "inst"); - assert!(worlds.is_empty()); - } - - #[test] - fn scan_worlds_missing_dir_returns_empty() { - let tmp = tempfile::tempdir().unwrap(); - let worlds = scan_worlds(tmp.path(), "ghost"); - assert!(worlds.is_empty()); - } - - #[test] - fn scan_worlds_finds_directories() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_saves_dir(tmp.path(), "inst"); - std::fs::create_dir(dir.join("My World")).unwrap(); - std::fs::create_dir(dir.join("Creative")).unwrap(); - let worlds = scan_worlds(tmp.path(), "inst"); - assert_eq!(worlds.len(), 2); - } - - #[test] - fn scan_worlds_ignores_files() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_saves_dir(tmp.path(), "inst"); - std::fs::create_dir(dir.join("World1")).unwrap(); - std::fs::write(dir.join("stray-file.txt"), "not a world").unwrap(); - let worlds = scan_worlds(tmp.path(), "inst"); - assert_eq!(worlds.len(), 1); - } - - #[test] - fn scan_worlds_disabled_world() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_saves_dir(tmp.path(), "inst"); - std::fs::create_dir(dir.join("ActiveWorld")).unwrap(); - std::fs::create_dir(dir.join("HiddenWorld.disabled")).unwrap(); - let worlds = scan_worlds(tmp.path(), "inst"); - let active = worlds - .iter() - .find(|w| w.file_stem == "ActiveWorld") - .unwrap(); - let hidden = worlds - .iter() - .find(|w| w.file_stem == "HiddenWorld") - .unwrap(); - assert!(active.enabled); - assert!(!hidden.enabled); - } - - #[test] - fn scan_worlds_sorted_case_insensitive() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_saves_dir(tmp.path(), "inst"); - std::fs::create_dir(dir.join("Zeta")).unwrap(); - std::fs::create_dir(dir.join("alpha")).unwrap(); - std::fs::create_dir(dir.join("Beta")).unwrap(); - let worlds = scan_worlds(tmp.path(), "inst"); - let names: Vec<&str> = worlds.iter().map(|w| w.name.as_str()).collect(); - assert_eq!(names, vec!["alpha", "Beta", "Zeta"]); - } -} diff --git a/src/instance/log_files.rs b/src/instance/log_files.rs index ec244f0..858f6ff 100644 --- a/src/instance/log_files.rs +++ b/src/instance/log_files.rs @@ -61,12 +61,6 @@ pub fn read_log_file(path: &Path) -> Vec { mod tests { use super::*; - fn setup_log_dir(tmp: &Path, instance: &str) -> PathBuf { - let dir = log_dir(tmp, instance); - std::fs::create_dir_all(&dir).unwrap(); - dir - } - #[test] fn log_dir_builds_correct_path() { let p = log_dir(Path::new("/instances"), "my-world"); @@ -75,67 +69,4 @@ mod tests { PathBuf::from("/instances/my-world/.minecraft/logs/launches") ); } - - #[test] - fn scan_log_files_empty_dir() { - let tmp = tempfile::tempdir().unwrap(); - setup_log_dir(tmp.path(), "inst"); - let entries = scan_log_files(tmp.path(), "inst"); - assert!(entries.is_empty()); - } - - #[test] - fn scan_log_files_finds_logs() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_log_dir(tmp.path(), "inst"); - std::fs::write(dir.join("2024-01-01_12-00-00.log"), "line1\nline2").unwrap(); - std::fs::write(dir.join("2024-01-02_12-00-00.log"), "line3").unwrap(); - let entries = scan_log_files(tmp.path(), "inst"); - assert_eq!(entries.len(), 2); - assert_eq!(entries[0].name, "2024-01-02_12-00-00.log"); - assert_eq!(entries[1].name, "2024-01-01_12-00-00.log"); - } - - #[test] - fn scan_log_files_ignores_non_log() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_log_dir(tmp.path(), "inst"); - std::fs::write(dir.join("notes.txt"), "not a log").unwrap(); - std::fs::write(dir.join("real.log"), "log line").unwrap(); - let entries = scan_log_files(tmp.path(), "inst"); - assert_eq!(entries.len(), 1); - assert_eq!(entries[0].name, "real.log"); - } - - #[test] - fn scan_log_files_missing_dir_returns_empty() { - let tmp = tempfile::tempdir().unwrap(); - let entries = scan_log_files(tmp.path(), "ghost"); - assert!(entries.is_empty()); - } - - #[test] - fn read_log_file_returns_lines() { - let tmp = tempfile::tempdir().unwrap(); - let path = tmp.path().join("test.log"); - std::fs::write(&path, "alpha\nbeta\ngamma").unwrap(); - let lines = read_log_file(&path); - assert_eq!(lines, vec!["alpha", "beta", "gamma"]); - } - - #[test] - fn read_log_file_missing_returns_empty() { - let lines = read_log_file(Path::new("/nonexistent/test.log")); - assert!(lines.is_empty()); - } - - #[test] - fn create_log_file_creates_path() { - let tmp = tempfile::tempdir().unwrap(); - let path = create_log_file(tmp.path(), "inst"); - assert!(path.is_some()); - let path = path.unwrap(); - assert!(path.to_string_lossy().ends_with(".log")); - assert!(path.parent().unwrap().exists()); - } } diff --git a/src/instance/screenshots.rs b/src/instance/screenshots.rs index 95225bf..62b5acd 100644 --- a/src/instance/screenshots.rs +++ b/src/instance/screenshots.rs @@ -46,87 +46,3 @@ pub fn scan_screenshots(instances_dir: &Path, instance_name: &str) -> Vec PathBuf { - let dir = tmp.join(instance).join(".minecraft").join("screenshots"); - std::fs::create_dir_all(&dir).unwrap(); - dir - } - - fn tiny_png() -> Vec { - let img = image::RgbImage::from_pixel(1, 1, image::Rgb([255, 255, 255])); - let mut buf = std::io::Cursor::new(Vec::new()); - img.write_to(&mut buf, image::ImageFormat::Png).unwrap(); - buf.into_inner() - } - - #[test] - fn scan_screenshots_empty_dir() { - let tmp = tempfile::tempdir().unwrap(); - setup_screenshots_dir(tmp.path(), "inst"); - let screenshots = scan_screenshots(tmp.path(), "inst"); - assert!(screenshots.is_empty()); - } - - #[test] - fn scan_screenshots_missing_dir_returns_empty() { - let tmp = tempfile::tempdir().unwrap(); - let screenshots = scan_screenshots(tmp.path(), "ghost"); - assert!(screenshots.is_empty()); - } - - #[test] - fn scan_screenshots_finds_images() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_screenshots_dir(tmp.path(), "inst"); - std::fs::write(dir.join("2024-01-01.png"), tiny_png()).unwrap(); - std::fs::write(dir.join("2024-01-02.png"), tiny_png()).unwrap(); - let screenshots = scan_screenshots(tmp.path(), "inst"); - assert_eq!(screenshots.len(), 2); - } - - #[test] - fn scan_screenshots_ignores_non_images() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_screenshots_dir(tmp.path(), "inst"); - std::fs::write(dir.join("pic.png"), tiny_png()).unwrap(); - std::fs::write(dir.join("notes.txt"), "not an image").unwrap(); - let screenshots = scan_screenshots(tmp.path(), "inst"); - assert_eq!(screenshots.len(), 1); - } - - #[test] - fn scan_screenshots_sorted_newest_first() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_screenshots_dir(tmp.path(), "inst"); - std::fs::write(dir.join("aaa.png"), tiny_png()).unwrap(); - std::fs::write(dir.join("zzz.png"), tiny_png()).unwrap(); - let screenshots = scan_screenshots(tmp.path(), "inst"); - assert_eq!(screenshots[0].name, "zzz.png"); - assert_eq!(screenshots[1].name, "aaa.png"); - } - - #[test] - fn scan_screenshots_reads_dimensions() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_screenshots_dir(tmp.path(), "inst"); - std::fs::write(dir.join("shot.png"), tiny_png()).unwrap(); - let screenshots = scan_screenshots(tmp.path(), "inst"); - assert_eq!(screenshots[0].width, 1); - assert_eq!(screenshots[0].height, 1); - } - - #[test] - fn scan_screenshots_bad_image_uses_default_dimensions() { - let tmp = tempfile::tempdir().unwrap(); - let dir = setup_screenshots_dir(tmp.path(), "inst"); - std::fs::write(dir.join("corrupt.png"), b"not a real png").unwrap(); - let screenshots = scan_screenshots(tmp.path(), "inst"); - assert_eq!(screenshots.len(), 1); - assert_eq!(screenshots[0].width, 1920); - assert_eq!(screenshots[0].height, 1080); - } -} diff --git a/src/main.rs b/src/main.rs index ec7b258..2301b5d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,5 @@ #[tokio::main] async fn main() { - // Run before logging::init() so the cache rename isn't blocked by a - // freshly-created ~/.cache/rmcl/ directory. rmcl::migrate_legacy_rename(); // Guard must stay in scope to keep the log file writer alive diff --git a/tests/content_scanners.rs b/tests/content_scanners.rs new file mode 100644 index 0000000..aa4dd80 --- /dev/null +++ b/tests/content_scanners.rs @@ -0,0 +1,187 @@ +// integration tests for the public content-scanner APIs: shaders, worlds, +// and resource packs. each scanner walks an instance's .minecraft subdir +// and returns ContentEntry rows; shape varies slightly per content type +// (worlds are directories only, packs and shaders accept .zip or dir, etc.). + +use std::path::{Path, PathBuf}; + +use rmcl::instance::content::resource_packs::scan_resource_packs; +use rmcl::instance::content::shaders::scan_shaders; +use rmcl::instance::content::worlds::scan_worlds; + +fn setup_subdir(tmp: &Path, instance: &str, sub: &str) -> PathBuf { + let dir = tmp.join(instance).join(".minecraft").join(sub); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + +// minimal zip header byte stream; enough that the scanner accepts the file +// without panicking even though it can't actually unpack metadata. +const ZIP_HEADER: &[u8] = b"PK\x03\x04"; + +// ---------- shaders ---------- + +#[test] +fn shaders_empty_dir_returns_empty() { + let tmp = tempfile::tempdir().unwrap(); + setup_subdir(tmp.path(), "inst", "shaderpacks"); + assert!(scan_shaders(tmp.path(), "inst").is_empty()); +} + +#[test] +fn shaders_missing_dir_returns_empty() { + let tmp = tempfile::tempdir().unwrap(); + assert!(scan_shaders(tmp.path(), "ghost").is_empty()); +} + +#[test] +fn shaders_finds_zip_and_dir() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_subdir(tmp.path(), "inst", "shaderpacks"); + std::fs::write(dir.join("shader-a.zip"), ZIP_HEADER).unwrap(); + std::fs::create_dir(dir.join("shader-b")).unwrap(); + assert_eq!(scan_shaders(tmp.path(), "inst").len(), 2); +} + +#[test] +fn shaders_disabled_variants() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_subdir(tmp.path(), "inst", "shaderpacks"); + std::fs::write(dir.join("active.zip"), ZIP_HEADER).unwrap(); + std::fs::write(dir.join("off.zip.disabled"), ZIP_HEADER).unwrap(); + std::fs::create_dir(dir.join("dirshader.disabled")).unwrap(); + let shaders = scan_shaders(tmp.path(), "inst"); + let active = shaders.iter().find(|s| s.file_stem == "active").unwrap(); + let off = shaders.iter().find(|s| s.file_stem == "off").unwrap(); + let diroff = shaders.iter().find(|s| s.file_stem == "dirshader").unwrap(); + assert!(active.enabled); + assert!(!off.enabled); + assert!(!diroff.enabled); +} + +#[test] +fn shaders_ignores_non_shader_files() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_subdir(tmp.path(), "inst", "shaderpacks"); + std::fs::write(dir.join("readme.txt"), "not a shader").unwrap(); + std::fs::write(dir.join("valid.zip"), ZIP_HEADER).unwrap(); + assert_eq!(scan_shaders(tmp.path(), "inst").len(), 1); +} + +// ---------- worlds ---------- +// worlds are always directories (no zip form), so the test shape differs. + +#[test] +fn worlds_empty_dir_returns_empty() { + let tmp = tempfile::tempdir().unwrap(); + setup_subdir(tmp.path(), "inst", "saves"); + assert!(scan_worlds(tmp.path(), "inst").is_empty()); +} + +#[test] +fn worlds_missing_dir_returns_empty() { + let tmp = tempfile::tempdir().unwrap(); + assert!(scan_worlds(tmp.path(), "ghost").is_empty()); +} + +#[test] +fn worlds_finds_directories() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_subdir(tmp.path(), "inst", "saves"); + std::fs::create_dir(dir.join("My World")).unwrap(); + std::fs::create_dir(dir.join("Creative")).unwrap(); + assert_eq!(scan_worlds(tmp.path(), "inst").len(), 2); +} + +#[test] +fn worlds_ignores_files() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_subdir(tmp.path(), "inst", "saves"); + std::fs::create_dir(dir.join("World1")).unwrap(); + std::fs::write(dir.join("stray-file.txt"), "not a world").unwrap(); + assert_eq!(scan_worlds(tmp.path(), "inst").len(), 1); +} + +#[test] +fn worlds_disabled_world() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_subdir(tmp.path(), "inst", "saves"); + std::fs::create_dir(dir.join("ActiveWorld")).unwrap(); + std::fs::create_dir(dir.join("HiddenWorld.disabled")).unwrap(); + let worlds = scan_worlds(tmp.path(), "inst"); + let active = worlds + .iter() + .find(|w| w.file_stem == "ActiveWorld") + .unwrap(); + let hidden = worlds + .iter() + .find(|w| w.file_stem == "HiddenWorld") + .unwrap(); + assert!(active.enabled); + assert!(!hidden.enabled); +} + +#[test] +fn worlds_sorted_case_insensitive() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_subdir(tmp.path(), "inst", "saves"); + std::fs::create_dir(dir.join("Zeta")).unwrap(); + std::fs::create_dir(dir.join("alpha")).unwrap(); + std::fs::create_dir(dir.join("Beta")).unwrap(); + let worlds = scan_worlds(tmp.path(), "inst"); + let names: Vec<&str> = worlds.iter().map(|w| w.name.as_str()).collect(); + assert_eq!(names, vec!["alpha", "Beta", "Zeta"]); +} + +// ---------- resource packs ---------- + +#[test] +fn resource_packs_empty_dir_returns_empty() { + let tmp = tempfile::tempdir().unwrap(); + setup_subdir(tmp.path(), "inst", "resourcepacks"); + assert!(scan_resource_packs(tmp.path(), "inst").is_empty()); +} + +#[test] +fn resource_packs_missing_dir_returns_empty() { + let tmp = tempfile::tempdir().unwrap(); + assert!(scan_resource_packs(tmp.path(), "ghost").is_empty()); +} + +#[test] +fn resource_packs_finds_zips_and_dirs() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_subdir(tmp.path(), "inst", "resourcepacks"); + std::fs::write(dir.join("pack-a.zip"), ZIP_HEADER).unwrap(); + std::fs::create_dir(dir.join("pack-b")).unwrap(); + assert_eq!(scan_resource_packs(tmp.path(), "inst").len(), 2); +} + +#[test] +fn resource_packs_disabled_variants() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_subdir(tmp.path(), "inst", "resourcepacks"); + std::fs::write(dir.join("on.zip"), ZIP_HEADER).unwrap(); + std::fs::write(dir.join("off.zip.disabled"), ZIP_HEADER).unwrap(); + std::fs::create_dir(dir.join("diron")).unwrap(); + std::fs::create_dir(dir.join("diroff.disabled")).unwrap(); + let packs = scan_resource_packs(tmp.path(), "inst"); + assert_eq!(packs.len(), 4); + let on_zip = packs.iter().find(|p| p.file_stem == "on").unwrap(); + let off_zip = packs.iter().find(|p| p.file_stem == "off").unwrap(); + let on_dir = packs.iter().find(|p| p.file_stem == "diron").unwrap(); + let off_dir = packs.iter().find(|p| p.file_stem == "diroff").unwrap(); + assert!(on_zip.enabled); + assert!(!off_zip.enabled); + assert!(on_dir.enabled); + assert!(!off_dir.enabled); +} + +#[test] +fn resource_packs_ignores_non_pack_files() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_subdir(tmp.path(), "inst", "resourcepacks"); + std::fs::write(dir.join("notes.txt"), "not a pack").unwrap(); + std::fs::write(dir.join("valid.zip"), ZIP_HEADER).unwrap(); + assert_eq!(scan_resource_packs(tmp.path(), "inst").len(), 1); +} diff --git a/tests/log_files.rs b/tests/log_files.rs new file mode 100644 index 0000000..6e8b8f8 --- /dev/null +++ b/tests/log_files.rs @@ -0,0 +1,76 @@ +// integration tests for the public log_files API. +// these tests touch the filesystem and exercise the module as an external +// consumer would. + +use std::path::{Path, PathBuf}; + +use rmcl::instance::log_files::{create_log_file, log_dir, read_log_file, scan_log_files}; + +fn setup_log_dir(tmp: &Path, instance: &str) -> PathBuf { + let dir = log_dir(tmp, instance); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + +#[test] +fn scan_log_files_empty_dir() { + let tmp = tempfile::tempdir().unwrap(); + setup_log_dir(tmp.path(), "inst"); + let entries = scan_log_files(tmp.path(), "inst"); + assert!(entries.is_empty()); +} + +#[test] +fn scan_log_files_finds_logs() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_log_dir(tmp.path(), "inst"); + std::fs::write(dir.join("2024-01-01_12-00-00.log"), "line1\nline2").unwrap(); + std::fs::write(dir.join("2024-01-02_12-00-00.log"), "line3").unwrap(); + let entries = scan_log_files(tmp.path(), "inst"); + assert_eq!(entries.len(), 2); + assert_eq!(entries[0].name, "2024-01-02_12-00-00.log"); + assert_eq!(entries[1].name, "2024-01-01_12-00-00.log"); +} + +#[test] +fn scan_log_files_ignores_non_log() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_log_dir(tmp.path(), "inst"); + std::fs::write(dir.join("notes.txt"), "not a log").unwrap(); + std::fs::write(dir.join("real.log"), "log line").unwrap(); + let entries = scan_log_files(tmp.path(), "inst"); + assert_eq!(entries.len(), 1); + assert_eq!(entries[0].name, "real.log"); +} + +#[test] +fn scan_log_files_missing_dir_returns_empty() { + let tmp = tempfile::tempdir().unwrap(); + let entries = scan_log_files(tmp.path(), "ghost"); + assert!(entries.is_empty()); +} + +#[test] +fn read_log_file_returns_lines() { + let tmp = tempfile::tempdir().unwrap(); + let path = tmp.path().join("test.log"); + std::fs::write(&path, "alpha\nbeta\ngamma").unwrap(); + let lines = read_log_file(&path); + assert_eq!(lines, vec!["alpha", "beta", "gamma"]); +} + +#[test] +fn read_log_file_missing_returns_empty() { + let lines = read_log_file(Path::new("/nonexistent/test.log")); + assert!(lines.is_empty()); +} + +#[test] +fn create_log_file_creates_path() { + let tmp = tempfile::tempdir().unwrap(); + let path = create_log_file(tmp.path(), "inst"); + assert!(path.is_some()); + let path = path.unwrap(); + assert!(path.to_string_lossy().ends_with(".log")); + assert!(path.parent().unwrap().exists()); +} diff --git a/tests/screenshots.rs b/tests/screenshots.rs new file mode 100644 index 0000000..699c104 --- /dev/null +++ b/tests/screenshots.rs @@ -0,0 +1,85 @@ +// integration tests for the public scan_screenshots API. + +use std::path::{Path, PathBuf}; + +use rmcl::instance::screenshots::scan_screenshots; + +fn setup_screenshots_dir(tmp: &Path, instance: &str) -> PathBuf { + let dir = tmp.join(instance).join(".minecraft").join("screenshots"); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + +fn tiny_png() -> Vec { + let img = image::RgbImage::from_pixel(1, 1, image::Rgb([255, 255, 255])); + let mut buf = std::io::Cursor::new(Vec::new()); + img.write_to(&mut buf, image::ImageFormat::Png).unwrap(); + buf.into_inner() +} + +#[test] +fn scan_screenshots_empty_dir() { + let tmp = tempfile::tempdir().unwrap(); + setup_screenshots_dir(tmp.path(), "inst"); + let screenshots = scan_screenshots(tmp.path(), "inst"); + assert!(screenshots.is_empty()); +} + +#[test] +fn scan_screenshots_missing_dir_returns_empty() { + let tmp = tempfile::tempdir().unwrap(); + let screenshots = scan_screenshots(tmp.path(), "ghost"); + assert!(screenshots.is_empty()); +} + +#[test] +fn scan_screenshots_finds_images() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_screenshots_dir(tmp.path(), "inst"); + std::fs::write(dir.join("2024-01-01.png"), tiny_png()).unwrap(); + std::fs::write(dir.join("2024-01-02.png"), tiny_png()).unwrap(); + let screenshots = scan_screenshots(tmp.path(), "inst"); + assert_eq!(screenshots.len(), 2); +} + +#[test] +fn scan_screenshots_ignores_non_images() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_screenshots_dir(tmp.path(), "inst"); + std::fs::write(dir.join("pic.png"), tiny_png()).unwrap(); + std::fs::write(dir.join("notes.txt"), "not an image").unwrap(); + let screenshots = scan_screenshots(tmp.path(), "inst"); + assert_eq!(screenshots.len(), 1); +} + +#[test] +fn scan_screenshots_sorted_newest_first() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_screenshots_dir(tmp.path(), "inst"); + std::fs::write(dir.join("aaa.png"), tiny_png()).unwrap(); + std::fs::write(dir.join("zzz.png"), tiny_png()).unwrap(); + let screenshots = scan_screenshots(tmp.path(), "inst"); + assert_eq!(screenshots[0].name, "zzz.png"); + assert_eq!(screenshots[1].name, "aaa.png"); +} + +#[test] +fn scan_screenshots_reads_dimensions() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_screenshots_dir(tmp.path(), "inst"); + std::fs::write(dir.join("shot.png"), tiny_png()).unwrap(); + let screenshots = scan_screenshots(tmp.path(), "inst"); + assert_eq!(screenshots[0].width, 1); + assert_eq!(screenshots[0].height, 1); +} + +#[test] +fn scan_screenshots_bad_image_uses_default_dimensions() { + let tmp = tempfile::tempdir().unwrap(); + let dir = setup_screenshots_dir(tmp.path(), "inst"); + std::fs::write(dir.join("corrupt.png"), b"not a real png").unwrap(); + let screenshots = scan_screenshots(tmp.path(), "inst"); + assert_eq!(screenshots.len(), 1); + assert_eq!(screenshots[0].width, 1920); + assert_eq!(screenshots[0].height, 1080); +}