use crate::{
engine::{Engine, FieldEngines},
index::{Dedup, Elected, ElectionLabel, IndexBackend, IndexGraphql, IndexModel, Upstream},
model::{GraphqlKind, NodeKind},
prefix::EmissionPrefix,
};
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn dressed(graphql: Option<IndexGraphql>) -> IndexModel {
IndexModel {
prefix: EmissionPrefix::default(),
numbers: crate::index::Numbers::default(),
field_numbers: Vec::new(),
index: ident("Frames"),
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::new(
ident("frames"),
ElectionLabel::List,
syn::parse_quote!(::std::vec::Vec<::ingraph::Embedding<f32, 4, Clip>>),
None,
),
Elected::new(
ident("poster"),
ElectionLabel::Embedding,
syn::parse_quote!(::ingraph::Embedding<f32, 4, Clip>),
None,
),
],
labelled: Vec::new(),
searchable: Vec::new(),
engines: vec![
FieldEngines {
field: ident("frames"),
engines: vec![Engine {
resource: Some(syn::parse_quote!(crate::Lance)),
..Engine::bare(ident("lance"))
}],
fanout: false,
},
FieldEngines {
field: ident("poster"),
engines: vec![Engine::bare(ident("hnsw"))],
fanout: false,
},
],
stores: Vec::new(),
extension: None,
data: false,
keys: false,
subscribe: false,
graphql,
rename: None,
slot: None,
fields: Vec::new(),
}
}
fn with_skin() -> IndexModel {
dressed(Some(IndexGraphql::of(GraphqlKind::AsyncGraphql)))
}
fn bare() -> IndexModel {
dressed(None)
}
fn renamed(skin: impl FnOnce(&mut IndexBackend)) -> IndexModel {
let mut backend = IndexBackend::of(GraphqlKind::AsyncGraphql);
skin(&mut backend);
dressed(Some(IndexGraphql {
backends: vec![backend],
}))
}
fn word(spelled: &str) -> syn::LitStr {
syn::LitStr::new(spelled, proc_macro2::Span::call_site())
}
fn published(model: &IndexModel) -> Vec<String> {
let said = model.tier().emit().to_string();
let mut names = Vec::new();
let mut rest = said.as_str();
while let Some(at) = rest.find("Borrowed (\"") {
rest = &rest[at + "Borrowed (\"".len()..];
if let Some(end) = rest.find('"') {
names.push(rest[..end].to_owned());
rest = &rest[end..];
}
}
names
}
fn emitted(model: &IndexModel) -> String {
model.tier().emit().to_string().split_whitespace().collect()
}
fn declared(model: &IndexModel) -> Vec<String> {
let file: syn::File = syn::parse2(model.tier().emit()).expect("the tier emission parses");
file
.items
.into_iter()
.filter_map(|item| match item {
syn::Item::Struct(declared) => Some(declared.ident.to_string()),
syn::Item::Enum(declared) => Some(declared.ident.to_string()),
_ => None,
})
.collect()
}
const FACES: [&str; 4] = [
"async_graphql::InputType",
"async_graphql::InputObjectType",
"async_graphql::OneofObjectType",
"async_graphql::resolver_utils::EnumItem",
];
#[test]
fn a_declared_backend_lands_every_hand_written_face() {
let said = emitted(&with_skin());
for face in FACES {
let spelled: String = face.split_whitespace().collect();
assert!(
said.contains(&spelled),
"a dressed declaration should land `{face}`",
);
}
}
#[test]
fn a_declaration_that_named_no_backend_lands_none_of_them() {
let said = emitted(&bare());
for face in FACES {
let spelled: String = face.split_whitespace().collect();
assert!(
!said.contains(&spelled),
"an undressed declaration should land no `{face}`",
);
}
}
#[test]
fn the_binding_gate_itself_is_absent_without_a_backend_word() {
assert!(!emitted(&bare()).contains("if_async_graphql"));
assert!(emitted(&with_skin()).contains("if_async_graphql"));
}
#[test]
fn the_two_emissions_declare_exactly_the_same_types() {
assert_eq!(declared(&with_skin()), declared(&bare()));
assert!(
!declared(&bare()).is_empty(),
"the fixture should mint types for the claim to be about",
);
}
#[test]
fn the_selector_enum_survives_the_gate_and_only_its_face_falls() {
let bones = declared(&bare());
assert!(
bones.iter().any(|name| name == "FramesFramesEngineInput"),
"the `@oneOf` selector is a Rust enum a backend-only pipeline matches on: {bones:?}",
);
assert!(!emitted(&bare()).contains("OneofObjectType"));
assert!(emitted(&with_skin()).contains("OneofObjectType"));
}
#[test]
fn the_roster_is_the_same_whether_or_not_a_backend_was_named() {
let dressed: Vec<String> = with_skin()
.tier()
.emitted_types()
.iter()
.map(ToString::to_string)
.collect();
let plain: Vec<String> = bare()
.tier()
.emitted_types()
.iter()
.map(ToString::to_string)
.collect();
assert_eq!(dressed, plain);
}
#[test]
fn a_backend_the_declaration_did_not_name_dresses_nothing() {
let block = IndexGraphql {
backends: vec![IndexBackend::of(GraphqlKind::AsyncGraphql)],
};
assert!(block.renders(GraphqlKind::AsyncGraphql));
let empty = IndexGraphql {
backends: Vec::new(),
};
assert!(
!empty.renders(GraphqlKind::AsyncGraphql),
"a block naming nobody dresses nobody",
);
}
#[test]
fn every_minted_name_is_built_on_the_declarations_own() {
let model = bare();
let index = model.index.to_string();
for name in declared(&model) {
assert!(
name
.strip_prefix(&index)
.is_some_and(|rest| !rest.is_empty()),
"`{name}` is not `{index}` plus a segment, so a base rename could not reach it",
);
}
}
#[test]
fn the_backend_prefix_reaches_every_sdl_name_and_no_rust_one() {
let model = renamed(|skin| skin.prefix = Some(word("shotSearch")));
let names = published(&model);
assert!(!names.is_empty(), "the fixture publishes a schema");
for name in &names {
assert!(
name.starts_with("ShotSearch"),
"`{name}` kept its Rust base under a backend rename: {names:?}",
);
}
assert!(names.iter().any(|name| name == "ShotSearchFuseOptions"));
assert_eq!(declared(&model), declared(&bare()));
}
#[test]
fn a_per_face_override_names_that_face_verbatim_and_leaves_the_rest_on_the_base() {
let model = renamed(|skin| {
skin.prefix = Some(word("shotSearch"));
skin.options = Some(word("ShotKnobs"));
skin.question = Some(word("ShotAsk"));
});
let names = published(&model);
for owed in ["ShotKnobs", "ShotAsk"] {
assert!(
names.iter().any(|name| name == owed),
"the override should name its face outright: {names:?}",
);
}
assert!(
!names.iter().any(|name| name == "ShotSearchFuseOptions"),
"the base should not also publish the face its override named: {names:?}",
);
assert!(
names.iter().any(|name| name.starts_with("ShotSearch")),
"the base still reaches everything the overrides did not name: {names:?}",
);
}
#[test]
fn an_override_with_no_base_moves_only_its_own_face() {
let model = renamed(|skin| skin.options = Some(word("ShotKnobs")));
let names = published(&model);
assert!(names.iter().any(|name| name == "ShotKnobs"));
assert!(
names.iter().any(|name| name == "FramesFuseQuestion"),
"every face the override did not name keeps the declaration's own: {names:?}",
);
assert!(
!names.iter().any(|name| name == "FramesFuseOptions"),
"and the renamed one is gone from the schema: {names:?}",
);
}
#[test]
fn the_tier_fused_door_publishes_nothing_for_a_word_to_move() {
let mut model = with_skin();
model.embeddings[0] = Elected::new(
ident("frames"),
ElectionLabel::Embedding,
syn::parse_quote!(::ingraph::Embedding<f32, 4, Clip>),
None,
);
let evidence = model.tier().evidence_name().to_string();
assert!(
declared(&model).contains(&evidence),
"the evidence is a declared Rust type",
);
assert!(
!published(&model).contains(&evidence),
"and it publishes no schema name: {:?}",
published(&model),
);
let names = published(&model);
for absent in ["FramesSighting", "FramesFusedKeyMatch"] {
assert!(
!names.iter().any(|name| name == absent),
"the tier's fused door published `{absent}`, so its answer is on the wire and the word the \
front end refuses now has something to move: {names:?}",
);
}
}
#[test]
fn a_fanned_road_does_publish_an_answer_and_the_evidence_is_still_not_it() {
let mut model = with_skin();
model.engines[1].fanout = true;
model.engines[1].engines = vec![
Engine {
resource: Some(syn::parse_quote!(crate::Lance)),
..Engine::bare(ident("lance"))
},
Engine {
resource: Some(syn::parse_quote!(crate::Hnsw)),
..Engine::bare(ident("hnsw"))
},
];
let said = model.emit().to_string();
for owed in ["FramesPosterSighting", "FramesPosterFusedKeyMatch"] {
assert!(
said.contains(&format!("\"{owed}\"")),
"the fanned road publishes `{owed}`",
);
}
let evidence = model.tier().slot_evidence_name(&model.embeddings[1]);
assert!(
said.contains(&format!("enum {evidence}")),
"the slot evidence is a declared Rust type",
);
assert!(
!said.contains(&format!("\"{evidence}\"")),
"and it is not what the schema publishes",
);
}
#[test]
fn the_engine_selector_is_named_only_where_the_declaration_mints_exactly_one() {
let model = renamed(|skin| skin.engine = Some(word("WhichStore")));
assert!(published(&model).iter().any(|name| name == "WhichStore"));
let mut two = with_skin();
two.engines[1].engines = vec![Engine {
resource: Some(syn::parse_quote!(crate::Hnsw)),
..Engine::bare(ident("hnsw"))
}];
assert!(two.tier().engine_selector().is_none());
assert!(with_skin().tier().engine_selector().is_some());
}