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).
This commit is contained in:
objz committed 2026-05-26 21:11:38 +02:00
1 parent c6fce8592a
commit cc0dde106a
10 files changed
+349 -353

No files matched your search

+187
View File
@@ -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);
}
+76
View File
@@ -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());
}
+85
View File
@@ -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<u8> {
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);
}