use bevy::ecs::message::MessageUpdateSystems;
use bevy::ecs::schedule::{LogLevel, ScheduleBuildSettings, ScheduleLabel};
use bevy::prelude::*;
use crate::*;
fn strict_app() -> App {
let mut app = App::new();
app.add_plugins((MinimalPlugins, SteamKitPlugin::default()));
for label in [First.intern(), PreUpdate.intern(), Update.intern(), PostUpdate.intern(), Last.intern()] {
app.edit_schedule(label, |s| {
s.set_build_settings(ScheduleBuildSettings { ambiguity_detection: LogLevel::Error, ..default() });
});
}
app
}
fn frames(app: &mut App, n: usize) {
for _ in 0..n {
app.update();
}
}
#[test]
fn the_kit_pumps_exactly_once_per_frame() {
let fake = FakeSteamBackend::new();
let mut app = strict_app();
app.insert_resource(SteamBackendRes(Box::new(fake.clone())));
for n in 1..=5 {
app.update();
assert_eq!(fake.pump_count(), n);
}
}
#[test]
fn no_backend_no_pump_and_nothing_panics() {
let fake = FakeSteamBackend::new();
let mut app = strict_app();
frames(&mut app, 3);
assert_eq!(fake.pump_count(), 0);
app.insert_resource(SteamBackendRes(Box::new(fake.clone())));
frames(&mut app, 2);
assert_eq!(fake.pump_count(), 2);
app.world_mut().remove_resource::<SteamBackendRes>();
frames(&mut app, 2);
assert_eq!(fake.pump_count(), 2);
}
#[test]
fn the_pump_buffer_holds_only_this_frames_events() {
let fake = FakeSteamBackend::new();
let mut app = strict_app();
app.insert_resource(SteamBackendRes(Box::new(fake.clone())));
frames(&mut app, 1);
assert!(app.world().resource::<PumpedEvents>().0.is_empty());
#[cfg(feature = "lobby")]
{
fake.push_event(BackendEvent::LobbyJoinRequested { lobby: 3, from: 0 });
frames(&mut app, 1);
assert_eq!(app.world().resource::<PumpedEvents>().0, vec![BackendEvent::LobbyJoinRequested { lobby: 3, from: 0 }]);
frames(&mut app, 1);
assert!(app.world().resource::<PumpedEvents>().0.is_empty(), "replaced, never replayed");
fake.push_event(BackendEvent::LobbyJoinRequested { lobby: 4, from: 0 });
frames(&mut app, 1);
app.world_mut().remove_resource::<SteamBackendRes>();
frames(&mut app, 1);
assert!(app.world().resource::<PumpedEvents>().0.is_empty());
}
}
#[derive(Resource, Default)]
struct LostSeen(Vec<SteamLost>);
fn record_lost(mut r: MessageReader<SteamLost>, mut seen: ResMut<LostSeen>) {
seen.0.extend(r.read().cloned());
}
fn needs_backend(backend: Res<SteamBackendRes>) {
let _ = backend.0.local_id();
}
fn lost_app(fake: &FakeSteamBackend) -> App {
let mut app = strict_app();
app.insert_resource(SteamBackendRes(Box::new(fake.clone())))
.init_resource::<LostSeen>()
.add_systems(Update, (record_lost, needs_backend).before(SteamKitSystems::Requests));
app
}
#[test]
fn steam_exiting_makes_the_backend_inert_and_reports_steam_lost_once() {
let fake = FakeSteamBackend::new();
let mut app = lost_app(&fake);
frames(&mut app, 2);
fake.simulate_steam_exit();
frames(&mut app, 1);
let res = app.world().resource::<SteamBackendRes>();
assert_eq!(res.0.local_id(), fake.local_id(), "the resource stays; identity still answers");
assert!(res.0.pump().is_empty());
assert_eq!(app.world().resource::<LostSeen>().0, vec![SteamLost { reason: SteamLostReason::SteamExited }], "readable in that frame's Update");
assert!(app.world().resource::<PumpedEvents>().0.is_empty(), "the core's event is not handed to features");
frames(&mut app, 5);
assert_eq!(fake.pump_count(), 3, "never pumped again");
assert_eq!(app.world().resource::<LostSeen>().0.len(), 1);
}
#[test]
fn a_panic_in_the_pump_is_caught_and_treated_as_steam_lost() {
let fake = FakeSteamBackend::new();
let mut app = lost_app(&fake);
frames(&mut app, 1);
#[cfg(feature = "lobby")]
fake.push_event(BackendEvent::LobbyJoinRequested { lobby: 3, from: 0 });
fake.panic_in_next_pump();
frames(&mut app, 1);
assert_eq!(app.world().resource::<SteamBackendRes>().0.local_id(), fake.local_id());
assert_eq!(app.world().resource::<LostSeen>().0, vec![SteamLost { reason: SteamLostReason::PumpPanicked }]);
assert!(app.world().resource::<PumpedEvents>().0.is_empty(), "that frame's events are dropped");
frames(&mut app, 3);
assert_eq!(fake.pump_count(), 2);
assert_eq!(app.world().resource::<LostSeen>().0.len(), 1);
let again = FakeSteamBackend::new();
app.insert_resource(SteamBackendRes(Box::new(again.clone())));
frames(&mut app, 2);
assert_eq!(again.pump_count(), 2);
}
#[cfg(feature = "lobby")]
#[test]
fn events_pumped_with_steam_lost_are_still_applied() {
#[derive(Resource, Default)]
struct Joins(Vec<JoinRequested>);
let fake = FakeSteamBackend::new();
let mut app = lost_app(&fake);
let record_joins = |mut r: MessageReader<JoinRequested>, mut j: ResMut<Joins>| j.0.extend(r.read().cloned());
app.init_resource::<Joins>().add_systems(Update, record_joins.after(SteamKitSystems::Requests));
frames(&mut app, 1);
fake.push_event(BackendEvent::LobbyJoinRequested { lobby: 3, from: 0 });
fake.simulate_steam_exit();
frames(&mut app, 1);
assert_eq!(app.world().resource::<Joins>().0.len(), 1, "applied before the backend was made inert");
assert_eq!(app.world().resource::<LostSeen>().0.len(), 1);
}
fn game_system_in_first(backend: Option<Res<SteamBackendRes>>) {
let _ = backend.map(|b| b.0.local_id());
}
#[test]
fn a_strict_app_with_the_kit_and_game_systems_builds_and_runs() {
let fake = FakeSteamBackend::new();
let mut app = strict_app();
app.insert_resource(SteamBackendRes(Box::new(fake.clone())))
.add_systems(First, game_system_in_first.after(SteamKitSystems::Callbacks).before(MessageUpdateSystems))
.add_systems(Update, game_system_in_first.before(SteamKitSystems::Requests));
frames(&mut app, 3);
assert_eq!(fake.pump_count(), 3);
}
#[test]
fn the_fake_answers_core_queries() {
let fake = FakeSteamBackend::new();
assert_eq!(fake.local_id(), 76_561_197_960_265_729);
assert!(is_individual_steam_id64(fake.local_id()));
fake.set_local_id(76_561_197_960_265_731);
assert_eq!(fake.local_id(), 76_561_197_960_265_731);
assert_eq!(fake.friend_name(76_561_197_960_265_730), "");
fake.set_friend_name(76_561_197_960_265_730, "Test Friend");
assert_eq!(fake.friend_name(76_561_197_960_265_730), "Test Friend");
assert_eq!(fake.launch_command_line(), "");
fake.set_launch_command_line("-x");
assert_eq!(fake.launch_command_line(), "-x");
#[cfg(not(feature = "friends"))]
assert!(fake.pump().is_empty());
#[cfg(feature = "friends")]
assert_eq!(fake.pump(), vec![BackendEvent::PersonaChanged { steam_id: 76_561_197_960_265_730, flags: 1 }]);
assert_eq!(fake.pump_count(), 1);
assert!(fake.calls().is_empty(), "queries and pumps are not recorded");
}
#[test]
fn steam_id64_validation() {
assert!(is_individual_steam_id64(76_561_198_000_000_042)); assert!(is_individual_steam_id64(76_561_197_960_265_729));
assert!(!is_individual_steam_id64(0));
assert!(!is_individual_steam_id64(1));
assert!(!is_individual_steam_id64(12345));
assert!(!is_individual_steam_id64(76_561_197_960_265_728));
assert!(!is_individual_steam_id64(103_582_791_429_521_408));
assert!(!is_individual_steam_id64(109_775_241_000_000_000));
assert!(!is_individual_steam_id64((2u64 << 56) | (1 << 52) | (1 << 32) | 5));
}