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 bodies(model: &RecordModel) -> Vec<(String, bool)> {
model
.polls
.as_ref()
.map(|polls| {
polls
.entries
.iter()
.map(|entry| (entry.name.to_string(), entry.mounted()))
.collect()
})
.unwrap_or_default()
}
#[test]
fn a_light_entry_is_the_nodes_own_body() {
let model = operator(&syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
})
.expect("`light` names this node's own sweep method");
assert_eq!(bodies(&model), [("orphans".to_owned(), false)]);
}
#[test]
fn a_mixed_node_splits_by_the_word_each_entry_wrote() {
let model = operator(&syn::parse_quote! {
#[ingraph(
poll(scrape, every = "10m", handler = "app::Scraper", async(inline)),
poll(vacuum, cron = "0 3 * * *", light, async(inline)),
)]
struct Indexer { #[ingraph(id)] id: Uuid7 }
})
.expect("a node may mix a mounted clock with a light one");
assert_eq!(
bodies(&model),
[("scrape".to_owned(), true), ("vacuum".to_owned(), false)]
);
let polls = model.polls.expect("the clocks were read");
assert_eq!(
polls
.sweeping()
.map(|(_, entry)| entry.name.to_string())
.collect::<Vec<_>>(),
["vacuum"],
"the sweep trait holds the `light` clock and nothing else"
);
}
#[test]
fn an_entry_that_names_no_body_is_refused() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(names(&messages, "does not say WHO runs it"), "{messages:?}");
assert!(
names(&messages, "`handler = \"…\"`")
&& names(&messages, "`light`")
&& names(&messages, "ReaperSweeps"),
"{messages:?}"
);
}
#[test]
fn an_entry_that_names_both_bodies_is_refused() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", light, handler = "app::Scraper", async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "names both `handler = \"…\"` and `light`"),
"{messages:?}"
);
}
#[test]
fn a_light_entry_cannot_be_detached() {
for detached in ["spawn", "rayon", "async(spawn)"] {
let mode: proc_macro2::TokenStream = detached.parse().expect("a mode word");
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", light, #mode))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "is `light` and runs") && names(&messages, "DETACHES the tick"),
"{detached}: {messages:?}"
);
}
}
#[test]
fn a_light_entry_runs_in_place() {
for inline in ["block", "async(inline)"] {
let mode: proc_macro2::TokenStream = inline.parse().expect("a mode word");
operator(&syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", light, #mode))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
})
.unwrap_or_else(|e| panic!("`light` runs in place under `{inline}`: {e}"));
}
}
#[test]
fn a_mounted_clock_may_still_detach() {
operator(&syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", handler = "app::Scraper", spawn))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
})
.expect("a citizen is a value the deployment built, so it can be handed over");
}
#[test]
fn light_carries_nothing() {
for written in [
quote::quote!(light = "app::Scraper"),
quote::quote!(light(app::Scraper)),
] {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", #written, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "`light` is a BARE word"),
"{written}: {messages:?}"
);
}
}
#[test]
fn light_is_declared_once() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", light, light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "`light` is declared more than once"),
"{messages:?}"
);
}
#[test]
fn light_is_not_a_clocks_name() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(light, every = "10m", async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "opens with the NAME this entry is minting")
&& names(&messages, "cannot also mint a clock called that"),
"{messages:?}"
);
}
#[test]
fn a_near_miss_of_light_is_named_where_it_was_written() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, ligth, every = "10m", async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "mints ONE name") && names(&messages, "did you mean `light`?"),
"{messages:?}"
);
}
#[test]
fn the_unknown_entry_row_offers_light() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", ligth = "yes", async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "unknown entry in `poll(…)`") && names(&messages, "did you mean `light`?"),
"{messages:?}"
);
}
#[test]
fn a_second_clock_name_that_is_near_nothing_gains_no_clause() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, stale, every = "10m", light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(names(&messages, "mints ONE name"), "{messages:?}");
assert!(!names(&messages, "did you mean"), "{messages:?}");
}