Files
arena-tracker/crates/core/tests/follow.rs
T

209 lines
6.3 KiB
Rust

mod common;
use std::cell::Cell;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, mpsc};
use std::thread;
use std::time::{Duration, Instant};
use arena_tracker_core::{
Account, ChampSelect, ChampionRow, ChampionState, ClientError, ClientEvent, FollowedMatch,
Format, MATCH_RETRIES, MatchEnd, ProgressCheck, RETRY_EVERY_MS, Scope, SeasonScope,
SourceError, Tracker, Unpickable, UpdateProblem,
};
use common::{
FakeClient, FakeHistory, PlayedMatch, RawChampSelect, S2, S2_TRACK, SELF_PUUID, at, date,
payload, payloads, played_by, played_match, recorded_account, run, seasons,
};
const OTHER_PUUID: &str = "fixture-puuid-other";
/// A moment inside the second test Season.
fn now() -> i64 {
at(date(2026, 4, 10), 12)
}
fn other_account() -> Account {
Account {
puuid: OTHER_PUUID.into(),
game_name: "Autre".into(),
tag_line: "FIX".into(),
}
}
/// One Arena 3x6 Match of the recorded Account, placed first with Jinx.
/// A Match started `minutes` after the rounds' clock starts (`now()`): a game the client follows.
fn new_match(id: &str, minutes: i64) -> String {
payload(id, 1750, 222, "Jinx", 1, true, now() + minutes * 60_000)
}
struct World {
dir: tempfile::TempDir,
history: Arc<FakeHistory>,
client: FakeClient,
calls: Arc<AtomicUsize>,
}
impl World {
/// The recorded Account has played `matches`; the client runs and is signed in as them.
fn new(matches: &[PlayedMatch]) -> World {
World::with_history(FakeHistory::new(recorded_account(), payloads(matches)))
}
fn with_history(history: FakeHistory) -> World {
let calls = history.call_counter();
World {
dir: tempfile::tempdir().unwrap(),
history: Arc::new(history),
client: FakeClient::recorded(),
calls,
}
}
fn tracker(&self) -> Tracker {
self.open().with_client(Box::new(self.client.clone()))
}
fn open(&self) -> Tracker {
Tracker::open(
&self.dir.path().join("arena.db"),
Box::new(self.history.clone()),
)
.unwrap()
.with_seasons(seasons())
}
fn source_calls(&self) -> usize {
self.calls.load(Ordering::SeqCst)
}
}
thread_local! {
/// The time of the latest round (each test runs on its own thread).
static CLOCK: Cell<i64> = Cell::new(now());
}
/// A round `ms` after the previous one.
fn round_after(tracker: &Tracker, ms: i64) -> bool {
let now_ms = CLOCK.with(|clock| clock.replace(clock.get() + ms) + ms);
tracker.follow_client(now_ms, None, &mut |_| {})
}
/// A round one retry interval after the previous one.
fn round(tracker: &Tracker) -> bool {
round_after(tracker, RETRY_EVERY_MS)
}
/// The live champ select the page would show, read once at this instant.
fn shown(tracker: &Tracker) -> Result<Option<(String, ChampSelect)>, ClientError> {
tracker.champ_select_with(&tracker.client_snapshot())
}
fn completed(tracker: &Tracker, puuid: &str) -> usize {
let scope = Scope {
season: SeasonScope::Season(S2.into()),
format: None,
};
let stats = tracker.statistics(puuid, &scope).unwrap();
stats.challenge.unwrap().completed
}
/// A Match won with each of `n` different champions.
fn top1_matches(n: u32) -> Vec<PlayedMatch> {
(0..n)
.map(|i| played_match(1000 + i, 1, at(date(2026, 4, 2), i64::from(i))))
.collect()
}
fn s2_matches() -> Vec<PlayedMatch> {
run(11, &[4, 1], at(date(2026, 4, 2), 0))
}
/// The recorded Account and another one, who has one Match.
fn two_accounts() -> FakeHistory {
let theirs = payload(
"EUW1_theirs",
1750,
222,
"Jinx",
3,
true,
at(date(2026, 4, 3), 0),
);
FakeHistory::new(recorded_account(), payloads(&s2_matches()))
.with_account(other_account(), vec![played_by(&theirs, OTHER_PUUID)])
}
/// The Match of a game the client gave this id, as Riot has it.
fn match_of_game(game_id: u64) -> String {
let id = format!("EUW1_{game_id}");
payload(&id, 1750, 103, "Ahri", 2, true, at(date(2026, 4, 5), 2))
}
/// Eliminated early from the recorded game A, the player queues again and game B ends before
/// A's lobby has finished: no Match is published yet.
fn two_games_in_a_row(world: &World, tracker: &Tracker) -> u64 {
round(tracker);
play_arena_game(world, tracker);
round(tracker);
let game_b = common::recorded_game_id() + 1;
world.client.set_game_id(Some(game_b));
world.client.set_phase("InProgress", true);
round(tracker);
world.client.set_phase("EndOfGame", true);
game_b
}
fn failures() -> [(SourceError, UpdateProblem); 4] {
[
(SourceError::MissingKey, UpdateProblem::MissingKey),
(SourceError::InvalidKey, UpdateProblem::InvalidKey),
(SourceError::Network("coupé".into()), UpdateProblem::Network),
(
SourceError::RateLimited {
retry_after: Duration::ZERO,
},
UpdateProblem::RateLimited,
),
]
}
/// The Match of the game the recorded client session is playing, as Riot has it.
fn finished_match(queue_id: u32) -> String {
let id = format!("EUW1_{}", common::recorded_game_id());
payload(&id, queue_id, 222, "Jinx", 1, true, at(date(2026, 4, 5), 1))
}
/// Plays an Arena game up to its end: the next round sees it ended.
fn play_arena_game(world: &World, tracker: &Tracker) {
world.client.set_game_id(Some(common::recorded_game_id()));
world.client.set_phase("InProgress", true);
round(tracker);
world.client.set_phase("EndOfGame", true);
}
/// A round one retry interval after the previous one, and the followed Matches it reports.
fn reported_round(tracker: &Tracker) -> Vec<FollowedMatch> {
let now_ms = CLOCK.with(|clock| clock.replace(clock.get() + RETRY_EVERY_MS) + RETRY_EVERY_MS);
let mut events = Vec::new();
tracker.follow_client_with(
now_ms,
None,
&tracker.client_snapshot(),
&mut |_| {},
&mut |e| events.push(e),
);
events
}
#[path = "follow/accounts.rs"]
mod accounts;
#[path = "follow/champ_select.rs"]
mod champ_select;
#[path = "follow/live_matches.rs"]
mod live_matches;
#[path = "follow/pending_matches.rs"]
mod pending_matches;
#[path = "follow/sync_status.rs"]
mod sync_status;