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/<artifact.path>.
  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.
This commit is contained in:
objz committed 2026-05-27 18:03:57 +02:00
1 parent d612f83446
commit 0d1b7984fc
8 files changed
+431 -1

No files matched your search

+167 -1
View File
@@ -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 /<base>/ab/<hash>.
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;