use crate::{
model::{
ChangesFeed, ChannelOptions, DatabaseKind, FanIn, Kind, NodeKind, RecordModel, Subscription,
Upstream, Upstreams,
},
tests::model,
};
fn upstream(node: &str, channel: Option<ChannelOptions>, changes: Option<ChangesFeed>) -> 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,
notify: false,
}
}
fn compact(model: &RecordModel) -> String {
model.emit().to_string().split_whitespace().collect()
}
#[test]
fn the_sources_table_is_the_declarations_own_order() {
let mut model = model("Captions", &["media_id"], "id");
model.subscriptions = Some(Subscription {
upstreams: vec![
upstream("asr::Segment", Some(ChannelOptions::Bounded(512)), None),
upstream("media::Ocr", Some(ChannelOptions::Unbounded), None),
upstream("HttpFetch", None, None),
],
});
let emitted = compact(&model);
assert!(
emitted.contains("constUPSTREAMS:&'static[::ingraph::__private::drive::Upstream]=&["),
"{emitted}"
);
let table = emitted
.split("constUPSTREAMS:&'static[::ingraph::__private::drive::Upstream]=&[")
.nth(1)
.expect("the table");
let first = table.find("asr::Segment").expect("the first entry");
let second = table.find("media::Ocr").expect("the second entry");
let third = table.find("HttpFetch").expect("the third entry");
assert!(first < second && second < third, "{table}");
assert!(
table.contains(".channelled(::ingraph::__private::drive::ChannelOptions::Bounded(512usize))"),
"the author's shipped depth rides the row: {table}"
);
assert!(
table.contains(".channelled(::ingraph::__private::drive::ChannelOptions::Unbounded)"),
"{table}"
);
assert!(
table.contains("Upstream::of::<HttpFetch>(\"HttpFetch\")"),
"a upstream with no words is the bare row: {table}"
);
}
#[test]
fn a_declared_changes_listener_states_its_feeder() {
let mut model = model("Reaper", &[], "id");
model.subscriptions = Some(Subscription {
upstreams: vec![
upstream(
"media::Store",
None,
Some(ChangesFeed {
database: DatabaseKind::Sqlite,
external: false,
}),
),
upstream(
"other::Store",
None,
Some(ChangesFeed {
database: DatabaseKind::Sqlite,
external: true,
}),
),
],
});
let emitted = compact(&model);
assert!(
emitted.contains(
".listening(::ingraph::__private::drive::Listen::Changes(::ingraph::__private::drive::\
Feeder::InGraph))"
),
"a default `changes(<db>)` claims an in-graph feeder: {emitted}"
);
assert!(
emitted.contains(
".listening(::ingraph::__private::drive::Listen::Changes(::ingraph::__private::drive::\
Feeder::External))"
),
"`external` declares the cross-process shape: {emitted}"
);
}
#[test]
fn the_single_word_form_is_one_row() {
let mut model = model("Titles", &[], "id");
model.subscribe = Some(syn::parse_str("media::Frame").expect("a path"));
let emitted = compact(&model);
assert!(
emitted.contains("Upstream::of::<media::Frame>(\"media::Frame\")"),
"{emitted}"
);
}
#[test]
fn the_effect_and_the_lane_are_read_off_the_kind() {
let stored = compact(&model("Scene", &[], "id"));
assert!(
stored.contains(
"constEFFECT:::ingraph::__private::drive::Effect=::ingraph::__private::drive::Effect::Lands;"
),
"{stored}"
);
assert!(stored.contains("constHOLDS_LANE:bool=true;"), "{stored}");
let mut pure = model("Decode", &[], "id");
pure.kind = NodeKind::Operator;
let pure = compact(&pure);
assert!(
pure.contains(
"constEFFECT:::ingraph::__private::drive::Effect=::ingraph::__private::drive::Effect::Pure;"
),
"{pure}"
);
assert!(pure.contains("constHOLDS_LANE:bool=false;"), "{pure}");
}
#[test]
fn the_options_word_is_the_declared_type() {
let mut named = model("Shout", &[], "id");
named.options = Some(syn::parse_str("shout::Options").expect("a path"));
assert!(
compact(&named).contains("typeOptions=shout::Options;"),
"{}",
compact(&named)
);
let silent = compact(&model("Keep", &[], "id"));
assert!(silent.contains("typeOptions=();"), "{silent}");
}
#[test]
fn a_head_states_an_empty_table() {
let emitted = compact(&model("Watch", &[], "id"));
assert!(
emitted.contains("constUPSTREAMS:&'static[::ingraph::__private::drive::Upstream]=&[];"),
"{emitted}"
);
}
#[test]
fn a_fusions_upstreams_are_rows() {
let mut model = model("Fused", &[], "id");
model.upstreams = Some(Upstreams::Fanned(FanIn {
kinds: vec![
Kind {
node: syn::parse_str("a::Left").expect("a path"),
variant: syn::parse_str("Left").expect("an ident"),
},
Kind {
node: syn::parse_str("a::Right").expect("a path"),
variant: syn::parse_str("Right").expect("an ident"),
},
],
}));
let emitted = compact(&model);
assert!(emitted.contains("Upstream::of::<a::Left>"), "{emitted}");
assert!(emitted.contains("Upstream::of::<a::Right>"), "{emitted}");
}
fn faced(name: &str, streaming: bool, asynchronous: bool) -> RecordModel {
let mut model = model(name, &[], "id");
model.kind = NodeKind::Operator;
if streaming {
model.streaming = Some(syn::parse_quote!(stream));
}
if asynchronous {
model.asynchronous = Some(syn::parse_quote!(r#async));
}
model
}
fn autonomous(name: &str, asynchronous: bool) -> RecordModel {
let mut model = model(name, &[], "id");
model.kind = NodeKind::Operator;
model.autonomous = Some(syn::parse_quote!(autonomous));
if asynchronous {
model.asynchronous = Some(syn::parse_quote!(r#async));
}
model
}
#[test]
fn the_face_word_is_the_pair_seat() {
let streaming = compact(&faced("Demux", true, false));
assert!(
streaming.contains("typePair=::ingraph::__private::drive::Streaming;"),
"{streaming}"
);
assert!(!streaming.contains("constSTREAMS"), "{streaming}");
let buffered = compact(&faced("Describe", false, false));
assert!(
buffered.contains("typePair=::ingraph::__private::drive::Buffered;"),
"{buffered}"
);
let owned = compact(&autonomous("Decode", false));
assert!(
owned.contains("typePair=::ingraph::__private::drive::Autonomous;"),
"{owned}"
);
}
#[test]
fn the_mode_seat_is_the_same_seat_on_both_pairs() {
for streaming in [false, true] {
let blocking = compact(&faced("Demux", streaming, false));
assert!(
blocking.contains("typeMode=::ingraph::__private::operator::Block;"),
"{blocking}"
);
let awaited = compact(&faced("Demux", streaming, true));
assert!(
awaited.contains("typeMode=::ingraph::__private::operator::Async;"),
"{awaited}"
);
}
for asynchronous in [false, true] {
let emitted = compact(&autonomous("Decode", asynchronous));
let seat = if asynchronous { "Async" } else { "Block" };
let spelled = format!("typeMode=::ingraph::__private::operator::{seat};");
assert!(emitted.contains(&spelled), "{emitted}");
}
}
#[test]
fn the_attribution_word_is_the_projection_seat() {
let mut model = faced("Keyframe", false, false);
let silent = compact(&model);
assert!(
silent.contains("typeAttribution<__INGRAPH_OUT__>=();"),
"{silent}"
);
assert!(
silent.contains("impl::ingraph::__private::drive::Attributes<Keyframe>"),
"the projection is emitted whatever the node installs: {silent}"
);
assert!(
!silent
.contains("typeAttribution<__INGRAPH_OUT__>=::ingraph::__private::drive::RootProjection"),
"a declaration that wrote no word installs nothing: {silent}"
);
model.attributed = true;
let attributed = compact(&model);
assert!(
attributed.contains(
"typeAttribution<__INGRAPH_OUT__>=::ingraph::__private::drive::RootProjection<\
__INGRAPH_OUT__>;"
),
"{attributed}"
);
}
#[test]
fn the_attribution_seat_is_the_same_seat_on_every_pair() {
let spelled = "typeAttribution<__INGRAPH_OUT__>=::ingraph::__private::drive::RootProjection<\
__INGRAPH_OUT__>;";
for streaming in [false, true] {
for asynchronous in [false, true] {
let mut model = faced("Demux", streaming, asynchronous);
model.attributed = true;
let emitted = compact(&model);
assert!(emitted.contains(spelled), "{emitted}");
}
}
let mut owned = autonomous("Decode", false);
owned.attributed = true;
let emitted = compact(&owned);
assert!(emitted.contains(spelled), "{emitted}");
}
#[test]
fn the_face_audit_names_the_cell_the_words_select() {
const ROSTER: [(bool, bool, &str); 3] = [
(false, true, "AsyncOperator"),
(true, false, "StreamOperator"),
(true, true, "AsyncStreamOperator"),
];
for (streaming, asynchronous, face) in ROSTER {
let emitted = compact(&faced("Demux", streaming, asynchronous));
let bound = format!("__INGRAPH_NODE__:::ingraph::__private::operator::{face}>");
assert!(emitted.contains(&bound), "missing `{bound}` in: {emitted}");
}
for (asynchronous, face) in [
(false, "AutonomousOperator"),
(true, "AsyncAutonomousOperator"),
] {
let emitted = compact(&autonomous("Decode", asynchronous));
let bound = format!("__INGRAPH_NODE__:::ingraph::__private::operator::{face}>");
assert!(emitted.contains(&bound), "missing `{bound}` in: {emitted}");
}
let bare = compact(&faced("Describe", false, false));
assert!(!bare.contains("__ig_awaits"), "{bare}");
}
#[test]
fn the_receipt_word_states_the_type_this_message_is_answered_with() {
let mut model = model("SceneOpen", &["media_id"], "id");
model.receipt = Some(syn::parse_str("media::SceneId").expect("a path"));
let emitted = compact(&model);
assert!(
emitted.contains("impl::ingraph::__private::operator::ReceiptsforSceneOpen{"),
"the sibling impl is the message's own: {emitted}"
);
assert!(
emitted.contains("typeReceipt=media::SceneId;"),
"the author's path, verbatim: {emitted}"
);
}
#[test]
fn a_declaration_that_declares_no_receipt_emits_no_seat() {
let model = model("Scene", &["media_id"], "id");
let emitted = compact(&model);
assert!(
!emitted.contains("::ingraph::__private::operator::Receipts"),
"no word, no impl: {emitted}"
);
assert!(
!emitted.contains("typeReceipt="),
"no word, no seat: {emitted}"
);
assert!(
emitted.contains("typeOptions=();"),
"the MEMBER seat is filled even in silence, which is the contrast: {emitted}"
);
}