test: harden migration and UI coverage

This commit is contained in:
objz committed 2026-07-27 00:37:48 +02:00
1 parent 9a390fd7a4
commit e21f68fd7c
6 files changed
+146 -15

No files matched your search

+2 -1
View File
@@ -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}");
}
+2 -1
View File
@@ -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]
+126 -6
View File
@@ -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"
);
}
}
@@ -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(),
+10 -6
View File
@@ -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]);
}
+2 -1
View File
@@ -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());
}