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 products(model: &RecordModel) -> Vec<String> {
model
.products
.as_ref()
.map(|products| {
products
.products
.iter()
.map(|product| product.variant.to_string())
.collect()
})
.unwrap_or_default()
}
#[test]
fn one_product_is_written_as_one_string() {
let model = operator(&syn::parse_quote! {
#[ingraph(emits = "media::Caption")]
struct Describe { #[ingraph(id)] id: Uuid7 }
})
.expect("one quoted product is legal");
assert_eq!(products(&model), ["Caption"]);
assert!(!model.products.expect("the word was read").is_sum());
}
#[test]
fn several_products_are_written_as_a_list() {
let model = operator(&syn::parse_quote! {
#[ingraph(emits = ["Caption", "media::Summary", "Digest"])]
struct Describe { #[ingraph(id)] id: Uuid7 }
})
.expect("a quoted product list is legal");
assert_eq!(products(&model), ["Caption", "Summary", "Digest"]);
}
#[test]
fn the_container_spelling_of_emits_names_itself_as_the_older_one() {
let messages = refusals(syn::parse_quote! {
#[ingraph(emits(Caption, Summary))]
struct Describe { #[ingraph(id)] id: Uuid7 }
});
for rule in [
"`emits = \"…\"` names this node's ONE product",
"the older spelling",
] {
assert!(names(&messages, rule), "missing `{rule}` in: {messages:?}");
}
}
#[test]
fn a_product_that_is_not_a_string_gets_the_words_own_sentence() {
let messages = refusals(syn::parse_quote! {
#[ingraph(emits = Caption)]
struct Describe { #[ingraph(id)] id: Uuid7 }
});
assert!(names(&messages, "the older spelling"), "{messages:?}");
}
#[test]
fn emits_is_declared_once() {
let messages = refusals(syn::parse_quote! {
#[ingraph(emits = "Caption", emits = "Summary")]
struct Describe { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "`emits` is declared more than once"),
"{messages:?}"
);
}
#[test]
fn a_selected_product_is_quoted() {
let model = operator(&syn::parse_quote! {
#[ingraph(subscribe(
upstream(node = "vision::Detector", out = "Boxes"),
upstream(node = "vision::Detector", out = "Masks"),
))]
struct Caption { #[ingraph(id)] id: Uuid7 }
})
.expect("two products of one upstream are two edges");
assert_eq!(
model
.subscriptions
.expect("the container was read")
.upstreams
.iter()
.map(|source| source.variant.to_string())
.collect::<Vec<_>>(),
["Boxes", "Masks"],
);
}
#[test]
fn the_bare_spelling_of_out_names_itself_as_the_older_one() {
let messages = refusals(syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "vision::Detector", out = Boxes)))]
struct Caption { #[ingraph(id)] id: Uuid7 }
});
for rule in ["`out = \"<Variant>\"`", "the older spelling"] {
assert!(names(&messages, rule), "missing `{rule}` in: {messages:?}");
}
}
#[test]
fn a_selected_product_is_one_word() {
let messages = refusals(syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "vision::Detector", out = "vision::Boxes")))]
struct Caption { #[ingraph(id)] id: Uuid7 }
});
assert!(names(&messages, "`out = \"<Variant>\"`"), "{messages:?}");
}
fn clocks(model: &RecordModel) -> Vec<(String, String)> {
model
.polls
.as_ref()
.map(|polls| {
polls
.entries
.iter()
.map(|entry| (entry.name.to_string(), entry.variant.to_string()))
.collect()
})
.unwrap_or_default()
}
#[test]
fn the_clocks_sit_beside_the_container_in_declaration_order() {
let model = operator(&syn::parse_quote! {
#[ingraph(
subscribe(upstream(node = "media::Frame"), upstream(node = "asr::Segment")),
poll(orphans, every = "10m", light, async(inline)),
poll(sweep_stale, cron = "0 3 * * *", light, async(inline)),
)]
struct Reaper { #[ingraph(id)] id: Uuid7 }
})
.expect("poll entries beside a container are legal");
assert_eq!(
clocks(&model),
[
("orphans".to_owned(), "Orphans".to_owned()),
("sweep_stale".to_owned(), "SweepStale".to_owned()),
]
);
assert_eq!(
model
.subscriptions
.expect("the container was read")
.upstreams
.len(),
2
);
}
#[test]
fn a_node_may_be_driven_by_its_clock_alone() {
let model = operator(&syn::parse_quote! {
#[ingraph(poll(compact, every = "1h", light, async(inline)))]
struct Compactor { #[ingraph(id)] id: Uuid7 }
})
.expect("a clock is a whole reason to exist");
assert_eq!(
clocks(&model),
[("compact".to_owned(), "Compact".to_owned())]
);
assert!(model.subscriptions.is_none());
}
#[test]
fn an_entry_runs_on_one_clock_and_never_two() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", cron = "0 3 * * *", light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "names both `every` and `cron`"),
"{messages:?}"
);
}
#[test]
fn an_entry_with_no_cadence_is_refused() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "says nothing about when it is due"),
"{messages:?}"
);
}
#[test]
fn a_zero_interval_is_a_spin_and_is_refused() {
for written in ["0s", "0", "PT0S"] {
let interval = syn::LitStr::new(written, proc_macro2::Span::call_site());
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = #interval, light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "is a SPIN") && names(&messages, written),
"{written}: {messages:?}"
);
}
}
#[test]
fn a_negative_interval_is_refused_by_the_same_rule() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "-PT2S", light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(names(&messages, "is a SPIN"), "{messages:?}");
}
#[test]
fn an_unreadable_interval_quotes_what_was_written() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "ten minutes", light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "`every = \"ten minutes\"` is not a duration"),
"{messages:?}"
);
}
#[test]
fn the_interval_speaks_the_one_duration_grammar() {
for (written, seconds) in [
("10m", 600u64),
("1h 30m", 5_400),
("PT1H", 3_600),
("1d", 86_400),
("500ms", 0),
] {
let interval = syn::LitStr::new(written, proc_macro2::Span::call_site());
let model = operator(&syn::parse_quote! {
#[ingraph(poll(orphans, every = #interval, light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
})
.unwrap_or_else(|e| panic!("{written}: {e}"));
let PollCadence::Every(read) = &model
.polls
.as_ref()
.expect("one clock")
.entries
.first()
.expect("one entry")
.cadence
else {
panic!("{written} is an interval");
};
assert_eq!(read.as_secs(), seconds, "{written}");
}
}
#[test]
fn a_garbage_cron_is_refused_with_the_string_quoted() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(nightly, cron = "99 3 * * *", light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "`cron = \"99 3 * * *\"` is not a schedule"),
"{messages:?}"
);
}
#[test]
fn the_dialect_is_five_fields_and_the_zone_is_a_word() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(nightly, cron = "0 3 * * * Asia/Shanghai", light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(names(&messages, "classic 5 fields"), "{messages:?}");
assert!(names(&messages, "`tz = \"<IANA>\"`"), "{messages:?}");
let model = operator(&syn::parse_quote! {
#[ingraph(poll(nightly, cron = "0 3 * * *", tz = "Asia/Shanghai", light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
})
.expect("a zone beside a calendar expression is legal");
let PollCadence::Calendar { expression, zone } = &model
.polls
.as_ref()
.expect("one clock")
.entries
.first()
.expect("one entry")
.cadence
else {
panic!("a calendar entry");
};
assert_eq!(expression, "0 3 * * *");
assert_eq!(zone.as_deref(), Some("Asia/Shanghai"));
}
#[test]
fn a_calendar_entry_with_no_zone_carries_none() {
let model = operator(&syn::parse_quote! {
#[ingraph(poll(nightly, cron = "0 3 * * *", light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
})
.expect("a calendar expression with no zone is legal");
let PollCadence::Calendar { zone, .. } = &model
.polls
.as_ref()
.expect("one clock")
.entries
.first()
.expect("one entry")
.cadence
else {
panic!("a calendar entry");
};
assert!(zone.is_none());
}
#[test]
fn a_zone_beside_an_interval_is_refused() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", tz = "Asia/Shanghai", light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "`tz` has nothing to qualify there"),
"{messages:?}"
);
}
#[test]
fn ack_inside_a_poll_entry_is_a_named_refusal() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", ack(manual), light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "a poll inbound carries NO ack") && names(&messages, "the NEXT TICK"),
"{messages:?}"
);
}
#[test]
fn two_entries_cannot_mint_one_word() {
let messages = refusals(syn::parse_quote! {
#[ingraph(
poll(sweep_stale, every = "10m", light, async(inline)),
poll(sweepStale, every = "1h", light, async(inline))
)]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "both come out as `SweepStale`"),
"{messages:?}"
);
}
#[test]
fn an_entry_that_mints_nothing_is_refused() {
for declaration in [
syn::parse_quote! {
#[ingraph(poll(every = "10m"))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
},
syn::parse_quote! {
#[ingraph(poll())]
struct Reaper { #[ingraph(id)] id: Uuid7 }
},
] {
let messages = refusals(declaration);
assert!(names(&messages, "opens with the NAME"), "{messages:?}");
}
}
#[test]
fn an_entry_mints_one_name() {
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:?}");
}
#[test]
fn the_entry_refuses_what_it_cannot_name() {
let messages = refusals(syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", write, listen(channel), light, async(inline)))]
struct Reaper { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "unknown entry in `poll(…)`"),
"{messages:?}"
);
}
fn record(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
let (host, options) = crate::host::split(input);
crate::parse::parse(&host, &options, NodeKind::Record)
}
fn record_refusals(input: syn::DeriveInput) -> Vec<String> {
record(&input)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect()
}
#[test]
fn the_face_word_and_the_mode_word_are_two_axes() {
let buffered = operator(&syn::parse_quote! {
struct Describe { #[ingraph(id)] id: Uuid7 }
})
.expect("a bare declaration is legal");
assert!(buffered.streaming.is_none());
assert!(buffered.asynchronous.is_none());
let streaming = operator(&syn::parse_quote! {
#[ingraph(stream)]
struct Demux { #[ingraph(id)] id: Uuid7 }
})
.expect("the face word is legal on an operator");
assert!(streaming.streaming.is_some());
assert!(streaming.asynchronous.is_none());
let awaited = operator(&syn::parse_quote! {
#[ingraph(stream, async)]
struct Demux { #[ingraph(id)] id: Uuid7 }
})
.expect("the two words multiply");
assert!(awaited.streaming.is_some());
assert!(awaited.asynchronous.is_some());
}
#[test]
fn the_face_word_composes_with_the_edge_and_the_products() {
let model = operator(&syn::parse_quote! {
#[ingraph(stream, subscribe = "media::File", emits = ["Packet", "Caption"])]
struct Demux { #[ingraph(id)] id: Uuid7 }
})
.expect("the word costs the rest of the grammar nothing");
assert!(model.streaming.is_some());
assert!(model.subscribe.is_some());
assert_eq!(products(&model), ["Packet", "Caption"]);
}
#[test]
fn the_face_word_carries_nothing() {
for declaration in [
syn::parse_quote! {
#[ingraph(stream = "wide")]
struct Demux { #[ingraph(id)] id: Uuid7 }
},
syn::parse_quote! {
#[ingraph(stream(in_flight = 4))]
struct Demux { #[ingraph(id)] id: Uuid7 }
},
] {
let messages = refusals(declaration);
assert!(names(&messages, "`stream` is a bare word"), "{messages:?}");
}
}
#[test]
fn the_face_word_is_written_once() {
let messages = refusals(syn::parse_quote! {
#[ingraph(stream, stream)]
struct Demux { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "one declaration wears one pair"),
"{messages:?}"
);
}
#[test]
fn the_face_word_is_an_operators() {
let messages = record_refusals(syn::parse_quote! {
#[ingraph(sqlite, stream)]
struct Chunk { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "`stream` is an `operator`'s word"),
"{messages:?}"
);
}
#[test]
fn a_streaming_node_does_not_also_poll() {
for declaration in [
syn::parse_quote! {
#[ingraph(stream, poll(orphans, every = "10m", light, async(inline)))]
struct Demux { #[ingraph(id)] id: Uuid7 }
},
syn::parse_quote! {
#[ingraph(poll(orphans, every = "10m", light, async(inline)), stream)]
struct Demux { #[ingraph(id)] id: Uuid7 }
},
] {
let messages = refusals(declaration);
assert!(
names(&messages, "`poll(…)` is the BUFFERED pair's word"),
"{messages:?}"
);
}
}
#[test]
fn the_one_storage_law_calls_an_aggregates_storage_its_folded_rows() {
let sentence = crate::parse::owns_one_storage(NodeKind::Aggregate);
assert!(
sentence.contains("an aggregate's is the folded rows it keeps"),
"{sentence}"
);
assert!(
sentence.contains("The storage node subscribes to this aggregate and shares its key"),
"{sentence}"
);
}
#[test]
fn the_one_storage_law_says_something_of_its_own_about_each_kind() {
let kinds = [
NodeKind::Operator,
NodeKind::Entity,
NodeKind::Record,
NodeKind::Aggregate,
NodeKind::Fulltext,
NodeKind::Vector,
NodeKind::Blob,
];
for kind in kinds {
let sentence = crate::parse::owns_one_storage(kind);
assert!(
sentence.contains(&format!("{} {}'s is", kind.article(), kind.noun())),
"{kind:?} is not named in its own law: {sentence}"
);
}
for (at, one) in kinds.iter().enumerate() {
for other in &kinds[at + 1..] {
assert_ne!(
crate::parse::owns_one_storage(*one),
crate::parse::owns_one_storage(*other),
"{one:?} and {other:?} read alike"
);
}
}
}