use quote::ToTokens as _;
use super::{IndexModel, NodeKind};
use crate::engine::{Engine, FieldEngines};
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn compact(model: &IndexModel) -> String {
model
.emit()
.to_token_stream()
.to_string()
.split_whitespace()
.collect()
}
fn fulltext(title: Option<syn::Path>, body: Option<syn::Path>) -> IndexModel {
let mut model = super::answers::plain();
model.kind = NodeKind::Fulltext;
model.embeddings = Vec::new();
model.searchable = vec![ident("title"), ident("body")];
model.engines = vec![
FieldEngines {
field: ident("title"),
engines: vec![Engine {
resource: title,
..Engine::bare(ident("lexical"))
}],
fanout: false,
},
FieldEngines {
field: ident("body"),
engines: vec![Engine {
resource: body,
stored: true,
..Engine::bare(ident("lexical"))
}],
fanout: false,
},
];
model
}
fn vector(resource: Option<syn::Path>) -> IndexModel {
let mut model = super::answers::plain();
model.engines = vec![FieldEngines {
field: ident("clip"),
engines: vec![Engine {
resource,
..Engine::bare(ident("toy"))
}],
fanout: false,
}];
model
}
#[test]
fn two_legs_pointing_at_one_target_share_one_impl() {
let emitted = compact(&fulltext(
Some(syn::parse_quote!(crate::Ft)),
Some(syn::parse_quote!(crate::Ft)),
));
assert_eq!(
emitted.matches("implDescribe<crate::Ft>forShots").count()
+ emitted
.matches("impl::ingraph::__private::schema::Describe<crate::Ft>forShots")
.count(),
1,
"one target, one impl: {emitted}"
);
assert!(
emitted.contains(
"::ingraph::__private::slot::SlotName::new(\"shots__title__lexical\"),&[::ingraph::__privat\
e::transform::SlotColumn::text(\"title\")],false)"
),
"{emitted}"
);
assert!(
emitted.contains(
"::ingraph::__private::slot::SlotName::new(\"shots__body__lexical\"),&[::ingraph::__private\
::transform::SlotColumn::text(\"body\")],true)"
),
"the `stored` word rides the blueprint: {emitted}"
);
}
#[test]
fn two_targets_are_two_impls_and_two_steps() {
let emitted = compact(&fulltext(
Some(syn::parse_quote!(crate::Ft)),
Some(syn::parse_quote!(crate::Remote)),
));
assert!(emitted.contains("Describe<crate::Ft>forShots"), "{emitted}");
assert!(
emitted.contains("Describe<crate::Remote>forShots"),
"{emitted}"
);
for resource in ["crate::Ft", "crate::Remote"] {
assert!(
emitted.contains(&format!(
"::ingraph::__private::drive::Creation::habitat::<{resource},::ingraph::__private::operator\
::Selected<{{<{resource}as::ingraph::__private::resource::DeclaredResource>::SYNC}}>,Self,\
>(__ingraph_creation).await?;"
)),
"the step reads the target's own MODE off its declaration: {emitted}"
);
}
}
#[test]
fn the_pointed_targets_are_stated_as_demands() {
let one = compact(&fulltext(
Some(syn::parse_quote!(crate::Ft)),
Some(syn::parse_quote!(crate::Ft)),
));
assert!(
one.contains(
"constTARGETS:&'static[fn()->::ingraph::__private::resource::ResourceDemand]=&[::ingraph::__p\
rivate::resource::ResourceDemand::of::<crate::Ft>,];"
),
"two legs in one mount are ONE demand: {one}"
);
let two = compact(&fulltext(
Some(syn::parse_quote!(crate::Ft)),
Some(syn::parse_quote!(crate::Remote)),
));
assert!(
two.contains(
"&[::ingraph::__private::resource::ResourceDemand::of::<crate::Ft>,::ingraph::__private::reso\
urce::ResourceDemand::of::<crate::Remote>,];"
),
"two targets are two demands, in the order the legs named them: {two}"
);
}
#[test]
fn a_declaration_with_no_pointed_leg_states_no_mount() {
let emitted = compact(&fulltext(None, None));
assert!(!emitted.contains("constTARGETS:"), "{emitted}");
}
#[test]
fn an_unpointed_leg_becomes_an_unbound_row() {
let emitted = compact(&fulltext(Some(syn::parse_quote!(crate::Ft)), None));
assert!(
emitted.contains(
"constUNBOUND:&'static[::ingraph::__private::drive::UnboundCorpus]=&[::ingraph::__private::dr\
ive::UnboundCorpus::new(::ingraph::__private::slot::SlotName::new(\"shots__body__lexical\"\
),::ingraph::__private::resource::ResourceKind::Documents),];"
),
"{emitted}"
);
assert!(
!emitted.contains("Describe<crate::Remote>"),
"an unpointed leg names no target: {emitted}"
);
}
#[test]
fn a_declaration_with_no_pointed_leg_emits_no_walk() {
let emitted = compact(&fulltext(None, None));
assert!(emitted.contains("constUNBOUND:"), "{emitted}");
assert!(
!emitted.contains("fnmake("),
"nothing to make, so nothing is written: {emitted}"
);
}
#[test]
fn a_vector_corpus_carries_the_width_its_field_declares() {
let emitted = compact(&vector(Some(syn::parse_quote!(crate::Vectors))));
assert!(
emitted.contains("::ingraph::__private::transform::SlotSpec::new("),
"{emitted}"
);
assert!(
emitted.contains(
"::ingraph::__private::transform::SlotColumn::new(\"clip\",::ingraph::__private::store::Colum\
nDoor::Bytes)"
),
"a coordinate crosses as bytes: {emitted}"
);
assert!(
emitted.contains(
"<<::ingraph::Embedding<f32,4,Clip>as::ingraph::__private::election::EmbeddingMarker>::Itemas::in\
graph::__private::election::EmbeddingShape>::WIDTH"
),
"the width is the FIELD's, projected rather than restated: {emitted}"
);
assert!(
emitted.contains("::ingraph::__private::resource::ResourceKind::Coordinates")
|| !emitted.contains("constUNBOUND"),
"a pointed vector leg leaves no unbound row: {emitted}"
);
}
#[test]
fn a_blob_declaration_owns_no_corpus_and_says_so() {
let mut model = super::answers::plain();
model.kind = NodeKind::Blob;
model.embeddings = Vec::new();
model.engines = Vec::new();
let emitted = compact(&model);
assert!(
emitted.contains("impl::ingraph::__private::drive::HabitatsforShots{}"),
"{emitted}"
);
}