use super::{
IndexModel,
tests::{document, embedding_index, two_elections},
};
use crate::{model::NodeKind, prefix::EmissionPrefix};
fn emitted(model: &IndexModel) -> String {
model.emit().to_string()
}
fn door<'a>(emission: &'a str, name: &str) -> &'a str {
let after = emission
.split(&format!("async fn {name} <"))
.nth(1)
.unwrap_or_else(|| panic!("`{name}` is emitted: {emission}"));
let end = [after.find("# [doc"), after.find("async fn")]
.into_iter()
.flatten()
.min()
.unwrap_or(after.len());
&after[..end]
}
#[test]
fn every_election_gets_its_own_get_door() {
let emission = emitted(&two_elections());
for field in ["caption_embedding", "image_embedding"] {
assert_eq!(
emission.matches(&format!("async fn get_{field} <")).count(),
2,
"`get_{field}` is not written once per gate half: {emission}"
);
}
}
#[test]
fn a_kind_with_no_election_gets_no_get_door() {
for model in [
document(),
IndexModel {
prefix: EmissionPrefix::default(),
kind: NodeKind::Blob,
..document()
},
] {
let emission = emitted(&model);
assert!(
!emission.contains("async fn get_"),
"{:?} carries a by-key door it cannot answer: {emission}",
model.kind
);
}
}
#[test]
fn the_get_door_takes_one_scope() {
let emission = emitted(&embedding_index());
let signature = door(&emission, "get_image_embedding")
.split('{')
.next()
.expect("the door has a signature");
assert!(
signature.contains("own :"),
"no own-read scope: {signature}"
);
assert!(
!signature.contains("scope :"),
"a one-crossing door takes the knn's scope: {signature}"
);
assert!(signature.contains("key : &"), "{signature}");
}
#[test]
fn the_get_door_answers_the_checked_embedding() {
let emission = emitted(&embedding_index());
let signature = door(&emission, "get_image_embedding")
.split('{')
.next()
.expect("the door has a signature");
assert!(
signature.contains("Embedding"),
"the door answers something else: {signature}"
);
assert!(!signature.contains("Ranked"), "{signature}");
assert!(!signature.contains("floor"), "{signature}");
assert!(!signature.contains("limit"), "{signature}");
}
#[test]
fn the_get_door_carries_no_dedup_bound() {
let emission = emitted(&embedding_index());
let signature = door(&emission, "get_image_embedding")
.split('{')
.next()
.expect("the door has a signature");
assert!(
signature.contains(":: Field"),
"the address walk's own bound is missing: {signature}"
);
assert!(!signature.contains("VectorField"), "{signature}");
for bound in ["Hash", "PartialEq", "Seen"] {
assert!(
!signature.contains(bound),
"the read asks a key for `{bound}`: {signature}"
);
}
}
#[test]
fn the_get_door_is_the_by_key_read_and_nothing_else() {
let emission = emitted(&embedding_index());
let body = door(&emission, "get_image_embedding");
for call in ["vector_addressed", "get_vectors"] {
assert!(body.contains(call), "the read is missing `{call}`: {body}");
}
for search in ["knn", "ranked_keys", "VectorQuery", "vector_keyed"] {
assert!(
!body.contains(search),
"the read reaches `{search}` — a getter that searches is a second query path: {body}"
);
}
}
#[test]
fn the_arity_and_width_judgments_are_written_once() {
let emission = emitted(&embedding_index());
for judgment in ["Unindexed", "Several", "Width"] {
assert_eq!(
emission.matches(judgment).count(),
2,
"`{judgment}` is stated in more than one place — once per gate half is the whole budget: \
{emission}"
);
}
}
#[test]
fn the_similar_door_delegates_to_the_get_door() {
let emission = emitted(&embedding_index());
let body = door(&emission, "retrieve_similar_by_image_embedding");
assert!(
body.contains("get_image_embedding"),
"the similar door does not go through the read: {body}"
);
for own in ["vector_addressed", "get_vectors", "Unindexed", "Width"] {
assert!(
!body.contains(own),
"the similar door still does the read's own work (`{own}`): {body}"
);
}
}
#[test]
fn the_get_door_names_itself_in_its_span() {
let emission = emitted(&embedding_index());
assert!(
emission.contains("entry = \"get_image_embedding\""),
"the door opens no span of its own: {emission}"
);
}