Audit identified six high-priority coverage gaps. This commit closes them with 30 net new passing tests. - tests/net_retry.rs (new): 5 wiremock-based tests for the retry envelope in net/mod.rs. Covers get_json + download_file across: 5xx retry-then-succeed, 4xx fail-fast (asserted via wiremock expect(1) so any retry attempt fails the test), and exhaust at MAX_RETRIES = 3 (asserted via expect(4)). The exhaust test takes ~3.5s of wall time because the production code sleeps 500 + 1000 + 2000 ms between attempts; this is unavoidable without making the delays configurable. - tests/launch_pipeline.rs: 3 new error-path tests against build_launch_invocation. merged_profile_missing_main_class_fails surfaces the LaunchError::Parse branch when meta.json deserialises cleanly but lacks mainClass. legacy_loader_profile_missing_loader_version_errors and legacy_loader_profile_missing_installer_json_errors exercise the two Parse branches inside migrate_legacy_loader_profile_if_needed. - instance/manager.rs: 6 tests for rename (happy path, noop on same name, empty target rejected, missing source, target collision) and touch_last_played (sets the field to a stamp within 5 seconds of now). The InstanceManager rename surface had no coverage before; mutating the validation order or the std::fs::rename target would not have failed any existing test. - instance/content/mods.rs: 4 new precedence-chain tests. The reader chain is fabric > quilt > forge-toml (which itself tries neoforge.mods.toml before mods.toml) > mcmod.info. Only the fabric-over-forge precedence had a test before. Adds quilt > forge, forge_toml > mcmod_info, neoforge.mods.toml > mods.toml, and a test that a mods.toml carrying only logoFile (no [[mods]] array) still resolves the icon. - tui/widgets/instances.rs: 12 new tests for format_last_played. One case for the None input (returns "Never played"), then an rstest covering each bucket boundary in the match (0/59 just-now, 60/3599 minutes, 3600/86399 hours, 86400/2591999 days, 2592000/31535999 months, >=31536000 over-a-year). A mutation that bumps any boundary by one second, or swaps a unit suffix, fails at least one case.
154 lines
4.4 KiB
Rust
154 lines
4.4 KiB
Rust
// integration tests for the retry envelope in src/net/mod.rs. wiremock
|
|
// stands in for live upstream APIs so we can assert that 5xx responses
|
|
// retry, 4xx responses fail fast, and the cap (MAX_RETRIES = 3, total
|
|
// 4 attempts) is honoured. these tests exercise public HttpClient methods,
|
|
// not the private retry helper directly.
|
|
//
|
|
// note: get_with_retry sleeps between attempts (500ms, 1000ms, 2000ms),
|
|
// so the gives-up-after-max-retries test takes about 3.5s of wall time.
|
|
// nothing to be done about that without making the delays configurable.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use serde::Deserialize;
|
|
use serde_json::json;
|
|
use wiremock::matchers::{method, path};
|
|
use wiremock::{Mock, MockServer, ResponseTemplate};
|
|
|
|
use rmcl::net::{HttpClient, download_file};
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
struct ApiResponse {
|
|
ok: bool,
|
|
}
|
|
|
|
// ---------- get_json (via get_with_retry) ----------
|
|
|
|
#[tokio::test]
|
|
async fn get_json_retries_5xx_then_succeeds() {
|
|
let server = MockServer::start().await;
|
|
|
|
// first request: 503. second and beyond: 200.
|
|
Mock::given(method("GET"))
|
|
.and(path("/api"))
|
|
.respond_with(ResponseTemplate::new(503))
|
|
.up_to_n_times(1)
|
|
.expect(1)
|
|
.mount(&server)
|
|
.await;
|
|
Mock::given(method("GET"))
|
|
.and(path("/api"))
|
|
.respond_with(ResponseTemplate::new(200).set_body_json(json!({"ok": true})))
|
|
.expect(1)
|
|
.mount(&server)
|
|
.await;
|
|
|
|
let url = format!("{}/api", server.uri());
|
|
let result: ApiResponse = HttpClient::new().get_json(&url).await.unwrap();
|
|
assert!(result.ok);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn get_json_fails_fast_on_4xx() {
|
|
let server = MockServer::start().await;
|
|
|
|
// expect(1) makes wiremock panic on drop if the mock matches more than
|
|
// once. that's how we assert "no retries" without watching the clock.
|
|
Mock::given(method("GET"))
|
|
.and(path("/api"))
|
|
.respond_with(ResponseTemplate::new(404))
|
|
.expect(1)
|
|
.mount(&server)
|
|
.await;
|
|
|
|
let url = format!("{}/api", server.uri());
|
|
let err = HttpClient::new()
|
|
.get_json::<ApiResponse>(&url)
|
|
.await
|
|
.unwrap_err();
|
|
assert!(
|
|
format!("{err:?}").contains("404"),
|
|
"expected 404 in error, got: {err:?}"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn get_json_gives_up_after_max_retries() {
|
|
let server = MockServer::start().await;
|
|
|
|
// MAX_RETRIES = 3 means we expect exactly 4 attempts before giving up.
|
|
// expect(4) asserts that.
|
|
Mock::given(method("GET"))
|
|
.and(path("/api"))
|
|
.respond_with(ResponseTemplate::new(503))
|
|
.expect(4)
|
|
.mount(&server)
|
|
.await;
|
|
|
|
let url = format!("{}/api", server.uri());
|
|
let err = HttpClient::new()
|
|
.get_json::<ApiResponse>(&url)
|
|
.await
|
|
.unwrap_err();
|
|
assert!(
|
|
format!("{err:?}").contains("503"),
|
|
"expected 503 in final error, got: {err:?}"
|
|
);
|
|
}
|
|
|
|
// ---------- download_file ----------
|
|
|
|
#[tokio::test]
|
|
async fn download_file_retries_5xx_then_succeeds() {
|
|
let server = MockServer::start().await;
|
|
|
|
Mock::given(method("GET"))
|
|
.and(path("/file.bin"))
|
|
.respond_with(ResponseTemplate::new(502))
|
|
.up_to_n_times(1)
|
|
.expect(1)
|
|
.mount(&server)
|
|
.await;
|
|
Mock::given(method("GET"))
|
|
.and(path("/file.bin"))
|
|
.respond_with(ResponseTemplate::new(200).set_body_bytes(b"hello, retried".to_vec()))
|
|
.expect(1)
|
|
.mount(&server)
|
|
.await;
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let dest = tmp.path().join("downloaded.bin");
|
|
let url = format!("{}/file.bin", server.uri());
|
|
|
|
download_file(&HttpClient::new(), &url, &dest, |_, _| {})
|
|
.await
|
|
.unwrap();
|
|
|
|
let content = std::fs::read(&dest).unwrap();
|
|
assert_eq!(content, b"hello, retried");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn download_file_fails_fast_on_4xx() {
|
|
let server = MockServer::start().await;
|
|
|
|
Mock::given(method("GET"))
|
|
.and(path("/file.bin"))
|
|
.respond_with(ResponseTemplate::new(404))
|
|
.expect(1)
|
|
.mount(&server)
|
|
.await;
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let dest: PathBuf = tmp.path().join("never-written.bin");
|
|
let url = format!("{}/file.bin", server.uri());
|
|
|
|
let err = download_file(&HttpClient::new(), &url, &dest, |_, _| {})
|
|
.await
|
|
.unwrap_err();
|
|
assert!(
|
|
format!("{err:?}").contains("404"),
|
|
"expected 404 in error, got: {err:?}"
|
|
);
|
|
}
|