use crate::{
engine::{Engine, FieldEngines},
index::{Dedup, Elected, ElectionLabel, Fold, IndexGraphql, IndexModel, Upstream},
model::{GraphqlKind, NodeKind},
prefix::EmissionPrefix,
};
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn elected(field: &str, label: ElectionLabel, fold: Option<Fold>) -> Elected {
Elected::new(
ident(field),
label,
syn::parse_quote!(::ingraph::Embedding<f32, 4, Clip>),
fold,
)
}
fn set(field: &str, fold: Option<Fold>) -> Elected {
Elected::new(
ident(field),
ElectionLabel::List,
syn::parse_quote!(::std::vec::Vec<::ingraph::Embedding<f32, 4, Clip>>),
fold,
)
}
fn engines(field: &str, words: &[&str], fanout: bool) -> FieldEngines {
FieldEngines {
field: ident(field),
engines: words.iter().map(|word| Engine::bare(ident(word))).collect(),
fanout,
}
}
fn index(name: &str, kind: NodeKind) -> IndexModel {
IndexModel {
prefix: EmissionPrefix::default(),
numbers: crate::index::Numbers::default(),
field_numbers: Vec::new(),
index: ident(name),
kind,
vis: syn::parse_quote!(pub),
krate: syn::parse_quote!(::ingraph),
source: Upstream {
node: syn::parse_quote!(Caption),
projections: Vec::new(),
},
dedup: Dedup::Hash,
embeddings: Vec::new(),
labelled: Vec::new(),
searchable: Vec::new(),
engines: Vec::new(),
stores: Vec::new(),
extension: None,
data: false,
keys: false,
subscribe: false,
graphql: Some(IndexGraphql::of(GraphqlKind::AsyncGraphql)),
rename: None,
slot: None,
fields: Vec::new(),
}
}
pub(super) fn one_slot() -> IndexModel {
let mut model = index("Captions", NodeKind::Vector);
model.embeddings = vec![elected("image_embedding", ElectionLabel::Embedding, None)];
model.engines = vec![engines("image_embedding", &["hnsw"], false)];
model
}
pub(super) fn two_slots() -> IndexModel {
let mut model = index("Frames", NodeKind::Vector);
model.embeddings = vec![
set(
"frames",
Some(Fold::Custom(syn::parse_quote!(app::PositionWeighted))),
),
elected("poster", ElectionLabel::Embedding, None),
];
model.engines = vec![
engines("frames", &["hnsw"], false),
engines("poster", &["hnsw"], false),
];
model
}
pub(super) fn fanned() -> IndexModel {
let mut model = index("Shots", NodeKind::Vector);
model.embeddings = vec![elected("clip", ElectionLabel::Embedding, None)];
model.engines = vec![engines("clip", &["hnsw", "lance"], true)];
model
}
pub(super) fn fulltext() -> IndexModel {
let mut model = index("Transcript", NodeKind::Fulltext);
model.searchable = vec![ident("body"), ident("title")];
model.engines = vec![
engines("body", &["tantivy"], false),
engines("title", &["tantivy"], false),
];
model
}
pub(super) fn blob() -> IndexModel {
let mut model = index("Posters", NodeKind::Blob);
model.stores = vec![Engine::bare(ident("fs"))];
model
}
fn compact(model: &IndexModel) -> String {
model.tier().emit().to_string().split_whitespace().collect()
}
fn rendered(model: &IndexModel) -> String {
let tokens = model.tier().emit();
syn::parse2::<syn::File>(tokens.clone())
.map_or_else(|_| tokens.to_string(), |file| prettyplease::unparse(&file))
}
#[test]
fn the_binding_table_picks_one_package_per_tier_shape() {
assert_eq!(
one_slot()
.tier()
.options_name()
.map(|name| name.to_string()),
Some("CaptionsImageEmbeddingVectorOptions".to_owned()),
"a single-slot vector tier IS its leaf",
);
assert_eq!(
fanned().tier().options_name().map(|name| name.to_string()),
Some("ShotsClipFuseOptions".to_owned()),
"a fanned slot is a tier, not a leaf",
);
assert_eq!(
two_slots()
.tier()
.options_name()
.map(|name| name.to_string()),
Some("FramesFuseOptions".to_owned()),
"several elections fuse",
);
assert_eq!(
fulltext()
.tier()
.options_name()
.map(|name| name.to_string()),
Some("TranscriptFulltextOptions".to_owned()),
"a full-text declaration has ONE subject however many fields it elects",
);
}
#[test]
fn the_question_tree_binds_by_the_same_table() {
assert_eq!(
one_slot()
.tier()
.question_name()
.map(|name| name.to_string()),
Some("CaptionsImageEmbeddingVectorQuestion".to_owned()),
);
assert_eq!(
fanned().tier().question_name().map(|name| name.to_string()),
Some("ShotsClipVectorQuestion".to_owned()),
"the legs are ONE field's coordinates in several stores, so the question does not descend",
);
assert_eq!(
two_slots()
.tier()
.question_name()
.map(|name| name.to_string()),
Some("FramesFuseQuestion".to_owned()),
);
assert_eq!(
fulltext()
.tier()
.question_name()
.map(|name| name.to_string()),
Some("TranscriptFulltextQuestion".to_owned()),
);
}
#[test]
fn every_retrieving_tier_binds_both_seats_through_fusable() {
let emitted = compact(&two_slots());
assert!(
emitted.contains("::retrieve::FusableforFrames"),
"{}",
rendered(&two_slots()),
);
assert!(
emitted.contains("typeOptions=FramesFuseOptions;"),
"{}",
rendered(&two_slots()),
);
assert!(
emitted.contains("typeQuestion=FramesFuseQuestion;"),
"{}",
rendered(&two_slots()),
);
}
#[test]
fn a_fulltext_tier_emits_no_vector_flavoured_type() {
let emitted = compact(&fulltext());
for absent in [
"VectorOptions",
"VectorQuestion",
"Semantics",
"coordinates",
] {
assert!(
!emitted.contains(absent),
"`{absent}` reached a full-text declaration:\n{}",
rendered(&fulltext()),
);
}
assert!(
emitted.contains("structTranscriptFulltextOptions"),
"{}",
rendered(&fulltext())
);
}
#[test]
fn a_vector_tier_emits_no_fulltext_flavoured_type() {
let emitted = compact(&two_slots());
assert!(
!emitted.contains("FulltextOptions"),
"{}",
rendered(&two_slots()),
);
assert!(
!emitted.contains("FulltextQuestion"),
"{}",
rendered(&two_slots()),
);
}
#[test]
fn a_blob_family_emits_no_fusion_tree_at_all() {
assert!(compact(&blob()).is_empty(), "{}", rendered(&blob()),);
assert_eq!(blob().tier().options_name(), None);
assert_eq!(blob().tier().question_name(), None);
}
#[test]
fn every_package_row_is_an_option_because_every_one_defers() {
let emitted = compact(&two_slots());
for row in [
"semantics:::core::option::Option<FramesFramesSemantics>",
"fusion:::core::option::Option<::ingraph::__private::retrieve::FusionOptions>",
"question:::core::option::Option<::std::string::String>",
] {
assert!(
emitted.contains(row),
"`{row}` is missing:\n{}",
rendered(&two_slots()),
);
}
}
#[test]
fn a_package_is_an_options_pattern_whole() {
let emitted = compact(&one_slot());
assert!(
emitted.contains("constfnnew()->Self"),
"{}",
rendered(&one_slot())
);
assert!(
compact(&two_slots()).contains("semantics:::core::option::Option::None"),
"`new()` names nothing:\n{}",
rendered(&two_slots()),
);
assert!(
emitted.contains("fndefault()->Self{Self::new()}"),
"{}",
rendered(&one_slot()),
);
let two = compact(&two_slots());
assert!(two.contains("fnwith_semantics(mutself,"), "{two}");
assert!(
two.contains("constfnsemantics(&self)->::core::option::Option<&FramesFramesSemantics>"),
"{two}",
);
assert!(
emitted.contains("pubstructCaptionsImageEmbeddingVectorOptions{}"),
"{}",
rendered(&one_slot()),
);
}
#[test]
fn every_route_of_a_tier_wears_the_edge_wrapper() {
let emitted = compact(&two_slots());
assert!(
emitted.contains("frames:::core::option::Option<::ingraph::__private::retrieve::Route<FramesFramesVectorOptions>>"),
"{}",
rendered(&two_slots()),
);
assert!(
emitted.contains("poster:::core::option::Option<::ingraph::__private::retrieve::Route<FramesPosterVectorOptions>>"),
"{}",
rendered(&two_slots()),
);
}
#[test]
fn a_fanned_slot_becomes_a_tier_with_one_route_per_leg() {
let emitted = compact(&fanned());
assert!(
emitted.contains("structShotsClipHnswLegOptions"),
"{}",
rendered(&fanned())
);
assert!(
emitted.contains("structShotsClipLanceLegOptions"),
"{}",
rendered(&fanned())
);
assert!(
emitted.contains("structShotsClipFuseOptions"),
"{}",
rendered(&fanned())
);
assert!(
emitted.contains(
"hnsw:::core::option::Option<::ingraph::__private::retrieve::Route<ShotsClipHnswLegOptions>>"
),
"{}",
rendered(&fanned()),
);
assert!(
emitted.contains("lance:::core::option::Option<::ingraph::__private::retrieve::Route<ShotsClipLanceLegOptions>>"),
"{}",
rendered(&fanned()),
);
assert!(
emitted
.contains("fusion:::core::option::Option<::ingraph::__private::retrieve::FusionOptions>"),
"a mini tier carries its own algorithm:\n{}",
rendered(&fanned()),
);
}
#[test]
fn a_portable_slot_stays_a_leaf() {
let mut model = fanned();
model.engines = vec![engines("clip", &["hnsw", "lance"], false)];
let emitted = compact(&model);
assert!(!emitted.contains("LegOptions"), "{}", rendered(&model),);
assert!(
emitted.contains("structShotsClipVectorOptions"),
"{}",
rendered(&model)
);
}
#[test]
fn a_one_route_tier_mints_no_fuse_package() {
let emitted = compact(&one_slot());
assert!(
emitted.contains("pubstructCaptionsImageEmbeddingVectorOptions"),
"the leaf package the absences are measured against is missing:\n{}",
rendered(&one_slot()),
);
assert!(
!emitted.contains("CaptionsFuseOptions"),
"{}",
rendered(&one_slot()),
);
assert!(
!emitted.contains("CaptionsFuseQuestion"),
"{}",
rendered(&one_slot()),
);
}
#[test]
fn a_set_election_mints_the_wire_fold_vocabulary() {
let emitted = compact(&two_slots());
assert!(
emitted.contains("enumFramesFramesSemantics{"),
"{}",
rendered(&two_slots())
);
for variant in ["Sum,", "Max,", "PositionWeighted,"] {
assert!(
emitted.contains(variant),
"`{variant}` is missing:\n{}",
rendered(&two_slots()),
);
}
}
#[test]
fn a_declared_fold_joins_the_citizens_and_becomes_the_default() {
let emitted = compact(&two_slots());
assert!(
emitted.contains("constDECLARED:Self=Self::PositionWeighted;"),
"{}",
rendered(&two_slots()),
);
}
#[test]
fn an_election_naming_no_fold_declares_the_shipped_citizen() {
let mut model = two_slots();
model.embeddings = vec![set("frames", None)];
model.engines = vec![engines("frames", &["hnsw"], false)];
let emitted = compact(&model);
assert!(
emitted.contains("constDECLARED:Self=Self::Sum;"),
"{}",
rendered(&model),
);
assert!(
!emitted.contains("PositionWeighted"),
"an election that named nothing mints no custom word:\n{}",
rendered(&model),
);
}
#[test]
fn a_single_election_mints_no_fold_vocabulary() {
let emitted = compact(&one_slot());
assert!(!emitted.contains("Semantics"), "{}", rendered(&one_slot()));
assert!(!emitted.contains("semantics:"), "{}", rendered(&one_slot()));
}
#[test]
fn the_fold_word_crosses_back_through_a_visitor() {
let emitted = compact(&two_slots());
assert!(
emitted.contains("fnwith_fold<__INGRAPH_SCORE__,__INGRAPH_VISITOR__>"),
"{}",
rendered(&two_slots()),
);
for arm in [
"Self::Sum=>::ingraph::__private::retrieve::FoldVisitor::visit(__ingraph_visitor,<::ingraph::__private::retrieve::Sumas::core::default::Default>::default())",
"Self::Max=>::ingraph::__private::retrieve::FoldVisitor::visit(__ingraph_visitor,<::ingraph::__private::retrieve::Maxas::core::default::Default>::default())",
"Self::PositionWeighted=>::ingraph::__private::retrieve::FoldVisitor::visit(__ingraph_visitor,<app::PositionWeightedas::core::default::Default>::default())",
] {
assert!(
emitted.contains(arm),
"the arm is missing:\n{}",
rendered(&two_slots()),
);
}
}
#[test]
fn the_fold_words_render_in_the_graphql_namespaces_casing() {
let emitted = compact(&two_slots());
for word in [
r#"name:"SUM""#,
r#"name:"MAX""#,
r#"name:"POSITION_WEIGHTED""#,
] {
assert!(
emitted.contains(word),
"`{word}` is missing:\n{}",
rendered(&two_slots()),
);
}
}
#[test]
fn every_options_package_carries_a_hand_written_input_face() {
let emitted = compact(&two_slots());
assert!(
emitted.contains("::async_graphql::InputTypeforFramesFuseOptions"),
"{}",
rendered(&two_slots()),
);
assert!(
emitted.contains("::async_graphql::InputObjectTypeforFramesFuseOptions"),
"{}",
rendered(&two_slots()),
);
assert!(
emitted.contains("oneof:false"),
"a package is an ordinary input object; the @oneOf flag is the FLAVOUR's:\n{}",
rendered(&two_slots()),
);
}
#[test]
fn the_question_tree_crosses_as_text_and_leaves_its_coordinates_behind() {
let emitted = compact(&two_slots());
for faced in [
"::async_graphql::InputTypeforFramesFuseQuestion",
"::async_graphql::InputTypeforFramesFramesVectorQuestion",
] {
assert!(emitted.contains(faced), "{}", rendered(&two_slots()));
}
assert!(
emitted.contains("coordinates:::core::option::Option<"),
"{}",
rendered(&two_slots()),
);
assert!(
!emitted.contains("\"coordinates\""),
"a row with no wire form reached the wire:\n{}",
rendered(&two_slots()),
);
assert!(
emitted.contains("coordinates:::core::option::Option::None"),
"the read does not fill the absent row:\n{}",
rendered(&two_slots()),
);
}
#[test]
fn a_route_is_asked_in_the_elections_own_point() {
let emitted = compact(&two_slots());
assert!(
emitted.contains("coordinates:::core::option::Option<::std::vec::Vec<<::std::vec::Vec<"),
"the SET election is asked with a set of its points:\n{}",
rendered(&two_slots()),
);
assert!(
emitted.contains("coordinates:::core::option::Option<<::ingraph::Embedding<f32,4,Clip>as"),
"the SINGLE election is asked with one point:\n{}",
rendered(&two_slots()),
);
assert!(
emitted.contains("EmbeddingMarker>::Item"),
"the point is PROJECTED off the carrier:\n{}",
rendered(&two_slots()),
);
}
#[test]
fn every_tier_shape_parses_as_a_file() {
for model in [one_slot(), two_slots(), fanned(), fulltext()] {
syn::parse2::<syn::File>(model.tier().emit())
.unwrap_or_else(|error| panic!("the tier parses as a file: {error}"));
}
}
#[test]
fn a_custom_fold_colliding_with_a_citizens_word_is_refused_by_name() {
let mut model = two_slots();
model.embeddings = vec![set(
"frames",
Some(Fold::Custom(syn::parse_quote!(app::Sum))),
)];
model.engines = vec![engines("frames", &["hnsw"], false)];
let emitted = compact(&model);
assert!(emitted.contains("compile_error!"), "{}", rendered(&model));
assert!(
emitted.contains("app::Sum"),
"the refusal names the path its author wrote:\n{}",
rendered(&model),
);
assert!(
!emitted.contains("enumFramesFramesSemantics"),
"no vocabulary is minted beside the refusal:\n{}",
rendered(&model),
);
}
#[test]
fn a_declaration_that_elects_a_framework_word_is_refused_by_name() {
let mut model = two_slots();
model.embeddings = vec![
elected("fusion", ElectionLabel::Embedding, None),
elected("poster", ElectionLabel::Embedding, None),
];
model.engines = vec![
engines("fusion", &["hnsw"], false),
engines("poster", &["hnsw"], false),
];
let emitted = compact(&model);
assert!(emitted.contains("compile_error!"), "{}", rendered(&model));
assert!(
emitted.contains("tworowsnamed`fusion`"),
"the refusal names the row:\n{}",
rendered(&model),
);
}
#[test]
fn the_question_tree_is_covered_by_the_same_check() {
let mut model = two_slots();
model.embeddings = vec![
elected("question", ElectionLabel::Embedding, None),
elected("poster", ElectionLabel::Embedding, None),
];
model.engines = vec![
engines("question", &["hnsw"], false),
engines("poster", &["hnsw"], false),
];
assert!(
compact(&model).contains("tworowsnamed`question`"),
"{}",
rendered(&model),
);
}