use quote::format_ident;
use crate::{
model::{
Grant, Grants, PollBody, PollCadence, PollEntry, PollMode, Polls, RecordModel,
default_due_name, default_polling_name, default_sweeps_name, poll_grants_name,
},
name::RustSafeName,
tests::{emission, model},
};
fn polling() -> RecordModel {
let mut model = model("Reaper", &["media_id"], "id");
model.polls = Some(Polls {
entries: vec![
entry("hourly", &["media::Media"], None),
entry("nightly", &[], Some("app::PromScraper")),
],
});
model
}
fn entry(name: &str, granted: &[&str], handler: Option<&str>) -> PollEntry {
use heck::ToUpperCamelCase as _;
PollEntry {
name: format_ident!("{name}"),
variant: format_ident!("{}", name.to_upper_camel_case()),
cadence: PollCadence::Every(core::time::Duration::new(600, 0)),
grants: (!granted.is_empty()).then(|| Grants {
grants: granted.iter().copied().map(grant).collect(),
}),
body: handler.map_or(PollBody::Light, |path| {
PollBody::Citizen(syn::parse_str(path).expect("a type path"))
}),
mode: PollMode::AsyncInline,
concurrent: false,
}
}
fn grant(path: &str) -> Grant {
let node: syn::Path = syn::parse_str(path).expect("a type path");
let segment = &node
.segments
.last()
.expect("a path has at least one segment")
.ident;
let mount = RustSafeName::new(segment.to_string(), proc_macro2::Span::call_site());
Grant { node, mount }
}
fn scoped_types(file: &syn::File) -> Vec<String> {
file
.items
.iter()
.filter_map(|item| match item {
syn::Item::Struct(item) => Some(item.ident.to_string()),
syn::Item::Enum(item) => Some(item.ident.to_string()),
syn::Item::Trait(item) => Some(item.ident.to_string()),
_ => None,
})
.collect()
}
fn all_items(file: &syn::File) -> Vec<&syn::Item> {
let mut found = Vec::new();
for item in &file.items {
found.push(item);
if let syn::Item::Const(anonymous) = item
&& let syn::Expr::Block(block) = &*anonymous.expr
{
found.extend(
block
.block
.stmts
.iter()
.filter_map(|statement| match statement {
syn::Stmt::Item(inner) => Some(inner),
_ => None,
}),
);
}
}
found
}
#[test]
fn the_enumeration_names_exactly_the_types_a_polling_node_declares() {
let model = polling();
let named: Vec<String> = model
.emitted_types()
.iter()
.map(|declared| declared.name.to_string())
.collect();
assert_eq!(named, scoped_types(&emission(&model)), "`{}`", model.record);
}
#[test]
fn every_polling_mint_arrives_with_what_it_is() {
let model = polling();
let record = &model.record;
let roles: Vec<(String, String)> = model
.emitted_types()
.iter()
.map(|declared| (declared.name.to_string(), declared.role.to_string()))
.collect();
for (name, role) in [
(
poll_grants_name(record, &format_ident!("Hourly")),
"a clock's grants context",
),
(default_polling_name(record), "the lending"),
(default_sweeps_name(record), "the sweep trait"),
] {
assert!(
roles.contains(&(name.to_string(), role.to_owned())),
"`{name}` is {role}: {roles:?}",
);
}
}
#[test]
fn the_polling_types_declare_only_names_no_declaration_can_reach() {
let model = polling();
let file = emission(&model);
let record = &model.record;
let minted: Vec<String> = [
poll_grants_name(record, &format_ident!("Hourly")),
default_polling_name(record),
default_sweeps_name(record),
default_due_name(record),
]
.iter()
.map(ToString::to_string)
.collect();
let mut found = Vec::new();
for item in all_items(&file) {
let syn::Item::Impl(declared) = item else {
continue;
};
let (None, syn::Type::Path(path)) = (&declared.trait_, &*declared.self_ty) else {
continue;
};
let Some(owner) = path.path.segments.last().map(|last| last.ident.to_string()) else {
continue;
};
if !minted.contains(&owner) {
continue;
}
found.extend(declared.items.iter().filter_map(|item| match item {
syn::ImplItem::Fn(method) => Some(method.sig.ident.to_string()),
_ => None,
}));
}
found.sort_unstable();
found.dedup();
assert_eq!(
found,
["at", "new", "schedule", "upstream"],
"the poll vocabulary's three readers and the constructors — every one of them fixed",
);
}