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

200 lines
7.8 KiB
Rust

use super::*;
#[test]
fn arena_champ_select_replays_bans_hovers_and_lock_in_for_the_current_challenge() {
use serde_json::Value;
fn assert_pick(
pick: &ChampSelect,
rows: &[ChampionRow],
current: Option<(u32, ChampionState)>,
remaining: &[u32],
) {
assert_eq!(pick.anvil_champions, Some(vec![233, 64, 5, 16, 68]));
let row_of = |id: u32| rows.iter().find(|r| r.champion.id == id);
assert_eq!(
pick.current
.and_then(row_of)
.map(|r| (r.champion.id, r.attempts, r.state)),
current.map(|(id, state)| (id, Some(1), Some(state)))
);
// Pickable and not Completed: the player's own hovered or locked champion stays in
// while not Completed.
let mut ids: Vec<_> = rows
.iter()
.filter(|r| {
r.state.is_some_and(|s| s != ChampionState::Completed)
&& pick.can_pick(r.champion.id)
})
.map(|r| r.champion.id)
.collect();
ids.sort_unstable();
assert_eq!(ids, remaining);
}
// The reasons the champ select gives for the champions of the capture. The anvil champions
// 64, 5 and 68 are not in the client's pickable list. The player's own pick is never
// unpickable, whatever the bans and allies say.
fn assert_reasons(pick: &ChampSelect, banned: &[u32], taken: &[u32]) {
for id in [12, 16, 25, 50, 64, 5, 68, 103, 233] {
let expected = if pick.current == Some(id) {
Unpickable::default()
} else {
Unpickable {
banned: banned.contains(&id),
taken_by_ally: taken.contains(&id),
not_offered: [64, 5, 68].contains(&id),
}
};
assert_eq!(pick.unpickable(id), expected, "champion {id}");
}
}
let capture: Value =
serde_json::from_str(include_str!("../fixtures/client/arena_champ_select.json")).unwrap();
let world = World::new(
&[12, 16, 25, 50, 103, 233].map(|id| played_match(id, if id == 16 { 1 } else { 4 }, now())),
);
let tracker = world.tracker();
// The fake replays the captured raw responses through the same reader as the live client.
let set_frame = |frame: usize| {
world.client.set_champ_select(RawChampSelect {
pickable: capture["pickableChampionIds"].to_string(),
disabled: capture["disabledChampionIds"].to_string(),
anvil: Some(capture["crowdFavoriteChampionIds"].to_string()),
session: capture["sessions"][frame].to_string(),
});
};
set_frame(0);
world.client.set_phase("ChampSelect", false);
round(&tracker);
assert_eq!(shown(&tracker), Ok(None));
// The captured gameflow body goes through the live client's parser too.
world.client.set_gameflow(&capture["gameflow"].to_string());
let rows = tracker
.statistics(
SELF_PUUID,
&Scope {
season: SeasonScope::Season(S2.into()),
format: None,
},
)
.unwrap()
.rows;
// The first read fetches the session and the champion lists once.
let (account, pick) = shown(&tracker).unwrap().unwrap();
assert_eq!(account, "Joueur#EUW");
assert_pick(&pick, &rows, None, &[12, 25, 50, 103, 233]);
assert_reasons(&pick, &[], &[]);
assert_eq!(world.client.champ_select_reads(), (1, 1));
// Every later change arrives as the client's session event: neither the session nor the
// lists are read again.
// The ban of 12 is completed in frame 1 and the ban of 50 in frame 2. The ally's pre-pick of
// 25 (frame 1) leaves it as it is; the confirmed pick (frame 3) takes it.
for (frame, current, remaining, banned, taken) in [
(1, None, vec![25, 50, 103, 233], vec![12], vec![]),
(
2,
Some((233, ChampionState::InProgress)),
vec![25, 103, 233],
vec![12, 50],
vec![],
),
(
3,
Some((16, ChampionState::Completed)),
vec![103, 233],
vec![12, 50],
vec![25],
),
(
4,
Some((16, ChampionState::Completed)),
vec![103, 233],
vec![12, 50],
vec![25],
),
] {
world
.client
.send_champ_select_event(&capture["sessions"][frame].to_string());
assert_eq!(tracker.wait_for_client(), ClientEvent::ChampSelect);
let (_, pick) = shown(&tracker).unwrap().unwrap();
assert_pick(&pick, &rows, current, &remaining);
assert_reasons(&pick, &banned, &taken);
assert_eq!(world.client.champ_select_reads(), (1, 1));
}
// Leaving the champ select ends it and drops its lists; the next one reads them again.
world.client.set_phase("InProgress", true);
assert_eq!(shown(&tracker), Ok(None));
set_frame(0);
world.client.set_gameflow(&capture["gameflow"].to_string());
// Bans and ally picks expired with the first champ select.
let (_, pick) = shown(&tracker).unwrap().unwrap();
assert_reasons(&pick, &[], &[]);
assert_eq!(world.client.champ_select_reads(), (2, 2));
}
#[test]
fn the_anvil_list_is_unknown_when_the_client_fails_to_give_it_and_empty_when_it_says_so() {
use serde_json::Value;
let capture: Value =
serde_json::from_str(include_str!("../fixtures/client/arena_champ_select.json")).unwrap();
let anvil_champions = |anvil: Option<&str>| {
let world = World::new(&[]);
let tracker = world.tracker();
world.client.set_champ_select(RawChampSelect {
pickable: capture["pickableChampionIds"].to_string(),
disabled: capture["disabledChampionIds"].to_string(),
anvil: anvil.map(str::to_string),
session: capture["sessions"][0].to_string(),
});
world.client.set_gameflow(&capture["gameflow"].to_string());
shown(&tracker).unwrap().unwrap().1.anvil_champions
};
assert_eq!(anvil_champions(None), None);
assert_eq!(anvil_champions(Some("not json")), None);
assert_eq!(anvil_champions(Some("[]")), Some(vec![]));
assert_eq!(anvil_champions(Some("[3,1,2]")), Some(vec![3, 1, 2]));
}
#[test]
fn a_failed_champ_select_read_keeps_the_pick_until_the_champ_select_really_ends() {
use serde_json::Value;
let capture: Value =
serde_json::from_str(include_str!("../fixtures/client/arena_champ_select.json")).unwrap();
let world = World::new(&[]);
let tracker = world.tracker();
world.client.set_champ_select(RawChampSelect {
pickable: capture["pickableChampionIds"].to_string(),
disabled: capture["disabledChampionIds"].to_string(),
anvil: Some(capture["crowdFavoriteChampionIds"].to_string()),
session: capture["sessions"][0].to_string(),
});
world.client.set_gameflow(&capture["gameflow"].to_string());
let live = shown(&tracker).unwrap();
assert!(live.is_some());
// A failed gameflow or session request is not an answer that the champ select is over.
world
.client
.fail_gameflow(Some(ClientError::Other("HTTP 500".into())));
assert_eq!(shown(&tracker), Err(ClientError::Other("HTTP 500".into())));
world.client.fail_gameflow(None);
world
.client
.fail_champ_select(Some(ClientError::Other("HTTP 500".into())));
assert_eq!(shown(&tracker), Err(ClientError::Other("HTTP 500".into())));
world.client.fail_champ_select(None);
assert_eq!(shown(&tracker).unwrap(), live);
// Leaving champ select, or the client going away, does end it.
world.client.set_phase("InProgress", true);
assert_eq!(shown(&tracker), Ok(None));
world.client.sign_out();
assert_eq!(shown(&tracker), Ok(None));
}