Files
arena-tracker/crates/core/tests/statistics.rs
T
nessarandClaude Fable 5.2 c44cf52a6a Add core: Match import, statistics and game client following
Core half of tickets #4 to #12, behind one public interface tested with
fake sources and a real SQLite database:

- import of Arena Match history from the Riot API, idempotent and
  resumable, with a sync mark written only when a sync completes
- Season table, champion catalog, Challenge summary, champion rows,
  chart series, Season and Format scopes
- game client port and adapter: signed-in Account, end of an Arena
  game fetched by its id, Official Progress cross-check
- update problems reported for banners while stored data stays readable

The app crate is adapted in the next commit.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 19:06:52 +01:00

596 lines
19 KiB
Rust

mod common;
use arena_tracker_core::{
Champion, ChampionRow, ChampionState, Column, Error, Format, Scope, Season, SeasonScope,
StateFilter, Statistics, Tracker, sort_rows,
};
use common::jan_2026 as jan;
use common::{
CATALOG, FakeHistory, RIOT_ID, S1, S2, SELF_PUUID, at, date, imported_tracker, payloads,
played_match, recorded_account, run, seasons,
};
use std::sync::atomic::Ordering;
fn stats(tracker: &Tracker, season: &str, format: Option<Format>) -> Statistics {
let scope = Scope {
season: SeasonScope::Season(season.into()),
format,
};
tracker.statistics(SELF_PUUID, &scope).unwrap()
}
fn all_seasons(tracker: &Tracker) -> Statistics {
let scope = Scope {
season: SeasonScope::AllSeasons,
format: None,
};
tracker.statistics(SELF_PUUID, &scope).unwrap()
}
fn row(stats: &Statistics, champion_id: u32) -> &ChampionRow {
stats
.rows
.iter()
.find(|r| r.champion.id == champion_id)
.unwrap()
}
#[test]
fn attempts_run_up_to_the_first_top_1_and_no_further() {
let dir = tempfile::tempdir().unwrap();
let tracker = imported_tracker(dir.path(), &run(11, &[5, 4, 1, 3], jan(2, 0)));
let stats = stats(&tracker, S1, None);
let yi = row(&stats, 11);
assert_eq!(yi.state, Some(ChampionState::Completed));
assert_eq!(yi.attempts, Some(3));
assert_eq!(yi.matches, 4);
let challenge = stats.challenge.unwrap();
assert_eq!((challenge.completed, challenge.first_tries), (1, 0));
assert_eq!(challenge.attempts, 3);
}
#[test]
fn champion_states() {
let dir = tempfile::tempdir().unwrap();
let matches = [run(222, &[1], jan(2, 0)), run(103, &[3, 5], jan(2, 0))].concat();
let tracker = imported_tracker(dir.path(), &matches);
let stats = stats(&tracker, S1, None);
let first_try = row(&stats, 222);
assert_eq!(first_try.state, Some(ChampionState::Completed));
assert_eq!(first_try.attempts, Some(1));
let in_progress = row(&stats, 103);
assert_eq!(in_progress.state, Some(ChampionState::InProgress));
assert_eq!(in_progress.attempts, Some(2));
let never_played = row(&stats, 8);
assert_eq!(never_played.state, Some(ChampionState::NeverPlayed));
assert_eq!(never_played.attempts, Some(0));
assert_eq!(stats.challenge.unwrap().first_tries, 1);
}
#[test]
fn challenge_totals_count_every_attempt_but_average_only_completed_champions() {
let dir = tempfile::tempdir().unwrap();
let matches = [
run(11, &[5, 4, 1, 3], jan(2, 0)), // Completed in 3
run(222, &[1], jan(3, 0)), // First Try
run(103, &[3, 5], jan(4, 0)), // in progress, 2 Attempts so far
]
.concat();
let tracker = imported_tracker(dir.path(), &matches);
let challenge = stats(&tracker, S1, None).challenge.unwrap();
assert_eq!(challenge.completed, 2);
assert_eq!(challenge.total, CATALOG.len());
assert_eq!(challenge.attempts, 3 + 1 + 2);
assert_eq!(challenge.average_attempts, Some(2.0));
assert_eq!(challenge.first_tries, 1);
}
#[test]
fn average_attempts_is_none_when_nothing_is_completed() {
let dir = tempfile::tempdir().unwrap();
let tracker = imported_tracker(dir.path(), &run(103, &[3, 5], jan(2, 0)));
let challenge = stats(&tracker, S1, None).challenge.unwrap();
assert_eq!(challenge.completed, 0);
assert_eq!(challenge.average_attempts, None);
assert_eq!(challenge.attempts, 2);
}
#[test]
fn a_top_1_in_any_format_completes_the_champion() {
let dir = tempfile::tempdir().unwrap();
let matches = [
played_match(11, 5, jan(2, 0)).in_format(1740),
played_match(11, 1, jan(2, 1)).in_format(1700),
]
.to_vec();
let tracker = imported_tracker(dir.path(), &matches);
let stats = stats(&tracker, S1, Some(Format::BraveryArena));
// Only the Bravery Arena Match is in scope, yet the Challenge still sees both.
assert_eq!(row(&stats, 11).matches, 1);
assert_eq!(row(&stats, 11).state, Some(ChampionState::Completed));
assert_eq!(row(&stats, 11).attempts, Some(2));
assert_eq!(stats.challenge.unwrap().completed, 1);
}
#[test]
fn the_format_filter_changes_statistics_and_never_the_challenge() {
let dir = tempfile::tempdir().unwrap();
let matches = vec![
played_match(11, 5, jan(2, 0)),
played_match(11, 1, jan(2, 1)).in_format(1740),
played_match(11, 3, jan(2, 2)),
played_match(11, 6, jan(2, 4)).in_format(1740),
played_match(222, 2, jan(3, 0)).in_format(1740),
];
let tracker = imported_tracker(dir.path(), &matches);
let everything = stats(&tracker, S1, None);
let filtered = stats(&tracker, S1, Some(Format::Arena3x6));
let (all, only) = (row(&everything, 11), row(&filtered, 11));
assert_eq!(
(all.matches, all.top1, all.wins, all.best_placement),
(4, 1, 2, Some(1))
);
assert_eq!(all.average_placement, Some(3.75));
assert_eq!(all.last_match_ms, Some(jan(2, 4)));
assert_eq!(
(only.matches, only.top1, only.wins, only.best_placement),
(2, 0, 1, Some(3))
);
assert_eq!(only.average_placement, Some(4.0));
assert_eq!(only.last_match_ms, Some(jan(2, 2)));
assert_eq!(row(&filtered, 222).matches, 0);
assert_eq!(row(&filtered, 222).last_match_ms, None);
assert_eq!(
everything.charts.placements,
[(1, 1), (2, 1), (3, 1), (4, 0), (5, 1), (6, 1)]
);
assert_eq!(
filtered.charts.placements,
[(1, 0), (2, 0), (3, 1), (4, 0), (5, 1)]
);
// Unchanged: the Challenge, every state and Attempts count, the two Challenge charts.
assert_eq!(everything.challenge, filtered.challenge);
for (a, b) in everything.rows.iter().zip(&filtered.rows) {
assert_eq!(
(a.state, a.attempts),
(b.state, b.attempts),
"{}",
a.champion.name
);
}
assert_eq!(
everything.charts.completed_over_time,
filtered.charts.completed_over_time
);
assert_eq!(
everything.charts.attempts_to_complete,
filtered.charts.attempts_to_complete
);
assert_eq!(everything.formats, [Format::Arena3x6, Format::BraveryArena]);
assert_eq!(filtered.formats, everything.formats);
}
#[test]
fn a_season_starts_at_its_first_millisecond() {
let dir = tempfile::tempdir().unwrap();
let s1_start = date(2026, 1, 1).start_ms();
let s2_start = date(2026, 4, 1).start_ms();
let matches = vec![
played_match(1, 4, s1_start - 1), // before the first Season
played_match(8, 4, s1_start), // first instant of S1
played_match(11, 4, s2_start - 1), // last millisecond of S1
played_match(19, 4, s2_start), // first instant of S2
];
let tracker = imported_tracker(dir.path(), &matches);
let in_season = |name| -> Vec<u32> {
let stats = stats(&tracker, name, None);
let mut ids: Vec<u32> = stats
.rows
.iter()
.filter(|r| r.matches > 0)
.map(|r| r.champion.id)
.collect();
ids.sort();
ids
};
assert_eq!(in_season(S1), [8, 11]);
assert_eq!(in_season(S2), [19]);
let everything = all_seasons(&tracker);
assert_eq!(everything.rows.iter().filter(|r| r.matches > 0).count(), 4);
assert_eq!(row(&everything, 1).matches, 1);
}
#[test]
fn each_season_has_its_own_challenge_and_the_current_one_is_found_by_date() {
let dir = tempfile::tempdir().unwrap();
let matches = [
run(11, &[1], jan(2, 0)),
run(11, &[4], at(date(2026, 4, 2), 0)),
]
.concat();
let tracker = imported_tracker(dir.path(), &matches);
let past = stats(&tracker, S1, None);
let current = stats(&tracker, S2, None);
assert_eq!(past.challenge.unwrap().completed, 1);
let challenge = current.challenge.clone().unwrap();
assert_eq!((challenge.completed, challenge.attempts), (0, 1));
assert_eq!(row(&current, 11).state, Some(ChampionState::InProgress));
let name = |ms| tracker.current_season(ms).map(|s| s.name);
assert_eq!(name(date(2026, 1, 1).start_ms() - 1), None);
assert_eq!(name(date(2026, 1, 1).start_ms()), Some(S1.into()));
assert_eq!(name(date(2026, 4, 1).start_ms() - 1), Some(S1.into()));
assert_eq!(name(date(2026, 4, 1).start_ms()), Some(S2.into()));
assert_eq!(name(date(2100, 1, 1).start_ms()), Some(S2.into()));
assert_eq!(tracker.seasons(), seasons());
}
#[test]
fn the_all_seasons_view_has_no_challenge() {
let dir = tempfile::tempdir().unwrap();
let matches = [
run(11, &[1], jan(2, 0)),
run(11, &[4], at(date(2026, 4, 2), 0)),
run(222, &[3], at(date(2025, 6, 1), 0)),
]
.concat();
let tracker = imported_tracker(dir.path(), &matches);
let stats = all_seasons(&tracker);
assert_eq!(stats.challenge, None);
let yi = row(&stats, 11);
assert_eq!((yi.matches, yi.top1, yi.best_placement), (2, 1, Some(1)));
assert_eq!(yi.average_placement, Some(2.5));
assert_eq!((yi.attempts, yi.state), (None, None));
assert_eq!(
(row(&stats, 222).attempts, row(&stats, 222).state),
(None, None)
);
assert_eq!(row(&stats, 8).state, Some(ChampionState::NeverPlayed));
assert_eq!(row(&stats, 8).attempts, None);
assert!(stats.charts.completed_over_time.is_empty());
assert_eq!(stats.charts.attempts_to_complete, [0; 5]);
assert_eq!(stats.charts.placements, [(1, 1), (2, 0), (3, 1), (4, 1)]);
}
#[test]
fn an_unknown_season_is_an_error() {
let dir = tempfile::tempdir().unwrap();
let tracker = imported_tracker(dir.path(), &[]);
let scope = Scope {
season: SeasonScope::Season("Saison Z".into()),
format: None,
};
let err = tracker.statistics(SELF_PUUID, &scope).unwrap_err();
assert!(matches!(err, Error::UnknownSeason(name) if name == "Saison Z"));
}
#[test]
fn an_edited_season_table_reassigns_stored_matches_without_importing_again() {
let dir = tempfile::tempdir().unwrap();
let matches = [run(11, &[4], jan(1, 1)), run(19, &[4], jan(3, 1))].concat();
drop(imported_tracker(dir.path(), &matches));
let history = FakeHistory::new(recorded_account(), payloads(&matches));
let fetches = history.fetch_counter();
let tracker = Tracker::open(&dir.path().join("arena.db"), Box::new(history))
.unwrap()
.with_seasons(vec![
Season {
name: "Début".into(),
start: date(2026, 1, 1),
official_track: None,
},
Season {
name: "Suite".into(),
start: date(2026, 1, 2),
official_track: None,
},
]);
let report = tracker.sync(RIOT_ID, &mut |_| {}).unwrap();
assert_eq!((report.added, fetches.load(Ordering::SeqCst)), (0, 0));
let played = |name| -> Vec<u32> {
stats(&tracker, name, None)
.rows
.iter()
.filter(|r| r.matches > 0)
.map(|r| r.champion.id)
.collect()
};
assert_eq!(played("Début"), [11]);
assert_eq!(played("Suite"), [19]);
assert!(
tracker
.statistics(
SELF_PUUID,
&Scope {
season: SeasonScope::Season(S1.into()),
format: None
}
)
.is_err()
);
}
#[test]
fn row_values_for_one_champion() {
let dir = tempfile::tempdir().unwrap();
let matches = vec![
played_match(11, 4, jan(2, 1)),
played_match(11, 1, jan(2, 2)).in_format(1740),
played_match(11, 2, jan(2, 3)).in_format(1700),
];
let tracker = imported_tracker(dir.path(), &matches);
let stats = stats(&tracker, S1, None);
let yi = row(&stats, 11);
assert_eq!(
yi.champion,
Champion {
id: 11,
key: "MasterYi".into(),
name: "Maître Yi".into()
}
);
assert_eq!(yi.matches, 3);
assert_eq!(yi.best_placement, Some(1));
assert_eq!(yi.top1, 1);
assert_eq!(yi.wins, 2);
assert_eq!(yi.average_placement, Some(7.0 / 3.0));
assert_eq!(yi.last_match_ms, Some(jan(2, 3)));
let unplayed = row(&stats, 8);
assert_eq!(unplayed.matches, 0);
assert_eq!(
(
unplayed.best_placement,
unplayed.average_placement,
unplayed.last_match_ms
),
(None, None, None)
);
}
#[test]
fn the_chart_series_are_empty_without_matches() {
let dir = tempfile::tempdir().unwrap();
let tracker = imported_tracker(dir.path(), &[]);
let charts = stats(&tracker, S1, None).charts;
assert!(charts.placements.is_empty());
assert!(charts.completed_over_time.is_empty());
assert_eq!(charts.attempts_to_complete, [0; 5]);
}
#[test]
fn the_chart_series_follow_the_completions() {
let dir = tempfile::tempdir().unwrap();
let matches = [
run(1, &[1], jan(2, 1)),
run(8, &[1], jan(2, 5)),
run(11, &[4, 1], jan(5, 0)),
run(19, &[3, 3, 3, 3, 1], jan(6, 0)),
run(103, &[6, 6, 6, 6, 6, 6, 1], jan(6, 8)),
run(222, &[2, 2, 1], jan(10, 0)),
]
.concat();
let tracker = imported_tracker(dir.path(), &matches);
let charts = stats(&tracker, S1, None).charts;
assert_eq!(
charts.completed_over_time,
[
(date(2026, 1, 2), 2),
(date(2026, 1, 5), 3),
(date(2026, 1, 6), 5),
(date(2026, 1, 10), 6),
]
);
// 1 Attempt: Annie and Vladimir; 2: Maître Yi; 3: Jinx; 5 or more: Warwick and Ahri.
assert_eq!(charts.attempts_to_complete, [2, 1, 1, 0, 2]);
}
fn bare_row(name: &str, id: u32) -> ChampionRow {
ChampionRow {
champion: Champion {
id,
key: name.into(),
name: name.into(),
},
portrait: None,
matches: 0,
best_placement: None,
top1: 0,
wins: 0,
average_placement: None,
last_match_ms: None,
attempts: None,
state: None,
}
}
fn names(rows: &[ChampionRow]) -> Vec<&str> {
rows.iter().map(|r| r.champion.name.as_str()).collect()
}
#[test]
fn rows_sort_by_column_with_missing_values_last_and_ties_by_name() {
let with_attempts = |name, id, attempts| ChampionRow {
attempts,
..bare_row(name, id)
};
let mut rows = vec![
with_attempts("Bard", 3, Some(3)),
with_attempts("Zed", 4, None),
with_attempts("Annie", 1, Some(3)),
with_attempts("Ahri", 2, Some(1)),
with_attempts("Jinx", 5, Some(0)),
];
sort_rows(&mut rows, Column::Attempts, true);
assert_eq!(names(&rows), ["Annie", "Bard", "Ahri", "Jinx", "Zed"]);
sort_rows(&mut rows, Column::Attempts, false);
assert_eq!(names(&rows), ["Jinx", "Ahri", "Annie", "Bard", "Zed"]);
}
#[test]
fn rows_sort_by_name_ignoring_case_and_accents() {
let mut rows: Vec<_> = ["Zoé", "Zed", "Maître Yi", "Nami", "ahri"]
.iter()
.zip(1..)
.map(|(name, id)| bare_row(name, id))
.collect();
sort_rows(&mut rows, Column::Champion, false);
assert_eq!(names(&rows), ["ahri", "Maître Yi", "Nami", "Zed", "Zoé"]);
sort_rows(&mut rows, Column::Champion, true);
assert_eq!(names(&rows), ["Zoé", "Zed", "Nami", "Maître Yi", "ahri"]);
}
#[test]
fn rows_sort_by_numbers_and_dates() {
let rows = vec![
ChampionRow {
average_placement: Some(2.5),
last_match_ms: Some(10),
matches: 1,
..bare_row("A", 1)
},
ChampionRow {
average_placement: Some(1.5),
last_match_ms: Some(30),
matches: 3,
..bare_row("B", 2)
},
bare_row("C", 3),
];
let order = |column, descending| {
let mut sorted = rows.clone();
sort_rows(&mut sorted, column, descending);
sorted
.iter()
.map(|r| r.champion.name.clone())
.collect::<Vec<_>>()
.concat()
};
assert_eq!(order(Column::AveragePlacement, false), "BAC");
assert_eq!(order(Column::AveragePlacement, true), "ABC");
assert_eq!(order(Column::LastMatch, true), "BAC");
assert_eq!(order(Column::Matches, true), "BAC");
assert_eq!(order(Column::Wins, false), "ABC"); // all zero: by name
}
#[test]
fn state_filters_keep_the_expected_rows() {
let with_state = |state| ChampionRow {
state,
..bare_row("A", 1)
};
let completed = with_state(Some(ChampionState::Completed));
let in_progress = with_state(Some(ChampionState::InProgress));
let never = with_state(Some(ChampionState::NeverPlayed));
let unknown = with_state(None);
let kept = |filter: StateFilter| -> Vec<bool> {
[&completed, &in_progress, &never, &unknown]
.iter()
.map(|r| filter.keeps(r))
.collect()
};
assert_eq!(kept(StateFilter::All), [true, true, true, true]);
assert_eq!(kept(StateFilter::Completed), [true, false, false, false]);
assert_eq!(kept(StateFilter::Remaining), [false, true, true, false]);
assert_eq!(kept(StateFilter::NeverPlayed), [false, false, true, false]);
}
#[test]
fn open_tracker_alone_uses_the_shipped_seasons() {
let dir = tempfile::tempdir().unwrap();
let tracker = Tracker::open(
&dir.path().join("arena.db"),
Box::new(FakeHistory::new(recorded_account(), vec![])),
)
.unwrap();
assert_eq!(tracker.seasons(), arena_tracker_core::default_seasons());
}
#[test]
fn rows_sort_by_state_with_unknown_states_last() {
let with_state = |name, id, state| ChampionRow {
state,
..bare_row(name, id)
};
let mut rows = vec![
with_state("Zed", 1, Some(ChampionState::NeverPlayed)),
with_state("Bard", 2, None),
with_state("Nami", 3, Some(ChampionState::InProgress)),
with_state("Jinx", 4, Some(ChampionState::Completed)),
with_state("Ahri", 5, Some(ChampionState::NeverPlayed)),
with_state("Annie", 6, Some(ChampionState::Completed)),
];
sort_rows(&mut rows, Column::State, false);
assert_eq!(
names(&rows),
["Annie", "Jinx", "Nami", "Ahri", "Zed", "Bard"]
);
sort_rows(&mut rows, Column::State, true);
assert_eq!(
names(&rows),
["Ahri", "Zed", "Nami", "Annie", "Jinx", "Bard"]
);
}
#[test]
fn attempts_follow_start_times_not_the_order_matches_were_stored_in() {
let dir = tempfile::tempdir().unwrap();
// Riot hands the newest Match over first: the top 1 is stored before the two failures.
let matches = [
played_match(11, 1, jan(5, 0)),
played_match(11, 4, jan(3, 0)),
played_match(11, 6, jan(2, 0)),
];
let tracker = Tracker::open(
&dir.path().join("arena.db"),
Box::new(FakeHistory::new(recorded_account(), payloads(&matches)).delivered_as_given()),
)
.unwrap()
.with_seasons(seasons());
tracker.sync(RIOT_ID, &mut |_| {}).unwrap();
let stats = stats(&tracker, S1, None);
let yi = row(&stats, 11);
assert_eq!(
(yi.state, yi.attempts),
(Some(ChampionState::Completed), Some(3))
);
assert_eq!(stats.challenge.unwrap().average_attempts, Some(3.0));
}