Files
arena-tracker/crates/core/tests/game_data.rs
T
nessar a4a2a51670
CI / core (pull_request) Successful in 1m39s
Keep incomplete catalogs and offscreen tooltips out of the recap (#135)
Reject patch lists without any names before writing the cache and retry unusable caches at the same version. Scope visible tooltips to their player so inactive item slots need no tooltip state.
2026-11-25 09:13:08 +01:00

455 lines
17 KiB
Rust

mod common;
use arena_tracker_core::{Error, GameCatalog, GameData, SourceError};
use common::{
FakeCatalog, RECORDED_ITEMS, RECORDED_VERSION, VERSIONS_URL, arena_url, augments_url, icon_png,
item_icon_url, items_url,
};
use serde_json::Value;
const RABADON_ICON: &str = "https://ddragon.leagueoflegends.com/cdn/16.23.1/img/item/3089.png";
const ADAPT_ICON: &str = "https://raw.communitydragon.org/16.23/plugins/rcp-be-lol-game-data/global/default/assets/ux/cherry/augments/icons/adapt_small.png";
/// Six items and nine augments, each with an icon.
const ICONS: usize = 15;
/// Refreshes and counts the changes the caller was told of.
fn refresh(data: &GameData, source: &FakeCatalog) -> Result<usize, Error> {
let mut changes = 0;
data.refresh(source, &mut || changes += 1)?;
Ok(changes)
}
fn filled() -> (tempfile::TempDir, GameData, FakeCatalog) {
let dir = tempfile::tempdir().unwrap();
let data = GameData::new(dir.path().join("game-data"));
let source = FakeCatalog::recorded();
refresh(&data, &source).unwrap();
source.forget_asked();
(dir, data, source)
}
#[test]
fn the_catalog_is_filled_in_french_with_its_icons() {
let dir = tempfile::tempdir().unwrap();
let data = GameData::new(dir.path().join("game-data"));
let source = FakeCatalog::recorded();
assert!(data.load().is_empty());
// Told once the names are stored, and once the icons are.
assert_eq!(refresh(&data, &source).unwrap(), 2);
let catalog = data.load();
assert_eq!(catalog.version.as_deref(), Some(RECORDED_VERSION));
assert_eq!(catalog.item_name(3089), "Coiffe de Rabadon");
assert_eq!(catalog.item_name(443055), "Fulmination");
assert_eq!(catalog.augment_name(1205), "Adaptation");
assert_eq!(catalog.augment_name(93), "Échauffement");
let augment = catalog.augment(1205).unwrap();
assert_eq!(augment.name_id, "ARAM_ADAPt");
assert_eq!(augment.rarity.as_deref(), Some("kSilver"));
assert_eq!(catalog.augment(1327).unwrap().name_id, "Adamant");
let item_icon = catalog.item_icon(3089).unwrap();
assert_eq!(std::fs::read(item_icon).unwrap(), icon_png(RABADON_ICON));
let augment_icon = catalog.augment_icon(1205).unwrap();
assert_eq!(std::fs::read(augment_icon).unwrap(), icon_png(ADAPT_ICON));
assert_eq!(source.icons_asked().len(), ICONS);
assert_eq!(
source.asked()[..4],
[
VERSIONS_URL.to_owned(),
augments_url("16.23"),
arena_url("16.23"),
items_url("16.23.1"),
]
);
}
#[test]
fn the_catalog_keeps_the_raw_descriptions_for_the_bubbles() {
let (dir, _, _) = filled();
let file = std::fs::read(dir.path().join("game-data/catalog.json")).unwrap();
let stored: Value = serde_json::from_slice(&file).unwrap();
let item = |id| {
(stored["items"].as_array().unwrap().iter())
.find(|i| i["id"] == id)
.unwrap()
};
let augment = |id| {
(stored["augments"].as_array().unwrap().iter())
.find(|a| a["id"] == id)
.unwrap()
};
assert!(
item(3089)["description"]
.as_str()
.unwrap()
.starts_with("<mainText>")
);
assert!(
item(222524)["description"]
.as_str()
.unwrap()
.contains("0 sec")
);
assert!(
augment(93)["summary"]
.as_str()
.unwrap()
.contains("@MaxStacks@%")
);
assert_eq!(augment(93)["values"]["MaxStacks"][3], 60.0);
assert!(
augment(214)["summary"]
.as_str()
.unwrap()
.contains("@SpinDamageAmp*100@")
);
assert_eq!(augment(323)["values"]["APRatio"], serde_json::json!([0.0]));
assert_eq!(augment(1205)["summary"], "");
assert_eq!(augment(1205)["values"], serde_json::json!({}));
}
#[test]
fn load_prepares_the_texts_of_the_bubbles() {
let (_dir, data, _) = filled();
let catalog = data.load();
assert_eq!(
catalog.item_description(3089).as_deref(),
Some("+130\u{a0}puissance\n\nOpus magique\nVous augmentez votre puissance totale de 30%.")
);
assert!(
(catalog.item_description(222524).unwrap()).contains("pendant ? sec"),
"a value the game computes is never 0"
);
let echauffement = catalog.augment_description(93).unwrap();
assert!(
echauffement.ends_with("(jusqu'à 20% max)."),
"{echauffement}"
);
// No entry in CommunityDragon's arena file, and an id nobody knows.
assert_eq!(catalog.augment_description(1205), None);
assert_eq!(catalog.augment_description(9_999_999), None);
assert_eq!(catalog.item_description(9_999_999), None);
}
#[test]
fn the_same_version_asks_only_for_the_versions() {
let (_dir, data, source) = filled();
assert_eq!(refresh(&data, &source).unwrap(), 0);
assert_eq!(source.asked(), [VERSIONS_URL]);
}
#[test]
fn a_cache_missing_a_usable_list_is_repaired_even_at_the_same_version() {
let (dir, data, source) = filled();
let path = dir.path().join("game-data/catalog.json");
let original: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
for (list, field) in [
("items", "name"),
("augments", "name"),
("augments", "summary"),
] {
let mut damaged = original.clone();
for entry in damaged[list].as_array_mut().unwrap() {
entry[field] = "".into();
}
std::fs::write(&path, serde_json::to_vec(&damaged).unwrap()).unwrap();
assert_eq!(refresh(&data, &source).unwrap(), 1, "{list}.{field}");
let repaired: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
assert_eq!(repaired, original);
assert!(source.icons_asked().is_empty());
}
}
#[test]
fn a_new_version_renews_the_names_and_fetches_only_the_new_or_renamed_icons() {
let (_dir, data, source) = filled();
// The next patch renames an item, gives another one a new picture and adds one.
let mut list: Value = serde_json::from_str(RECORDED_ITEMS).unwrap();
list["data"]["3089"]["name"] = "Coiffe de Rabadon (corrigée)".into();
list["data"]["1001"]["image"]["full"] = "1001_v2.png".into();
list["data"]["1337"] = serde_json::json!({
"name": "Nouvel objet", "description": "<mainText>Neuf</mainText>", "image": {"full": "1337.png"}
});
source.publish_data_dragon("16.24.1", &list.to_string());
source.publish_community_dragon("16.24");
assert_eq!(refresh(&data, &source).unwrap(), 2);
let catalog = data.load();
assert_eq!(catalog.version.as_deref(), Some("16.24.1"));
assert_eq!(catalog.item_name(3089), "Coiffe de Rabadon (corrigée)");
assert_eq!(catalog.item_name(1337), "Nouvel objet");
assert_eq!(
source.icons_asked(),
[
item_icon_url("16.24.1", "1001_v2.png"),
item_icon_url("16.24.1", "1337.png"),
]
);
let moved = std::fs::read(catalog.item_icon(1001).unwrap()).unwrap();
assert_eq!(moved, icon_png(&item_icon_url("16.24.1", "1001_v2.png")));
// The unchanged icons are kept, from the previous version.
let kept = std::fs::read(catalog.item_icon(3089).unwrap()).unwrap();
assert_eq!(kept, icon_png(RABADON_ICON));
assert!(catalog.augment_icon(1205).is_some());
}
#[test]
fn communitydragon_behind_a_patch_keeps_the_old_catalog_and_the_next_check_retries() {
let (_dir, data, source) = filled();
source.publish_data_dragon("16.24.1", RECORDED_ITEMS);
// CommunityDragon answers 404 for patch 16.24: nothing is written, not even the items.
assert!(matches!(refresh(&data, &source), Err(Error::Source(_))));
let catalog = data.load();
assert_eq!(catalog.version.as_deref(), Some(RECORDED_VERSION));
assert_eq!(catalog.augment_name(1205), "Adaptation");
assert!(catalog.item_icon(3089).is_some_and(|path| path.is_file()));
assert!(
catalog
.augment_icon(1205)
.is_some_and(|path| path.is_file())
);
assert!(source.icons_asked().is_empty());
assert!(
!source.asked().contains(&items_url("16.24.1")),
"the big items list is not downloaded for nothing"
);
// CommunityDragon catches up: the next check goes through.
source.publish_community_dragon("16.24");
assert_eq!(
refresh(&data, &source).unwrap(),
1,
"the names; every icon is kept"
);
assert_eq!(data.load().version.as_deref(), Some("16.24.1"));
}
#[test]
fn a_source_that_fails_or_answers_nonsense_leaves_the_old_catalog_whole() {
let (_dir, data, source) = filled();
source.publish_data_dragon("16.24.1", RECORDED_ITEMS);
source.publish_community_dragon("16.24");
let step = |source: &FakeCatalog| {
assert!(matches!(refresh(&data, source), Err(Error::Source(_))));
let catalog = data.load();
assert_eq!(catalog.version.as_deref(), Some(RECORDED_VERSION));
assert_eq!(catalog.item_name(3089), "Coiffe de Rabadon");
assert_eq!(catalog.augment_name(93), "Échauffement");
assert!(catalog.augment_description(93).is_some());
};
source.fail(&arena_url("16.24"), SourceError::Network("coupé".into()));
step(&source);
source.fail(&arena_url("16.24"), SourceError::Other("HTTP 500".into()));
step(&source);
source.serve(&arena_url("16.24"), "pas du JSON");
step(&source);
source.serve(&arena_url("16.24"), r#"{"augments":[{}]}"#);
step(&source);
source.serve(
&arena_url("16.24"),
r#"{"augments":[{"id":93,"desc":" "}]}"#,
);
step(&source);
source.serve(
&arena_url("16.24"),
r#"{"augments":[{"id":424242,"desc":"Autre"}]}"#,
);
step(&source);
source.publish_community_dragon("16.24");
source.serve(&augments_url("16.24"), "[]");
step(&source);
source.publish_community_dragon("16.24");
source.serve(&items_url("16.24.1"), r#"{"data": {}}"#);
step(&source);
source.fail(VERSIONS_URL, SourceError::Network("coupé".into()));
step(&source);
}
#[test]
fn a_patch_with_nameless_items_or_augments_is_retried_without_replacing_the_catalog() {
let (dir, data, source) = filled();
let path = dir.path().join("game-data/catalog.json");
let old = std::fs::read(&path).unwrap();
source.publish_community_dragon("16.24");
source.publish_data_dragon("16.24.1", r#"{"data":{"1":{}}}"#);
assert!(matches!(refresh(&data, &source), Err(Error::Source(_))));
assert_eq!(std::fs::read(&path).unwrap(), old);
source.publish_data_dragon("16.24.1", RECORDED_ITEMS);
source.serve(&augments_url("16.24"), r#"[{"id":93,"nameTRA":" "}]"#);
assert!(matches!(refresh(&data, &source), Err(Error::Source(_))));
assert_eq!(std::fs::read(&path).unwrap(), old);
assert!(source.icons_asked().is_empty());
source.publish_community_dragon("16.24");
assert_eq!(refresh(&data, &source).unwrap(), 1);
assert_eq!(data.load().version.as_deref(), Some("16.24.1"));
}
#[test]
fn a_connection_lost_mid_download_resumes_at_the_next_check() {
let dir = tempfile::tempdir().unwrap();
let data = GameData::new(dir.path().to_owned());
let source = FakeCatalog::recorded();
// The versions, the three lists and five icons, then the network goes down.
source.lose_network_after(4 + 5);
assert_eq!(
refresh(&data, &source).unwrap(),
2,
"names, then the icons so far"
);
let have = |c: &GameCatalog| {
[1001, 3089, 223089, 443055, 222524, 443081]
.iter()
.filter(|&&id| c.item_icon(id).is_some())
.count()
+ [1205, 1141, 1327, 2087, 1214, 214, 322, 93, 323]
.iter()
.filter(|&&id| c.augment_icon(id).is_some())
.count()
};
assert_eq!(data.load().item_name(3089), "Coiffe de Rabadon");
assert_eq!(have(&data.load()), 5);
// Back, at the same version: the lists are not read again, the missing icons are.
source.reconnect();
source.forget_asked();
assert_eq!(refresh(&data, &source).unwrap(), 1);
assert_eq!(source.icons_asked().len(), ICONS - 5);
assert_eq!(
source.asked().len(),
1 + ICONS - 5,
"the versions and the icons"
);
assert_eq!(have(&data.load()), ICONS);
}
#[test]
fn a_missing_icon_is_skipped_and_not_asked_again_at_this_version() {
let dir = tempfile::tempdir().unwrap();
let data = GameData::new(dir.path().to_owned());
let source = FakeCatalog::recorded();
source.fail(RABADON_ICON, SourceError::NotFound);
refresh(&data, &source).unwrap();
let catalog = data.load();
assert_eq!(catalog.item_icon(3089), None);
assert_eq!(catalog.item_name(3089), "Coiffe de Rabadon");
assert!(catalog.item_icon(1001).is_some());
source.forget_asked();
refresh(&data, &source).unwrap();
assert_eq!(source.asked(), [VERSIONS_URL]);
}
#[test]
fn a_server_error_on_an_icon_stops_the_pass_and_the_next_check_asks_again() {
let dir = tempfile::tempdir().unwrap();
let data = GameData::new(dir.path().to_owned());
let source = FakeCatalog::recorded();
source.fail(RABADON_ICON, SourceError::Other("HTTP 500".into()));
refresh(&data, &source).unwrap();
assert_eq!(data.load().item_icon(3089), None);
// The pass was not marked complete: the next check resumes it, without the lists.
source.forget_asked();
refresh(&data, &source).unwrap();
assert!(source.icons_asked().contains(&RABADON_ICON.to_owned()));
assert!(!source.asked().contains(&items_url("16.23.1")));
source.fail(
RABADON_ICON,
SourceError::RateLimited {
retry_after: Default::default(),
},
);
source.forget_asked();
refresh(&data, &source).unwrap();
assert_eq!(source.icons_asked(), [RABADON_ICON]);
assert_eq!(data.load().item_icon(3089), None);
}
#[test]
fn an_unknown_id_gets_the_neutral_icon_and_name() {
let (_dir, data, _) = filled();
let catalog = data.load();
assert_eq!(catalog.item_name(9_999_999), "Objet 9999999");
assert_eq!(catalog.item_icon(9_999_999), None);
assert_eq!(catalog.augment_name(424_242), "Amélioration 424242");
assert_eq!(catalog.augment_icon(424_242), None);
assert_eq!(catalog.augment(424_242), None);
}
#[test]
fn offline_with_no_catalog_keeps_the_neutral_names() {
let dir = tempfile::tempdir().unwrap();
let data = GameData::new(dir.path().to_owned());
let source = FakeCatalog::recorded();
source.lose_network_after(0);
assert!(matches!(refresh(&data, &source), Err(Error::Source(_))));
let catalog = data.load();
assert!(catalog.is_empty());
assert_eq!(catalog.item_name(3089), "Objet 3089");
assert_eq!(catalog.augment_icon(1205), None);
}
#[test]
fn a_cache_that_cannot_be_read_is_rebuilt() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("catalog.json"), "{ not json").unwrap();
let data = GameData::new(dir.path().to_owned());
assert!(data.load().is_empty());
refresh(&data, &FakeCatalog::recorded()).unwrap();
assert_eq!(data.load().item_name(1001), "Bottes");
}
#[test]
fn a_cache_made_from_the_client_still_loads_and_is_replaced() {
let dir = tempfile::tempdir().unwrap();
let old = r#"{"version": "16.22.719.8541", "locale": "en_US", "icons_complete": true,
"items": [{"id": 3089, "name": "Rabadon's Deathcap",
"icon": "/lol-game-data/assets/ASSETS/Items/Icons2D/3089_Mage_T3_Deathcap.png"}],
"augments": [{"id": 1205, "name_id": "ARAM_ADAPt", "name": "Adapt", "rarity": "kSilver",
"icon": "/lol-game-data/assets/ASSETS/UX/Cherry/Augments/Icons/ADAPt_small.png"}]}"#;
std::fs::write(dir.path().join("catalog.json"), old).unwrap();
std::fs::create_dir(dir.path().join("items")).unwrap();
std::fs::write(dir.path().join("items/3089.png"), "\u{89}PNG old").unwrap();
let data = GameData::new(dir.path().to_owned());
assert_eq!(data.load().item_name(3089), "Rabadon's Deathcap");
refresh(&data, &FakeCatalog::recorded()).unwrap();
let catalog = data.load();
assert_eq!(catalog.version.as_deref(), Some(RECORDED_VERSION));
assert_eq!(catalog.item_name(3089), "Coiffe de Rabadon");
let icon = std::fs::read(catalog.item_icon(3089).unwrap()).unwrap();
assert_eq!(
icon,
icon_png(RABADON_ICON),
"the client's picture was dropped"
);
}
#[test]
fn the_augment_name_id_matches_the_death_recap_events_in_both_sources() {
let (_dir, data, _) = filled();
let catalog = data.load();
// The ids and names the Death Recap matches on, as CommunityDragon spells them.
assert_eq!(catalog.augment(93).unwrap().name_id, "WarmupRoutine");
assert_eq!(catalog.augment(2087).unwrap().name_id, "ARAM_Archmage");
}