From 5f43128a8035e34add6dadb90d55273fcab2a383 Mon Sep 17 00:00:00 2001 From: objz Date: Wed, 27 May 2026 18:19:26 +0200 Subject: [PATCH] added: tests for patches helpers, forge installer detection, url_encode, profile fetchers Closes the seven remaining tractable coverage gaps the final review flagged. All tests-only or tiny *_from variants matching the existing pattern. 18 new tests. - src/instance/launch/patches.rs: seven inline tests for the lwjgl3ify patch helpers that were entirely untested before. strip_replaced_libs has happy + no-op cases. parse_add_opens covers the basic Add-Opens extraction, the MANIFEST.MF continuation-line case (the tricky one: jar manifests wrap lines with a trailing space + leading-space marker that gets stripped on join), missing manifest, and missing file. add_lwjgl3 verifies that only jars that actually exist in lib_dir end up on the classpath. - src/net/forge.rs: three inline tests for has_legacy_install_profile. Builds synthetic forge installer zips in a tempdir with and without the versionInfo key in install_profile.json; covers the missing-jar branch with a path that doesn't exist. - src/net/modrinth.rs: five-case rstest for url_encode covering the unreserved set, space, RFC 3986 reserved characters, multi-byte UTF-8 (the encoder operates per-byte so emoji exercises the multi-byte path), and empty input. - src/net/fabric.rs / src/net/quilt.rs: added fetch_*_profile_from and fetch_*_profile_with_raw_from variants mirroring the existing pattern. Existing fetch_*_profile and *_with_raw delegate to them with the upstream constants. - tests/net_loaders.rs: two new wiremock tests (fabric_fetch_profile parses libraries, quilt_fetch_profile parses libraries) using the new _from variants. - src/tui/widgets/popups/import_modpack/render.rs: extra snapshot for the Confirm step when loader_version is None (vanilla pack or old mmc pack). The previous Confirm snapshot covered Some(_) only. Production touches stay minimal: the four new pub fn delegators in fabric.rs and quilt.rs, all behaviour-preserving. --- src/instance/launch/patches.rs | 134 ++++++++++++++++++ src/net/fabric.rs | 23 ++- src/net/forge.rs | 53 +++++++ src/net/modrinth.rs | 14 ++ src/net/quilt.rs | 22 ++- .../widgets/popups/import_modpack/render.rs | 35 +++++ ...s_confirm_step_without_loader_version.snap | 18 +++ tests/net_loaders.rs | 58 +++++++- 8 files changed, 351 insertions(+), 6 deletions(-) create mode 100644 src/tui/widgets/popups/import_modpack/snapshots/rmcl__tui__widgets__popups__import_modpack__render__tests__import_modpack_renders_confirm_step_without_loader_version.snap diff --git a/src/instance/launch/patches.rs b/src/instance/launch/patches.rs index 35a2128..882b44f 100644 --- a/src/instance/launch/patches.rs +++ b/src/instance/launch/patches.rs @@ -324,3 +324,137 @@ async fn replace_log4j_fixed(lib_dir: &Path, classpath: &mut [PathBuf]) { } } } + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write; + + // builds an in-memory zip with the given entries, writes it to tmp, and + // returns the path. shared by parse_add_opens tests. + fn make_zip(tmp: &Path, name: &str, entries: &[(&str, &[u8])]) -> PathBuf { + let path = tmp.join(name); + let file = std::fs::File::create(&path).unwrap(); + let mut zip = zip::ZipWriter::new(file); + let opts: zip::write::SimpleFileOptions = Default::default(); + for (filename, bytes) in entries { + zip.start_file(*filename, opts).unwrap(); + zip.write_all(bytes).unwrap(); + } + zip.finish().unwrap(); + path + } + + #[test] + fn strip_replaced_libs_removes_dominated_prefixes() { + let mut classpath = vec![ + PathBuf::from("/libs/launchwrapper-1.12.jar"), + PathBuf::from("/libs/asm-all-5.0.3.jar"), + PathBuf::from("/libs/lwjgl-2.9.4.jar"), + PathBuf::from("/libs/lwjgl_util-2.9.4.jar"), + PathBuf::from("/libs/commons-compress-1.4.1.jar"), + PathBuf::from("/libs/commons-io-2.4.jar"), + PathBuf::from("/libs/guava-15.0.jar"), + // these stay + PathBuf::from("/libs/log4j-core-2.0.jar"), + PathBuf::from("/libs/guava-21.0.jar"), + ]; + strip_replaced_libs(&mut classpath); + assert_eq!( + classpath, + vec![ + PathBuf::from("/libs/log4j-core-2.0.jar"), + PathBuf::from("/libs/guava-21.0.jar"), + ] + ); + } + + #[test] + fn strip_replaced_libs_keeps_unrelated_entries() { + let mut classpath = vec![ + PathBuf::from("/libs/log4j-core-2.0.jar"), + PathBuf::from("/libs/mixin-0.8.5.jar"), + ]; + let original = classpath.clone(); + strip_replaced_libs(&mut classpath); + assert_eq!(classpath, original); + } + + #[test] + fn parse_add_opens_extracts_module_args() { + let tmp = tempfile::tempdir().unwrap(); + let manifest = b"Manifest-Version: 1.0\nAdd-Opens: java.base/java.lang java.base/java.util\n"; + let zip_path = make_zip(tmp.path(), "patches.zip", &[("META-INF/MANIFEST.MF", manifest)]); + let args = parse_add_opens(&zip_path).expect("parsed"); + assert_eq!( + args, + vec![ + "--add-opens", + "java.base/java.lang=ALL-UNNAMED", + "--add-opens", + "java.base/java.util=ALL-UNNAMED", + ] + ); + } + + #[test] + fn parse_add_opens_handles_continuation_lines() { + // jar manifests wrap long lines: the line ends with a trailing space, + // then the next physical line starts with a leading space marker. + // when joined per the MANIFEST.MF spec, the trailing space remains + // and the leading space marker is consumed, so the two values stay + // separated by exactly one space when split_whitespace runs. + let tmp = tempfile::tempdir().unwrap(); + let manifest = + b"Manifest-Version: 1.0\nAdd-Opens: java.base/java.lang \n java.base/sun.security.util\n"; + let zip_path = make_zip( + tmp.path(), + "patches-continuation.zip", + &[("META-INF/MANIFEST.MF", manifest)], + ); + let args = parse_add_opens(&zip_path).expect("parsed"); + assert_eq!( + args, + vec![ + "--add-opens", + "java.base/java.lang=ALL-UNNAMED", + "--add-opens", + "java.base/sun.security.util=ALL-UNNAMED", + ] + ); + } + + #[test] + fn parse_add_opens_returns_none_when_manifest_missing() { + let tmp = tempfile::tempdir().unwrap(); + let zip_path = make_zip(tmp.path(), "no-manifest.zip", &[("other.txt", b"x")]); + assert!(parse_add_opens(&zip_path).is_none()); + } + + #[test] + fn parse_add_opens_returns_none_for_missing_file() { + assert!(parse_add_opens(Path::new("/nonexistent/x.zip")).is_none()); + } + + #[test] + fn add_lwjgl3_inserts_only_jars_that_exist() { + let tmp = tempfile::tempdir().unwrap(); + let lib_dir = tmp.path(); + + // pre-create only the lwjgl core jar; everything else absent so the + // function should skip them silently. proves we don't insert paths + // for jars that aren't there. + let core_jar = lib_dir.join("org/lwjgl/lwjgl/3.3.3/lwjgl-3.3.3.jar"); + std::fs::create_dir_all(core_jar.parent().unwrap()).unwrap(); + std::fs::write(&core_jar, b"jar").unwrap(); + + let mut classpath = vec![PathBuf::from("/leading/forge-patches.jar")]; + add_lwjgl3(lib_dir, &mut classpath); + + // forge-patches stays at index 0; lwjgl core is inserted at index 1. + // none of the other modules existed so nothing else was added. + assert_eq!(classpath[0], PathBuf::from("/leading/forge-patches.jar")); + assert_eq!(classpath[1], core_jar); + assert_eq!(classpath.len(), 2); + } +} diff --git a/src/net/fabric.rs b/src/net/fabric.rs index 8a9820b..c5e4e80 100644 --- a/src/net/fabric.rs +++ b/src/net/fabric.rs @@ -87,10 +87,19 @@ pub async fn fetch_fabric_profile( client: &HttpClient, game_version: &str, loader_version: &str, +) -> Result { + fetch_fabric_profile_from(client, FABRIC_META_BASE, game_version, loader_version).await +} + +pub async fn fetch_fabric_profile_from( + client: &HttpClient, + meta_base: &str, + game_version: &str, + loader_version: &str, ) -> Result { let url = format!( "{}/versions/loader/{}/{}/profile/json", - FABRIC_META_BASE, game_version, loader_version + meta_base, game_version, loader_version ); client.get_json(&url).await } @@ -103,10 +112,20 @@ pub async fn fetch_fabric_profile_with_raw( client: &HttpClient, game_version: &str, loader_version: &str, +) -> Result<(FabricProfile, Vec), NetError> { + fetch_fabric_profile_with_raw_from(client, FABRIC_META_BASE, game_version, loader_version) + .await +} + +pub async fn fetch_fabric_profile_with_raw_from( + client: &HttpClient, + meta_base: &str, + game_version: &str, + loader_version: &str, ) -> Result<(FabricProfile, Vec), NetError> { let url = format!( "{}/versions/loader/{}/{}/profile/json", - FABRIC_META_BASE, game_version, loader_version + meta_base, game_version, loader_version ); client.get_json_with_raw(&url, "Fabric profile").await } diff --git a/src/net/forge.rs b/src/net/forge.rs index 68e76ae..96dee85 100644 --- a/src/net/forge.rs +++ b/src/net/forge.rs @@ -333,4 +333,57 @@ mod tests { Err(e) => panic!("fetch_forge_game_versions failed: {}", e), } } + + // builds an in-memory zip in a tempdir with the given json as + // install_profile.json. lets the legacy-install-profile detector be + // tested without an actual forge installer. + fn make_installer_zip(tmp: &std::path::Path, json: &serde_json::Value) -> std::path::PathBuf { + use std::io::Write; + let path = tmp.join("installer.jar"); + let file = std::fs::File::create(&path).unwrap(); + let mut zip = zip::ZipWriter::new(file); + let opts: zip::write::SimpleFileOptions = Default::default(); + zip.start_file("install_profile.json", opts).unwrap(); + zip.write_all(serde_json::to_string(json).unwrap().as_bytes()) + .unwrap(); + zip.finish().unwrap(); + path + } + + #[test] + fn has_legacy_install_profile_true_when_version_info_present() { + let tmp = tempfile::tempdir().unwrap(); + let jar = make_installer_zip( + tmp.path(), + &serde_json::json!({ + "install": {}, + "versionInfo": { + "id": "1.7.10-Forge10.13.4.1614-1.7.10", + "mainClass": "net.minecraft.launchwrapper.Launch" + } + }), + ); + assert!(has_legacy_install_profile(&jar)); + } + + #[test] + fn has_legacy_install_profile_false_when_version_info_absent() { + let tmp = tempfile::tempdir().unwrap(); + let jar = make_installer_zip( + tmp.path(), + &serde_json::json!({ + "spec": 1, + "minecraft": "1.20.1", + "data": {} + }), + ); + assert!(!has_legacy_install_profile(&jar)); + } + + #[test] + fn has_legacy_install_profile_false_for_missing_jar() { + assert!(!has_legacy_install_profile(std::path::Path::new( + "/nonexistent/installer.jar" + ))); + } } diff --git a/src/net/modrinth.rs b/src/net/modrinth.rs index 353ca83..598ba16 100644 --- a/src/net/modrinth.rs +++ b/src/net/modrinth.rs @@ -157,6 +157,20 @@ pub fn parse_mrpack(path: &std::path::Path) -> Result { mod tests { use super::*; + // covers each branch of url_encode: unreserved bytes pass through; the + // reserved set + spaces + non-ascii bytes get percent-encoded. emoji + // exercises multi-byte UTF-8 since the encoder operates on bytes, not + // chars, so each byte of the codepoint encodes separately. + #[rstest::rstest] + #[case::ascii_unreserved("abcXYZ0-9_.~", "abcXYZ0-9_.~")] + #[case::space("hello world", "hello%20world")] + #[case::reserved("/?&=#", "%2F%3F%26%3D%23")] + #[case::utf8_emoji("\u{2603}", "%E2%98%83")] + #[case::empty("", "")] + fn url_encode_handles(#[case] input: &str, #[case] expected: &str) { + assert_eq!(url_encode(input), expected); + } + #[test] fn loader_from_fabric_deps() { let mut deps = HashMap::new(); diff --git a/src/net/quilt.rs b/src/net/quilt.rs index e6de3a4..4a94aef 100644 --- a/src/net/quilt.rs +++ b/src/net/quilt.rs @@ -83,10 +83,19 @@ pub async fn fetch_quilt_profile( client: &HttpClient, game_version: &str, loader_version: &str, +) -> Result { + fetch_quilt_profile_from(client, QUILT_META_BASE, game_version, loader_version).await +} + +pub async fn fetch_quilt_profile_from( + client: &HttpClient, + meta_base: &str, + game_version: &str, + loader_version: &str, ) -> Result { let url = format!( "{}/versions/loader/{}/{}/profile/json", - QUILT_META_BASE, game_version, loader_version + meta_base, game_version, loader_version ); client.get_json(&url).await } @@ -99,10 +108,19 @@ pub async fn fetch_quilt_profile_with_raw( client: &HttpClient, game_version: &str, loader_version: &str, +) -> Result<(QuiltProfile, Vec), NetError> { + fetch_quilt_profile_with_raw_from(client, QUILT_META_BASE, game_version, loader_version).await +} + +pub async fn fetch_quilt_profile_with_raw_from( + client: &HttpClient, + meta_base: &str, + game_version: &str, + loader_version: &str, ) -> Result<(QuiltProfile, Vec), NetError> { let url = format!( "{}/versions/loader/{}/{}/profile/json", - QUILT_META_BASE, game_version, loader_version + meta_base, game_version, loader_version ); client.get_json_with_raw(&url, "Quilt profile").await } diff --git a/src/tui/widgets/popups/import_modpack/render.rs b/src/tui/widgets/popups/import_modpack/render.rs index 6185604..5508af5 100644 --- a/src/tui/widgets/popups/import_modpack/render.rs +++ b/src/tui/widgets/popups/import_modpack/render.rs @@ -355,4 +355,39 @@ mod tests { .unwrap(); insta::assert_snapshot!(terminal.backend()); } + + // Confirm step with loader_version=None: covers the branch where the + // pack didn't declare a loader version (rare upstream, but happens for + // older mmc packs). render_confirm_step has to handle the Option. + #[test] + fn import_modpack_renders_confirm_step_without_loader_version() { + use crate::instance::import::{ImportSummary, PackFormat}; + use crate::instance::models::ModLoader; + use std::path::PathBuf; + + let _serial = TEST_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); + { + let mut guard = IMPORT_STATE.lock().expect("IMPORT_STATE lock"); + *guard = ImportWizardState::default(); + guard.step = ImportStep::Confirm; + guard.summary = Some(ImportSummary { + name: "Vanilla Pack".into(), + pack_version: "2.0".into(), + game_version: "1.20.1".into(), + loader: ModLoader::Vanilla, + loader_version: None, + mod_count: 0, + override_count: 12, + format: PackFormat::Mmc, + archive_path: PathBuf::from("/tmp/vanilla.zip"), + }); + } + + let backend = TestBackend::new(60, 14); + let mut terminal = Terminal::new(backend).unwrap(); + terminal + .draw(|f| render(f, f.area(), FocusedArea::ImportPopup)) + .unwrap(); + insta::assert_snapshot!(terminal.backend()); + } } diff --git a/src/tui/widgets/popups/import_modpack/snapshots/rmcl__tui__widgets__popups__import_modpack__render__tests__import_modpack_renders_confirm_step_without_loader_version.snap b/src/tui/widgets/popups/import_modpack/snapshots/rmcl__tui__widgets__popups__import_modpack__render__tests__import_modpack_renders_confirm_step_without_loader_version.snap new file mode 100644 index 0000000..9a037c3 --- /dev/null +++ b/src/tui/widgets/popups/import_modpack/snapshots/rmcl__tui__widgets__popups__import_modpack__render__tests__import_modpack_renders_confirm_step_without_loader_version.snap @@ -0,0 +1,18 @@ +--- +source: src/tui/widgets/popups/import_modpack/render.rs +expression: terminal.backend() +--- +"╭Import Modpack────────────────────────────────────────────╮" +"│Name: Vanilla Pack │" +"│Pack Version: 2.0 │" +"│MC Version: 1.20.1 │" +"│Loader: Vanilla │" +"│Mods: 0 files │" +"│Overrides: 12 files │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"╰──────────────────────────────────[h] back [Enter] import╯" diff --git a/tests/net_loaders.rs b/tests/net_loaders.rs index d703777..8cbedda 100644 --- a/tests/net_loaders.rs +++ b/tests/net_loaders.rs @@ -8,10 +8,14 @@ use wiremock::matchers::{method, path}; use wiremock::{Mock, MockServer, ResponseTemplate}; use rmcl::net::HttpClient; -use rmcl::net::fabric::{fetch_fabric_game_versions_from, fetch_fabric_versions_from}; +use rmcl::net::fabric::{ + fetch_fabric_game_versions_from, fetch_fabric_profile_from, fetch_fabric_versions_from, +}; use rmcl::net::forge::{fetch_forge_game_versions_from, fetch_forge_versions_from}; use rmcl::net::neoforge::fetch_neoforge_versions_from; -use rmcl::net::quilt::{fetch_quilt_game_versions_from, fetch_quilt_versions_from}; +use rmcl::net::quilt::{ + fetch_quilt_game_versions_from, fetch_quilt_profile_from, fetch_quilt_versions_from, +}; // ---------- forge ---------- @@ -111,6 +115,32 @@ async fn fabric_fetch_versions_parses_loader_entries() { assert_eq!(versions[0].loader.version, "0.15.0"); } +#[tokio::test] +async fn fabric_fetch_profile_parses_libraries() { + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/versions/loader/1.20.1/0.15.0/profile/json")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "id": "fabric-loader-0.15.0-1.20.1", + "mainClass": "net.fabricmc.loader.impl.launch.knot.KnotClient", + "libraries": [ + { "name": "net.fabricmc:fabric-loader:0.15.0", "url": "https://maven.fabricmc.net/" } + ] + }))) + .mount(&server) + .await; + + let profile = + fetch_fabric_profile_from(&HttpClient::new(), &server.uri(), "1.20.1", "0.15.0") + .await + .expect("fabric profile"); + + assert_eq!(profile.id, "fabric-loader-0.15.0-1.20.1"); + assert_eq!(profile.main_class, "net.fabricmc.loader.impl.launch.knot.KnotClient"); + assert_eq!(profile.libraries.len(), 1); + assert_eq!(profile.libraries[0].url, "https://maven.fabricmc.net/"); +} + // ---------- quilt ---------- #[tokio::test] @@ -147,6 +177,30 @@ async fn quilt_fetch_versions_parses_loader_entries() { assert_eq!(versions[0].loader.version, "0.23.0"); } +#[tokio::test] +async fn quilt_fetch_profile_parses_libraries() { + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/versions/loader/1.20.1/0.23.0/profile/json")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "id": "quilt-loader-0.23.0-1.20.1", + "mainClass": "org.quiltmc.loader.impl.launch.knot.KnotClient", + "libraries": [ + { "name": "org.quiltmc:quilt-loader:0.23.0", "url": "https://maven.quiltmc.org/repository/release/" } + ] + }))) + .mount(&server) + .await; + + let profile = + fetch_quilt_profile_from(&HttpClient::new(), &server.uri(), "1.20.1", "0.23.0") + .await + .expect("quilt profile"); + + assert_eq!(profile.id, "quilt-loader-0.23.0-1.20.1"); + assert_eq!(profile.libraries[0].url, "https://maven.quiltmc.org/repository/release/"); +} + // ---------- neoforge ---------- #[tokio::test]