use super::{Dedup, Elected, ElectionLabel, IndexGraphql, IndexModel, Numbers, Upstream};
use crate::{
engine::{Engine, FieldEngines},
model::{GraphqlKind, NodeKind},
prefix::EmissionPrefix,
};
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn declared(label: ElectionLabel) -> IndexModel {
elections(&[("clip", label)])
}
fn elected(field: &str, label: ElectionLabel) -> Elected {
let ty: syn::Type = match label {
ElectionLabel::List => syn::parse_quote!(::std::vec::Vec<::ingraph::Embedding<f32, 4, Clip>>),
ElectionLabel::Embedding => syn::parse_quote!(::ingraph::Embedding<f32, 4, Clip>),
};
Elected::new(ident(field), label, ty, None)
}
fn elections(fields: &[(&str, ElectionLabel)]) -> IndexModel {
IndexModel {
prefix: EmissionPrefix::default(),
numbers: Numbers::default(),
field_numbers: Vec::new(),
index: ident("Shots"),
kind: NodeKind::Vector,
vis: syn::parse_quote!(pub),
krate: syn::parse_quote!(::ingraph),
source: Upstream {
node: syn::parse_quote!(Keyframe),
projections: Vec::new(),
},
dedup: Dedup::Hash,
embeddings: fields
.iter()
.map(|(field, label)| elected(field, *label))
.collect(),
labelled: Vec::new(),
searchable: Vec::new(),
engines: fields
.iter()
.map(|(field, _)| FieldEngines {
field: ident(field),
engines: vec![Engine {
resource: Some(syn::parse_quote!(crate::Store)),
..Engine::bare(ident("toy"))
}],
fanout: false,
})
.collect(),
stores: Vec::new(),
data: false,
keys: false,
subscribe: false,
extension: None,
graphql: Some(IndexGraphql::of(GraphqlKind::AsyncGraphql)),
rename: None,
slot: None,
fields: Vec::new(),
}
}
fn tight(model: &IndexModel) -> String {
model.emit().to_string().split_whitespace().collect()
}
fn both() -> (String, String) {
(
tight(&declared(ElectionLabel::Embedding)),
tight(&declared(ElectionLabel::List)),
)
}
fn resolver<'a>(emission: &'a str, method: &str) -> &'a str {
let at = emission
.split(&format!("asyncfn{method}("))
.nth(1)
.unwrap_or_else(|| panic!("`{method}` is emitted: {emission}"));
let end = [at.find("#[doc"), at.find("asyncfn")]
.into_iter()
.flatten()
.min()
.unwrap_or(at.len());
&at[..end]
}
fn door(method: &str, extra: &str) -> String {
format!(
"Shots::{method}::<<crate::Storeas::ingraph::__private::store::VectorMount>::Handle{extra}>"
)
}
#[test]
fn the_label_picks_the_by_example_door() {
let (unit, set) = both();
let unit_leg = resolver(&unit, "__async_graphql_shots_similar_by_clip_keys");
let set_leg = resolver(&set, "__async_graphql_shots_similar_by_clip_keys");
assert!(
unit_leg.contains(&door("retrieve_similar_by_clip", "")),
"the unit road left the single-vector door: {unit_leg}",
);
assert!(
!unit_leg.contains("retrieve_similar_by_clip_with"),
"the unit road reached the set door: {unit_leg}",
);
assert!(
set_leg.contains(&door("retrieve_similar_by_clip_with", ",_")),
"the set road did not reach the set door: {set_leg}",
);
assert!(
!set_leg.contains("retrieve_similar_by_clip::<"),
"the set road also reached the single-vector door: {set_leg}",
);
}
#[test]
fn the_label_moves_the_road_and_never_the_name() {
let (unit, set) = both();
for (label, emission) in [("unit", &unit), ("set", &set)] {
for (name, minted) in [
("shotsSimilarByClipKeys", 2),
("shotsSimilarByClip", 1),
("shotsByClipKeys", 2),
("shotsByClip", 1),
] {
assert_eq!(
emission
.matches(&format!("graphql(name=\"{name}\")"))
.count(),
minted,
"the {label} road does not mint `{name}` exactly {minted} time(s): {emission}",
);
}
}
}
#[test]
fn the_set_road_answers_the_set_hit_family() {
let (unit, set) = both();
assert!(
set.contains("graphql::FoldedMatch<__INGRAPH_ANSWER_KEY__,"),
"the keys tier's set hit is missing: {set}",
);
assert!(
set.contains("graphql::FoldedMatch<__INGRAPH_ANSWER_ENTITY__,"),
"the hydrated tier's set hit is missing: {set}",
);
assert!(set.contains("graphql::folded_matches("), "{set}");
assert!(
set.contains("retrieve::Folded::map_node"),
"the crossing did not carry the fold's own answer: {set}",
);
assert!(
set.contains("retrieve::folded_filled("),
"the hydrated set road did not use the fill loop at its own arity: {set}",
);
assert!(!unit.contains("FoldedMatch"), "{unit}");
assert!(!unit.contains("folded_filled"), "{unit}");
}
#[test]
fn the_question_road_is_routed_by_the_label_too() {
let (unit, set) = both();
let unit_leg = resolver(&unit, "__async_graphql_shots_by_clip_keys");
let set_leg = resolver(&set, "__async_graphql_shots_by_clip_keys");
assert!(
unit_leg.contains(&door("retrieve_keys_by_clip", "")),
"the unit question road moved: {unit_leg}",
);
assert!(
!unit_leg.contains("retrieve_keys_by_clip_with"),
"the unit question road reached the `_with` door: {unit_leg}",
);
assert!(
unit_leg.contains("graphql::Match<"),
"the unit question road stopped answering the single-vector hit: {unit_leg}",
);
assert!(
set_leg.contains(&door("retrieve_keys_by_clip_with", ",_")),
"the set question road did not reach the `_with` door: {set_leg}",
);
assert!(
!set_leg.contains("retrieve_keys_by_clip::<"),
"the set question road also reached the single-vector door: {set_leg}",
);
assert!(
set_leg.contains("graphql::FoldedMatch<"),
"the set question road did not answer the folded hit: {set_leg}",
);
}
#[test]
fn the_fold_is_the_installed_one_or_the_elections_own() {
let set = tight(&declared(ElectionLabel::List));
assert!(
set.contains("graphql::installed_fold::<crate::Store,ShotsByClip>"),
"the deployment's seat is not keyed on (engine × election): {set}",
);
assert!(
set.contains("graphql::FoldDoor::semantics("),
"the installed fold is not read off the door: {set}",
);
assert!(
set.contains("Shots::fold_by_clip()"),
"the election's own fold is not the fallback: {set}",
);
assert!(
!set.contains("graphql(name=\"fold\")"),
"the request was given a fold to choose: {set}",
);
}
#[test]
fn the_set_doors_span_carries_the_folds_name() {
let set = tight(&declared(ElectionLabel::List));
assert!(
set.contains("fold=<__INGRAPH_FOLD__as::ingraph::__private::retrieve::FoldSemantics<"),
"the span does not name the fold it ran: {set}",
);
assert!(
set.contains("entry=\"retrieve_similar_by_clip_with\""),
"the span field landed on some other door: {set}",
);
assert!(
!set.contains("fold.name()"),
"the span reached the fold's name through inference: {set}",
);
let unit = tight(&declared(ElectionLabel::Embedding));
assert!(!unit.contains("fold=<"), "{unit}");
}
#[test]
fn the_set_road_lands_no_type_of_its_own() {
let roster = |label| -> Vec<String> {
declared(label)
.emitted_types()
.into_iter()
.map(|declared| declared.name.to_string())
.collect()
};
let unit = roster(ElectionLabel::Embedding);
let set = roster(ElectionLabel::List);
for name in &unit {
assert!(
set.contains(name),
"the label dropped `{name}` from the roster: {set:?}",
);
}
for name in &set {
assert!(
!["FoldedMatch", "StreamSighting", "Match"]
.iter()
.any(|hit| name.contains(hit)),
"a hit shape was minted per declaration: {name}",
);
}
for roster in [&unit, &set] {
assert_eq!(
roster
.iter()
.filter(|name| *name == "ShotsClipQuestion")
.count(),
1,
"{roster:?}",
);
}
}
#[test]
fn each_election_is_routed_by_its_own_label() {
let emission = tight(&elections(&[
("clip", ElectionLabel::List),
("poster", ElectionLabel::Embedding),
]));
let set = resolver(&emission, "__async_graphql_shots_similar_by_clip_keys");
assert!(
set.contains(&door("retrieve_similar_by_clip_with", ",_")),
"the set election was not routed to the set door: {set}",
);
let unit = resolver(&emission, "__async_graphql_shots_similar_by_poster_keys");
assert!(
unit.contains(&door("retrieve_similar_by_poster", "")),
"the unit election beside it was routed with its neighbour: {unit}",
);
assert!(!unit.contains("retrieve_similar_by_poster_with"), "{unit}",);
}