mod common; use std::collections::HashMap; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; use arena_tracker_core::{Account, Scope, SeasonScope, SourceError, Tracker}; use common::{ FakeClient, FakeHistory, PlayedMatch, SELF_PUUID, at, date, payload, payloads, played_by, played_match, recorded_account, }; const OTHER_PUUID: &str = "fixture-puuid-other"; const NOW: i64 = 1_775_000_000_000; fn other_account() -> Account { Account { puuid: OTHER_PUUID.into(), game_name: "Autre".into(), tag_line: "FIX".into(), } } fn own_matches() -> Vec { vec![played_match(222, 1, at(date(2026, 4, 2), 0))] } fn map(points: &[(u32, u64)]) -> Option> { Some(points.iter().copied().collect()) } struct World { dir: tempfile::TempDir, history: Arc, client: FakeClient, requests: Arc, } impl World { fn new(history: FakeHistory) -> World { let requests = history.mastery_counter(); World { dir: tempfile::tempdir().unwrap(), history: Arc::new(history), client: FakeClient::recorded(), requests, } } /// The recorded Account (one Match, points known to Riot) and another one with a Match. fn two_accounts() -> World { let theirs = payload( "EUW1_theirs", 1750, 103, "Ahri", 3, true, at(date(2026, 4, 3), 0), ); World::new( FakeHistory::new(recorded_account(), payloads(&own_matches())) .with_account(other_account(), vec![played_by(&theirs, OTHER_PUUID)]) .with_mastery(SELF_PUUID, &[(222, 154_320), (103, 27_500)]) .with_mastery(OTHER_PUUID, &[(103, 900)]), ) } fn tracker(&self) -> Tracker { Tracker::open( &self.dir.path().join("arena.db"), Box::new(self.history.clone()), ) .unwrap() .with_client(Box::new(self.client.clone())) } fn requests(&self) -> usize { self.requests.load(Ordering::SeqCst) } fn round(&self, tracker: &Tracker) { tracker.follow_client(NOW, None, &mut |_| {}); } } #[test] fn an_account_never_fetched_has_unknown_points() { let world = World::two_accounts(); let tracker = world.tracker(); assert_eq!(tracker.mastery(SELF_PUUID).unwrap(), None); world.round(&tracker); assert_eq!(tracker.mastery(OTHER_PUUID).unwrap(), None); } #[test] fn the_first_round_stores_the_points_of_the_synced_account() { let world = World::two_accounts(); let tracker = world.tracker(); world.round(&tracker); assert_eq!( tracker.mastery(SELF_PUUID).unwrap(), map(&[(222, 154_320), (103, 27_500)]) ); assert_eq!(world.requests(), 1); assert_eq!(tracker.status().problem, None); } #[test] fn another_account_has_its_own_points() { let world = World::two_accounts(); let tracker = world.tracker(); world.round(&tracker); world.client.sign_in("Autre", "FIX"); world.round(&tracker); assert_eq!(tracker.mastery(OTHER_PUUID).unwrap(), map(&[(103, 900)])); assert_eq!( tracker.mastery(SELF_PUUID).unwrap(), map(&[(222, 154_320), (103, 27_500)]) ); assert_eq!(world.requests(), 2); } #[test] fn the_points_are_kept_for_the_next_launch() { let world = World::two_accounts(); world.round(&world.tracker()); let reopened = world.tracker(); assert_eq!( reopened.mastery(SELF_PUUID).unwrap(), map(&[(222, 154_320), (103, 27_500)]) ); } #[test] fn a_refresh_replaces_the_list_with_the_one_riot_returns() { let world = World::two_accounts(); let tracker = world.tracker(); world.round(&tracker); world .history .set_mastery(SELF_PUUID, &[(222, 160_000), (51, 40)]); tracker.refresh(&mut |_| {}).unwrap(); assert_eq!( tracker.mastery(SELF_PUUID).unwrap(), map(&[(222, 160_000), (51, 40)]), "103 is gone from the list: it has no points now" ); assert_eq!(world.requests(), 2); // An empty list is a known list: nothing is unknown any more. world.history.set_mastery(SELF_PUUID, &[]); tracker.refresh(&mut |_| {}).unwrap(); assert_eq!(tracker.mastery(SELF_PUUID).unwrap(), map(&[])); } #[test] fn a_failed_fetch_keeps_the_last_values_and_records_no_problem() { let world = World::two_accounts(); let tracker = world.tracker(); world.round(&tracker); let last = map(&[(222, 154_320), (103, 27_500)]); for error in [ SourceError::InvalidKey, SourceError::Network("hors ligne".into()), SourceError::MissingKey, SourceError::NotFound, ] { world.history.fail_mastery_with(Some(error)); let report = tracker.refresh(&mut |_| {}).unwrap(); assert_eq!(report.account, recorded_account()); assert_eq!(tracker.mastery(SELF_PUUID).unwrap(), last); assert_eq!(tracker.status().problem, None); } assert_eq!(world.requests(), 5); world.history.fail_mastery_with(None); world.history.set_mastery(SELF_PUUID, &[(222, 1)]); tracker.refresh(&mut |_| {}).unwrap(); assert_eq!(tracker.mastery(SELF_PUUID).unwrap(), map(&[(222, 1)])); } #[test] fn a_first_failed_fetch_leaves_the_points_unknown() { let world = World::two_accounts(); world .history .fail_mastery_with(Some(SourceError::InvalidKey)); let tracker = world.tracker(); world.round(&tracker); assert_eq!(world.requests(), 1); assert_eq!(tracker.mastery(SELF_PUUID).unwrap(), None); assert_eq!(tracker.status().problem, None); assert_eq!(tracker.match_count(SELF_PUUID).unwrap(), 1); } #[test] fn a_failed_sync_requests_no_points() { let world = World::two_accounts(); let tracker = world.tracker(); world .history .fail_with(Some(SourceError::Network("x".into()))); world.round(&tracker); assert_eq!(world.requests(), 0); assert_eq!(tracker.mastery(SELF_PUUID).unwrap(), None); } #[test] fn an_account_without_a_stored_match_makes_no_request() { let world = World::new( FakeHistory::new(recorded_account(), payloads(&own_matches())) .with_account(other_account(), vec![]) .with_mastery(OTHER_PUUID, &[(103, 900)]), ); let tracker = world.tracker(); world.client.sign_in("Autre", "FIX"); world.round(&tracker); assert_eq!(tracker.match_count(OTHER_PUUID).unwrap(), 0); assert_eq!(world.requests(), 0, "no Match, no platform"); assert_eq!(tracker.mastery(OTHER_PUUID).unwrap(), None); } #[test] fn rounds_that_do_not_talk_to_riot_request_no_points() { let world = World::two_accounts(); let tracker = world.tracker(); world.round(&tracker); assert_eq!(world.requests(), 1); for phase in ["ChampSelect", "InProgress", "None", "None"] { world.client.set_phase(phase, false); world.round(&tracker); } let _ = tracker.status(); let _ = tracker.mastery(SELF_PUUID); assert_eq!(world.requests(), 1, "no polling, nothing on a read"); tracker.refresh(&mut |_| {}).unwrap(); assert_eq!(world.requests(), 2, "a refresh is one such moment"); } #[test] fn a_match_fetched_by_its_game_id_is_followed_by_one_request() { let world = World::two_accounts(); let tracker = world.tracker(); world.round(&tracker); world.client.set_game_id(Some(common::recorded_game_id())); world.client.set_phase("InProgress", true); world.round(&tracker); world.client.set_phase("EndOfGame", true); // Riot does not have the Match yet: the try asks for it, but not for the points. world.round(&tracker); assert_eq!(world.requests(), 1); let id = format!("EUW1_{}", common::recorded_game_id()); world.history.add_unlisted(payload( &id, 1800, 222, "Jinx", 1, true, at(date(2026, 4, 5), 1), )); world.history.set_mastery(SELF_PUUID, &[(222, 154_900)]); tracker.follow_client(NOW + 60_000, None, &mut |_| {}); assert_eq!(tracker.match_count(SELF_PUUID).unwrap(), 2); assert_eq!(world.requests(), 2); assert_eq!(tracker.mastery(SELF_PUUID).unwrap(), map(&[(222, 154_900)])); } fn points_of(tracker: &Tracker, puuid: &str, champion_id: u32) -> Option { let scope = Scope { season: SeasonScope::AllSeasons, format: None, }; let stats = tracker.statistics(puuid, &scope).unwrap(); let row = stats .rows .iter() .find(|row| row.champion.id == champion_id) .expect("the champion has a row"); row.mastery_points } #[test] fn the_statistics_rows_carry_the_points_once_they_are_fetched() { let world = World::two_accounts(); world .history .fail_mastery_with(Some(SourceError::InvalidKey)); let tracker = world.tracker(); // The Matches are synced, the points never fetched. world.round(&tracker); assert_eq!(points_of(&tracker, SELF_PUUID, 222), None); world.history.fail_mastery_with(None); tracker.refresh(&mut |_| {}).unwrap(); assert_eq!(points_of(&tracker, SELF_PUUID, 222), Some(154_320)); // Missing from the last list: known, no points. world.history.set_mastery(SELF_PUUID, &[(51, 40)]); tracker.refresh(&mut |_| {}).unwrap(); assert_eq!(points_of(&tracker, SELF_PUUID, 222), Some(0)); }