use ingraph_codegen::prelude::*;
fn operator(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
let (host, options) = crate::host::split(input);
crate::parse::parse(&host, &options, NodeKind::Operator)
}
fn refusals(input: syn::DeriveInput) -> Vec<String> {
operator(&input)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect()
}
fn names(messages: &[String], rule: &str) -> bool {
messages.iter().any(|message| message.contains(rule))
}
fn channels(model: &RecordModel) -> Vec<Option<ChannelOptions>> {
model
.subscriptions
.as_ref()
.map(|subscription| {
subscription
.upstreams
.iter()
.map(|source| source.channel)
.collect()
})
.unwrap_or_default()
}
#[test]
fn the_two_words_are_read_per_source() {
let model = operator(&syn::parse_quote! {
#[ingraph(subscribe(
upstream(node = "asr::Segment", channel = bounded(512)),
upstream(node = "media::Ocr", channel = unbounded),
upstream(node = "HttpFetch"),
))]
struct Captions { #[ingraph(id)] id: Uuid7 }
})
.expect("both words are legal, and saying nothing is legal");
assert_eq!(
channels(&model),
[
Some(ChannelOptions::Bounded(512)),
Some(ChannelOptions::Unbounded),
None
]
);
}
#[test]
fn a_zero_depth_is_admitted_as_the_rendezvous_it_is() {
let model = operator(&syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "media::Ocr", channel = bounded(0))))]
struct Captions { #[ingraph(id)] id: Uuid7 }
})
.expect("zero is a depth the runtime has");
assert_eq!(channels(&model), [Some(ChannelOptions::Bounded(0))]);
}
#[test]
fn bounded_without_a_depth_is_refused_by_name() {
let said = refusals(syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "media::Ocr", channel = bounded)))]
struct Captions { #[ingraph(id)] id: Uuid7 }
});
assert!(names(&said, "`bounded` is a depth"), "{said:?}");
assert!(names(&said, "unbounded"), "{said:?}");
}
#[test]
fn unbounded_with_a_depth_is_refused_by_name() {
let said = refusals(syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "media::Ocr", channel = unbounded(512))))]
struct Captions { #[ingraph(id)] id: Uuid7 }
});
assert!(names(&said, "`unbounded` takes nothing"), "{said:?}");
}
#[test]
fn any_other_value_is_refused_naming_the_two_shapes() {
for written in [
quote::quote!(channel = 512),
quote::quote!(channel = "unbounded"),
quote::quote!(channel = spinning),
] {
let said = refusals(syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "media::Ocr", #written)))]
struct Captions { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&said, "`channel = bounded(512)` and `channel = unbounded`"),
"{written}: {said:?}"
);
}
}
#[test]
fn one_edge_is_opened_one_way() {
let said = refusals(syn::parse_quote! {
#[ingraph(subscribe(upstream(
node = "media::Ocr",
channel = bounded(4),
channel = unbounded,
)))]
struct Captions { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&said, "`channel` is declared more than once"),
"{said:?}"
);
}
#[test]
fn a_poll_entry_refuses_the_word_by_name() {
let said = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", channel = unbounded, light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(names(&said, "a poll entry has NO channel"), "{said:?}");
assert!(names(&said, "`subscribe(…)`"), "{said:?}");
}
#[test]
fn the_containers_vocabulary_sentence_names_the_word() {
let said = refusals(syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "media::Ocr", depth = 4)))]
struct Captions { #[ingraph(id)] id: Uuid7 }
});
assert!(names(&said, "channel = bounded(…) | unbounded"), "{said:?}");
}
#[test]
fn two_sources_whose_shelf_names_collide_are_refused() {
let said = refusals(syn::parse_quote! {
#[ingraph(subscribe(
upstream(node = "one::AB"),
upstream(node = "two::Ab"),
))]
struct Captions { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&said, "CHANNEL SHELF names would collide"),
"{said:?}"
);
assert!(names(&said, "`ab`"), "{said:?}");
}