refactor: reorganize source and tests

This commit is contained in:
objz committed 2026-07-27 11:28:57 +02:00
1 parent 15d76c18a0
commit c6205a337c
139 files changed
+5532 -5548

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
use super::*;
// 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() {
let tmp = tempfile::tempdir().unwrap();
assert!(!has_legacy_install_profile(
&tmp.path().join("missing-installer.jar")
));
}
+144
View File
@@ -0,0 +1,144 @@
use super::*;
// the factory maps every ModLoader variant to its concrete installer.
// covering all five arms catches a misordered match or a copy-paste typo
// that would route, say, NeoForge to the Forge installer.
#[rstest::rstest]
#[case::vanilla(ModLoader::Vanilla)]
#[case::forge(ModLoader::Forge)]
#[case::neoforge(ModLoader::NeoForge)]
#[case::fabric(ModLoader::Fabric)]
#[case::quilt(ModLoader::Quilt)]
fn get_installer_returns_matching_loader_type(#[case] loader: ModLoader) {
let installer = get_installer(loader);
assert_eq!(installer.loader_type(), loader);
}
#[test]
fn save_installer_profile_copies_raw_bytes_verbatim() {
use tempfile::TempDir;
let tmp = TempDir::new().unwrap();
let instance_dir = tmp.path().join("instance");
let meta_dir = tmp.path().join("meta");
// a synthetic installer version JSON with the modern arguments
// object - exactly the shape we used to strip.
let installer_json = br#"{
"id": "1.20.1-forge-47.2.0",
"inheritsFrom": "1.20.1",
"mainClass": "cpw.mods.bootstraplauncher.BootstrapLauncher",
"libraries": [{ "name": "net.minecraftforge:forge:47.2.0" }],
"arguments": {
"game": ["--launchTarget", "forge_client"],
"jvm": ["--add-opens", "java.base/sun.security.util=cpw.mods.securejarhandler"]
}
}"#;
let ver_dir = instance_dir
.join(crate::storage::MINECRAFT_DIR_NAME)
.join("versions")
.join("1.20.1-forge-47.2.0");
std::fs::create_dir_all(&ver_dir).unwrap();
let ver_json_path = ver_dir.join("1.20.1-forge-47.2.0.json");
std::fs::write(&ver_json_path, installer_json).unwrap();
save_installer_profile(
&instance_dir,
&meta_dir,
"1.20.1-forge-47.2.0",
"forge-1.20.1-47.2.0.json",
)
.unwrap();
let saved = std::fs::read(
meta_dir
.join("cache/loaders/profiles")
.join("forge-1.20.1-47.2.0.json"),
)
.unwrap();
assert_eq!(
saved,
installer_json.to_vec(),
"saved profile should be byte-for-byte identical to installer output"
);
}
// shape-pinning test: a synthetic versionInfo from a 1.7.10 forge
// install_profile.json must deserialise as a LaunchProfile so the
// launch flow's render_args + resolve pipeline can consume it. no
// filesystem round-trip; serde_json directly on the literal bytes.
#[test]
fn legacy_forge_version_info_deserialises_as_launch_profile() {
let bytes = br#"{
"id": "1.7.10-Forge10.13.4.1614-1.7.10",
"mainClass": "net.minecraft.launchwrapper.Launch",
"minecraftArguments": "--username ${auth_player_name} --tweakClass cpw.mods.fml.common.launcher.FMLTweaker",
"libraries": [
{ "name": "net.minecraftforge:forge:10.13.4.1614", "url": "http://files.minecraftforge.net/maven/" },
{ "name": "net.minecraft:launchwrapper:1.9" }
]
}"#;
let profile: crate::launch_profile::model::LaunchProfile =
serde_json::from_slice(bytes).unwrap();
assert_eq!(profile.id, "1.7.10-Forge10.13.4.1614-1.7.10");
assert_eq!(
profile.main_class.as_deref(),
Some("net.minecraft.launchwrapper.Launch")
);
// legacy forge profiles omit inheritsFrom; the launch flow's
// implicit fallback adds it before resolve.
assert!(profile.inherits_from.is_none());
assert!(
profile
.minecraft_arguments
.as_deref()
.unwrap()
.contains("--tweakClass")
);
assert_eq!(profile.libraries.len(), 2);
assert_eq!(
profile.libraries[0].name,
"net.minecraftforge:forge:10.13.4.1614"
);
assert_eq!(
profile.libraries[0].url.as_deref(),
Some("http://files.minecraftforge.net/maven/")
);
// legacy libs typically have no downloads.artifact; they resolve
// at launch time via maven_coord_to_path(name).
assert!(profile.libraries[0].downloads.is_none());
}
// shape-pinning test: a synthetic upstream fabric profile (no
// inheritsFrom, no arguments, libraries with name+url) must
// deserialise as a LaunchProfile so the install path can write it
// through to disk and the launch flow can read it back.
#[test]
fn raw_fabric_profile_bytes_parse_as_launch_profile() {
let bytes = br#"{
"id": "fabric-loader-0.14.21-1.20.1",
"mainClass": "net.fabricmc.loader.impl.launch.knot.KnotClient",
"libraries": [
{ "name": "net.fabricmc:fabric-loader:0.14.21", "url": "https://maven.fabricmc.net/" },
{ "name": "net.fabricmc:intermediary:1.20.1", "url": "https://maven.fabricmc.net/" }
]
}"#;
let parsed: crate::launch_profile::model::LaunchProfile =
serde_json::from_slice(bytes).unwrap();
assert_eq!(parsed.id, "fabric-loader-0.14.21-1.20.1");
assert_eq!(
parsed.main_class.as_deref(),
Some("net.fabricmc.loader.impl.launch.knot.KnotClient")
);
// upstream Fabric profiles omit inheritsFrom; the launch flow's
// implicit fallback handles it before resolve.
assert!(parsed.inherits_from.is_none());
assert!(parsed.arguments.is_none());
assert_eq!(parsed.libraries.len(), 2);
assert_eq!(
parsed.libraries[0].url.as_deref(),
Some("https://maven.fabricmc.net/")
);
}
+32
View File
@@ -0,0 +1,32 @@
use super::*;
#[test]
fn maven_3_part_coord() {
assert_eq!(
maven_coord_to_path("org.example:artifact:1.0"),
Some("org/example/artifact/1.0/artifact-1.0.jar".to_string())
);
}
#[test]
fn maven_4_part_coord_with_classifier() {
assert_eq!(
maven_coord_to_path("org.example:artifact:1.0:sources"),
Some("org/example/artifact/1.0/artifact-1.0-sources.jar".to_string())
);
}
#[test]
fn maven_nested_group() {
assert_eq!(
maven_coord_to_path("com.google.code.gson:gson:2.10"),
Some("com/google/code/gson/gson/2.10/gson-2.10.jar".to_string())
);
}
#[test]
fn maven_invalid_coordinates() {
for coordinate in ["org.example:artifact", "a:b:c:d:e", "just-a-string", ""] {
assert_eq!(maven_coord_to_path(coordinate), None);
}
}
+33
View File
@@ -0,0 +1,33 @@
use super::*;
use crate::net::mojang::{LatestVersions, VersionEntry, VersionManifest};
#[test]
fn manifest_types_map_to_stable_game_versions() {
let manifest = VersionManifest {
latest: LatestVersions {
release: "1.21.1".to_owned(),
snapshot: "24w01a".to_owned(),
},
versions: vec![
VersionEntry {
id: "1.21.1".to_owned(),
version_type: "release".to_owned(),
url: String::new(),
sha1: String::new(),
},
VersionEntry {
id: "24w01a".to_owned(),
version_type: "snapshot".to_owned(),
url: String::new(),
sha1: String::new(),
},
],
};
let versions = game_versions_from_manifest(manifest);
assert_eq!(versions[0].id, "1.21.1");
assert!(versions[0].stable);
assert_eq!(versions[1].id, "24w01a");
assert!(!versions[1].stable);
}