use super::{Dedup, IndexGraphql, IndexModel, Projected, Upstream};
use crate::{
engine::{Engine, FieldEngines},
model::{Column, GraphqlKind, NodeKind, Projection},
prefix::EmissionPrefix,
};
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn column(name: &str, ty: &str) -> Column {
Column {
name: ident(name),
source: ident(name),
ty: syn::parse_str(ty).expect("a type"),
project: Projection::Borrowed,
}
}
fn projected(field: &str, ty: &str) -> Projected {
Projected {
field: ident(field),
from: ident(field),
ty: syn::parse_str(ty).expect("a type"),
}
}
fn leg(word: &str, resource: Option<syn::Path>) -> Engine {
Engine {
resource,
..Engine::bare(ident(word))
}
}
fn document(subscribe: bool, fields: Vec<(&str, Vec<Engine>)>) -> IndexModel {
IndexModel {
prefix: EmissionPrefix::default(),
numbers: crate::index::Numbers::default(),
field_numbers: Vec::new(),
stores: Vec::new(),
extension: None,
data: false,
keys: false,
subscribe,
graphql: Some(IndexGraphql::of(GraphqlKind::AsyncGraphql)),
rename: None,
slot: None,
index: ident("Media"),
kind: NodeKind::Fulltext,
vis: syn::Visibility::Public(syn::token::Pub::default()),
krate: syn::parse_quote!(::ingraph),
source: Upstream {
node: syn::parse_quote!(asr::Segment),
projections: fields
.iter()
.map(|(name, _)| projected(name, "String"))
.collect(),
},
dedup: Dedup::Hash,
embeddings: Vec::new(),
labelled: Vec::new(),
searchable: fields.iter().map(|(name, _)| ident(name)).collect(),
engines: fields
.iter()
.map(|(name, engines)| FieldEngines {
field: ident(name),
engines: engines.clone(),
fanout: false,
})
.collect(),
fields: fields
.iter()
.map(|(name, _)| column(name, "String"))
.collect(),
}
}
fn pointed(subscribe: bool) -> IndexModel {
document(
subscribe,
vec![(
"body",
vec![leg("lexical", Some(syn::parse_quote!(app::Ft)))],
)],
)
}
fn tight(model: &IndexModel) -> String {
model.emit().to_string().replace(' ', "")
}
#[test]
fn the_word_adds_a_node_and_moves_nothing_else() {
let bare = tight(&pointed(false));
let wearing = tight(&pointed(true));
assert!(
!bare.contains("structMediaIndexer"),
"a declaration that wrote no word minted a node: {bare}",
);
assert!(
wearing.contains("structMediaIndexer"),
"the word minted no node: {wearing}",
);
assert!(
wearing.len() > bare.len(),
"the wearing expansion is not larger than the bare one",
);
for fragment in [
"FulltextRoadforMediaBodyLexical",
"HabitatsforMedia",
"pubstructMediaBodyLexical",
] {
assert!(
bare.contains(fragment) && wearing.contains(fragment),
"`{fragment}` is not in both expansions",
);
}
}
#[test]
fn the_node_states_its_edge_its_mount_and_its_landing() {
let emitted = tight(&pointed(true));
assert!(
emitted.contains("Upstream::of::<asr::Segment>(\"asr::Segment\")"),
"{emitted}",
);
assert!(
emitted.contains("ResourceDemand::of::<app::Ft>"),
"the node demanded no mount: {emitted}",
);
assert!(emitted.contains("constHOLDS_LANE:bool=true"), "{emitted}");
assert!(emitted.contains("Effect::Lands"), "{emitted}");
}
#[test]
fn the_node_wears_the_awaited_face_and_emits_the_declaration() {
let emitted = tight(&pointed(true));
assert!(
emitted.contains("operator::AsyncOperatorforMediaIndexer"),
"{emitted}",
);
assert!(
emitted.contains("typeIn=asr::Segment;typeOut=Media;"),
"{emitted}"
);
assert!(
emitted.contains("store::IndexRefusal;"),
"the node refuses in some type other than the seam's: {emitted}",
);
}
#[test]
fn two_legs_at_one_resource_hold_one_mount() {
let emitted = tight(&document(
true,
vec![
(
"title",
vec![leg("lexical", Some(syn::parse_quote!(app::Ft)))],
),
(
"body",
vec![leg("lexical", Some(syn::parse_quote!(app::Ft)))],
),
],
));
assert!(emitted.contains("__ingraph_mount_0"), "{emitted}");
assert!(
!emitted.contains("__ingraph_mount_1"),
"one resource grew two mounts: {emitted}",
);
for corpus in [
"MediaTitleLexicalas::ingraph",
"MediaBodyLexicalas::ingraph",
] {
assert!(
emitted.contains(corpus),
"`{corpus}` is not written: {emitted}"
);
}
}
#[test]
fn two_resources_are_two_mounts() {
let emitted = tight(&document(
true,
vec![
(
"title",
vec![leg("lexical", Some(syn::parse_quote!(app::Ft)))],
),
(
"body",
vec![leg("replica", Some(syn::parse_quote!(app::Other)))],
),
],
));
assert!(emitted.contains("__ingraph_mount_0"), "{emitted}");
assert!(emitted.contains("__ingraph_mount_1"), "{emitted}");
assert!(
emitted.contains("ResourceDemand::of::<app::Ft>"),
"{emitted}"
);
assert!(
emitted.contains("ResourceDemand::of::<app::Other>"),
"{emitted}",
);
}
#[test]
fn an_unpointed_declaration_mints_no_node_even_wearing_the_word() {
let emitted = tight(&document(true, vec![("body", vec![leg("lexical", None)])]));
assert!(
!emitted.contains("structMediaIndexer"),
"a subscriber was minted for a declaration with nowhere to write: {emitted}",
);
}
#[test]
fn a_kind_without_the_road_mints_no_node() {
for kind in [NodeKind::Vector, NodeKind::Blob] {
let model = IndexModel {
prefix: EmissionPrefix::default(),
kind,
..pointed(true)
};
assert!(
!model.subscriber().to_string().contains("MediaIndexer"),
"{kind:?} reached the subscriber road",
);
}
}