use super::{
IndexModel,
tests::{embedding_index, two_elections},
};
use crate::index::ElectionLabel;
fn emitted(model: &IndexModel) -> String {
model.emit().to_string()
}
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 door<'a>(emission: &'a str, name: &str) -> &'a str {
let at = emission
.split(&format!("async fn {name} <"))
.nth(1)
.unwrap_or_else(|| panic!("`{name}` is emitted: {emission}"));
let end = [at.find("# [doc"), at.find("async fn")]
.into_iter()
.flatten()
.min()
.unwrap_or(at.len());
&at[..end]
}
#[test]
fn only_a_set_election_gets_the_by_example_set_door() {
let single = emitted(&embedding_index());
assert!(
!single.contains("retrieve_similar_by_image_embedding_with"),
"a single election grew the set door: {single}",
);
assert!(
single.contains("retrieve_similar_by_image_embedding"),
"the single-vector by-example door is gone: {single}",
);
let set = emitted(&as_set(embedding_index()));
assert!(
set.contains("retrieve_similar_by_image_embedding_with"),
"a set election has no by-example set door: {set}",
);
}
#[test]
fn every_set_election_gets_its_own_by_example_set_door() {
let emission = emitted(&as_set(two_elections()));
for field in ["caption_embedding", "image_embedding"] {
assert_eq!(
emission
.matches(&format!("async fn retrieve_similar_by_{field}_with <"))
.count(),
2,
"`retrieve_similar_by_{field}_with` is not written once per gate half: {emission}",
);
}
}
#[test]
fn the_body_is_the_recipe_at_the_set_grain() {
let emission = emitted(&as_set(embedding_index()));
let body = door(&emission, "retrieve_similar_by_image_embedding_with");
for call in [
"get_image_embedding",
"knn",
"vector_keyed",
"owner_best_excluding",
"set_scored",
] {
assert!(
body.contains(call),
"the recipe is missing `{call}`: {body}"
);
}
assert!(
!body.contains("get_vectors"),
"the seam call is written a second time beside the door that owns it: {body}",
);
}
#[test]
fn the_door_never_reaches_the_plain_collapse() {
let emission = emitted(&as_set(embedding_index()));
let body = door(&emission, "retrieve_similar_by_image_embedding_with");
assert!(
!body.contains("owner_best ::"),
"the door collapses without the seed: {body}",
);
assert!(
!body.contains("retain"),
"the door filters instead of seeding the membership: {body}",
);
}
#[test]
fn the_set_read_judges_what_a_set_can_be_judged_on() {
let emission = emitted(&as_set(embedding_index()));
let body = door(&emission, "retrieve_similar_by_image_embedding_with");
assert!(
!body.contains("Several"),
"a set of several is what this door is FOR: {body}",
);
let group = door(&emission, "get_image_embedding");
assert!(group.contains("Unindexed"), "{group}");
assert!(group.contains("Width"), "{group}");
assert!(
!group.contains("Several"),
"the group read judged an arity its election declared: {group}",
);
}
#[test]
fn the_by_key_read_answers_the_shape_the_label_declares() {
let grouped = emitted(&as_set(embedding_index()));
let signature = door(&grouped, "get_image_embedding")
.split('{')
.next()
.expect("the door has a signature");
assert!(
signature.contains("Held"),
"a set election's by-key read does not answer the group: {signature}",
);
let single = emitted(&embedding_index());
let signature = door(&single, "get_image_embedding")
.split('{')
.next()
.expect("the door has a signature");
assert!(
!signature.contains("Held"),
"a unit election's by-key read grew a group: {signature}",
);
assert!(
door(&single, "get_image_embedding").contains("Several"),
"a unit election stopped refusing an arity its declaration states",
);
}
#[test]
fn the_single_vector_by_example_door_still_needs_exactly_one() {
let emission = emitted(&as_set(embedding_index()));
let body = door(&emission, "retrieve_similar_by_image_embedding");
assert!(
body.contains("Several"),
"the single-vector door silently picked one of several: {body}",
);
assert!(
body.contains("get_image_embedding"),
"it stopped reaching the door that owns the read: {body}",
);
}
#[test]
fn the_by_example_set_door_takes_no_store() {
let emission = emitted(&as_set(embedding_index()));
let signature = door(&emission, "retrieve_similar_by_image_embedding_with")
.split('{')
.next()
.expect("the door has a signature");
assert!(
!signature.contains("AsyncGather"),
"a tier-A door gathers: {signature}"
);
assert!(
!signature.contains("filter"),
"a tier-A door filters: {signature}"
);
}
#[test]
fn the_door_takes_a_scope_per_crossing_and_clones_the_asking_one() {
let emission = emitted(&as_set(embedding_index()));
let signature = door(&emission, "retrieve_similar_by_image_embedding_with")
.split('{')
.next()
.expect("the door has a signature");
assert!(
signature.contains("own :"),
"no own-read scope: {signature}"
);
assert!(signature.contains("scope :"), "no knn scope: {signature}");
assert!(
signature.contains("clone :: Clone"),
"the asking scope is not cloneable, so `m` calls cannot be made: {signature}",
);
assert!(signature.contains("key : &"), "{signature}");
}
#[test]
fn the_door_answers_in_folded() {
let emission = emitted(&as_set(embedding_index()));
let signature = door(&emission, "retrieve_similar_by_image_embedding_with")
.split('{')
.next()
.expect("the door has a signature");
assert!(signature.contains("Folded"), "{signature}");
assert!(
signature.contains("OwnReadError"),
"the door hands back some other vocabulary: {signature}",
);
}
#[test]
fn the_door_names_itself_in_its_span() {
let emission = emitted(&as_set(embedding_index()));
assert!(
emission.contains("entry = \"retrieve_similar_by_image_embedding_with\""),
"the door opens no span of its own: {emission}",
);
}