use crate::{
model::{ChannelOptions, RecordModel, Subscription, Upstream, default_channels_name},
tests::model,
};
fn fused() -> RecordModel {
let mut model = model("Captions", &["media_id"], "id");
model.subscriptions = Some(Subscription {
upstreams: vec![
upstream("asr::Segment", Some(ChannelOptions::Bounded(512))),
upstream("media::Ocr", Some(ChannelOptions::Unbounded)),
upstream("HttpFetch", None),
],
});
model
}
fn upstream(node: &str, channel: Option<ChannelOptions>) -> Upstream {
let node: syn::Path = syn::parse_str(node).expect("a path");
let variant = node
.segments
.last()
.expect("a path has a last segment")
.ident
.clone();
Upstream {
node,
keyed: false,
out: None,
variant,
channel,
changes: None,
notify: false,
}
}
fn compact(model: &RecordModel) -> String {
model.emit().to_string().split_whitespace().collect()
}
#[test]
fn every_source_gets_a_field_named_by_the_p1_law() {
let model = fused();
let emitted = compact(&model);
assert_eq!(default_channels_name(&model.record), "CaptionsChannels");
assert!(emitted.contains("structCaptionsChannels{"), "{emitted}");
for field in ["segment", "ocr", "http_fetch"] {
assert!(
emitted.contains(&format!(
"{field}:::core::option::Option<::ingraph::__private::drive::ChannelOptions>"
)),
"`{field}` is a field of the shelf: {emitted}"
);
}
}
#[test]
fn the_shelf_defaults_its_fields_and_refuses_unknown_keys() {
let emitted = compact(&fused());
assert_eq!(
emitted.matches("#[serde(default,rename=").count(),
3,
"{emitted}"
);
assert!(emitted.contains("deny_unknown_fields"), "{emitted}");
}
#[test]
fn a_keyword_named_source_keeps_its_own_key() {
let mut model = model("Boxes", &["media_id"], "id");
model.subscriptions = Some(Subscription {
upstreams: vec![upstream("shape::Box", None)],
});
let emitted = compact(&model);
assert!(emitted.contains(r#"rename="box""#), "{emitted}");
assert!(
emitted.contains("box_:::core::option::Option<"),
"{emitted}"
);
}
#[test]
fn the_declared_table_carries_the_authors_own_words_in_order() {
let emitted = compact(&fused());
assert!(
emitted.contains(
"constDECLARED:[::core::option::Option<::ingraph::__private::drive::ChannelOptions>;3usize]=[\
::core::option::Option::Some(::ingraph::__private::drive::ChannelOptions::Bounded(512usize)),\
::core::option::Option::Some(::ingraph::__private::drive::ChannelOptions::Unbounded),\
::core::option::Option::None,]"
),
"{emitted}"
);
assert!(emitted.contains("constCOUNT:usize=3usize"), "{emitted}");
}
#[test]
fn the_shelf_hands_its_table_to_the_launch_floor() {
let emitted = compact(&fused());
assert!(
emitted.contains("impl::ingraph::__private::drive::ChannelShelfforCaptionsChannels"),
"{emitted}"
);
assert!(
emitted.contains("into_iter([self.segment,self.ocr,self.http_fetch,])"),
"{emitted}"
);
}
#[test]
fn a_node_with_no_container_emits_no_shelf() {
let model = model("Plain", &["media_id"], "id");
let shelf = default_channels_name(&model.record).to_string();
assert!(
!compact(&model).contains(&format!("struct{shelf}")),
"{shelf}"
);
}
#[test]
fn one_source_still_gets_a_shelf() {
let mut model = model("Thumbs", &["media_id"], "id");
model.subscriptions = Some(Subscription {
upstreams: vec![upstream("media::Frame", Some(ChannelOptions::Bounded(4)))],
});
let emitted = compact(&model);
assert!(emitted.contains("structThumbsChannels{"), "{emitted}");
assert!(emitted.contains("constCOUNT:usize=1usize"), "{emitted}");
}