use serde_json::{Value, json};
use super::*;
use crate::model::conformance::spec;
#[test]
fn backoff_starts_at_the_pause_and_stays_under_the_ceiling() {
let timing = Timing::DEFAULT;
assert_eq!(timing.backoff(0), Duration::from_millis(500));
for _ in 0..200 {
let first = timing.backoff(1);
assert!(
(Duration::from_millis(500)..=Duration::from_secs(1)).contains(&first),
"{first:?}"
);
let late = timing.backoff(40);
assert!((Duration::from_millis(500)..=Duration::from_secs(5)).contains(&late), "{late:?}");
}
}
#[test]
fn subscription_names_the_cursor_and_the_token_only_when_private() {
let public =
serde_json::to_value(Subscription { after: Cursor::new(42), token: None }).unwrap();
assert_eq!(public, json!({"after": "42"}));
let private =
serde_json::to_value(Subscription { after: Cursor::new(7), token: Some("t") }).unwrap();
assert_eq!(private, json!({"after": "7", "token": "t"}));
}
#[test]
fn frames_decode_as_controls_or_pages() {
let frame = decode_frame(r#"{"type":"resync_required","reason":"gone"}"#).unwrap();
assert!(
matches!(frame, Frame::Control { kind: ControlKind::ResyncRequired, ref reason } if reason == "gone")
);
let frame = decode_frame(r#"{"type":"later_kind","reason":"r"}"#).unwrap();
assert!(matches!(frame, Frame::Control { kind: ControlKind::Other, .. }));
let frame = decode_frame(&crate::testing::json::fixture("event_page").to_string()).unwrap();
let Frame::Page(page) = frame else { panic!("{frame:?}") };
assert_eq!(page.next_cursor, Cursor::new(1007));
assert!(decode_frame(r#"{"events":[]}"#).is_err());
assert!(decode_frame("not json").is_err());
}
#[test]
fn wire_types_match_the_spec() {
let schemas = &spec()["components"]["schemas"];
let properties = |name: &str| -> Vec<String> {
let mut keys: Vec<String> =
schemas[name]["properties"].as_object().unwrap().keys().cloned().collect();
keys.sort();
keys
};
assert_eq!(properties("StreamSubscription"), ["after", "token"]);
assert_eq!(properties("StreamControl"), ["reason", "type"]);
for kind in schemas["StreamControl"]["properties"]["type"]["enum"].as_array().unwrap() {
let decoded: ControlKind = serde_json::from_value(kind.clone()).unwrap();
assert_ne!(decoded, ControlKind::Other, "{kind}");
}
let operation = &spec()["paths"][crate::transport::ops::STREAM.path]["get"]["x-websocket"];
assert_eq!(
operation["clientMessage"]["$ref"],
Value::from("#/components/schemas/StreamSubscription")
);
}
#[test]
fn authentication_control_maps_to_an_unauthenticated_api_error() {
let error = unauthenticated("revoked");
assert!(error.is_unauthenticated());
assert_eq!(error.status(), Some(401));
assert_eq!(error.code(), Some(&ErrorCode::Unauthenticated));
assert_eq!(error.api().unwrap().message(), "revoked");
assert!(!error.is_retryable());
}