use crate::{
engine::{Engine, FieldEngines},
index::{Dedup, Elected, ElectionLabel, IndexModel, Upstream},
model::NodeKind,
prefix::EmissionPrefix,
};
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn leg(word: &str, rrf: bool, typed: bool) -> Engine {
Engine {
rrf,
resource: typed.then(|| syn::parse_quote!(crate::Store)),
..Engine::bare(ident(word))
}
}
fn fanned(legs: Vec<Engine>) -> IndexModel {
IndexModel {
prefix: EmissionPrefix::default(),
numbers: crate::index::Numbers::default(),
field_numbers: Vec::new(),
index: ident("Shots"),
kind: NodeKind::Vector,
vis: syn::parse_quote!(pub),
krate: syn::parse_quote!(::ingraph),
source: Upstream {
node: syn::parse_quote!(Keyframe),
projections: Vec::new(),
},
dedup: Dedup::Hash,
embeddings: vec![Elected::new(
ident("clip"),
ElectionLabel::Embedding,
syn::parse_quote!(::ingraph::Embedding<f32, 4, Clip>),
None,
)],
labelled: Vec::new(),
searchable: Vec::new(),
engines: vec![FieldEngines {
field: ident("clip"),
engines: legs,
fanout: true,
}],
stores: Vec::new(),
data: false,
keys: false,
subscribe: false,
extension: None,
graphql: None,
rename: None,
slot: None,
fields: Vec::new(),
}
}
fn tight(model: &IndexModel) -> String {
model.emit().to_string().split_whitespace().collect()
}
fn packages(model: &IndexModel) -> String {
model.tier().emit().to_string().split_whitespace().collect()
}
#[test]
fn the_fused_face_is_present_exactly_where_two_legs_participate() {
for (participants, tiered) in [(0_usize, false), (1, false), (2, true), (3, true)] {
let legs = (0..3)
.map(|at| leg(["lance", "hnsw", "faiss"][at], at < participants, false))
.collect();
let model = fanned(legs);
let emitted = tight(&model);
assert_eq!(
emitted.contains("structShotsClipFuseOptions"),
tiered,
"{participants} participants minted the wrong slot package: {emitted}",
);
assert_eq!(
emitted.contains("structShotsClipVectorOptions"),
!tiered,
"{participants} participants minted the wrong slot package: {emitted}",
);
assert_eq!(
emitted.contains("enumShotsClipFusedEvidence"),
tiered,
"{participants} participants minted the wrong evidence face: {emitted}",
);
assert_eq!(
emitted.contains("fnvector_fuse_by_clip"),
tiered,
"{participants} participants minted the wrong door: {emitted}",
);
}
}
#[test]
fn a_fan_for_redundancy_keeps_its_slots_and_loses_its_leaderboard() {
let model = fanned(vec![leg("lance", false, false), leg("hnsw", false, false)]);
let emitted = tight(&model);
for absent in [
"ShotsClipFuseOptions",
"ShotsClipFusedEvidence",
"ShotsClipFusedError",
"ShotsClipLanceLegOptions",
"vector_fuse_by_clip",
] {
assert!(
!emitted.contains(absent),
"a fan nobody put in the leaderboard minted `{absent}`: {emitted}",
);
}
assert_eq!(
model.engines[0].slots().len(),
2,
"the write fan lost a slot to a READ word",
);
}
#[test]
fn a_leg_outside_the_leaderboard_has_no_route_seat() {
let model = fanned(vec![
leg("lance", true, false),
leg("hnsw", true, false),
leg("faiss", false, false),
]);
let emitted = packages(&model);
assert!(emitted.contains("ShotsClipLanceLegOptions"), "{emitted}");
assert!(emitted.contains("ShotsClipHnswLegOptions"), "{emitted}");
assert!(
!emitted.contains("ShotsClipFaissLegOptions"),
"a leg outside the leaderboard grew a route package: {emitted}",
);
assert!(
emitted.contains("fnwith_lance") && emitted.contains("fnwith_hnsw"),
"{emitted}",
);
assert!(
!emitted.contains("fnwith_faiss"),
"a leg outside the leaderboard grew a route seat: {emitted}",
);
}
#[test]
fn the_selector_names_every_typed_leg_and_the_leaderboard_does_not() {
let model = fanned(vec![
leg("lance", true, true),
leg("hnsw", true, false),
leg("faiss", false, true),
]);
let emitted = packages(&model);
assert!(emitted.contains("enumShotsClipEngineInput"), "{emitted}");
assert!(
emitted.contains("Lance(ShotsClipLanceEngineInput)"),
"{emitted}",
);
assert!(
emitted.contains("Faiss(ShotsClipFaissEngineInput)"),
"a typed leg outside the leaderboard is unnameable: {emitted}",
);
assert!(
!emitted.contains("Hnsw(ShotsClipHnswEngineInput)"),
"an untyped leg became addressable: {emitted}",
);
assert!(
emitted.contains("fnwith_engine"),
"the mini tier grew no selector seat: {emitted}",
);
}
#[test]
fn the_evidence_enum_holds_every_leg_an_answer_can_name() {
let model = fanned(vec![
leg("lance", true, true),
leg("hnsw", true, false),
leg("faiss", false, true),
leg("scann", false, false),
]);
let emitted = packages(&model);
assert!(
emitted.contains(
"enumShotsClipFusedEvidence<__INGRAPH_SCORE_LANCE__,__INGRAPH_SCORE_HNSW__,\
__INGRAPH_SCORE_FAISS__,>"
),
"{emitted}",
);
assert!(
emitted.contains(
"enumShotsClipFusedError<__INGRAPH_ERROR_LANCE__,__INGRAPH_ERROR_HNSW__,\
__INGRAPH_ERROR_FAISS__,>"
),
"{emitted}",
);
assert!(
!emitted.contains("Scann("),
"a leg that is neither folded nor nameable got a variant: {emitted}",
);
}
#[test]
fn the_mini_doors_are_rostered() {
let model = fanned(vec![leg("lance", true, false), leg("hnsw", true, false)]);
let names: Vec<String> = model
.emitted_methods()
.into_iter()
.map(|emitted| emitted.name.to_string())
.collect();
for owed in ["vector_fuse_by_clip", "vector_fuse_similar_by_clip"] {
assert!(names.contains(&owed.to_owned()), "{names:?}");
}
let types: Vec<String> = model
.tier()
.emitted_types()
.into_iter()
.map(|name| name.to_string())
.collect();
for owed in ["ShotsClipFusedEvidence", "ShotsClipFusedError"] {
assert!(types.contains(&owed.to_owned()), "{types:?}");
}
}
#[test]
fn a_fanned_set_election_fans_and_does_not_fuse() {
let mut model = fanned(vec![leg("lance", true, false), leg("hnsw", true, false)]);
model.embeddings = vec![Elected::new(
ident("clip"),
ElectionLabel::List,
syn::parse_quote!(::std::vec::Vec<::ingraph::Embedding<f32, 4, Clip>>),
None,
)];
let emitted = tight(&model);
assert!(
emitted.contains("structShotsClipFuseOptions"),
"the write fan lost its package: {emitted}",
);
assert!(
!emitted.contains("fnvector_fuse_by_clip"),
"a set election grew a door whose ordinal does not exist: {emitted}",
);
}