diff --git a/crates/core/src/league_client.rs b/crates/core/src/league_client.rs index e7f0702..0e918d0 100644 --- a/crates/core/src/league_client.rs +++ b/crates/core/src/league_client.rs @@ -318,12 +318,17 @@ fn tls_config() -> Result { ) } -/// A rejected certificate is worth telling apart from a client that is simply not there. +/// A rejected certificate, a timeout or a dropped connection is worth telling apart from a +/// client that is simply not there (nothing listens): the first ones say nothing about the draft. fn transport_error(e: ureq::Error) -> ClientError { match e { ureq::Error::Tls(_) | ureq::Error::Rustls(_) => { ClientError::Other(format!("Connexion TLS au client refusée : {e}")) } + ureq::Error::Timeout(_) => ClientError::Other(format!("Le client ne répond pas : {e}")), + ureq::Error::Io(ref io) if io.kind() != ErrorKind::ConnectionRefused => { + ClientError::Other(format!("Échange avec le client interrompu : {e}")) + } _ => ClientError::Absent, } } @@ -358,11 +363,14 @@ pub(crate) fn parse(body: &str) -> Result { .map_err(|e| ClientError::Other(format!("Réponse du client illisible : {e}"))) } -/// Topic and data of a WebSocket event frame, `[8, "topic", data]`. +/// Topic and resource of a WebSocket event frame, +/// `[8, "topic", {"data": resource, "eventType": ..., "uri": ...}]`; the resource is null on a +/// `Delete`. fn decode_event(text: &str) -> Option<(String, Value)> { let frame = parse(text).ok()?; let topic = frame.get(1)?.as_str()?.to_owned(); - Some((topic, frame.get(2).cloned().unwrap_or(Value::Null))) + let data = frame.pointer("/2/data").cloned().unwrap_or(Value::Null); + Some((topic, data)) } fn decode_signed_in(body: &str) -> Result<(String, String), ClientError> { @@ -634,14 +642,84 @@ mod tests { #[test] fn reads_the_topic_and_data_of_an_event_frame() { - let frame = r#"[8,"OnJsonApiEvent_lol-champ-select_v1_session",{"localPlayerCellId":16}]"#; - let (topic, data) = decode_event(frame).unwrap(); + let (topic, data) = decode_event(&frame( + SESSION_EVENT, + "Update", + r#"{"localPlayerCellId":16}"#, + )) + .unwrap(); assert_eq!(topic, SESSION_EVENT); assert_eq!(data["localPlayerCellId"], 16); + let (_, data) = decode_event(&frame(SESSION_EVENT, "Delete", "null")).unwrap(); + assert!(data.is_null()); assert!(decode_event("not json").is_none()); assert!(decode_event("[]").is_none()); } + /// A frame as the client sends it: the resource sits in the envelope's `data`. + fn frame(topic: &str, event_type: &str, data: &str) -> String { + format!(r#"[8,"{topic}",{{"data":{data},"eventType":"{event_type}","uri":"/x"}}]"#) + } + + #[test] + fn a_raw_event_frame_updates_the_cached_session_and_lists() { + use serde_json::json; + use std::collections::HashSet; + + let client = LeagueClient::in_dir(PathBuf::new()); + *lock(&client.champion_lists) = ChampSelectCache { + lists: Some(ChampSelectLists::fetch("[1,2]", "[]", Some("[]")).unwrap()), + session: None, + }; + let apply = |topic, event_type, data: &str| { + let (topic, data) = decode_event(&frame(topic, event_type, data)).unwrap(); + client.apply_event(&topic, data) + }; + assert_eq!( + apply(SESSION_EVENT, "Update", r#"{"localPlayerCellId":16}"#), + ClientEvent::Selection + ); + assert_eq!( + apply(ANVIL_EVENT, "Update", "[9,8]"), + ClientEvent::Selection + ); + assert_eq!( + apply(PICKABLE_EVENT, "Update", "[2]"), + ClientEvent::Selection + ); + // A Delete carries no data: it neither wipes nor corrupts what is cached. + assert_eq!( + apply(SESSION_EVENT, "Delete", "null"), + ClientEvent::Selection + ); + assert_eq!(apply(ANVIL_EVENT, "Delete", "null"), ClientEvent::Selection); + assert_eq!( + apply(PICKABLE_EVENT, "Delete", "null"), + ClientEvent::Selection + ); + let cache = lock(&client.champion_lists); + assert_eq!(cache.session, Some(json!({"localPlayerCellId": 16}))); + let lists = cache.lists.as_ref().unwrap(); + assert_eq!(lists.anvil(), [9, 8]); + assert_eq!(&*lists.pickable(), &HashSet::from([2])); + } + + #[test] + fn only_a_client_that_is_gone_is_absent() { + let io = |kind| ureq::Error::Io(std::io::Error::from(kind)); + assert_eq!( + transport_error(io(ErrorKind::ConnectionRefused)), + ClientError::Absent + ); + for transient in [ + ureq::Error::Timeout(ureq::Timeout::Global), + io(ErrorKind::TimedOut), + io(ErrorKind::ConnectionReset), + ] { + assert!(matches!(transport_error(transient), ClientError::Other(_))); + } + } + #[test] fn events_update_only_the_list_they_concern() { use serde_json::json;