3dd17ad7d7
Horizon results and project pages show a project's rating from the registry service (GET playhorizon.dev/api/mods/ratings, fetched with the results, kept ten minutes): five pixel stars, the mean and how many, or "No ratings yet". Nothing shows when the service doesn't answer. The launcher has no GitHub account, so a project's page offers "Sign in with GitHub to rate": it opens the site's sign-in, which comes back to the project's page and its stars (one rating per GitHub account). HORIZON_MOD_RATINGS (a file or https:// address) points a test registry at its own ratings. The star icon is drawn for Horizon on the icons' grid. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
259 lines
9.2 KiB
Rust
259 lines
9.2 KiB
Rust
//! The registry source against the sample index of `horizon_mods::index`
|
|
//! (`crates/horizon-mods/tests/index/`), its files served from disk.
|
|
use super::*;
|
|
|
|
fn fixtures() -> PathBuf {
|
|
Path::new(env!("CARGO_MANIFEST_DIR")).join("../crates/horizon-mods/tests/index")
|
|
}
|
|
|
|
/// The sample index with its files on disk, as a local test registry lists them.
|
|
fn sample() -> Index {
|
|
let text = fs::read_to_string(fixtures().join("index-v1.json")).unwrap();
|
|
let mut index = Index::parse(&text).unwrap();
|
|
for p in &mut index.projects {
|
|
for v in &mut p.versions {
|
|
let path = fixtures().join(&v.file.name);
|
|
v.file.url = format!("file://{}", path.canonicalize().unwrap_or(path).display());
|
|
}
|
|
}
|
|
index
|
|
}
|
|
|
|
fn scratch(name: &str) -> PathBuf {
|
|
let dir = std::env::temp_dir().join(format!(
|
|
"horizon-launcher-registry-{name}-{}",
|
|
std::process::id()
|
|
));
|
|
let _ = fs::remove_dir_all(&dir);
|
|
fs::create_dir_all(&dir).unwrap();
|
|
dir
|
|
}
|
|
|
|
fn hud(index: &Index) -> Project {
|
|
project_of(index.project("coords_hud").unwrap()).unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn searches_by_kind_and_words() {
|
|
let index = sample();
|
|
let found = |text: &str, kind| -> Vec<String> {
|
|
search(&index, &Query::new(text, kind))
|
|
.into_iter()
|
|
.map(|p| p.id)
|
|
.collect()
|
|
};
|
|
assert_eq!(found("", Kind::Mod), ["coords_hud"]);
|
|
assert_eq!(found("coordinates HUD", Kind::Mod), ["coords_hud"]);
|
|
assert!(found("zoom", Kind::Mod).is_empty());
|
|
// Modpacks under Modpacks; a kind this launcher can't install yet (a world) never.
|
|
assert_eq!(found("", Kind::Modpack), ["cozy"]);
|
|
assert!(found("skyblock", Kind::ResourcePack).is_empty());
|
|
assert_eq!(found("", Kind::Shader), ["glow"]);
|
|
// The index's download counts, most downloaded first.
|
|
let mut counted = index.clone();
|
|
counted.projects.push(index::Project {
|
|
id: "zoom".into(),
|
|
name: "Zoom".into(),
|
|
downloads: 12_345,
|
|
..index.project("coords_hud").unwrap().clone()
|
|
});
|
|
counted.projects[0].downloads = 80;
|
|
let first = search(&counted, &Query::new("", Kind::Mod));
|
|
let order: Vec<(&str, u64)> = first.iter().map(|p| (p.id.as_str(), p.downloads)).collect();
|
|
assert_eq!(order, [("zoom", 12_345), ("coords_hud", 80)]);
|
|
let p = hud(&index);
|
|
assert_eq!(p.support(), super::super::Support::Works);
|
|
assert_eq!(p.page, "https://playhorizon.dev/mods/coords_hud");
|
|
}
|
|
|
|
#[test]
|
|
fn installs_checked_turns_on_and_removes() {
|
|
let index = sample();
|
|
let home = scratch("install");
|
|
let p = hud(&index);
|
|
let done = install_from(&index, true, None, &p, &home, &|_, _| {}).unwrap();
|
|
assert_eq!(done[0].file, "horizon-mods/coords_hud.hmod");
|
|
assert_eq!(done[0].verdict.as_deref(), Some("Verified"));
|
|
assert!(done[0].checksum.starts_with("SHA-256:e1135345"));
|
|
assert!(home.join("horizon-mods/coords_hud.hmod").is_file());
|
|
let list = fs::read_to_string(home.join("horizon-mods.txt")).unwrap();
|
|
assert_eq!(list, "mod.coords_hud:true\n");
|
|
assert_eq!(
|
|
fs::read_dir(home.join("mod-browser/downloads"))
|
|
.unwrap()
|
|
.count(),
|
|
0
|
|
);
|
|
// Installed already: nothing to do.
|
|
let again = install_from(&index, true, None, &p, &home, &|_, _| {}).unwrap();
|
|
assert_eq!(again, done);
|
|
|
|
let mut revoked = index.clone();
|
|
revoked.revoked.push(index::Revoked {
|
|
id: "coords_hud".into(),
|
|
sha256: None,
|
|
reason: "Test.".into(),
|
|
date: String::new(),
|
|
});
|
|
let said = turn_off_revoked(&revoked, &home);
|
|
assert_eq!(said, ["Coordinates HUD was taken down and is off: Test."]);
|
|
assert_eq!(
|
|
fs::read_to_string(home.join("horizon-mods.txt")).unwrap(),
|
|
""
|
|
);
|
|
|
|
installed::remove(&home, "coords_hud").unwrap();
|
|
assert!(!home.join("horizon-mods/coords_hud.hmod").exists());
|
|
assert!(installed::Installed::load(&home).entries.is_empty());
|
|
fs::remove_dir_all(&home).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn installs_a_shader_pack_into_shaderpacks() {
|
|
let index = sample();
|
|
let home = scratch("shader");
|
|
let p = project_of(index.project("glow").unwrap()).unwrap();
|
|
assert_eq!(p.support(), super::super::Support::Works);
|
|
let done = install_from(&index, true, None, &p, &home, &|_, _| {}).unwrap();
|
|
assert_eq!(done[0].file, "shaderpacks/glow-1.0.0.hshader");
|
|
assert_eq!(done[0].kind, Kind::Shader);
|
|
assert!(home.join("shaderpacks/glow-1.0.0.hshader").is_file());
|
|
// Not a mod: nothing turns on in horizon-mods.txt; the game's Shader Packs page picks it.
|
|
assert!(!home.join("horizon-mods.txt").exists());
|
|
|
|
let mut revoked = index.clone();
|
|
revoked.revoked.push(index::Revoked {
|
|
id: "glow".into(),
|
|
sha256: None,
|
|
reason: "Test.".into(),
|
|
date: String::new(),
|
|
});
|
|
let said = turn_off_revoked(&revoked, &home);
|
|
assert_eq!(said, ["Soft Glow was taken down: Test. Remove it."]);
|
|
|
|
installed::remove(&home, "glow").unwrap();
|
|
assert!(!home.join("shaderpacks/glow-1.0.0.hshader").exists());
|
|
// A file of the same name the registry didn't put there is left alone.
|
|
fs::write(home.join("shaderpacks/glow-1.0.0.hshader"), b"mine").unwrap();
|
|
let e = install_from(&index, true, None, &p, &home, &|_, _| {}).unwrap_err();
|
|
assert_eq!(
|
|
e,
|
|
"shaderpacks/glow-1.0.0.hshader is already there: move it away first."
|
|
);
|
|
fs::remove_dir_all(&home).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn refuses_a_changed_file_a_taken_id_and_local_files_from_the_real_registry() {
|
|
let index = sample();
|
|
let p = hud(&index);
|
|
|
|
let home = scratch("tampered");
|
|
let mut tampered = index.clone();
|
|
let cozy = tampered.projects[1].versions[0].file.url.clone();
|
|
tampered.projects[0].versions[0].file.url = cozy;
|
|
let e = install_from(&tampered, true, None, &p, &home, &|_, _| {}).unwrap_err();
|
|
assert!(e.contains("larger than its source says"), "{e}");
|
|
assert!(!home.join("horizon-mods").exists());
|
|
|
|
let e = install_from(&index, false, None, &p, &home, &|_, _| {}).unwrap_err();
|
|
assert!(e.contains("file servers"), "{e}");
|
|
|
|
fs::create_dir_all(home.join("horizon-mods/coords_hud")).unwrap();
|
|
let e = install_from(&index, true, None, &p, &home, &|_, _| {}).unwrap_err();
|
|
assert!(e.contains("already in horizon-mods"), "{e}");
|
|
|
|
let mut taken_down = index.clone();
|
|
taken_down.revoked.push(index::Revoked {
|
|
id: "coords_hud".into(),
|
|
sha256: Some(index.projects[0].versions[0].file.sha256.clone()),
|
|
reason: "Test.".into(),
|
|
date: String::new(),
|
|
});
|
|
let e = install_from(&taken_down, true, None, &p, &home, &|_, _| {}).unwrap_err();
|
|
assert_eq!(e, "Coordinates HUD was taken down: Test.");
|
|
fs::remove_dir_all(&home).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn registry_versions_run_when_made_for_this_game() {
|
|
use super::super::{pick, support, Support};
|
|
let v = |game: &str| vec![game.to_owned()];
|
|
let horizon = vec![LOADER.to_owned()];
|
|
assert_eq!(support(Kind::Mod, &v(GAME), &horizon), Support::Works);
|
|
assert!(!support(Kind::Mod, &v("26.2"), &horizon).installable());
|
|
let index = sample();
|
|
let p = index.project("coords_hud").unwrap();
|
|
let versions: Vec<Version> = p
|
|
.versions
|
|
.iter()
|
|
.map(|x| version_of(&index, p, x, None))
|
|
.collect();
|
|
assert_eq!(pick(Kind::Mod, &versions, false).unwrap().number, "0.1.0");
|
|
}
|
|
|
|
#[test]
|
|
fn owner_links_only_take_github_logins() {
|
|
for ok in ["Stink-O", "ludovic111", "a"] {
|
|
assert!(github_login(ok), "{ok}");
|
|
}
|
|
for bad in [
|
|
"",
|
|
"-x",
|
|
"x-",
|
|
"a--b",
|
|
"bad login",
|
|
"a/b",
|
|
"x?y=1",
|
|
&"a".repeat(40),
|
|
] {
|
|
assert!(!github_login(bad), "{bad}");
|
|
}
|
|
}
|
|
|
|
/// A version that calls mod API functions the game doesn't have is marked and skipped.
|
|
#[test]
|
|
fn versions_for_a_newer_horizon_are_not_installed() {
|
|
let mut index = sample();
|
|
index.projects[0].versions[0].api_level = Some(1);
|
|
let p = index.project("coords_hud").unwrap();
|
|
let e = newest(&index, p, Some(0)).unwrap_err();
|
|
assert_eq!(
|
|
e,
|
|
"Coordinates HUD needs a newer Horizon than the game you play: update it first."
|
|
);
|
|
assert!(newest(&index, p, Some(1)).is_ok());
|
|
assert!(newest(&index, p, None).is_ok());
|
|
let v = version_of(&index, p, &p.versions[0], Some(0));
|
|
assert_eq!(v.blocked.as_deref(), Some(NEEDS_NEWER));
|
|
assert_eq!(version_of(&index, p, &p.versions[0], Some(1)).blocked, None);
|
|
}
|
|
|
|
#[test]
|
|
fn ratings_keep_only_what_is_well_formed() {
|
|
let text = r#"{"projects": {
|
|
"zoom": {"average": 4.25, "count": 12},
|
|
"minimap": {"average": 9, "count": 3},
|
|
"keystrokes": {"average": 3, "count": 0},
|
|
"Bad Id": {"average": 4, "count": 1},
|
|
"fps_overlay": {"average": "5", "count": 1}
|
|
}}"#;
|
|
let ratings = parse_ratings(text);
|
|
assert_eq!(
|
|
ratings.into_iter().collect::<Vec<_>>(),
|
|
[(
|
|
"zoom".to_owned(),
|
|
Rating {
|
|
average: 4.25,
|
|
count: 12
|
|
}
|
|
)]
|
|
);
|
|
assert!(parse_ratings("not json").is_empty());
|
|
assert!(parse_ratings(r#"{"projects": []}"#).is_empty());
|
|
assert_eq!(
|
|
rate_url("zoom"),
|
|
"https://playhorizon.dev/api/mods/auth/login?back=/mods/zoom"
|
|
);
|
|
}
|