mod common;
use common::Fixture;
use serde_json::json;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
fn run_install_mod(fx: &Fixture, base_url: &str, url: &str) -> std::process::Output {
fx.cmd()
.env("MODDE_NEXUS_BASE_URL", base_url)
.env("MODDE_NEXUS_GRAPHQL_URL", base_url) .env("NEXUS_API_KEY", "test-key")
.args(["install", "mod", url])
.output()
.expect("spawn modde")
}
#[test]
fn install_mod_rejects_non_nexus_url() {
let fx = Fixture::new();
let output = fx
.cmd()
.env("NEXUS_API_KEY", "test-key")
.args(["install", "mod", "not a url"])
.output()
.expect("spawn modde");
assert!(
!output.status.success(),
"malformed URL must surface as failure"
);
}
#[tokio::test]
async fn install_mod_surfaces_404_on_mod_lookup() {
let fx = Fixture::new();
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/games/skyrimspecialedition/mods/99999.json"))
.respond_with(ResponseTemplate::new(404))
.mount(&server)
.await;
let output = run_install_mod(
&fx,
&server.uri(),
"https://www.nexusmods.com/skyrimspecialedition/mods/99999?tab=files&file_id=1",
);
assert!(!output.status.success(), "404 must surface as failure");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("failed to fetch mod info") || stderr.contains("404"),
"expected mod-info failure context in stderr; got:\n{stderr}"
);
}
#[tokio::test]
async fn install_mod_bails_on_non_premium_account() {
let fx = Fixture::new();
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/games/skyrimspecialedition/mods/12604.json"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"mod_id": 12604,
"name": "SkyUI",
"summary": null,
"version": "5.2",
"author": "SkyUI Team",
})))
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path("/users/validate.json"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"is_premium": false,
"name": "test-user",
})))
.mount(&server)
.await;
let output = run_install_mod(
&fx,
&server.uri(),
"https://www.nexusmods.com/skyrimspecialedition/mods/12604?tab=files&file_id=35834",
);
assert!(
!output.status.success(),
"non-premium accounts must not be allowed to download"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("Premium"),
"expected 'Premium' in error message; got:\n{stderr}"
);
}