use super::{
ElectionLabel, IndexModel,
tests::{document, embedding_index, two_elections},
};
const SEAT: &str = "options:::core::option::Option<&<__INGRAPH_ENGINE__as";
const ROUTE_SEAT: &str = "::core::option::Option<&<__INGRAPH_ENGINE_CAPTION_EMBEDDING__as";
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
}
fn searching() -> [(String, Vec<&'static str>); 2] {
[
(
tight(&as_set(embedding_index())),
vec![
"retrieve_keys_by_image_embedding",
"retrieve_similar_by_image_embedding",
"retrieve_keys_by_image_embedding_with",
"retrieve_similar_by_image_embedding_with",
],
),
(
tight(&document()),
vec!["retrieve_keys", "retrieve_keys_by_title"],
),
]
}
#[test]
fn every_searching_door_takes_the_seat_and_never_owns_it() {
for (emission, doors) in searching() {
for name in doors {
let body = door(&emission, name);
assert!(
body.contains(SEAT),
"`{name}` does not take the knob seat: {body}",
);
assert!(
!body.contains("options:::core::option::Option<__INGRAPH_ENGINE__"),
"`{name}` takes its knobs by value, which is a copy per leg at every fan: {body}",
);
}
}
}
#[test]
fn the_knobs_come_after_every_universal_fact() {
let emission = tight(&as_set(embedding_index()));
assert!(
door(&emission, "retrieve_keys_by_image_embedding")
.contains("limit:::core::num::NonZeroUsize,options:"),
"{emission}",
);
assert!(
door(&emission, "retrieve_similar_by_image_embedding_with")
.contains("depth:::core::option::Option<::core::num::NonZeroUsize>,options:",),
"the knobs did not follow the last number: {emission}",
);
}
#[test]
fn the_knobs_are_never_cloned_and_no_door_demands_that_they_could_be() {
for model in [document(), embedding_index(), two_elections()] {
let emitted = tight(&model);
assert!(
!emitted.contains("SearchOptions:::core::clone::Clone"),
"a door made its knobs clonable to spend them: {emitted}",
);
assert!(
!emitted.contains("::core::clone::Clone::clone(&options)"),
"a fan copied the request's knobs instead of lending them: {emitted}",
);
}
}
#[test]
fn every_engine_verb_is_handed_the_doors_own_knobs() {
let vectors = tight(&as_set(embedding_index()));
for (name, spent) in [
("retrieve_keys_by_image_embedding", "limit,options,)"),
("retrieve_similar_by_image_embedding", "limit,options,)"),
(
"retrieve_keys_by_image_embedding_with",
"__ingraph_depth,options,)",
),
(
"retrieve_similar_by_image_embedding_with",
"__ingraph_depth,options,)",
),
] {
let body = door(&vectors, name);
assert!(
body.contains(spent),
"`{name}` does not spend its own knobs at the `knn`: {body}",
);
}
let text = tight(&document());
for name in ["retrieve_keys", "retrieve_keys_by_title"] {
let body = door(&text, name);
assert!(
body.contains("limit,options,)"),
"`{name}` does not spend its own knobs at the `search`: {body}",
);
}
}
#[test]
fn tier_b_and_the_ask_road_forward_their_own_seat() {
let emitted = tight(&embedding_index());
let tier_b = door(&emitted, "retrieve_by_image_embedding");
assert!(tier_b.contains(SEAT), "tier B carries no seat: {tier_b}");
assert!(
tier_b.contains("::core::num::NonZeroUsize::MAX,options,)"),
"tier B hands its tier A something other than its own knobs: {tier_b}",
);
assert!(
!tier_b.contains("::core::num::NonZeroUsize::MAX,::core::option::Option::None,"),
"the explicit blank is still in the tier-B call: {tier_b}",
);
}
#[test]
fn each_tier_route_carries_and_spends_its_own_seat() {
let emitted = tight(&two_elections());
for name in ["vector_fuse", "vector_fuse_similar"] {
let body = door(&emitted, name);
assert_eq!(
body.matches(ROUTE_SEAT).count(),
1,
"`{name}` does not carry exactly one seat for the caption route: {body}",
);
assert!(
body.contains("caption_embedding_options"),
"`{name}` names no per-route seat: {body}",
);
assert!(
body.contains("image_embedding_options"),
"`{name}` names only one of its two routes' seats: {body}",
);
assert!(
!body.contains("::core::option::Option::None,)"),
"a route was handed the defaults where its own seat was in scope: {body}",
);
}
}
#[test]
fn a_fused_door_takes_no_row_out_of_a_routes_package() {
let emitted = tight(&two_elections());
for name in ["vector_fuse", "vector_fuse_similar"] {
let body = door(&emitted, name);
assert_eq!(
body.matches("Route::options),").count(),
0,
"`{name}` opened a route's package: {body}",
);
assert_eq!(
body.matches("::ceiling,").count(),
0,
"`{name}` read a ceiling off a request: {body}",
);
}
}
#[test]
fn the_members_own_seat_carries_its_knobs_and_spends_them() {
let emitted = tight(&embedding_index());
let capability = emitted
.split("fnkeys<'__ingraph_ctx>(")
.nth(1)
.expect("a vector declaration answers a fused read");
assert!(
capability.contains("__ingraph_options_0"),
"the seat carries no knobs: {capability}",
);
assert!(
capability.contains("__ingraph_ceiling,__ingraph_options_0,)"),
"the member's own door was handed something other than its seat's knobs: {capability}",
);
assert!(
!capability.contains("__ingraph_ceiling,::core::option::Option::None,)"),
"the seat was carried and then dropped: {capability}",
);
}