use super::{
ElectionLabel, IndexModel,
tests::{document, embedding_index, two_elections},
};
const LAW: &str = "::ingraph::__private::retrieve::route_depth(";
const EDGE: &str = "::ingraph::__private::retrieve::Route::depth";
fn tight(model: &IndexModel) -> String {
model.emit().to_string().split_whitespace().collect()
}
fn door<'a>(emission: &'a str, name: &str) -> &'a str {
let at = emission
.split(&format!("asyncfn{name}<"))
.nth(1)
.unwrap_or_else(|| panic!("`{name}` is emitted: {emission}"));
let end = [at.find("#[doc"), at.find("asyncfn")]
.into_iter()
.flatten()
.min()
.unwrap_or(at.len());
&at[..end]
}
fn as_set(mut model: IndexModel) -> IndexModel {
for elected in &mut model.embeddings {
elected.label = ElectionLabel::List;
elected.ty = syn::parse_quote!(::std::vec::Vec<::ingraph::Embedding<f32, 4, Clip>>);
}
model
}
#[test]
fn no_door_spells_a_chain_of_its_own() {
for model in [document(), embedding_index(), two_elections()] {
let emitted = tight(&model);
assert!(
!emitted.contains("NonZeroUsize::try_from"),
"a door widened a ceiling by hand: {emitted}",
);
}
}
#[test]
fn each_tier_route_resolves_its_own_edge_in_both_fused_doors() {
let emitted = tight(&two_elections());
for name in ["vector_fuse", "vector_fuse_similar"] {
let body = door(&emitted, name);
assert_eq!(
body.matches(LAW).count(),
2,
"`{name}` resolves once per route: {body}",
);
assert_eq!(
body.matches(EDGE).count(),
2,
"`{name}` reads each route's own edge: {body}",
);
}
}
#[test]
fn a_members_own_read_resolves_against_its_declaration_and_not_an_edge() {
let emitted = tight(&embedding_index());
let capability = emitted
.split("fnkeys<'__ingraph_ctx>(")
.nth(1)
.expect("a vector declaration answers a fused read");
assert_eq!(capability.matches(LAW).count(), 1, "{capability}");
assert!(
capability.contains("route_depth(::core::option::Option::None,"),
"a member read an edge fact its parent had already spent: {capability}",
);
assert!(
!capability.contains(EDGE),
"there is no `Route` at this grain to read one off: {capability}",
);
}
#[test]
fn both_set_doors_carry_the_streams_depth() {
let emitted = tight(&as_set(embedding_index()));
for name in [
"retrieve_similar_by_image_embedding_with",
"retrieve_keys_by_image_embedding_with",
] {
let body = door(&emitted, name);
assert!(
body.contains(
"limit:::core::num::NonZeroUsize,depth:::core::option::Option<::core::num::NonZeroUsize>,"
),
"`{name}` does not carry the streams\' depth beside its limit: {body}",
);
}
}
#[test]
fn the_set_doors_depth_is_resolved_once_and_reaches_the_engine() {
let emitted = tight(&as_set(embedding_index()));
for name in [
"retrieve_similar_by_image_embedding_with",
"retrieve_keys_by_image_embedding_with",
] {
let body = door(&emitted, name);
assert_eq!(body.matches(LAW).count(), 1, "`{name}`: {body}");
assert!(
body.contains("route_depth(depth,::core::option::Option::None,limit)"),
"`{name}` resolved something other than its own parameter: {body}",
);
assert!(
body.contains("__ingraph_depth,options,).await"),
"`{name}`: the resolved depth did not reach the engine: {body}",
);
assert!(
body.contains("&fold,::core::num::NonZeroUsize::get(limit)"),
"`{name}`: the fold stopped counting entities at `limit`: {body}",
);
}
}