use ingraph_codegen::{
index::{ElectionBackend, Field, IndexModel},
model::GraphqlKind,
prelude::NodeKind,
};
use super::tests::{fulltext, refused};
fn elected(block: proc_macro2::TokenStream) -> syn::Result<IndexModel> {
let (host, arguments) = crate::host::split(&syn::parse_quote! {
#[ingraph(upstream(node = "a::A", clip = "clip"), dedup(hash))]
struct D {
#[ingraph(embedding, knn, lance(resource = "crate::Lance"), graphql(async_graphql(#block)))]
clip: ::ingraph::Embedding<f32, 4, Clip>,
}
});
super::super::index::parse(&host, &arguments, NodeKind::Vector)
}
fn skin(model: &IndexModel) -> &ElectionBackend {
model
.numbers_of(&syn::Ident::new("clip", proc_macro2::Span::call_site()))
.expect("the field wrote a block")
.skin(GraphqlKind::AsyncGraphql)
.expect("the block named `async_graphql`")
}
fn refusals(block: proc_macro2::TokenStream) -> String {
elected(block)
.expect_err("the block is refused")
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join("\n")
}
#[test]
fn every_road_word_reaches_its_own_seat() {
for road in Field::ALL {
let word = road.as_str();
let written = format!("{word}(rename = \"aWord\")");
let block: proc_macro2::TokenStream = written.parse().expect("a block entry");
let model = elected(block).unwrap_or_else(|_| panic!("`{written}` is legal"));
let skin = skin(&model);
assert_eq!(
skin.road(road).map(syn::LitStr::value),
Some("aWord".to_owned()),
"`{word}` did not fill its own seat",
);
for other in Field::ALL.into_iter().filter(|other| *other != road) {
assert!(
skin.road(other).is_none(),
"`{word}` also filled `{}`",
other.as_str(),
);
}
}
}
#[test]
fn the_type_seats_are_read_and_reach_neither_road_nor_number() {
let model = elected(quote::quote! {
question(rename = "ClipAsk"),
engines(rename = "ClipLegs"),
sighting(rename = "ClipAgreement"),
fused_key_match(rename = "ClipLeader")
})
.expect("the four type seats are legal");
let skin = skin(&model);
for (read, spelled) in [
(skin.question.as_ref(), "ClipAsk"),
(skin.engines.as_ref(), "ClipLegs"),
(skin.sighting.as_ref(), "ClipAgreement"),
(skin.fused_key_match.as_ref(), "ClipLeader"),
] {
assert_eq!(read.map(syn::LitStr::value), Some(spelled.to_owned()));
}
assert!(Field::ALL.into_iter().all(|road| skin.road(road).is_none()));
assert!(skin.first.is_none());
}
#[test]
fn the_answer_seats_are_two_and_the_arms_are_neither() {
let union =
elected(quote::quote! { sighting(rename = "ClipAgreement") }).expect("`sighting` is a seat");
assert_eq!(
skin(&union).sighting.as_ref().map(syn::LitStr::value),
Some("ClipAgreement".to_owned()),
);
assert!(
skin(&union).fused_key_match.is_none(),
"the union's word filled the row's seat as well",
);
let row = elected(quote::quote! { fused_key_match(rename = "ClipLeader") })
.expect("`fused_key_match` is a seat");
assert_eq!(
skin(&row).fused_key_match.as_ref().map(syn::LitStr::value),
Some("ClipLeader".to_owned()),
);
assert!(
skin(&row).sighting.is_none(),
"the row's word filled the union's seat as well",
);
let refused = refusals(quote::quote! { sightings(rename = "aWord") });
assert!(
refused.contains("unknown entry in a backend block on `clip`"),
"{refused}",
);
assert!(
refused.contains("did you mean `sighting`"),
"the near miss is measured against the seat that does exist: {refused}",
);
}
#[test]
fn an_answer_seat_takes_one_name_and_that_name_is_a_wire_name() {
for word in ["sighting", "fused_key_match"] {
let twice: proc_macro2::TokenStream = format!("{word}(rename = \"A\", rename = \"B\")")
.parse()
.expect("a block entry");
let refused = refusals(twice);
assert!(
refused.contains(&format!("declared more than once in `{word}(…)`")),
"`{word}` took two names: {refused}",
);
let illegal: proc_macro2::TokenStream = format!("{word}(rename = \"9lives\")")
.parse()
.expect("a block entry");
let refused = refusals(illegal);
assert!(
refused.contains("9lives"),
"`{word}` published a name a schema cannot export: {refused}",
);
let empty: proc_macro2::TokenStream = format!("{word}(rename = \"\")")
.parse()
.expect("a block entry");
let refused = refusals(empty);
assert!(
!refused.is_empty(),
"`{word}` accepted the empty name: {refused}",
);
}
}
#[test]
fn the_count_is_written_flat_and_takes_no_rename_row() {
let model = elected(quote::quote! { first = 7 }).expect("`first = N` is the spelling");
assert_eq!(skin(&model).first.map(core::num::NonZeroU32::get), Some(7));
let refused = refusals(quote::quote! { first(rename = "howMany") });
assert!(
refused.contains("is a COUNT"),
"the refusal says what the word takes: {refused}",
);
}
#[test]
fn the_shipped_flat_spelling_is_refused_with_the_block_that_replaced_it() {
for word in ["keys", "fused", "similar"] {
let written = format!("{word} = \"aWord\"");
let block: proc_macro2::TokenStream = written.parse().expect("a block entry");
let refused = refusals(block);
assert!(
refused.contains(&format!("`{word}(…)` needs `rename = \"…\"`")),
"`{written}` was not sent to its block: {refused}",
);
assert!(
refused.contains("naming the field is the only thing it is for"),
"and the sentence names the AXIS — a road names a field, not a type: {refused}",
);
}
}
#[test]
fn an_unknown_word_is_measured_against_the_closed_set() {
let refused = refusals(quote::quote! { hydrate(rename = "aWord") });
assert!(
refused.contains("unknown entry in a backend block on `clip`"),
"{refused}",
);
assert!(
refused.contains("did you mean `hydrated`"),
"a near miss is measured against the road words: {refused}",
);
assert!(
refused.contains("`similar_hydrated(rename = \"…\")`"),
"the sentence spells the seats it offers: {refused}",
);
}
#[test]
fn a_road_name_is_held_to_the_wire_alphabet() {
let refused = refusals(quote::quote! { keys(rename = "9lives") });
assert!(refused.contains("9lives"), "{refused}");
}
#[test]
fn a_rust_keyword_is_a_legal_name_at_every_seat_in_this_block() {
let model = elected(quote::quote! {
question(rename = "type"), keys(rename = "where")
})
.expect("GraphQL's keywords are contextual and Rust's are not its business");
assert_eq!(
skin(&model).question.as_ref().map(syn::LitStr::value),
Some("type".to_owned()),
);
assert_eq!(
skin(&model).road(Field::Keys).map(syn::LitStr::value),
Some("where".to_owned()),
);
}
#[test]
fn a_seat_is_named_once() {
let refused = refusals(quote::quote! { keys(rename = "a", rename = "b") });
assert!(
refused.contains("declared more than once in `keys(…)`"),
"{refused}",
);
}
#[test]
fn the_frameworks_own_vocabulary_is_refused_at_an_election() {
for word in [
"score",
"rank",
"streams",
"ordinal",
"contribution",
"node",
"coordinates",
"key",
] {
let written = format!("{word}(rename = \"mine\")");
let block: proc_macro2::TokenStream = written.parse().expect("a block entry");
let refused = refusals(block);
assert!(
refused.contains(&format!("`{word}` is the FRAMEWORK's own word")),
"`{written}` was not refused as the floor: {refused}",
);
assert!(
refused.contains("a client written against one schema reads the next"),
"and the sentence says what the floor buys: {refused}",
);
assert!(
refused.contains("What IS nameable here is the TYPE behind an argument"),
"and where the author's coherent idea DOES have a seat: {refused}",
);
}
}
#[test]
fn the_retired_row_spelling_falls_to_the_unknown_entry_sentence() {
let refused = refusals(quote::quote! { entity(rename = "mine") });
assert!(
!refused.contains("is the FRAMEWORK's own word"),
"the floor still claims the retired spelling: {refused}",
);
assert!(
refused.contains("unknown entry in a backend block on `clip`"),
"{refused}",
);
assert!(
refused.contains("question(rename = \"…\")"),
"the unknown-entry sentence does not list the seats: {refused}",
);
}
#[test]
fn the_frameworks_own_vocabulary_is_refused_at_the_head() {
let refused = refused(
syn::parse_quote! {
#[ingraph(
upstream(node = "a::A", body = "text"),
dedup(hash),
graphql(async_graphql(score(rename = "points"))),
)]
struct D { #[ingraph(searchable)] body: String }
},
NodeKind::Fulltext,
);
assert!(
refused.contains("`score` is the FRAMEWORK's own word"),
"{refused}"
);
assert!(
refused.contains("`options(rename = \"…\")`"),
"and the head's own three seats are the ones it offers: {refused}",
);
}
#[test]
fn the_one_floor_row_with_a_seat_elsewhere_keeps_the_more_specific_answer() {
let refused = refused(
syn::parse_quote! {
#[ingraph(
upstream(node = "a::A", body = "text"),
dedup(hash),
graphql(async_graphql(evidence(rename = "WhoAgreed"))),
)]
struct D { #[ingraph(searchable)] body: String }
},
NodeKind::Fulltext,
);
assert!(
!refused.contains("is the FRAMEWORK's own word"),
"the general sentence displaced the specific one: {refused}",
);
assert!(
refused.contains("The RUST family is re-based by the top-level"),
"{refused}",
);
}
#[test]
fn the_block_reaches_a_full_text_election_by_the_same_words() {
let model = fulltext(&syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text"), dedup(hash))]
struct D {
#[ingraph(searchable, graphql(async_graphql(keys(rename = "bodyHits"))))]
body: String,
}
})
.expect("a full-text election dresses itself the same way");
assert_eq!(
model
.numbers_of(&syn::Ident::new("body", proc_macro2::Span::call_site()))
.and_then(|written| written.skin(GraphqlKind::AsyncGraphql))
.and_then(|skin| skin.road(Field::Keys))
.map(syn::LitStr::value),
Some("bodyHits".to_owned()),
);
}