use ingraph_codegen::{
index::IndexModel,
prelude::{NodeKind, Projection},
};
use super::tests::{fulltext, refused};
fn vector(input: &syn::DeriveInput) -> syn::Result<IndexModel> {
let (host, arguments) = crate::host::split(input);
super::super::index::parse(&host, &arguments, NodeKind::Vector)
}
fn slots(model: &IndexModel) -> Vec<String> {
model
.slots()
.into_iter()
.map(|slot| format!("{}::{}", slot.field, slot.engine))
.collect()
}
fn document(title: &str, body: &str) -> syn::DeriveInput {
let title: proc_macro2::TokenStream = title.parse().expect("a marker list");
let body: proc_macro2::TokenStream = body.parse().expect("a marker list");
syn::parse_quote! {
#[ingraph(upstream(node = "asr::Segment", title = "speaker", body = "text"), dedup(hash))]
pub struct SearchDocument {
#[ingraph(#title)]
title: String,
#[ingraph(#body)]
body: String,
}
}
}
#[test]
fn a_bare_word_beside_the_marker_is_an_engine() {
let model = fulltext(&document("searchable, lexical", "searchable")).expect("accepted");
assert_eq!(slots(&model), ["title::lexical", "body::body"]);
}
#[test]
fn an_engine_block_carries_its_own_options_verbatim() {
let model = fulltext(&document(
"searchable, hnsw(ef = 200, m = 16)",
"searchable",
))
.expect("accepted");
let carried = model.engines[0].engines[0].options.to_string();
assert!(carried.contains("ef"), "the knobs are carried: {carried}");
assert!(carried.contains("200"), "with their values: {carried}");
assert!(carried.contains("16"), "all of them: {carried}");
}
#[test]
fn several_engines_keep_the_order_they_were_written_in() {
let model =
fulltext(&document("searchable, hnsw, lexical, sonic", "searchable")).expect("accepted");
assert_eq!(
slots(&model),
[
"title::hnsw",
"title::lexical",
"title::sonic",
"body::body"
]
);
}
#[test]
fn an_engine_may_be_written_before_the_marker() {
let model = fulltext(&document("hnsw, searchable, lexical", "searchable")).expect("accepted");
assert_eq!(
slots(&model),
["title::hnsw", "title::lexical", "body::body"]
);
}
#[test]
fn the_framework_words_are_still_the_framework_s() {
let model = fulltext(&document("searchable, optional, hnsw", "searchable")).expect("accepted");
assert_eq!(model.fields[0].project, Projection::Optional);
assert_eq!(model.searchable.len(), 2);
assert_eq!(slots(&model), ["title::hnsw", "body::body"]);
}
#[test]
fn two_fields_may_share_an_engine_and_that_is_two_slots() {
let model = fulltext(&document("searchable, lexical", "searchable, lexical")).expect("accepted");
assert_eq!(slots(&model), ["title::lexical", "body::lexical"]);
}
#[test]
fn fanout_over_two_engines_is_accepted_and_recorded() {
let model =
fulltext(&document("searchable, hnsw, lexical, fanout", "searchable")).expect("accepted");
assert!(model.engines[0].fanout);
assert!(!model.engines[1].fanout);
}
#[test]
fn portability_and_fanout_declare_the_same_slots() {
let portable = fulltext(&document("searchable, hnsw, lexical", "searchable")).expect("accepted");
let fanned =
fulltext(&document("searchable, hnsw, lexical, fanout", "searchable")).expect("accepted");
assert_eq!(slots(&portable), slots(&fanned));
assert!(!portable.engines[0].fanout);
assert!(fanned.engines[0].fanout);
}
#[test]
fn an_election_naming_no_engine_takes_its_own_field_name() {
let model = fulltext(&document("searchable", "searchable")).expect("accepted");
assert_eq!(slots(&model), ["title::title", "body::body"]);
assert_eq!(model.engines.len(), 2);
assert!(
model
.engines
.iter()
.flat_map(|declared| declared.engines.iter())
.all(|engine| engine.resource.is_none()),
"a synthesized leg pointed at something",
);
}
#[test]
fn one_engine_cannot_be_fanned() {
let refusal = refused(
document("searchable, lexical, fanout", "searchable"),
NodeKind::Fulltext,
);
assert!(
refusal.contains("nothing to fan between"),
"the refusal names what is missing: {refusal}"
);
assert!(
refusal.contains("`lexical`"),
"and the engine it did name: {refusal}"
);
}
#[test]
fn no_engine_cannot_be_fanned_either() {
let refusal = refused(
document("searchable, fanout", "searchable"),
NodeKind::Fulltext,
);
assert!(
refusal.contains("declares NO engine"),
"the refusal names the absence: {refusal}"
);
}
#[test]
fn fanout_takes_no_block() {
let refusal = refused(
document("searchable, hnsw, lexical, fanout(eager)", "searchable"),
NodeKind::Fulltext,
);
assert!(
refusal.contains("is a BARE marker"),
"the refusal says which shape it takes: {refusal}"
);
}
#[test]
fn an_engine_is_declared_once_per_field() {
let refusal = refused(
document("searchable, lexical, lexical", "searchable"),
NodeKind::Fulltext,
);
assert!(
refusal.contains("more than once on `title`"),
"the ordinary duplicate family: {refusal}"
);
assert!(
refusal.contains("TWO FIELDS sharing an engine"),
"and it says what the legal repeat is: {refusal}"
);
}
#[test]
fn an_engine_on_an_unelected_field_is_refused_by_name() {
let refusal = refused(document("searchable", "lexical"), NodeKind::Fulltext);
assert!(
refusal.contains("is not elected"),
"the refusal names the missing half: {refusal}"
);
assert!(
refusal.contains("#[ingraph(searchable)]"),
"and offers the marker: {refusal}"
);
}
#[test]
fn a_kind_word_is_not_written_at_an_election() {
let on_its_own_kind = refused(
document("searchable, tantivy", "searchable"),
NodeKind::Fulltext,
);
let on_the_other = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "asr::Segment", image = "coords"), dedup(hash))]
pub struct Frames {
#[ingraph(embedding, knn, tantivy)]
image: Embedding<f32, 4>,
}
},
NodeKind::Vector,
);
for refusal in [&on_its_own_kind, &on_the_other] {
assert!(
refusal.contains("kind is said ONCE, at the resource"),
"the refusal says where the word went: {refusal}"
);
assert!(
refusal.contains("#[ingraph::resource(tantivy)]"),
"and shows the declaration that says it: {refusal}"
);
assert!(
refusal.contains("resource = \"crate::Store\""),
"and how this leg points at one: {refusal}"
);
}
}
#[test]
fn a_free_word_on_the_vector_kind_is_accepted() {
let model = vector(&syn::parse_quote! {
#[ingraph(upstream(node = "asr::Segment", image = "coords"), dedup(hash))]
pub struct Frames {
#[ingraph(embedding, knn, lance, hnsw(ef = 200))]
image: Embedding<f32, 4>,
}
})
.expect("accepted");
assert_eq!(slots(&model), ["image::lance", "image::hnsw"]);
}
#[test]
fn the_persist_wrapper_on_an_index_field_is_refused_by_name() {
let refusal = refused(
document("searchable, persist(tantivy)", "searchable"),
NodeKind::Fulltext,
);
assert!(
refusal.contains("the ENGINE IS THE STORE"),
"the refusal says why there is no wrapper: {refusal}"
);
assert!(
refusal.contains("written BARE beside the marker"),
"and shows the spelling that replaces it: {refusal}"
);
}
#[test]
fn an_artifact_family_takes_no_engine_word() {
let refusal = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "asr::Segment", body = "text"), dedup(hash))]
pub struct Artifacts {
#[ingraph(tantivy)]
body: String,
}
},
NodeKind::Blob,
);
assert!(
refusal.contains("reads as an ENGINE"),
"the refusal names what the word looks like: {refusal}"
);
assert!(
refusal.contains("elects nothing"),
"and why this kind has no use for one: {refusal}"
);
}
#[test]
fn fanout_on_a_kind_that_elects_nothing_is_refused_by_name() {
let refusal = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "asr::Segment", body = "text"), dedup(hash))]
pub struct Artifacts {
#[ingraph(fanout)]
body: String,
}
},
NodeKind::Blob,
);
assert!(
refusal.contains("declares none"),
"the refusal names the absence: {refusal}"
);
}
#[test]
fn an_engine_word_is_one_segment() {
let refusal = refused(
document("searchable, my::engine", "searchable"),
NodeKind::Fulltext,
);
assert!(
refusal.contains("an engine is named by ONE word"),
"the refusal states the shape: {refusal}"
);
}
#[test]
fn an_engine_is_a_block_and_not_a_value() {
let refusal = refused(
document("searchable, hnsw = \"app::Hnsw\"", "searchable"),
NodeKind::Fulltext,
);
assert!(
refusal.contains("declared as a BLOCK"),
"the refusal states the shape: {refusal}"
);
}
#[test]
fn the_head_storage_block_is_not_where_the_engines_are_named() {
let refusal = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "asr::Segment", body = "text"), fulltext(tantivy), dedup(hash))]
pub struct SearchDocument {
#[ingraph(searchable)]
body: String,
}
},
NodeKind::Fulltext,
);
assert!(
refusal.contains("a slot is one FIELD in one engine"),
"the refusal says why the head is the wrong grain: {refusal}"
);
assert!(
!refusal.contains("has not arrived"),
"and it is no longer a wait: {refusal}"
);
}