Files
arena-tracker/crates/app/src/tests.rs
T
nessarandClaude Opus 5.5 a23e7ffbac
CI / relay (pull_request) Successful in 30s
CI / core (pull_request) Successful in 2m1s
CI / relay (push) Successful in 38s
CI / core (push) Successful in 56s
Bring the window forward at the end of a game, as at a champ select
Every end of a game the page decides on (recap opened, Challenge with the
preference off, or a game left without its block) emits MatchEndShown,
formerly RecapOpened: « Paramètres » gives way and the window is raised
above the others without taking the focus, through tray::reveal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 21:17:36 +01:00

197 lines
8.0 KiB
Rust

use std::path::Path;
use std::sync::Arc;
use arena_tracker_core::{ChampionSort, Config, Preferences, RiotApi, Tracker, UpdateChannel};
use gpui_kit::{
AppContext as _, Entity, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers, TestAppContext,
VisualTestContext, WindowOptions, point,
};
use super::{LiveMatchStarted, MatchEndShown, RootView, Screen, SettingsStore, Setup};
use crate::card_images::tests::TestDir;
use crate::title_bar::{BAR_HEIGHT, BUTTON_WIDTH};
use crate::worker::UpdateChecked;
/// The app opened on the config file at `path` (the file is read, or missing).
fn open_app(
cx: &mut TestAppContext,
path: &Path,
load: fn(&Path) -> Result<Config, arena_tracker_core::Error>,
) -> (VisualTestContext, Entity<RootView>) {
// The app starts the follower, whose threads report to the page.
cx.executor().allow_parking();
cx.update(gpui_kit::init);
let (window, root) = cx.update(|cx| {
let config = load(path).unwrap();
let api = RiotApi::new("", &config.region);
let setup = Setup {
tracker: Arc::new(Tracker::open(Path::new(":memory:"), Box::new(api)).unwrap()),
// No cache read on a thread of its own: the test scheduler only allows its own.
game_data: None,
riot_id: String::new(),
notice: None,
store: SettingsStore::new(path.to_path_buf(), config),
};
gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| {
cx.new(|cx| RootView::new(setup, false, window, cx))
})
.unwrap()
});
(VisualTestContext::from_window(window, cx), root)
}
/// A first launch: no configuration and nothing stored.
fn first_launch(cx: &mut TestAppContext) -> (VisualTestContext, Entity<RootView>) {
open_app(cx, Path::new("unused.toml"), |_| {
Config::load_or_default(Path::new(":memory:"))
})
}
#[gpui::test]
fn a_ready_update_adds_a_title_bar_button_that_opens_the_settings(cx: &mut TestAppContext) {
let (mut cx, root) = first_launch(cx);
let cx = &mut cx;
// Centre of the third action button, title-bar drag area until an update is ready.
let update = point(BUTTON_WIDTH * 2.5, BAR_HEIGHT / 2.);
cx.update(|window, cx| window.draw(cx).clear(cx));
cx.simulate_click(update, Modifiers::none());
assert!(root.read_with(cx, |root, _| root.screen == Screen::Page));
let form = root.read_with(cx, |root, _| root.form.clone());
form.update(cx, |form, cx| {
let checked = UpdateChecked {
channel: UpdateChannel::Stable,
result: Ok(Some("1.0.1".into())),
};
form.update_checked(checked, cx);
});
cx.update(|window, cx| window.draw(cx).clear(cx));
cx.simulate_click(update, Modifiers::none());
assert!(root.read_with(cx, |root, _| root.screen == Screen::Settings));
}
#[gpui::test]
async fn a_first_launch_shows_the_page_and_can_refresh_without_a_key(cx: &mut TestAppContext) {
let (mut window, root) = first_launch(cx);
assert!(root.read_with(&window, |root, _| root.screen == Screen::Page));
// Centre of the second action button.
let settings = point(BUTTON_WIDTH * 1.5, BAR_HEIGHT / 2.);
window.update(|window, cx| window.draw(cx).clear(cx));
window.simulate_click(settings, Modifiers::none());
assert!(root.read_with(&window, |root, _| root.screen == Screen::Settings));
// The follower started with the app: once its first round is in, « Actualiser » is
// available, and « Paramètres » follows it from the keyboard.
window.update(|window, cx| root.update(cx, |root, cx| root.show_page(window, cx)));
let page = root.read_with(&window, |root, _| root.page.clone());
cx.condition(&page, |page, _| !page.busy()).await;
assert!(root.read_with(&window, |root, cx| root.can_refresh(cx)));
window.update(|window, cx| window.draw(cx).clear(cx));
window.simulate_keystrokes("tab tab");
let enter =
window.update(|_, cx| Keystroke::parse_with_os("enter", cx.platform_info().os).unwrap());
window.simulate_event(KeyDownEvent {
keystroke: enter.clone(),
is_held: false,
prefer_character_input: false,
});
window.simulate_event(KeyUpEvent { keystroke: enter });
assert!(root.read_with(&window, |root, _| root.screen == Screen::Settings));
}
#[gpui::test]
fn the_chosen_sort_is_saved_with_the_preferences_and_found_again_at_the_next_launch(
cx: &mut TestAppContext,
) {
let dir = TestDir::new("sort-76");
let path = dir.path().join("config.toml");
let (mut window, root) = open_app(cx, &path, Config::load_or_default);
let page = root.read_with(&window, |root, _| root.page.clone());
assert_eq!(
page.read_with(&window, |page, _| page.champion_sort()),
None
);
page.update(&mut window, |page, cx| {
page.choose_sort(ChampionSort::Name, cx)
});
let saved = std::fs::read_to_string(&path).unwrap();
assert!(saved.contains("champion_sort = \"name\""), "{saved}");
drop((window, root, page));
// The app opened again, from the file.
let (window, root) = open_app(cx, &path, Config::load);
let page = root.read_with(&window, |root, _| root.page.clone());
page.read_with(&window, |page, _| {
assert_eq!(page.champion_sort(), Some(ChampionSort::Name));
assert_eq!(page.home_filter(), arena_tracker_core::StateFilter::All);
});
}
#[gpui::test]
fn the_end_of_a_game_closes_the_settings_without_activating_the_window(cx: &mut TestAppContext) {
let dir = TestDir::new("recap-129");
let (mut window, root) = open_app(cx, &dir.path().join("config.toml"), Config::load_or_default);
let cx = &mut window;
cx.update(|window, cx| root.update(cx, |root, cx| root.open_settings(window, cx)));
// In League: the game has the keyboard.
cx.deactivate_window();
let page = root.read_with(cx, |root, _| root.page.clone());
page.update(cx, |_, cx| cx.emit(MatchEndShown));
cx.run_until_parked();
assert!(root.read_with(cx, |root, _| root.screen == Screen::Page));
cx.update(|_, cx| assert_eq!(cx.active_window(), None, "the window was activated"));
}
#[gpui::test]
fn a_live_match_closes_the_settings_without_activating_the_window(cx: &mut TestAppContext) {
let dir = TestDir::new("live-match-109");
let (mut window, root) = open_app(cx, &dir.path().join("config.toml"), Config::load_or_default);
let cx = &mut window;
cx.update(|window, cx| root.update(cx, |root, cx| root.open_settings(window, cx)));
// In League: the game has the keyboard.
cx.deactivate_window();
let page = root.read_with(cx, |root, _| root.page.clone());
page.update(cx, |_, cx| cx.emit(LiveMatchStarted));
cx.run_until_parked();
assert!(root.read_with(cx, |root, _| root.screen == Screen::Page));
cx.update(|_, cx| assert_eq!(cx.active_window(), None, "the window was activated"));
}
#[gpui::test]
fn the_recap_preference_is_on_by_default_saved_and_followed_by_the_page(cx: &mut TestAppContext) {
let dir = TestDir::new("recap-pref-129");
let path = dir.path().join("config.toml");
let (mut window, root) = open_app(cx, &path, Config::load_or_default);
let (page, form) = root.read_with(&window, |root, _| (root.page.clone(), root.form.clone()));
assert!(page.read_with(&window, |page, _| page.opens_recap_at_match_end()));
window.update(|window, cx| {
form.update(cx, |form, cx| {
let preferences = Preferences {
open_recap_at_match_end: false,
..form.saved_preferences()
};
form.set_preferences(preferences, window, cx);
})
});
assert!(!page.read_with(&window, |page, _| page.opens_recap_at_match_end()));
assert!(
!Config::load(&path)
.unwrap()
.preferences
.open_recap_at_match_end
);
drop((window, root, page, form));
// The app opened again, from the file.
let (window, root) = open_app(cx, &path, Config::load);
let page = root.read_with(&window, |root, _| root.page.clone());
assert!(!page.read_with(&window, |page, _| page.opens_recap_at_match_end()));
}