use crate::prefix::EmissionPrefix;
use quote::ToTokens as _;
use super::{
IndexGraphql, IndexModel, IndexRename,
tests::{document, embedding_index},
};
use crate::model::{GraphqlKind, NodeKind, Role};
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn tight(model: &IndexModel) -> String {
model.emit().to_string().split_whitespace().collect()
}
fn landed(model: &IndexModel) -> Vec<String> {
let file: syn::File = syn::parse2(model.emit()).expect("the 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()
}
fn undressed() -> IndexModel {
IndexModel {
prefix: EmissionPrefix::default(),
graphql: None,
..document()
}
}
#[test]
fn a_dressed_declaration_mints_its_unit_and_binds_the_seat() {
let model = document();
assert!(
landed(&model).contains(&"SearchDocumentQueries".to_owned()),
"{:?}",
landed(&model),
);
let emitted = tight(&model);
assert!(
emitted.contains("impl::ingraph::__private::transform::GraphqlforSearchDocument"),
"{emitted}",
);
assert!(
emitted.contains("typeQuery=SearchDocumentQueries;"),
"{emitted}",
);
}
#[test]
fn the_two_record_shaped_halves_are_the_empty_product() {
let emitted = tight(&document());
assert!(emitted.contains("typeInput=();"), "{emitted}");
assert!(emitted.contains("typeOutput=();"), "{emitted}");
}
#[test]
fn the_unit_and_its_binding_stand_outside_the_binding_gate() {
let file: syn::File = syn::parse2(document().emit()).expect("the emission parses");
let seated = file.items.iter().any(|item| {
matches!(item, syn::Item::Impl(block) if block
.trait_
.as_ref()
.is_some_and(|traited| traited.0.to_token_stream().to_string().contains("Graphql")))
});
assert!(seated, "the binding is a module-scope item");
let minted = file.items.iter().any(
|item| matches!(item, syn::Item::Struct(declared) if declared.ident == "SearchDocumentQueries"),
);
assert!(minted, "the unit is a module-scope item");
}
#[test]
fn an_undressed_declaration_mints_no_citizenship() {
let model = undressed();
assert!(!model.mounts());
assert!(model.citizen().is_empty());
assert!(
!landed(&model).contains(&"SearchDocumentQueries".to_owned()),
"{:?}",
landed(&model),
);
assert!(
!tight(&model).contains("transform::GraphqlforSearchDocument"),
"an undressed declaration bound the wire seat",
);
}
#[test]
fn an_artifact_family_mints_no_query_unit() {
let mut blob = document();
blob.kind = NodeKind::Blob;
blob.graphql = None;
assert!(!blob.mounts());
assert!(blob.citizen().is_empty());
}
#[test]
fn the_unit_is_on_the_roster_and_carries_its_own_role() {
let model = document();
let rostered: Vec<String> = model
.emitted_types()
.iter()
.map(|declared| declared.name.to_string())
.collect();
assert!(
rostered.contains(&"SearchDocumentQueries".to_owned()),
"{rostered:?}"
);
assert!(
model
.emitted_types()
.iter()
.any(|declared| matches!(declared.role, Role::Queries)),
);
assert!(
!undressed()
.emitted_types()
.iter()
.any(|declared| matches!(declared.role, Role::Queries)),
"an undressed declaration rostered a name it never mints",
);
}
#[test]
fn the_base_rename_moves_the_unit_with_the_family() {
let renamed = IndexModel {
prefix: EmissionPrefix::default(),
rename: Some(IndexRename::Base(ident("ShotSearch"))),
..document()
};
assert_eq!(renamed.queries_name().to_string(), "ShotSearchQueries");
assert!(landed(&renamed).contains(&"ShotSearchQueries".to_owned()));
assert!(
tight(&renamed).contains("typeQuery=ShotSearchQueries;"),
"the binding kept the old stem",
);
}
#[test]
fn a_skin_prefix_leaves_the_unit_where_the_bones_put_it() {
let mut model = document();
let mut skin = super::IndexBackend::of(GraphqlKind::AsyncGraphql);
skin.prefix = Some(syn::LitStr::new("wireFace", proc_macro2::Span::call_site()));
model.graphql = Some(IndexGraphql {
backends: vec![skin],
});
assert_eq!(model.queries_name().to_string(), "SearchDocumentQueries");
assert!(landed(&model).contains(&"SearchDocumentQueries".to_owned()));
}
#[test]
fn both_retrieving_kinds_mint_the_same_citizenship() {
let vector = embedding_index();
assert!(vector.mounts());
assert!(
tight(&vector).contains("typeQuery=SegmentEmbeddingQueries;"),
"{}",
tight(&vector),
);
}