use quote::format_ident;
use super::{Engine, EngineBinding, EngineKind, FieldEngines, Slot};
use crate::model::NodeKind;
fn slot(field: &str, engine: &str) -> Slot {
Slot {
field: format_ident!("{field}"),
engine: format_ident!("{engine}"),
}
}
fn stored(field: &str, engines: &[&str], fanout: bool) -> FieldEngines {
FieldEngines {
field: format_ident!("{field}"),
engines: engines
.iter()
.map(|word| Engine::bare(format_ident!("{word}")))
.collect(),
fanout,
}
}
#[test]
fn a_slot_name_carries_the_index_the_field_and_the_engine() {
let name = slot("image_embedding", "hnsw").name(&format_ident!("KeyframeVectors"));
assert_eq!(name, "keyframe_vectors__image_embedding__hnsw");
}
#[test]
fn the_double_underscore_is_what_keeps_two_slots_apart() {
let index = format_ident!("Doc");
let one = slot("a_b", "c").name(&index);
let other = slot("a", "b_c").name(&index);
assert_ne!(one, other);
assert_eq!(one, "doc__a_b__c");
assert_eq!(other, "doc__a__b_c");
}
#[test]
fn a_minted_slot_name_is_inside_the_slot_charset() {
let name = slot("title", "tantivy").name(&format_ident!("SearchDocument"));
assert!(!name.is_empty());
assert!(
name
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
);
assert!(!name.as_bytes()[0].is_ascii_digit());
}
#[test]
fn a_slot_marker_is_the_index_the_field_and_the_engine_in_camel_case() {
let marker = slot("image_embedding", "hnsw").marker(&format_ident!("Keyframe"));
assert_eq!(marker, format_ident!("KeyframeImageEmbeddingHnsw"));
}
#[test]
fn the_slot_set_is_the_field_crossed_with_the_engine() {
let declared = stored("title", &["tantivy", "hnsw"], false);
let slots = declared.slots();
assert_eq!(slots.len(), 2);
assert_eq!(slots[0], slot("title", "tantivy"));
assert_eq!(slots[1], slot("title", "hnsw"));
}
#[test]
fn a_field_that_named_no_engine_declares_no_slot() {
assert!(stored("title", &[], false).slots().is_empty());
}
#[test]
fn two_fields_sharing_one_engine_are_two_slots() {
let index = format_ident!("Doc");
let title = stored("title", &["tantivy"], false);
let body = stored("body", &["tantivy"], false);
let names: Vec<String> = title
.slots()
.into_iter()
.chain(body.slots())
.map(|slot| slot.name(&index))
.collect();
assert_eq!(names, ["doc__title__tantivy", "doc__body__tantivy"]);
}
#[test]
fn portability_and_fanout_declare_one_slot_set_and_differ_in_liveness() {
let portable = stored("title", &["tantivy", "hnsw"], false);
let fanned = stored("title", &["tantivy", "hnsw"], true);
assert_eq!(portable.slots(), fanned.slots());
assert!(!portable.fanout);
assert!(fanned.fanout);
}
#[test]
fn a_shipped_engines_gate_is_its_own_word_where_it_has_one() {
assert_eq!(
EngineKind::Tantivy.gate(),
Some(format_ident!("if_tantivy"))
);
assert_eq!(EngineKind::Fs.gate(), None);
}
#[test]
fn the_shipped_engines_are_tantivy_and_fs_and_they_serve_two_kinds() {
assert_eq!(
EngineKind::of(&format_ident!("tantivy")),
Some(EngineKind::Tantivy)
);
assert_eq!(EngineKind::Tantivy.serves(), NodeKind::Fulltext);
assert_eq!(EngineKind::of(&format_ident!("fs")), Some(EngineKind::Fs));
assert_eq!(EngineKind::Fs.serves(), NodeKind::Blob);
}
#[test]
fn a_third_party_word_is_not_in_the_shipped_table() {
assert_eq!(EngineKind::of(&format_ident!("hnsw")), None);
assert_eq!(EngineKind::of(&format_ident!("lance")), None);
}
#[test]
fn a_declared_block_answers_whether_it_is_shipped() {
let shipped = Engine::bare(format_ident!("tantivy"));
let theirs = Engine::bare(format_ident!("hnsw"));
assert_eq!(shipped.shipped(), Some(EngineKind::Tantivy));
assert_eq!(theirs.shipped(), None);
}
#[test]
fn a_binding_marker_is_the_root_and_the_word() {
let binding = EngineBinding {
word: format_ident!("hnsw"),
engine: syn::parse_quote!(app::hnsw::Hnsw),
};
assert_eq!(
binding.marker(&format_ident!("Media")),
format_ident!("MediaEngineHnsw")
);
}