use crate::{
engine::{Engine, FieldEngines},
index::{Dedup, Elected, ElectionLabel, IndexGraphql, IndexModel, Upstream},
model::{GraphqlKind, NodeKind},
prefix::EmissionPrefix,
};
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn index(name: &str, kind: NodeKind) -> IndexModel {
IndexModel {
prefix: EmissionPrefix::default(),
numbers: crate::index::Numbers::default(),
field_numbers: Vec::new(),
index: ident(name),
kind,
vis: syn::parse_quote!(pub),
krate: syn::parse_quote!(::ingraph),
source: Upstream {
node: syn::parse_quote!(Caption),
projections: Vec::new(),
},
dedup: Dedup::Hash,
embeddings: Vec::new(),
labelled: Vec::new(),
searchable: Vec::new(),
engines: Vec::new(),
stores: Vec::new(),
data: false,
keys: false,
subscribe: false,
graphql: Some(IndexGraphql::of(GraphqlKind::AsyncGraphql)),
rename: None,
slot: None,
extension: None,
fields: Vec::new(),
}
}
fn engine(word: &str, resource: Option<syn::Path>) -> Engine {
Engine {
resource,
..Engine::bare(ident(word))
}
}
fn blocks(field: &str, engines: Vec<Engine>, fanout: bool) -> FieldEngines {
FieldEngines {
field: ident(field),
engines,
fanout,
}
}
fn vector(engines: Vec<Engine>) -> IndexModel {
let mut model = index("Shots", NodeKind::Vector);
model.embeddings = vec![Elected::new(
ident("clip"),
ElectionLabel::Embedding,
syn::parse_quote!(::ingraph::Embedding<f32, 4, Clip>),
None,
)];
model.engines = vec![blocks("clip", engines, false)];
model
}
fn fulltext(body: Vec<Engine>, title: Vec<Engine>) -> IndexModel {
let mut model = index("Transcript", NodeKind::Fulltext);
model.searchable = vec![ident("body"), ident("title")];
model.engines = vec![blocks("body", body, false), blocks("title", title, false)];
model
}
fn compact(model: &IndexModel) -> String {
model.tier().emit().to_string().split_whitespace().collect()
}
fn rendered(model: &IndexModel) -> String {
let tokens = model.tier().emit();
syn::parse2::<syn::File>(tokens.clone())
.map_or_else(|_| tokens.to_string(), |file| prettyplease::unparse(&file))
}
fn lance() -> Engine {
engine("lance", Some(syn::parse_quote!(app::Lance)))
}
fn hnsw() -> Engine {
engine("hnsw", Some(syn::parse_quote!(app::hnsw::Hnsw)))
}
#[test]
fn an_untyped_declaration_grows_no_engine_seat() {
for model in [
vector(vec![engine("lance", None)]),
fulltext(vec![engine("tantivy", None)], Vec::new()),
] {
let emitted = compact(&model);
for absent in ["EngineInput", "engine:", "floor", "SearchOptions"] {
assert!(
!emitted.contains(absent),
"an untyped declaration mints `{absent}`: {}",
rendered(&model),
);
}
}
}
#[test]
fn a_bare_word_beside_a_typed_one_contributes_no_variant() {
let model = vector(vec![lance(), engine("hnsw", None)]);
let emitted = compact(&model);
assert!(
emitted.contains("ShotsClipLanceEngineInput"),
"the typed word has no variant: {}",
rendered(&model),
);
assert!(
!emitted.contains("ShotsClipHnswEngineInput"),
"the BARE word grew a variant with no currency to fill it: {}",
rendered(&model),
);
}
#[test]
fn the_floor_is_the_named_types_own_score() {
let vectors = compact(&vector(vec![lance()]));
assert!(
vectors.contains(
"floor:::core::option::Option<<app::Lanceas::ingraph::__private::store::VectorEngine>::Score>"
),
"{vectors}",
);
let documents = compact(&fulltext(
vec![engine("tantivy", Some(syn::parse_quote!(app::Tantivy)))],
Vec::new(),
));
assert!(
documents.contains(
"floor:::core::option::Option<<app::Tantivyas::ingraph::__private::store::FulltextEngine>::Score>"
),
"{documents}",
);
}
#[test]
fn the_options_are_the_named_types_own_search_options() {
let vectors = compact(&vector(vec![lance()]));
assert!(
vectors.contains(
"options:::core::option::Option<<app::Lanceas::ingraph::__private::store::VectorEngine>::SearchOptions>"
),
"{vectors}",
);
let documents = compact(&fulltext(
vec![engine("tantivy", Some(syn::parse_quote!(app::Tantivy)))],
Vec::new(),
));
assert!(
documents.contains(
"options:::core::option::Option<<app::Tantivyas::ingraph::__private::store::FulltextEngine>::SearchOptions>"
),
"{documents}",
);
}
#[test]
fn each_variant_carries_both_rows() {
let model = vector(vec![hnsw(), lance()]);
let emitted = compact(&model);
for word in ["Hnsw", "Lance"] {
let package = emitted
.split(&format!("structShotsClip{word}EngineInput{{"))
.nth(1)
.and_then(|tail| tail.split('}').next())
.unwrap_or_else(|| panic!("`{word}`'s package is emitted: {}", rendered(&model)));
assert!(package.contains("floor:"), "{word}: {package}");
assert!(package.contains("options:"), "{word}: {package}");
}
}
#[test]
fn two_typed_engines_share_one_oneof() {
let model = vector(vec![hnsw(), lance()]);
let emitted = compact(&model);
let choice = emitted
.split("enumShotsClipEngineInput{")
.nth(1)
.and_then(|tail| tail.split('}').next())
.expect("the choice is emitted");
assert!(
choice.contains("Hnsw(ShotsClipHnswEngineInput),")
&& choice.contains("Lance(ShotsClipLanceEngineInput),"),
"{}",
rendered(&model),
);
assert_eq!(
emitted.matches("EngineInput),").count(),
2,
"the choice holds a variant this declaration did not type: {}",
rendered(&model),
);
assert!(
emitted.contains("oneof:true"),
"the choice publishes as a record: {}",
rendered(&model),
);
}
#[test]
fn one_typed_engine_still_publishes_a_choice() {
let model = vector(vec![lance()]);
let emitted = compact(&model);
let choice = emitted
.split("enumShotsClipEngineInput{")
.nth(1)
.and_then(|tail| tail.split('}').next())
.expect("the choice is emitted");
assert!(
choice.contains("Lance(ShotsClipLanceEngineInput),"),
"{}",
rendered(&model),
);
assert_eq!(
emitted.matches("EngineInput),").count(),
1,
"a one-word slot published more than one variant: {}",
rendered(&model),
);
}
#[test]
fn the_leaf_carries_the_choice() {
let model = vector(vec![lance()]);
let emitted = compact(&model);
assert!(
emitted.contains("engine:::core::option::Option<ShotsClipEngineInput>"),
"{}",
rendered(&model),
);
assert!(
emitted.contains("fnwith_engine(mutself,engine:ShotsClipEngineInput)"),
"the row has no factory leg: {}",
rendered(&model),
);
}
#[test]
fn the_variant_crosses_under_its_engine_word() {
let emitted = compact(&vector(vec![engine(
"my_engine",
Some(syn::parse_quote!(app::Mine)),
)]));
assert!(emitted.contains("\"myEngine\""), "{emitted}");
assert!(
emitted.contains("ShotsClipMyEngineEngineInput"),
"{emitted}"
);
}
#[test]
fn a_fulltext_tier_unions_its_fields_words() {
let model = fulltext(
vec![engine("tantivy", Some(syn::parse_quote!(app::Tantivy)))],
vec![
engine("tantivy", Some(syn::parse_quote!(app::Tantivy))),
engine("sonic", Some(syn::parse_quote!(app::Sonic))),
],
);
let emitted = compact(&model);
assert_eq!(
emitted
.matches("structTranscriptTantivyEngineInput{")
.count(),
1,
"one word repeated on two fields minted two packages: {}",
rendered(&model),
);
let choice = emitted
.split("enumTranscriptEngineInput{")
.nth(1)
.and_then(|tail| tail.split('}').next())
.expect("the choice is emitted");
assert!(
choice.contains("Tantivy(TranscriptTantivyEngineInput),")
&& choice.contains("Sonic(TranscriptSonicEngineInput),"),
"{}",
rendered(&model),
);
}
#[test]
fn one_word_bound_to_two_engines_is_refused() {
let model = fulltext(
vec![engine("tantivy", Some(syn::parse_quote!(app::Tantivy)))],
vec![engine("tantivy", Some(syn::parse_quote!(other::Tantivy)))],
);
let emitted = compact(&model);
assert!(emitted.contains("compile_error!"), "{emitted}");
assert!(
emitted.contains("app::Tantivy") && emitted.contains("other::Tantivy"),
"the refusal does not quote both bindings: {emitted}",
);
}
#[test]
fn the_seat_is_rostered() {
let model = vector(vec![hnsw(), lance()]);
let types: Vec<String> = model
.tier()
.emitted_types()
.into_iter()
.map(|name| name.to_string())
.collect();
assert_eq!(
types,
[
"ShotsClipHnswEngineInput",
"ShotsClipLanceEngineInput",
"ShotsClipEngineInput",
"ShotsClipVectorOptions",
"ShotsClipVectorQuestion",
],
"the roster is not the emission's own walk",
);
let methods: Vec<String> = model
.tier()
.emitted_methods()
.into_iter()
.map(|(on, name)| format!("{on}::{name}"))
.collect();
for owed in [
"ShotsClipHnswEngineInput::with_floor",
"ShotsClipLanceEngineInput::floor",
"ShotsClipVectorOptions::with_engine",
"ShotsClipVectorOptions::engine",
] {
assert!(methods.contains(&owed.to_owned()), "{methods:?}");
}
}
#[test]
fn an_untyped_declarations_roster_names_no_engine() {
let model = vector(vec![engine("lance", None)]);
for name in model.tier().emitted_types() {
assert!(
!name.to_string().contains("EngineInput"),
"an untyped declaration rosters `{name}`",
);
}
for (on, name) in model.tier().emitted_methods() {
assert!(
name != "engine" && !on.to_string().contains("EngineInput"),
"an untyped declaration rosters `{on}::{name}`",
);
}
}
#[test]
fn every_typed_shape_parses_as_a_file() {
for model in [
vector(vec![lance()]),
vector(vec![hnsw(), lance()]),
fulltext(
vec![engine("tantivy", Some(syn::parse_quote!(app::Tantivy)))],
Vec::new(),
),
] {
let tokens = model.tier().emit();
syn::parse2::<syn::File>(tokens.clone())
.unwrap_or_else(|e| panic!("the tier does not parse: {e}\n{tokens}"));
}
}