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 count(written: u32) -> core::num::NonZeroU32 {
core::num::NonZeroU32::new(written).expect("a declared count is not zero")
}
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 declared(label: ElectionLabel, numbers: Numbers) -> IndexModel {
IndexModel {
prefix: EmissionPrefix::default(),
numbers,
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: vec![elected("clip", label)],
labelled: Vec::new(),
searchable: Vec::new(),
engines: vec![FieldEngines {
field: ident("clip"),
engines: vec![Engine {
resource: Some(syn::parse_quote!(crate::Store)),
..Engine::bare(ident("toy"))
}],
fanout: false,
}],
stores: Vec::new(),
data: false,
keys: true,
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, Numbers::default())),
tight(&declared(ElectionLabel::List, Numbers::default())),
)
}
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_question_door() {
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 road left the single-vector door: {unit_leg}",
);
assert!(
!unit_leg.contains("retrieve_keys_by_clip_with"),
"the unit road reached the set door: {unit_leg}",
);
assert!(
set_leg.contains(&door("retrieve_keys_by_clip_with", ",_")),
"the set road did not reach the `_with` door: {set_leg}",
);
assert!(
!set_leg.contains("retrieve_keys_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)] {
assert_eq!(
emission
.matches("graphql(name=\"shotsByClipKeys\")")
.count(),
1,
"the {label} road does not mint the question field exactly once: {emission}",
);
}
assert!(set.contains("graphql::FoldedMatch<"), "{set}");
assert!(set.contains("graphql::folded_matches("), "{set}");
assert!(!set.contains("graphql::Match<"), "{set}");
assert!(unit.contains("graphql::Match<"), "{unit}");
assert!(!unit.contains("FoldedMatch"), "{unit}");
}
#[test]
fn the_coordinates_arm_is_one_list_deeper_on_a_set() {
let (unit, set) = both();
assert!(
set.contains("Coordinates(::std::vec::Vec<::std::vec::Vec<"),
"the set question's arm is not a list of lists: {set}",
);
assert!(
unit.contains("Coordinates(::std::vec::Vec<<<::ingraph::Embedding"),
"the unit question's arm stopped being a flat list: {unit}",
);
for (label, emission) in [("unit", &unit), ("set", &set)] {
assert_eq!(
emission.matches("String::from(\"coordinates\")").count(),
2,
"the {label} question does not name its one arm twice (registry + parse): {emission}",
);
}
}
#[test]
fn the_two_refusals_a_point_list_can_earn() {
let (unit, set) = both();
assert!(
set.contains("noqueryvectors"),
"the empty list earns no named refusal: {set}",
);
assert!(
set.contains("queryvector{at}"),
"the width refusal does not name the offending index: {set}",
);
assert!(
!unit.contains("queryvector{at}"),
"the unit road grew an index it has no list to index: {unit}",
);
}
#[test]
fn the_stream_guard_lands_on_the_with_road_alone() {
let guarded = tight(&declared(
ElectionLabel::List,
Numbers {
streams: Some(count(2)),
..Numbers::default()
},
));
let question = resolver(&guarded, "__async_graphql_shots_by_clip_keys");
let by_example = resolver(&guarded, "__async_graphql_shots_similar_by_clip_keys");
assert!(
question.contains("__ingraph_asked>2"),
"the question road carries no guard: {question}",
);
assert!(
question.contains("atmost2queryvectors"),
"the refusal does not name the count: {question}",
);
assert!(
!by_example.contains("__ingraph_asked>"),
"the by-example road was guarded too: {by_example}",
);
let (_, open) = both();
assert!(
!open.contains("__ingraph_asked>"),
"a declaration that wrote no guard got one: {open}",
);
}
#[test]
fn the_elections_guard_beats_the_containers() {
let mut model = declared(
ElectionLabel::List,
Numbers {
streams: Some(count(4)),
..Numbers::default()
},
);
model.field_numbers.push(super::FieldNumbers {
field: ident("clip"),
numbers: Numbers {
streams: Some(count(2)),
..Numbers::default()
},
skins: Vec::new(),
});
let emission = tight(&model);
let question = resolver(&emission, "__async_graphql_shots_by_clip_keys");
assert!(
question.contains("__ingraph_asked>2"),
"the container's number won: {question}",
);
assert!(
!question.contains("__ingraph_asked>4"),
"both tiers reached the guard: {question}",
);
}
#[test]
fn the_guard_is_no_argument_and_no_answer_row() {
let guarded = tight(&declared(
ElectionLabel::List,
Numbers {
streams: Some(count(2)),
..Numbers::default()
},
));
assert!(
!guarded.contains("graphql(name=\"streams\")"),
"the guard became a request row: {guarded}",
);
}
#[test]
fn the_question_road_lands_no_type_of_its_own() {
let roster = |label| -> Vec<String> {
declared(label, Numbers::default())
.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 roster in [&unit, &set] {
assert_eq!(
roster
.iter()
.filter(|name| *name == "ShotsClipQuestion")
.count(),
1,
"{roster:?}",
);
}
}