From e21f68fd7c61b3e04997aec2de8cf7adfd826625 Mon Sep 17 00:00:00 2001 From: objz Date: Mon, 27 Jul 2026 00:37:48 +0200 Subject: [PATCH] test: harden migration and UI coverage --- src/instance/import/mod.rs | 3 +- src/instance/launch/patches.rs | 3 +- src/layout_migration.rs | 132 ++++++++++++++++++++- src/tui/widgets/content/discovery/tests.rs | 4 + src/tui/widgets/content/list/tests.rs | 16 ++- tests/log_files.rs | 3 +- 6 files changed, 146 insertions(+), 15 deletions(-) diff --git a/src/instance/import/mod.rs b/src/instance/import/mod.rs index 7568424..4ae6626 100644 --- a/src/instance/import/mod.rs +++ b/src/instance/import/mod.rs @@ -253,7 +253,8 @@ mod tests { #[test] fn detect_format_errors_on_missing_file() { - let err = detect_format(Path::new("/nonexistent/pack.zip")).unwrap_err(); + let tmp = tempfile::tempdir().unwrap(); + let err = detect_format(&tmp.path().join("missing.zip")).unwrap_err(); assert!(err.contains("Cannot open"), "got: {err}"); } diff --git a/src/instance/launch/patches.rs b/src/instance/launch/patches.rs index 52f40df..9e5da00 100644 --- a/src/instance/launch/patches.rs +++ b/src/instance/launch/patches.rs @@ -438,7 +438,8 @@ mod tests { #[test] fn parse_add_opens_returns_none_for_missing_file() { - assert!(parse_add_opens(Path::new("/nonexistent/x.zip")).is_none()); + let tmp = tempfile::tempdir().unwrap(); + assert!(parse_add_opens(&tmp.path().join("missing.zip")).is_none()); } #[test] diff --git a/src/layout_migration.rs b/src/layout_migration.rs index bb6eb76..117a582 100644 --- a/src/layout_migration.rs +++ b/src/layout_migration.rs @@ -705,6 +705,123 @@ mod tests { assert!(!cache_rebuild_pending(&meta)); } + #[test] + fn migration_resumes_from_the_recorded_journal() { + let temp = tempfile::tempdir().unwrap(); + let instances = temp.path().join("instances"); + let meta = temp.path().join("meta"); + let config = temp.path().join("config.toml"); + let done = instances.join("Done"); + let pending = instances.join("Pending"); + fs::create_dir_all(done.join("minecraft")).unwrap(); + fs::create_dir_all(pending.join(".minecraft/saves")).unwrap(); + + let metadata = MetadataPaths::new(&meta); + fs::create_dir_all(metadata.state()).unwrap(); + fs::create_dir_all(metadata.backups()).unwrap(); + let backup = metadata.backups().join("backup-resume"); + fs::create_dir_all(&backup).unwrap(); + write_json_atomic( + &metadata.migration_journal(), + &MigrationJournal { + version: LAYOUT_VERSION, + backup_dir: backup.clone(), + completed: vec!["backup".to_owned(), "instance:Done".to_owned()], + }, + ) + .unwrap(); + + assert_eq!(run(&instances, &meta, &config, |_| {}).unwrap(), backup); + assert!(done.join("minecraft").exists()); + assert!(pending.join("minecraft/saves").exists()); + assert!(!metadata.migration_journal().exists()); + } + + #[test] + fn stale_partial_backup_is_replaced() { + let temp = tempfile::tempdir().unwrap(); + let instances = temp.path().join("instances"); + let backup = temp.path().join("backups/backup-test"); + let partial = backup.with_extension("partial"); + fs::create_dir_all(instances.join("Example")).unwrap(); + fs::write(instances.join("Example/data.txt"), b"current").unwrap(); + fs::create_dir_all(&partial).unwrap(); + fs::write(partial.join("stale.txt"), b"stale").unwrap(); + + backup_user_data( + &instances, + &temp.path().join("missing-config.toml"), + &temp.path().join("meta"), + &backup, + |_, _, _| {}, + ) + .unwrap(); + + assert_eq!( + fs::read(backup.join("instances/Example/data.txt")).unwrap(), + b"current" + ); + assert!(!backup.join("stale.txt").exists()); + assert!(!partial.exists()); + } + + #[test] + fn backup_rejects_overlap_and_insufficient_space() { + let temp = tempfile::tempdir().unwrap(); + let instances = temp.path().join("instances"); + fs::create_dir_all(&instances).unwrap(); + + let overlap = + validate_backup_destination(&instances, &instances.join("backup"), 0).unwrap_err(); + assert!(matches!(overlap, MigrationError::BackupOverlap(_))); + + let outside = temp.path().join("backups/backup"); + let insufficient = validate_backup_destination(&instances, &outside, u64::MAX).unwrap_err(); + assert!(matches!( + insufficient, + MigrationError::InsufficientSpace { .. } + )); + } + + #[test] + fn migration_need_follows_legacy_data_marker_and_pending_rebuild() { + let temp = tempfile::tempdir().unwrap(); + let instances = temp.path().join("instances"); + let meta = temp.path().join("meta"); + fs::create_dir_all(instances.join("Example/.minecraft")).unwrap(); + + assert!(is_needed(&instances, &meta)); + initialize_new_layout(&meta).unwrap(); + assert!(!is_needed(&instances, &meta)); + fs::write(MetadataPaths::new(&meta).layout_marker(), b"invalid marker").unwrap(); + assert!(is_needed(&instances, &meta)); + initialize_new_layout(&meta).unwrap(); + fs::write( + MetadataPaths::new(&meta).cache_rebuild_pending(), + LAYOUT_VERSION.to_string(), + ) + .unwrap(); + assert!(is_needed(&instances, &meta)); + } + + #[cfg(unix)] + #[test] + fn backup_preserves_symlinks() { + let temp = tempfile::tempdir().unwrap(); + let source = temp.path().join("source"); + let destination = temp.path().join("destination"); + fs::create_dir_all(&source).unwrap(); + fs::write(source.join("target.txt"), b"target").unwrap(); + std::os::unix::fs::symlink("target.txt", source.join("link.txt")).unwrap(); + + copy_dir_recursive_with_progress(&source, &destination, &mut |_, _| {}).unwrap(); + + assert_eq!( + fs::read_link(destination.join("link.txt")).unwrap(), + PathBuf::from("target.txt") + ); + } + #[test] fn conflicting_visible_and_hidden_directories_are_not_merged() { let temp = tempfile::tempdir().unwrap(); @@ -720,18 +837,21 @@ mod tests { let temp = tempfile::tempdir().unwrap(); let source = temp.path().join("legacy"); let destination = temp.path().join("current"); - fs::create_dir_all(&source).unwrap(); - fs::create_dir_all(&destination).unwrap(); - fs::write(source.join("options.txt"), b"legacy").unwrap(); - fs::write(destination.join("options.txt"), b"current").unwrap(); + fs::create_dir_all(source.join("nested")).unwrap(); + fs::create_dir_all(destination.join("nested")).unwrap(); + fs::write(source.join("nested/options.txt"), b"legacy").unwrap(); + fs::write(destination.join("nested/options.txt"), b"current").unwrap(); let error = move_or_merge(&source, &destination).unwrap_err(); assert!(matches!(error, MigrationError::MergeConflict { .. })); assert_eq!( - fs::read(destination.join("options.txt")).unwrap(), + fs::read(destination.join("nested/options.txt")).unwrap(), b"current" ); - assert_eq!(fs::read(source.join("options.txt")).unwrap(), b"legacy"); + assert_eq!( + fs::read(source.join("nested/options.txt")).unwrap(), + b"legacy" + ); } } diff --git a/src/tui/widgets/content/discovery/tests.rs b/src/tui/widgets/content/discovery/tests.rs index af5f4d3..272a160 100644 --- a/src/tui/widgets/content/discovery/tests.rs +++ b/src/tui/widgets/content/discovery/tests.rs @@ -1,4 +1,5 @@ use super::*; +use crate::tui::tests::UI_TEST_LOCK; use chrono::Utc; use crossterm::event::KeyModifiers; @@ -282,6 +283,9 @@ fn discovery_delete_only_clears_the_matching_installed_badge() { #[test] fn successful_install_marks_the_project_and_closes_the_popup() { + let _guard = UI_TEST_LOCK + .lock() + .unwrap_or_else(|error| error.into_inner()); let project = DiscoveryProject { id: "project".to_owned(), slug: "project".to_owned(), diff --git a/src/tui/widgets/content/list/tests.rs b/src/tui/widgets/content/list/tests.rs index 1fe0c48..2ba019a 100644 --- a/src/tui/widgets/content/list/tests.rs +++ b/src/tui/widgets/content/list/tests.rs @@ -351,13 +351,17 @@ async fn streamed_entries_wait_for_their_rendered_icon() { let picker = ratatui_image::picker::Picker::halfblocks(); state.request_image_loads(&picker); - for _ in 0..20 { - tokio::time::sleep(std::time::Duration::from_millis(5)).await; - state.drain_image_loads(&picker); - if !state.filtered_indices().is_empty() { - break; + tokio::time::timeout(std::time::Duration::from_secs(5), async { + loop { + state.drain_image_loads(&picker); + if !state.filtered_indices().is_empty() { + break; + } + tokio::task::yield_now().await; } - } + }) + .await + .expect("icon render completed"); assert_eq!(state.filtered_indices(), vec![0]); } diff --git a/tests/log_files.rs b/tests/log_files.rs index 6e8b8f8..191f2e0 100644 --- a/tests/log_files.rs +++ b/tests/log_files.rs @@ -61,7 +61,8 @@ fn read_log_file_returns_lines() { #[test] fn read_log_file_missing_returns_empty() { - let lines = read_log_file(Path::new("/nonexistent/test.log")); + let tmp = tempfile::tempdir().unwrap(); + let lines = read_log_file(&tmp.path().join("missing.log")); assert!(lines.is_empty()); }