crate::endpoints! {
self: Query<ConnectRequest, ConnectReply>;
}
use crate::version::FrameworkVersion;
pub const PRESENCE_KEY: &str = "supervisor/presence";
#[derive(
phoxal_macros::DescribeWire,
Clone,
Copy,
Debug,
Eq,
PartialEq,
serde::Serialize,
serde::Deserialize,
)]
#[serde(tag = "schema")]
pub enum ConnectRequest {
#[serde(rename = "phoxal/supervisor-connect/v0")]
V0 {},
}
#[derive(
phoxal_macros::DescribeWire,
Clone,
Copy,
Debug,
Eq,
PartialEq,
serde::Serialize,
serde::Deserialize,
)]
#[serde(tag = "schema")]
pub enum ConnectReply {
#[serde(rename = "phoxal/supervisor-connect/v0")]
V0 { framework: FrameworkVersion },
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::{ConnectReply, ConnectRequest, FrameworkVersion, PRESENCE_KEY};
#[test]
fn the_bootstrap_key_is_pinned_to_its_literal() {
let bootstrap = crate::supervisor::api::topics().connect().client();
assert_eq!(bootstrap.key(), "supervisor/connect");
assert_ne!(PRESENCE_KEY, bootstrap.key());
}
#[test]
fn the_composed_bootstrap_wire_key_is_pinned_to_its_literal() {
const EXECUTION: &str = "1c8f3a5b7d9e0f2a4b6c8d0e1f325476";
let surface = crate::bus::__compat::contract_surface();
assert!(
surface.contains(
r#"{"name":"bus-key-composition","record":"identifier","value":"phoxal/{execution}/{topic}"}"#
),
"the bus must declare the key grammar the bootstrap composes into: {surface}"
);
let composed = "phoxal/{execution}/{topic}"
.replace("{execution}", EXECUTION)
.replace(
"{topic}",
crate::supervisor::api::topics().connect().client().key(),
);
assert_eq!(
composed,
"phoxal/1c8f3a5b7d9e0f2a4b6c8d0e1f325476/supervisor/connect"
);
}
#[test]
fn the_bootstrap_documents_are_pinned_to_their_literal_json() {
assert_eq!(
serde_json::to_value(ConnectRequest::V0 {}).unwrap(),
json!({"schema": "phoxal/supervisor-connect/v0"})
);
assert_eq!(
serde_json::to_value(ConnectReply::V0 {
framework: FrameworkVersion::CURRENT,
})
.unwrap(),
json!({
"schema": "phoxal/supervisor-connect/v0",
"framework": FrameworkVersion::CURRENT_SPELLING,
})
);
assert_eq!(
serde_json::to_value(ConnectReply::V0 {
framework: FrameworkVersion::new(9, 9, 9),
})
.unwrap(),
json!({
"schema": "phoxal/supervisor-connect/v0",
"framework": "9.9.9",
})
);
assert_eq!(
serde_json::from_value::<ConnectReply>(json!({
"schema": "phoxal/supervisor-connect/v0",
"framework": "9.9.9",
}))
.unwrap(),
ConnectReply::V0 {
framework: FrameworkVersion::new(9, 9, 9),
}
);
}
#[test]
fn the_bootstrap_documents_are_pinned_to_their_declared_wire_shape() {
use crate::__compat::wire::DescribeWire;
assert_eq!(
ConnectRequest::wire_schema().canonical_json(),
concat!(
r#"{"kind":"enum","representation":{"style":"internal","tag":"schema"},"#,
r#""variants":[{"body":{"fields":[],"kind":"struct"},"#,
r#""name":"phoxal/supervisor-connect/v0"}]}"#,
)
);
assert_eq!(
ConnectReply::wire_schema().canonical_json(),
concat!(
r#"{"kind":"enum","representation":{"style":"internal","tag":"schema"},"#,
r#""variants":[{"body":{"fields":[{"name":"framework","presence":"required",""#,
r#"schema":{"kind":"opaque","name":"FrameworkVersion","wire":{"kind":"string"}}}],"#,
r#""kind":"struct"},"name":"phoxal/supervisor-connect/v0"}]}"#,
)
);
for document in [
serde_json::to_value(ConnectRequest::V0 {}).expect("the request serializes"),
serde_json::to_value(ConnectReply::V0 {
framework: FrameworkVersion::CURRENT,
})
.expect("the reply serializes"),
] {
let schema = if document.get("framework").is_some() {
ConnectReply::wire_schema()
} else {
ConnectRequest::wire_schema()
};
assert_eq!(schema.conforms(&document), Ok(()), "{document}");
}
}
#[test]
fn the_bootstrap_documents_round_trip_on_the_bus_codec() {
let request = ConnectRequest::V0 {};
let encoded = rmp_serde::to_vec_named(&request).unwrap();
assert_eq!(
rmp_serde::from_slice::<ConnectRequest>(&encoded).unwrap(),
request
);
for framework in [FrameworkVersion::CURRENT, FrameworkVersion::new(9, 9, 9)] {
let reply = ConnectReply::V0 { framework };
let encoded = rmp_serde::to_vec_named(&reply).unwrap();
assert_eq!(
rmp_serde::from_slice::<ConnectReply>(&encoded).unwrap(),
reply
);
}
}
#[test]
fn a_foreign_schema_tag_fails_by_naming_itself() {
const FOREIGN: &str = "phoxal/supervisor-connect/v1";
let foreign = json!({"schema": FOREIGN, "framework": "9.9.9"});
let error = serde_json::from_value::<ConnectReply>(foreign.clone()).unwrap_err();
assert!(
error.to_string().contains(FOREIGN),
"the mismatch diagnostic must name the foreign tag: {error}"
);
let encoded = rmp_serde::to_vec_named(&foreign).unwrap();
let error = rmp_serde::from_slice::<ConnectReply>(&encoded).unwrap_err();
assert!(
error.to_string().contains(FOREIGN),
"the mismatch diagnostic must name the foreign tag: {error}"
);
}
}