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>
249 lines
7.6 KiB
Rust
249 lines
7.6 KiB
Rust
mod common;
|
|
|
|
use std::sync::atomic::Ordering;
|
|
|
|
use arena_tracker_core::{
|
|
ChampionState, Error, Scope, SeasonScope, Statistics, Tracker, default_seasons, load_seasons,
|
|
};
|
|
use common::{
|
|
CATALOG, FakeChampions, FakeHistory, SELF_PUUID, jan_2026, open_tracker, played_match,
|
|
recorded_account,
|
|
};
|
|
|
|
fn statistics(tracker: &Tracker) -> Statistics {
|
|
let scope = Scope {
|
|
season: SeasonScope::Season(common::S1.into()),
|
|
format: None,
|
|
};
|
|
tracker.statistics(SELF_PUUID, &scope).unwrap()
|
|
}
|
|
|
|
fn row_names(stats: &Statistics) -> Vec<&str> {
|
|
stats
|
|
.rows
|
|
.iter()
|
|
.map(|r| r.champion.name.as_str())
|
|
.collect()
|
|
}
|
|
|
|
/// A tracker on the same database file as `open_tracker`, whose history is empty.
|
|
fn reopen(dir: &std::path::Path, champions: FakeChampions) -> Tracker {
|
|
Tracker::open(
|
|
&dir.join("arena.db"),
|
|
Box::new(FakeHistory::new(recorded_account(), vec![])),
|
|
)
|
|
.unwrap()
|
|
.with_seasons(common::seasons())
|
|
.with_champions(Box::new(champions), dir.join("portraits"))
|
|
}
|
|
|
|
#[test]
|
|
fn the_catalog_is_fetched_once_while_the_game_version_is_unchanged() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let first = FakeChampions::new("1.0", &CATALOG);
|
|
let first_calls = first.catalog_counter();
|
|
let tracker = reopen(dir.path(), first);
|
|
|
|
tracker.refresh_champions().unwrap();
|
|
tracker.refresh_champions().unwrap();
|
|
|
|
assert_eq!(first_calls.load(Ordering::SeqCst), 1);
|
|
assert_eq!(statistics(&tracker).challenge.unwrap().total, 6);
|
|
drop(tracker);
|
|
|
|
let same_version = FakeChampions::new("1.0", &CATALOG);
|
|
let same_calls = same_version.catalog_counter();
|
|
reopen(dir.path(), same_version)
|
|
.refresh_champions()
|
|
.unwrap();
|
|
assert_eq!(same_calls.load(Ordering::SeqCst), 0);
|
|
|
|
let mut grown = CATALOG.to_vec();
|
|
grown.push((777, "Newcomer", "Nouveau"));
|
|
let new_version = FakeChampions::new("2.0", &grown);
|
|
let new_calls = new_version.catalog_counter();
|
|
let tracker = reopen(dir.path(), new_version);
|
|
tracker.refresh_champions().unwrap();
|
|
assert_eq!(new_calls.load(Ordering::SeqCst), 1);
|
|
assert_eq!(statistics(&tracker).challenge.unwrap().total, 7);
|
|
}
|
|
|
|
#[test]
|
|
fn a_failing_source_leaves_the_cached_catalog_usable() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
reopen(dir.path(), FakeChampions::new("1.0", &CATALOG))
|
|
.refresh_champions()
|
|
.unwrap();
|
|
|
|
let offline = reopen(dir.path(), FakeChampions::new("2.0", &CATALOG).failing());
|
|
|
|
assert!(matches!(offline.refresh_champions(), Err(Error::Source(_))));
|
|
assert_eq!(statistics(&offline).challenge.unwrap().total, 6);
|
|
}
|
|
|
|
#[test]
|
|
fn without_any_catalog_the_page_shows_the_champions_played() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let tracker = open_tracker(dir.path(), &[played_match(1, 3, jan_2026(2, 0))]).with_champions(
|
|
Box::new(FakeChampions::new("1.0", &CATALOG).failing()),
|
|
dir.path().join("portraits"),
|
|
);
|
|
assert!(tracker.refresh_champions().is_err());
|
|
tracker.sync(common::RIOT_ID, &mut |_| {}).unwrap();
|
|
|
|
let stats = statistics(&tracker);
|
|
|
|
assert_eq!(row_names(&stats), ["Annie"]);
|
|
assert_eq!(stats.challenge.unwrap().total, 1);
|
|
}
|
|
|
|
#[test]
|
|
fn a_champion_played_but_absent_from_the_catalog_still_gets_a_row() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let tracker = open_tracker(dir.path(), &[played_match(1, 3, jan_2026(2, 0))]).with_champions(
|
|
Box::new(FakeChampions::new("1.0", &CATALOG[1..])),
|
|
dir.path().join("portraits"),
|
|
);
|
|
tracker.refresh_champions().unwrap();
|
|
tracker.sync(common::RIOT_ID, &mut |_| {}).unwrap();
|
|
|
|
let stats = statistics(&tracker);
|
|
|
|
assert_eq!(stats.rows.len(), 6);
|
|
let annie = stats.rows.iter().find(|r| r.champion.id == 1).unwrap();
|
|
assert_eq!(
|
|
(annie.champion.key.as_str(), annie.champion.name.as_str()),
|
|
("Annie", "Annie")
|
|
);
|
|
assert_eq!(annie.state, Some(ChampionState::InProgress));
|
|
assert_eq!(stats.challenge.unwrap().total, 6);
|
|
}
|
|
|
|
#[test]
|
|
fn rows_are_ordered_by_champion_name_in_french_order() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let champions = [
|
|
(1, "Zoe", "Zoé"),
|
|
(2, "Zed", "Zed"),
|
|
(11, "MasterYi", "Maître Yi"),
|
|
(3, "Nami", "Nami"),
|
|
];
|
|
let tracker = reopen(dir.path(), FakeChampions::new("1.0", &champions));
|
|
tracker.refresh_champions().unwrap();
|
|
|
|
assert_eq!(
|
|
row_names(&statistics(&tracker)),
|
|
["Maître Yi", "Nami", "Zed", "Zoé"]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn portraits_are_downloaded_and_exposed_and_a_failed_one_is_left_out() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let tracker = reopen(
|
|
dir.path(),
|
|
FakeChampions::new("1.0", &CATALOG).without_portrait("Jinx"),
|
|
);
|
|
|
|
assert!(
|
|
statistics(&tracker)
|
|
.rows
|
|
.iter()
|
|
.all(|r| r.portrait.is_none())
|
|
);
|
|
tracker.refresh_champions().unwrap();
|
|
|
|
let stats = statistics(&tracker);
|
|
let portraits = dir.path().join("portraits");
|
|
for row in &stats.rows {
|
|
if row.champion.key == "Jinx" {
|
|
assert_eq!(row.portrait, None);
|
|
} else {
|
|
let path = portraits.join(format!("{}.png", row.champion.key));
|
|
assert_eq!(row.portrait.as_ref(), Some(&path));
|
|
assert_eq!(
|
|
std::fs::read(&path).unwrap(),
|
|
format!("png of {}", row.champion.key).into_bytes()
|
|
);
|
|
}
|
|
}
|
|
assert!(!portraits.join("Jinx.part").exists());
|
|
}
|
|
|
|
#[test]
|
|
fn refreshing_without_a_champion_source_does_nothing() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let tracker = open_tracker(dir.path(), &[]);
|
|
|
|
tracker.refresh_champions().unwrap();
|
|
|
|
assert!(!dir.path().join("portraits").exists());
|
|
}
|
|
|
|
#[test]
|
|
fn load_seasons_writes_the_shipped_table_when_the_file_is_missing() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("sub").join("seasons.toml");
|
|
|
|
let seasons = load_seasons(&path).unwrap();
|
|
|
|
assert_eq!(seasons, default_seasons());
|
|
assert!(
|
|
std::fs::read_to_string(&path)
|
|
.unwrap()
|
|
.contains("[[season]]")
|
|
);
|
|
assert_eq!(load_seasons(&path).unwrap(), seasons);
|
|
}
|
|
|
|
#[test]
|
|
fn load_seasons_reads_the_edited_file_sorted_by_start() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("seasons.toml");
|
|
std::fs::write(
|
|
&path,
|
|
"[[season]]\nname = \"Plus tard\"\nstart = 2027-02-01\nofficial_track = \"Piste X\"\n\n[[season]]\nname = \"Avant\"\nstart = 2026-12-31\n",
|
|
)
|
|
.unwrap();
|
|
|
|
let seasons = load_seasons(&path).unwrap();
|
|
|
|
let summary: Vec<_> = seasons
|
|
.iter()
|
|
.map(|s| (s.name.as_str(), s.start.to_string()))
|
|
.collect();
|
|
assert_eq!(
|
|
summary,
|
|
[
|
|
("Avant", "31/12/2026".to_string()),
|
|
("Plus tard", "01/02/2027".to_string())
|
|
]
|
|
);
|
|
let tracks: Vec<_> = seasons
|
|
.iter()
|
|
.map(|s| s.official_track.as_deref())
|
|
.collect();
|
|
assert_eq!(tracks, [None, Some("Piste X")]);
|
|
}
|
|
|
|
#[test]
|
|
fn load_seasons_rejects_invalid_files() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("seasons.toml");
|
|
for bad in [
|
|
"pas du toml",
|
|
"",
|
|
"[[season]]\nname = \"X\"\nstart = 2026-01-01T10:00:00\n",
|
|
"[[season]]\nname = \"X\"\nstart = 2026-01-01T10:00:00Z\n",
|
|
"[[season]]\nname = \"X\"\nstart = \"2026-01-01\"\n",
|
|
"[[season]]\nname = \"X\"\n",
|
|
"[[season]]\nstart = 2026-01-01\n",
|
|
] {
|
|
std::fs::write(&path, bad).unwrap();
|
|
assert!(
|
|
matches!(load_seasons(&path), Err(Error::Config(_))),
|
|
"{bad:?}"
|
|
);
|
|
}
|
|
}
|