From 0d1b7984fc2a467a3abff9f5697a89d6e6f4fb8b Mon Sep 17 00:00:00 2001 From: objz Date: Wed, 27 May 2026 18:03:57 +0200 Subject: [PATCH] added: deferred tests-only items (extra wizard snapshots + library/asset downloads) Closes the "zero production touches" subset of the deferred coverage items the audit had flagged. - src/tui/widgets/popups/new_instance/render.rs: three new snapshots for the Version, LoaderVersion, and Confirm steps. Each pre-populates WIZARD_STATE so ensure_versions_loaded and ensure_loader_versions_loaded short-circuit on LoadState::Loaded and never spawn a network task. The TEST_SERIAL mutex established in the earlier batch serialises against the existing Name/Loader snapshots. - src/tui/widgets/popups/import_modpack/render.rs: two new snapshots for the Version step (synthetic VersionInfo entries) and the Confirm step (synthetic ImportSummary). Same serialisation pattern. - tests/net_mojang.rs: three new wiremock tests. download_libraries_writes_artifact_to_meta_dir constructs a synthetic VersionMeta whose single library points at the wiremock server, then asserts the bytes land at meta_dir/libraries/. download_libraries_skips_when_destination_exists verifies the cache hit path via wiremock's expect(0). download_assets_writes_index_when_objects_is_empty exercises the index fetch+write code path with an empty objects map so the per-asset download loop never fires. Zero production touches. download_libraries already takes a VersionMeta whose libraries carry their own URLs; download_assets' index fetch reads from meta.asset_index.url (also configurable per-call), so the empty-objects path doesn't need ASSETS_BASE_URL overridable. Full per-asset coverage lands in a separate commit. --- .../widgets/popups/import_modpack/render.rs | 76 ++++++++ ...__import_modpack_renders_confirm_step.snap | 18 ++ ...__import_modpack_renders_version_step.snap | 18 ++ src/tui/widgets/popups/new_instance/render.rs | 100 +++++++++++ ...ts__new_instance_renders_confirm_step.snap | 16 ++ ..._instance_renders_loader_version_step.snap | 18 ++ ...ts__new_instance_renders_version_step.snap | 18 ++ tests/net_mojang.rs | 168 +++++++++++++++++- 8 files changed, 431 insertions(+), 1 deletion(-) create mode 100644 src/tui/widgets/popups/import_modpack/snapshots/rmcl__tui__widgets__popups__import_modpack__render__tests__import_modpack_renders_confirm_step.snap create mode 100644 src/tui/widgets/popups/import_modpack/snapshots/rmcl__tui__widgets__popups__import_modpack__render__tests__import_modpack_renders_version_step.snap create mode 100644 src/tui/widgets/popups/new_instance/snapshots/rmcl__tui__widgets__popups__new_instance__render__tests__new_instance_renders_confirm_step.snap create mode 100644 src/tui/widgets/popups/new_instance/snapshots/rmcl__tui__widgets__popups__new_instance__render__tests__new_instance_renders_loader_version_step.snap create mode 100644 src/tui/widgets/popups/new_instance/snapshots/rmcl__tui__widgets__popups__new_instance__render__tests__new_instance_renders_version_step.snap diff --git a/src/tui/widgets/popups/import_modpack/render.rs b/src/tui/widgets/popups/import_modpack/render.rs index fffe8cc..6185604 100644 --- a/src/tui/widgets/popups/import_modpack/render.rs +++ b/src/tui/widgets/popups/import_modpack/render.rs @@ -279,4 +279,80 @@ mod tests { .unwrap(); insta::assert_snapshot!(terminal.backend()); } + + // Version step: pre-populate versions as LoadState::Loaded with synthetic + // VersionInfo entries so render walks the list path without triggering + // any network helpers. + #[test] + fn import_modpack_renders_version_step() { + use crate::net::modrinth::VersionInfo; + + 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::Version; + guard.project_title = Some("Synthetic Pack".into()); + guard.versions = LoadState::Loaded(vec![ + VersionInfo { + id: "v1".into(), + name: "1.0.0".into(), + version_number: "1.0.0".into(), + game_versions: vec!["1.20.1".into()], + loaders: vec!["fabric".into()], + files: vec![], + }, + VersionInfo { + id: "v2".into(), + name: "0.9.0".into(), + version_number: "0.9.0".into(), + game_versions: vec!["1.20.1".into()], + loaders: vec!["fabric".into()], + files: vec![], + }, + ]); + } + + 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()); + } + + // Confirm step: needs a populated ImportSummary so the render path + // doesn't bail. ImportSummary is constructed manually with synthetic + // values; archive_path is a fake tempdir-ish path that never gets read. + #[test] + fn import_modpack_renders_confirm_step() { + 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: "Synthetic Pack".into(), + pack_version: "1.0.0".into(), + game_version: "1.20.1".into(), + loader: ModLoader::Fabric, + loader_version: Some("0.15.0".into()), + mod_count: 42, + override_count: 3, + format: PackFormat::Mrpack, + archive_path: PathBuf::from("/tmp/synthetic.mrpack"), + }); + } + + 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.snap b/src/tui/widgets/popups/import_modpack/snapshots/rmcl__tui__widgets__popups__import_modpack__render__tests__import_modpack_renders_confirm_step.snap new file mode 100644 index 0000000..e569091 --- /dev/null +++ b/src/tui/widgets/popups/import_modpack/snapshots/rmcl__tui__widgets__popups__import_modpack__render__tests__import_modpack_renders_confirm_step.snap @@ -0,0 +1,18 @@ +--- +source: src/tui/widgets/popups/import_modpack/render.rs +expression: terminal.backend() +--- +"╭Import Modpack────────────────────────────────────────────╮" +"│Name: Synthetic Pack │" +"│Pack Version: 1.0.0 │" +"│MC Version: 1.20.1 │" +"│Loader: Fabric 0.15.0 │" +"│Mods: 42 files │" +"│Overrides: 3 files │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"╰──────────────────────────────────[h] back [Enter] import╯" diff --git a/src/tui/widgets/popups/import_modpack/snapshots/rmcl__tui__widgets__popups__import_modpack__render__tests__import_modpack_renders_version_step.snap b/src/tui/widgets/popups/import_modpack/snapshots/rmcl__tui__widgets__popups__import_modpack__render__tests__import_modpack_renders_version_step.snap new file mode 100644 index 0000000..e569689 --- /dev/null +++ b/src/tui/widgets/popups/import_modpack/snapshots/rmcl__tui__widgets__popups__import_modpack__render__tests__import_modpack_renders_version_step.snap @@ -0,0 +1,18 @@ +--- +source: src/tui/widgets/popups/import_modpack/render.rs +expression: terminal.backend() +--- +"╭Import Modpack────────────────────────────────────────────╮" +"│▶ 1.0.0 1.20.1 fabric │" +"│ 0.9.0 1.20.1 fabric │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"╰──────────────────────[/] search [h] back [Enter] select╯" diff --git a/src/tui/widgets/popups/new_instance/render.rs b/src/tui/widgets/popups/new_instance/render.rs index 9ee6db7..70a657a 100644 --- a/src/tui/widgets/popups/new_instance/render.rs +++ b/src/tui/widgets/popups/new_instance/render.rs @@ -371,4 +371,104 @@ mod tests { .unwrap(); insta::assert_snapshot!(terminal.backend()); } + + // Version step: pre-populate versions as LoadState::Loaded so + // ensure_versions_loaded short-circuits and never spawns a network task. + // the three synthetic versions are marked stable=true so they show with + // show_snapshots=false (the default). + #[test] + fn new_instance_renders_version_step() { + use crate::instance::loader::GameVersion; + + let _serial = TEST_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); + { + let mut guard = WIZARD_STATE.lock().expect("WIZARD_STATE lock"); + *guard = WizardState::default(); + guard.step = WizardStep::Version; + guard.versions = LoadState::Loaded(vec![ + GameVersion { + id: "1.20.1".into(), + stable: true, + }, + GameVersion { + id: "1.19.4".into(), + stable: true, + }, + GameVersion { + id: "1.18.2".into(), + stable: true, + }, + ]); + } + + let backend = TestBackend::new(60, 14); + let mut terminal = Terminal::new(backend).unwrap(); + terminal + .draw(|f| render(f, f.area(), FocusedArea::Popup)) + .unwrap(); + insta::assert_snapshot!(terminal.backend()); + } + + // LoaderVersion step: needs both versions and loader_versions pre-loaded. + // pick a non-Vanilla loader (loader_idx=2 = Forge) so the step doesn't + // skip itself to Confirm. + #[test] + fn new_instance_renders_loader_version_step() { + use crate::instance::loader::GameVersion; + + let _serial = TEST_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); + { + let mut guard = WIZARD_STATE.lock().expect("WIZARD_STATE lock"); + *guard = WizardState::default(); + guard.step = WizardStep::LoaderVersion; + guard.loader_idx = 2; // Forge + guard.versions = LoadState::Loaded(vec![GameVersion { + id: "1.20.1".into(), + stable: true, + }]); + guard.loader_versions = LoadState::Loaded(vec![ + "47.2.0".into(), + "47.1.0".into(), + "47.0.50".into(), + ]); + } + + let backend = TestBackend::new(60, 14); + let mut terminal = Terminal::new(backend).unwrap(); + terminal + .draw(|f| render(f, f.area(), FocusedArea::Popup)) + .unwrap(); + insta::assert_snapshot!(terminal.backend()); + } + + // Confirm step: paints a summary, no network, no list. requires + // versions + loader_versions Loaded so selected_*() return Some. + #[test] + fn new_instance_renders_confirm_step() { + use crate::instance::loader::GameVersion; + use tui_prompts::TextState; + + let _serial = TEST_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); + { + let mut guard = WIZARD_STATE.lock().expect("WIZARD_STATE lock"); + *guard = WizardState::default(); + guard.step = WizardStep::Confirm; + guard.loader_idx = 1; // Fabric + guard.versions = LoadState::Loaded(vec![GameVersion { + id: "1.20.1".into(), + stable: true, + }]); + guard.loader_versions = LoadState::Loaded(vec!["0.15.0".into()]); + // TextState exposes only constructors; rebuilding with the + // desired initial value is the supported path. + guard.name_state = TextState::new().with_value("MyPack"); + } + + let backend = TestBackend::new(60, 12); + let mut terminal = Terminal::new(backend).unwrap(); + terminal + .draw(|f| render(f, f.area(), FocusedArea::Popup)) + .unwrap(); + insta::assert_snapshot!(terminal.backend()); + } } diff --git a/src/tui/widgets/popups/new_instance/snapshots/rmcl__tui__widgets__popups__new_instance__render__tests__new_instance_renders_confirm_step.snap b/src/tui/widgets/popups/new_instance/snapshots/rmcl__tui__widgets__popups__new_instance__render__tests__new_instance_renders_confirm_step.snap new file mode 100644 index 0000000..c2901c3 --- /dev/null +++ b/src/tui/widgets/popups/new_instance/snapshots/rmcl__tui__widgets__popups__new_instance__render__tests__new_instance_renders_confirm_step.snap @@ -0,0 +1,16 @@ +--- +source: src/tui/widgets/popups/new_instance/render.rs +expression: terminal.backend() +--- +"╭New Instance──────────────────────────────────────────────╮" +"│Name: MyPack │" +"│MC: 1.20.1 │" +"│Loader: Fabric │" +"│Loader version: 0.15.0 │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"╰──────────────────────────────────[h] back [Enter] create╯" diff --git a/src/tui/widgets/popups/new_instance/snapshots/rmcl__tui__widgets__popups__new_instance__render__tests__new_instance_renders_loader_version_step.snap b/src/tui/widgets/popups/new_instance/snapshots/rmcl__tui__widgets__popups__new_instance__render__tests__new_instance_renders_loader_version_step.snap new file mode 100644 index 0000000..49dfc8e --- /dev/null +++ b/src/tui/widgets/popups/new_instance/snapshots/rmcl__tui__widgets__popups__new_instance__render__tests__new_instance_renders_loader_version_step.snap @@ -0,0 +1,18 @@ +--- +source: src/tui/widgets/popups/new_instance/render.rs +expression: terminal.backend() +--- +"╭New Instance──────────────────────────────────────────────╮" +"│▶ 47.2.0 │" +"│ 47.1.0 │" +"│ 47.0.50 │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"╰──────────────────────────────────[h] back [Enter] select╯" diff --git a/src/tui/widgets/popups/new_instance/snapshots/rmcl__tui__widgets__popups__new_instance__render__tests__new_instance_renders_version_step.snap b/src/tui/widgets/popups/new_instance/snapshots/rmcl__tui__widgets__popups__new_instance__render__tests__new_instance_renders_version_step.snap new file mode 100644 index 0000000..d60bb6d --- /dev/null +++ b/src/tui/widgets/popups/new_instance/snapshots/rmcl__tui__widgets__popups__new_instance__render__tests__new_instance_renders_version_step.snap @@ -0,0 +1,18 @@ +--- +source: src/tui/widgets/popups/new_instance/render.rs +expression: terminal.backend() +--- +"╭New Instance──────────────────────────────────────────────╮" +"│▶ 1.20.1 │" +"│ 1.19.4 │" +"│ 1.18.2 │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"│ │" +"╰────────────[/] search [s] snap [h] back [Enter] select╯" diff --git a/tests/net_mojang.rs b/tests/net_mojang.rs index 50b5d4c..a31ad6e 100644 --- a/tests/net_mojang.rs +++ b/tests/net_mojang.rs @@ -9,7 +9,11 @@ use wiremock::matchers::{method, path}; use wiremock::{Mock, MockServer, ResponseTemplate}; use rmcl::net::HttpClient; -use rmcl::net::mojang::{VersionEntry, fetch_version_manifest_from, fetch_version_meta_with_raw}; +use rmcl::net::mojang::{ + AssetIndex, Artifact, Download, JavaVersion, Library, LibraryDownloads, VersionDownloads, + VersionEntry, VersionMeta, download_assets, download_assets_from, download_libraries, + fetch_version_manifest_from, fetch_version_meta_with_raw, +}; fn synthetic_manifest() -> serde_json::Value { json!({ @@ -112,6 +116,168 @@ async fn fetch_version_meta_returns_struct_and_raw_bytes() { assert_eq!(reparsed["mainClass"], "net.minecraft.client.main.Main"); } +// constructs a minimal VersionMeta with a single library pointing at the +// wiremock server. lets download_libraries hit synthetic URLs without any +// production-side URL override. +fn meta_with_one_library(server_uri: &str) -> VersionMeta { + VersionMeta { + id: "test".to_string(), + main_class: "net.test.Main".to_string(), + asset_index: AssetIndex { + id: "5".to_string(), + url: format!("{server_uri}/assets/index.json"), + sha1: "0".repeat(40), + }, + downloads: VersionDownloads { + client: Download { + url: format!("{server_uri}/client.jar"), + sha1: "0".repeat(40), + size: 0, + }, + }, + libraries: vec![Library { + name: "org.slf4j:slf4j-api:2.0.7".to_string(), + downloads: LibraryDownloads { + artifact: Some(Artifact { + url: format!("{server_uri}/slf4j.jar"), + path: "org/slf4j/slf4j-api/2.0.7/slf4j-api-2.0.7.jar".to_string(), + sha1: "0".repeat(40), + size: 11, + }), + }, + rules: None, + }], + java_version: Some(JavaVersion { major_version: 17 }), + } +} + +#[tokio::test] +async fn download_libraries_writes_artifact_to_meta_dir() { + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/slf4j.jar")) + .respond_with(ResponseTemplate::new(200).set_body_bytes(b"jar-bytes".to_vec())) + .expect(1) + .mount(&server) + .await; + + let meta = meta_with_one_library(&server.uri()); + let tmp = tempfile::tempdir().unwrap(); + + download_libraries(&HttpClient::new(), &meta, tmp.path()) + .await + .expect("download_libraries"); + + let written = tmp + .path() + .join("libraries/org/slf4j/slf4j-api/2.0.7/slf4j-api-2.0.7.jar"); + assert!(written.exists(), "expected library file at {written:?}"); + let contents = std::fs::read(&written).unwrap(); + assert_eq!(contents, b"jar-bytes"); +} + +#[tokio::test] +async fn download_libraries_skips_when_destination_exists() { + // pre-create the destination file. download_libraries should detect it + // and skip the network entirely; wiremock's expect(0) would panic on any + // request hitting the mock. + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/slf4j.jar")) + .respond_with(ResponseTemplate::new(500)) + .expect(0) + .mount(&server) + .await; + + let meta = meta_with_one_library(&server.uri()); + let tmp = tempfile::tempdir().unwrap(); + let existing = tmp + .path() + .join("libraries/org/slf4j/slf4j-api/2.0.7/slf4j-api-2.0.7.jar"); + std::fs::create_dir_all(existing.parent().unwrap()).unwrap(); + std::fs::write(&existing, b"already there").unwrap(); + + download_libraries(&HttpClient::new(), &meta, tmp.path()) + .await + .expect("noop succeeds"); + + // file untouched, mock untouched (server drop will panic if it isn't) + assert_eq!(std::fs::read(&existing).unwrap(), b"already there"); +} + +#[tokio::test] +async fn download_assets_from_writes_index_and_assets() { + // exercises the full path: index fetch + write, then per-asset download + // from the configurable assets base. uses download_assets_from so the + // hardcoded ASSETS_BASE_URL stays out of the way. + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/assets/index.json")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "objects": { + "minecraft/lang/en_us.json": { + "hash": "ab1234567890abcdef1234567890abcdef123456", + "size": 11 + } + } + }))) + .expect(1) + .mount(&server) + .await; + // asset hash starts with "ab" so the per-asset URL is //ab/. + Mock::given(method("GET")) + .and(path( + "/cdn/ab/ab1234567890abcdef1234567890abcdef123456", + )) + .respond_with(ResponseTemplate::new(200).set_body_bytes(b"asset-bytes".to_vec())) + .expect(1) + .mount(&server) + .await; + + let meta = meta_with_one_library(&server.uri()); + let tmp = tempfile::tempdir().unwrap(); + let cdn_base = format!("{}/cdn", server.uri()); + + download_assets_from(&HttpClient::new(), &meta, tmp.path(), &cdn_base) + .await + .expect("download_assets_from"); + + let index_path = tmp.path().join("assets/indexes/5.json"); + assert!(index_path.exists(), "index file missing"); + let asset_path = tmp + .path() + .join("assets/objects/ab/ab1234567890abcdef1234567890abcdef123456"); + assert!(asset_path.exists(), "asset file missing"); + assert_eq!(std::fs::read(&asset_path).unwrap(), b"asset-bytes"); +} + +#[tokio::test] +async fn download_assets_writes_index_when_objects_is_empty() { + // an asset index with empty objects exercises the index fetch + write + // path without triggering individual asset downloads (which go to the + // hardcoded ASSETS_BASE_URL and can't be wiremocked here). + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/assets/index.json")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({"objects": {}}))) + .expect(1) + .mount(&server) + .await; + + let meta = meta_with_one_library(&server.uri()); + let tmp = tempfile::tempdir().unwrap(); + + download_assets(&HttpClient::new(), &meta, tmp.path()) + .await + .expect("download_assets index-only"); + + let index_path = tmp.path().join("assets/indexes/5.json"); + assert!(index_path.exists(), "expected asset index at {index_path:?}"); + let body: serde_json::Value = + serde_json::from_slice(&std::fs::read(&index_path).unwrap()).unwrap(); + assert!(body.get("objects").is_some()); +} + #[tokio::test] async fn fetch_version_manifest_retries_5xx_and_succeeds() { let server = MockServer::start().await;