Add ChampionSource::card_portrait (Data Dragon loading art) and Tracker::refresh_card_portraits, which downloads the missing ones, crops them to the card's 0.68 aspect at the mockup's 24 % focus, shrinks them to 308 x 453 px and saves them as JPEG in portraits/cards via a .part file. ChampionRow gains card_portrait. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
863 lines
27 KiB
Rust
863 lines
27 KiB
Rust
mod common;
|
|
|
|
use arena_tracker_core::{
|
|
Account, Champion, ChampionChallenge, ChampionRow, ChampionState, Column, Error, Format, Marks,
|
|
Scope, Season, SeasonScope, StateFilter, Statistics, Tracker, sort_rows,
|
|
};
|
|
use common::jan_2026 as jan;
|
|
use common::{
|
|
CATALOG, FakeChampions, FakeHistory, RIOT_ID, S1, S2, SELF_PUUID, at, date, imported_tracker,
|
|
payload, payloads, played_by, 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(¤t, 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,
|
|
card_portrait: None,
|
|
matches: 0,
|
|
best_placement: None,
|
|
top1: 0,
|
|
wins: 0,
|
|
average_placement: None,
|
|
last_match_ms: None,
|
|
attempts: None,
|
|
state: None,
|
|
challenge: 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));
|
|
}
|
|
|
|
const NO_MARKS: Marks = Marks {
|
|
played: false,
|
|
won: false,
|
|
completed: false,
|
|
};
|
|
|
|
fn challenge(stats: &Statistics, champion_id: u32) -> ChampionChallenge {
|
|
row(stats, champion_id).challenge.unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn a_2nd_place_is_a_win_that_leaves_the_champion_remaining() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let tracker = imported_tracker(dir.path(), &[played_match(11, 2, jan(2, 0))]);
|
|
|
|
let stats = stats(&tracker, S1, None);
|
|
|
|
let yi = row(&stats, 11);
|
|
assert_eq!(yi.state, Some(ChampionState::InProgress));
|
|
let challenge = yi.challenge.unwrap();
|
|
assert_eq!(
|
|
(challenge.matches, challenge.wins, challenge.best_placement),
|
|
(1, 1, Some(2))
|
|
);
|
|
assert_eq!(challenge.completed_on, None);
|
|
assert_eq!(
|
|
challenge.marks(),
|
|
Marks {
|
|
played: true,
|
|
won: true,
|
|
completed: false
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_single_top_1_earns_the_three_marks() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let tracker = imported_tracker(dir.path(), &[played_match(11, 1, jan(2, 5))]);
|
|
|
|
let stats = stats(&tracker, S1, None);
|
|
|
|
let yi = row(&stats, 11);
|
|
assert_eq!(yi.state, Some(ChampionState::Completed));
|
|
assert_eq!(yi.attempts, Some(1));
|
|
let challenge = yi.challenge.unwrap();
|
|
assert_eq!(challenge.completed_on, Some(date(2026, 1, 2)));
|
|
assert_eq!(
|
|
challenge.marks(),
|
|
Marks {
|
|
played: true,
|
|
won: true,
|
|
completed: true
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_played_champion_without_a_win_has_only_the_first_mark() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let tracker = imported_tracker(dir.path(), &run(11, &[5, 4], jan(2, 0)));
|
|
|
|
let stats = stats(&tracker, S1, None);
|
|
|
|
let challenge = challenge(&stats, 11);
|
|
assert_eq!((challenge.matches, challenge.wins), (2, 0));
|
|
assert_eq!(challenge.best_placement, Some(4));
|
|
assert_eq!(
|
|
challenge.marks(),
|
|
Marks {
|
|
played: true,
|
|
won: false,
|
|
completed: false
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_never_played_champion_has_a_zeroed_challenge_and_no_marks() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let tracker = imported_tracker(dir.path(), &run(11, &[1], jan(2, 0)));
|
|
|
|
let stats = stats(&tracker, S1, None);
|
|
|
|
let unplayed = challenge(&stats, 8);
|
|
assert_eq!(
|
|
unplayed,
|
|
ChampionChallenge {
|
|
matches: 0,
|
|
wins: 0,
|
|
best_placement: None,
|
|
completed_on: None
|
|
}
|
|
);
|
|
assert_eq!(unplayed.marks(), NO_MARKS);
|
|
}
|
|
|
|
#[test]
|
|
fn the_all_seasons_view_has_no_champion_challenge() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let tracker = imported_tracker(dir.path(), &run(11, &[1], jan(2, 0)));
|
|
|
|
let stats = all_seasons(&tracker);
|
|
|
|
assert!(stats.rows.iter().all(|r| r.challenge.is_none()));
|
|
}
|
|
|
|
#[test]
|
|
fn the_format_filter_changes_the_columns_but_not_the_champion_challenge() {
|
|
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)),
|
|
played_match(222, 3, jan(2, 4)).in_format(1740),
|
|
];
|
|
let tracker = imported_tracker(dir.path(), &matches);
|
|
|
|
let everything = stats(&tracker, S1, None);
|
|
let only_3x6 = stats(&tracker, S1, Some(Format::from_queue(1750).unwrap()));
|
|
|
|
let challenges =
|
|
|stats: &Statistics| -> Vec<_> { stats.rows.iter().map(|r| r.challenge).collect() };
|
|
assert_eq!(challenges(&everything), challenges(&only_3x6));
|
|
let yi = challenge(&only_3x6, 11);
|
|
assert_eq!((yi.matches, yi.wins, yi.best_placement), (3, 2, Some(1)));
|
|
assert_eq!(yi.completed_on, Some(date(2026, 1, 2)));
|
|
assert_eq!(challenge(&only_3x6, 222).matches, 1);
|
|
// The Format-scoped columns do change.
|
|
assert_eq!(row(&everything, 11).matches, 3);
|
|
assert_eq!(row(&only_3x6, 11).matches, 2);
|
|
assert_eq!(row(&only_3x6, 11).best_placement, Some(2));
|
|
assert_eq!(row(&only_3x6, 222).matches, 0);
|
|
}
|
|
|
|
#[test]
|
|
fn the_champion_challenge_follows_the_season() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let matches = [
|
|
run(11, &[1, 2], jan(2, 0)),
|
|
run(11, &[6], at(date(2026, 4, 2), 0)),
|
|
]
|
|
.concat();
|
|
let tracker = imported_tracker(dir.path(), &matches);
|
|
|
|
let past = challenge(&stats(&tracker, S1, None), 11);
|
|
let current = challenge(&stats(&tracker, S2, None), 11);
|
|
|
|
assert_eq!((past.matches, past.wins), (2, 2));
|
|
assert_eq!(past.completed_on, Some(date(2026, 1, 2)));
|
|
assert_eq!(
|
|
current,
|
|
ChampionChallenge {
|
|
matches: 1,
|
|
wins: 0,
|
|
best_placement: Some(6),
|
|
completed_on: None
|
|
}
|
|
);
|
|
assert!(!current.marks().won);
|
|
}
|
|
|
|
#[test]
|
|
fn the_champion_challenge_follows_the_account() {
|
|
const OTHER_PUUID: &str = "fixture-puuid-other";
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let other = Account {
|
|
puuid: OTHER_PUUID.into(),
|
|
game_name: "Autre".into(),
|
|
tag_line: "FIX".into(),
|
|
};
|
|
let theirs = played_by(
|
|
&payload("EUW1_theirs", 1750, 11, "MasterYi", 1, true, jan(3, 0)),
|
|
OTHER_PUUID,
|
|
);
|
|
let history = FakeHistory::new(
|
|
recorded_account(),
|
|
payloads(&[played_match(11, 4, jan(2, 0))]),
|
|
)
|
|
.with_account(other, vec![theirs]);
|
|
let tracker = Tracker::open(&dir.path().join("arena.db"), Box::new(history))
|
|
.unwrap()
|
|
.with_seasons(seasons())
|
|
.with_champions(
|
|
Box::new(FakeChampions::new("1.0", &CATALOG)),
|
|
dir.path().join("portraits"),
|
|
);
|
|
tracker.refresh_champions().unwrap();
|
|
tracker.sync(RIOT_ID, &mut |_| {}).unwrap();
|
|
tracker.sync("Autre#FIX", &mut |_| {}).unwrap();
|
|
let scope = Scope {
|
|
season: SeasonScope::Season(S1.into()),
|
|
format: None,
|
|
};
|
|
|
|
let mine = tracker.statistics(SELF_PUUID, &scope).unwrap();
|
|
let theirs = tracker.statistics(OTHER_PUUID, &scope).unwrap();
|
|
|
|
let mine = challenge(&mine, 11);
|
|
assert_eq!((mine.matches, mine.wins, mine.completed_on), (1, 0, None));
|
|
let theirs = challenge(&theirs, 11);
|
|
assert_eq!(theirs.matches, 1);
|
|
assert_eq!(theirs.completed_on, Some(date(2026, 1, 3)));
|
|
}
|
|
|
|
#[test]
|
|
fn two_top_1_imported_out_of_order_keep_the_earlier_day_and_one_completion() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
// Riot hands the newest Match over first: the later top 1 is stored before everything else.
|
|
let matches = [
|
|
played_match(11, 1, jan(9, 0)),
|
|
played_match(11, 1, jan(5, 22)),
|
|
played_match(11, 3, jan(4, 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, Some(ChampionState::Completed));
|
|
assert_eq!(yi.attempts, Some(2)); // the Win, then the first top 1
|
|
let challenge = yi.challenge.unwrap();
|
|
assert_eq!(challenge.completed_on, Some(date(2026, 1, 5)));
|
|
assert_eq!((challenge.matches, challenge.wins), (3, 3));
|
|
let summary = stats.challenge.unwrap();
|
|
assert_eq!((summary.completed, summary.attempts), (1, 2));
|
|
assert_eq!(stats.charts.completed_over_time, [(date(2026, 1, 5), 1)]);
|
|
}
|
|
|
|
#[test]
|
|
fn the_first_top_1_day_matches_the_progress_curve() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
// The first top 1 of Maitre Yi is late in the evening: the day convention must agree.
|
|
let matches = [
|
|
run(1, &[1], jan(2, 1)),
|
|
run(11, &[4, 1, 1], jan(5, 23)),
|
|
run(222, &[1], jan(7, 0)),
|
|
]
|
|
.concat();
|
|
let tracker = imported_tracker(dir.path(), &matches);
|
|
|
|
let stats = stats(&tracker, S1, None);
|
|
|
|
let mut days: Vec<_> = stats
|
|
.rows
|
|
.iter()
|
|
.filter_map(|r| r.challenge?.completed_on)
|
|
.collect();
|
|
days.sort();
|
|
let curve: Vec<_> = stats
|
|
.charts
|
|
.completed_over_time
|
|
.iter()
|
|
.map(|(d, _)| *d)
|
|
.collect();
|
|
assert_eq!(days, curve);
|
|
assert_eq!(challenge(&stats, 11).completed_on, Some(date(2026, 1, 6)));
|
|
}
|