use super::super::{ArmModel, MemberModel, SearchModel, SeatModel, SharedBinding, SharedBridge};
use crate::{index::Dedup, model::GraphqlKind};
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn seat(name: &str) -> SeatModel {
SeatModel {
name: ident(name),
ty: syn::parse_quote!(::std::string::String),
}
}
fn member(field: &str, ty: &str) -> MemberModel {
MemberModel {
field: ident(field),
ty: syn::parse_str(ty).expect("an index type"),
weight: None,
depth: None,
floor: None,
arms: Vec::new(),
}
}
fn recipe(seats: Vec<SeatModel>, graphql: Option<GraphqlKind>) -> SearchModel {
SearchModel {
krate: syn::parse_quote!(::ingraph),
vis: syn::parse_quote!(pub),
name: ident("TravelSearch"),
seats,
dedup: Dedup::Hash,
limit: None,
graphql,
members: vec![member("subs", "media::Subs"), member("shots", "Shots")],
}
}
fn dressed() -> SearchModel {
recipe(vec![seat("text")], Some(GraphqlKind::AsyncGraphql))
}
fn arm(name: &str, tower: &str, bridge: SharedBridge) -> ArmModel {
seated("text", name, tower, bridge)
}
fn seated(seat: &str, name: &str, tower: &str, bridge: SharedBridge) -> ArmModel {
ArmModel {
name: ident(name),
tower: syn::parse_str(tower).expect("a tower path"),
shared: Some(SharedBinding {
seat: ident(seat),
bridge,
}),
floor: None,
}
}
fn choosing() -> SearchModel {
let mut model = recipe(
vec![seat("text"), seat("code")],
Some(GraphqlKind::AsyncGraphql),
);
model.members[0].arms = vec![seated(
"text",
"words",
"app::TextTower",
SharedBridge::Total,
)];
model.members[1].arms = vec![seated(
"code",
"codes",
"app::CodeTower",
SharedBridge::Total,
)];
model
}
fn bound(bridge: SharedBridge) -> SearchModel {
let mut model = dressed();
model.members[0].arms = vec![arm("zh", "app::ZhTower", bridge)];
model
}
fn tight(model: &SearchModel) -> String {
model.wire().to_string().split_whitespace().collect()
}
fn landed(model: &SearchModel) -> Vec<String> {
let file: syn::File = syn::parse2(model.wire()).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()
}
#[test]
fn an_undressed_recipe_mints_nothing() {
let plain = recipe(vec![seat("text")], None);
assert!(!plain.mounts());
assert!(plain.wire().is_empty());
}
#[test]
fn the_wire_road_needs_at_least_one_seat() {
let none = recipe(Vec::new(), Some(GraphqlKind::AsyncGraphql));
assert!(!none.mounts(), "a seatless recipe has nothing to be asked");
assert!(none.wire().is_empty());
assert!(dressed().mounts(), "one seat is the road");
assert!(!dressed().chooses(), "one seat is not a choice");
let several = recipe(
vec![seat("text"), seat("point")],
Some(GraphqlKind::AsyncGraphql),
);
assert!(several.mounts(), "several seats publish a choice");
assert!(several.chooses());
assert!(!several.wire().is_empty());
}
#[test]
fn the_unit_stands_outside_the_gate_and_the_answers_inside_it() {
let model = dressed();
assert_eq!(
landed(&model),
vec!["TravelSearchQueries".to_owned()],
"an answer type escaped the gate, or the unit fell inside it",
);
let emitted = tight(&model);
assert!(
emitted.contains("impl::ingraph::__private::transform::GraphqlforTravelSearch"),
"{emitted}",
);
assert!(
emitted.contains("typeQuery=TravelSearchQueries;"),
"{emitted}"
);
assert!(
emitted.contains("typeInput=();") && emitted.contains("typeOutput=();"),
"the two record-shaped halves stopped being the empty product: {emitted}",
);
}
#[test]
fn the_answer_family_is_a_sighting_per_member_and_a_hit_per_tier() {
let emitted = tight(&dressed());
for owed in [
"structTravelSearchSubsSighting",
"structTravelSearchShotsSighting",
"enumTravelSearchSighting",
"structTravelSearchKeyMatch",
"structTravelSearchMatch",
] {
assert!(emitted.contains(owed), "{owed} is missing: {emitted}");
}
assert!(
emitted.contains("Subs(TravelSearchSubsSighting),")
&& emitted.contains("Shots(TravelSearchShotsSighting),"),
"a member lost its union arm: {emitted}",
);
}
#[test]
fn every_members_fact_is_reached_by_projection() {
let emitted = tight(&dressed());
for owed in [
"<media::Subsas::ingraph::__private::graphql::FusedEngine>::Engine",
"<Shotsas::ingraph::__private::graphql::FusedEngine>::Engine",
"::ingraph::__private::graphql::FusedCitizen<",
"<media::Subsas::ingraph::__private::retrieve::AsyncFusedRead<",
"::ingraph::__private::graphql::WireRoot>::Address",
"::ingraph::__private::graphql::WireRoot>::Entity",
] {
assert!(emitted.contains(owed), "{owed} is missing: {emitted}");
}
assert!(
!emitted.contains("SubsEngine") && !emitted.contains("ShotsEngine"),
"an engine name was mangled out of a member's path: {emitted}",
);
}
#[test]
fn a_sighting_publishes_the_currency_the_ordinal_and_the_contribution() {
let emitted = tight(&dressed());
assert!(emitted.contains("#[graphql(name=\"score\")]"), "{emitted}");
assert!(
emitted.contains("#[graphql(name=\"ordinal\")]"),
"{emitted}"
);
assert!(
emitted.contains("#[graphql(name=\"contribution\")]"),
"{emitted}",
);
assert!(
emitted.contains("::ingraph::__private::graphql::WireScore>::Wire<'__ingraph_row>"),
"the member's currency stopped crossing as itself: {emitted}",
);
assert!(
emitted.contains("__ingraph_contribution"),
"the contribution stopped being carried out of the sighting: {emitted}",
);
assert!(
!emitted.contains("60.0") && !emitted.contains("1.0/"),
"the emission recomputed a number the fold already held: {emitted}",
);
}
#[test]
fn the_union_is_written_out_rather_than_derived() {
let emitted = tight(&dressed());
for owed in [
"resolver_utils::ContainerTypeforTravelSearchSighting",
"OutputTypeforTravelSearchSighting",
"UnionTypeforTravelSearchSighting",
"registry::MetaTypeId::Union",
] {
assert!(emitted.contains(owed), "{owed} is missing: {emitted}");
}
assert!(
!emitted.contains("derive(::ingraph::async_graphql::Union"),
"a derive reached the emission: {emitted}",
);
}
#[test]
fn the_two_fields_wear_the_tier_noun_law() {
let emitted = tight(&dressed());
assert!(
emitted.contains("#[graphql(name=\"travelSearchKeys\")]"),
"{emitted}",
);
assert!(
emitted.contains("#[graphql(name=\"travelSearch\")]"),
"{emitted}",
);
assert_eq!(
dressed().field_name(true),
"travelSearchKeys",
"the keys tier lost its noun",
);
assert_eq!(dressed().field_name(false), "travelSearch");
}
#[test]
fn the_arguments_are_the_seat_and_the_two_numbers() {
let emitted = tight(&dressed());
assert!(emitted.contains("#[graphql(name=\"text\")]"), "{emitted}");
assert!(
emitted.contains("#[graphql(name=\"first\",default=20i32)]")
|| emitted.contains("#[graphql(name=\"first\",default=20)]"),
"{emitted}",
);
assert!(emitted.contains("#[graphql(name=\"depth\")]"), "{emitted}");
assert!(
!emitted.contains("#[graphql(name=\"ceiling\")]"),
"a protective number reached the wire: {emitted}",
);
assert!(
!emitted.contains("#[graphql(name=\"capacity\")]")
&& !emitted.contains("#[graphql(name=\"ttl\")]"),
"a keeper knob whose grain is the STORE reached a question's surface: {emitted}",
);
}
#[test]
fn the_declared_limit_becomes_the_published_default() {
let mut model = dressed();
model.limit = core::num::NonZeroU32::new(7);
let emitted = tight(&model);
assert!(
emitted.contains("default=7i32") || emitted.contains("default=7)"),
"{emitted}",
);
assert_eq!(dressed().wire_first(), crate::index::FRAMEWORK_LIMIT);
}
#[test]
fn a_zero_page_answers_before_the_door() {
let emitted = tight(&dressed());
let at_zero = emitted
.find("NonZeroU32::new(__ingraph_first)")
.expect("the edge-zero read is emitted");
let at_read = emitted
.find("FusedCitizen<")
.expect("the members are asked through the citizen seam");
assert!(
at_zero < at_read,
"the zero read happens after a citizen lookup: {emitted}",
);
}
#[test]
fn both_fields_run_one_fold() {
let emitted = tight(&dressed());
assert_eq!(
emitted.matches("fuse_weighted::<_,_,").count(),
1,
"the fold is written twice: {emitted}",
);
assert_eq!(
emitted.matches("__ingraph_travel_search_ask(").count(),
4,
"the shared entry is not what the unpaged fields and the capture call: {emitted}",
);
assert_eq!(
emitted.matches("__ingraph_travel_search_stretch(").count(),
3,
"the paged entry is not what both paged fields call: {emitted}",
);
assert_eq!(
emitted.matches("WireRoot>::hydrate(").count(),
2,
"the hydrated tiers stopped hydrating: {emitted}",
);
assert!(
emitted.contains("Iterator::filter_map"),
"a hole stopped being dropped: {emitted}",
);
}
#[test]
fn the_paged_tier_is_a_second_pair_rather_than_a_change_to_the_first() {
let model = dressed();
assert_eq!(model.stretch_field_name(true), "travelSearchKeysStretch");
assert_eq!(model.stretch_field_name(false), "travelSearchStretch");
assert_eq!(
model.stretch_name(true).to_string(),
"TravelSearchKeyStretch"
);
assert_eq!(model.stretch_name(false).to_string(), "TravelSearchStretch");
let emitted = tight(&model);
for word in [
"#[graphql(name=\"travelSearchKeys\")]",
"#[graphql(name=\"travelSearch\")]",
"#[graphql(name=\"travelSearchKeysStretch\")]",
"#[graphql(name=\"travelSearchStretch\")]",
] {
assert!(emitted.contains(word), "{word} missing from {emitted}");
}
assert_eq!(
emitted.matches("#[graphql(name=\"text\")]").count(),
4,
"the seat is not on all four fields: {emitted}",
);
assert_eq!(
emitted
.matches("#[graphql(name=\"text\")]__async_graphql_text:::core::option::Option<")
.count(),
2,
"the paged seat is not the nullable one, or the unpaged seat became nullable: {emitted}",
);
assert_eq!(
emitted
.matches(
"#[graphql(name=\"pageToken\")]__async_graphql_page_token:::core::option::Option<\
::std::string::String>",
)
.count(),
2,
"the pageToken argument is not on exactly the two paged fields: {emitted}",
);
assert_eq!(
emitted
.matches("#[graphql(name=\"nextPageToken\")]asyncfn__async_graphql_next_page_token(&self)")
.count(),
2,
"the claim does not ride exactly the two stretch answers: {emitted}",
);
}
#[test]
fn the_resume_arm_asks_nothing() {
let emitted = tight(&dressed());
let stretch = emitted
.find("asyncfn__ingraph_travel_search_stretch(")
.expect("the paged entry is emitted");
let capture = emitted[stretch..]
.find("__ingraph_travel_search_ask(")
.expect("the capture arm calls the fold");
let resume = emitted[stretch..]
.find("FromStr>::from_str(")
.expect("the resume arm parses a page token");
assert!(
resume < capture,
"the resume arm reaches the fold: {emitted}",
);
assert!(
emitted.contains("SnapshotKeeper::read::<"),
"the cut is not the keeper's: {emitted}",
);
assert!(
emitted.contains("installed_keeper("),
"the paged road does not look a keeper up: {emitted}",
);
}
#[test]
fn the_capture_is_walled_by_the_one_depth_law() {
let emitted = tight(&dressed());
assert_eq!(
emitted.matches("route_depth(").count(),
4,
"the capture's wall is not the fold's own law: {emitted}",
);
assert!(
emitted.contains("SnapshotKeeper::depth("),
"the ceiling is not read off the keeper: {emitted}",
);
}
#[test]
fn both_roads_splice_one_edge_law() {
let model = dressed();
let edge = model
.member_edge(&model.members[0])
.to_string()
.split_whitespace()
.collect::<String>();
for road in [
tight(&model),
model.fuse().to_string().split_whitespace().collect(),
] {
assert!(
road.contains(&edge),
"a road wrote its own edge law: {road}"
);
}
}
#[test]
fn a_bound_member_looks_its_road_up_and_runs_it() {
let emitted = tight(&bound(SharedBridge::Total));
assert!(
emitted.contains(
"let(mut__ingraph_road,__ingraph_knobs,__ingraph_wall)=::ingraph::__private::graphql::\
installed_road::<app::ZhTower>(__ingraph_ctx,__ingraph_word)?;"
),
"{emitted}"
);
assert!(
emitted.contains(
"<app::ZhTowaras::ingraph::__private::retrieve::AssembledTower>::run(&mut__ingraph_road,"
) || emitted.contains(
"<app::ZhToweras::ingraph::__private::retrieve::AssembledTower>::run(&mut__ingraph_road,"
),
"{emitted}"
);
assert!(
emitted.contains(
"TrySharedQuestion<TravelSearchTextSeat,<app::ZhToweras::ingraph::__private::retrieve::\
SearchTower>::Answer>>::try_from_shared(&__ingraph_answered)"
),
"{emitted}"
);
assert!(emitted.contains("__ingraph_knobs,"), "{emitted}");
}
#[test]
fn a_bound_member_walks_inside_the_budget_its_door_named() {
let emitted = tight(&bound(SharedBridge::Total));
assert!(
emitted.contains(
"let__ingraph_walked=::ingraph::__private::retrieve::walk_within(__ingraph_wall,<app::\
ZhToweras::ingraph::__private::retrieve::AssembledTower>::asked_budget(::core::option::\
Option::as_ref(&__ingraph_request)),::core::any::type_name::<app::ZhTower>(),<app::\
ZhToweras::ingraph::__private::retrieve::AssembledTower>::run(&mut__ingraph_road,\
__ingraph_asked,__ingraph_knobs,::core::clone::Clone::clone(&__ingraph_request),),).await;"
),
"{emitted}"
);
assert!(
emitted.contains("::core::result::Result::Err(__ingraph_spent)=>"),
"{emitted}"
);
}
#[test]
fn an_unbound_member_crosses_from_the_seat_exactly_as_it_did() {
let emitted = tight(&bound(SharedBridge::Total));
assert!(
emitted.contains(
"TrySharedQuestion<TravelSearchTextSeat,::std::string::String>>::try_from_shared(\
__ingraph_broadcast)"
),
"the second member's crossing moved: {emitted}"
);
assert_eq!(
emitted.matches("installed_road::<").count(),
1,
"a member with no arm reached for a road: {emitted}"
);
let plain = tight(&dressed());
assert!(!plain.contains("installed_road"), "{plain}");
assert!(!plain.contains("AssembledTower"), "{plain}");
assert!(!plain.contains("walk_within"), "{plain}");
}
#[test]
fn each_bridge_crosses_the_way_its_word_named() {
let total = tight(&bound(SharedBridge::Total));
assert!(
total.contains(
"::core::convert::From<_>>::from(::core::clone::Clone::clone(\
__ingraph_broadcast))"
),
"{total}"
);
let fallible = tight(&bound(SharedBridge::Fallible));
assert!(
fallible.contains(
"::core::convert::TryFrom<_>>::try_from(::core::clone::Clone::clone(\
__ingraph_broadcast))"
),
"{fallible}"
);
let hooked = tight(&bound(SharedBridge::Hook(syn::parse_quote!(app::cross))));
assert!(
hooked.contains("app::cross(__ingraph_broadcast)"),
"{hooked}"
);
assert!(
!hooked.contains("Clone::clone(__ingraph_broadcast)"),
"a hook takes the borrow: {hooked}"
);
let tried = tight(&bound(SharedBridge::FallibleHook(syn::parse_quote!(
app::try_cross
))));
assert!(
tried.contains("matchapp::try_cross(__ingraph_broadcast)"),
"{tried}"
);
}
#[test]
fn one_seat_publishes_the_bare_argument_and_no_choice_at_all() {
let emitted = tight(&dressed());
assert!(
!emitted.contains("TravelSearchAskInput"),
"a one-seat recipe minted a choice: {emitted}",
);
assert!(
!emitted.contains("oneof:true") && !emitted.contains("OneofObjectType"),
"a one-seat recipe published a `@oneOf`: {emitted}",
);
assert!(
!emitted.contains("#[graphql(name=\"ask\")]"),
"a one-seat recipe named the choice's argument: {emitted}",
);
assert_eq!(
landed(&dressed()),
vec!["TravelSearchQueries".to_owned()],
"a type appeared beside the unit at module scope",
);
assert_eq!(
emitted.matches("#[graphql(name=\"text\")]").count(),
4,
"the seat stopped being the argument on all four fields: {emitted}",
);
}
#[test]
fn several_seats_publish_one_oneof_argument() {
let model = choosing();
let emitted = tight(&model);
assert_eq!(
landed(&model),
vec!["TravelSearchQueries".to_owned()],
"the choice escaped the gate, or the unit fell inside it",
);
assert!(emitted.contains("pubenumTravelSearchAskInput"), "{emitted}");
assert!(
emitted.contains("Text(::std::string::String),")
&& emitted.contains("Code(::std::string::String),"),
"a seat lost its variant: {emitted}",
);
assert!(emitted.contains("oneof:true"), "{emitted}");
assert!(
emitted.contains("InputObjectTypeforTravelSearchAskInput")
&& emitted.contains("OneofObjectTypeforTravelSearchAskInput"),
"{emitted}",
);
assert_eq!(
emitted.matches("__ingraph_object.len()==1").count(),
2,
"the exactly-one read is not one per seat: {emitted}",
);
assert!(
emitted.contains("::std::string::String::from(\"text\")")
&& emitted.contains("::std::string::String::from(\"code\")"),
"{emitted}",
);
assert_eq!(
emitted.matches("#[graphql(name=\"ask\")]").count(),
4,
"the choice is not the argument on all four fields: {emitted}",
);
assert!(
!emitted.contains("#[graphql(name=\"text\")]"),
"a seat stayed an argument of its own beside the choice: {emitted}",
);
assert!(
!emitted.contains(
"#[derive(::core::fmt::Debug,::core::clone::Clone,::core::cmp::PartialEq,)]\
pubenumTravelSearchAskInput"
),
"the choice put an obligation on a seat type: {emitted}",
);
}
#[test]
fn each_seat_reaches_only_the_members_whose_arms_bought_it() {
let emitted = tight(&choosing());
let text_arm = emitted
.find("TravelSearchAskInput::Text(__ingraph_broadcast)=>{")
.expect("the text seat has an arm");
let code_arm = emitted
.find("TravelSearchAskInput::Code(__ingraph_broadcast)=>{")
.expect("the code seat has an arm");
assert!(text_arm < code_arm, "the arms are out of declaration order");
let text = &emitted[text_arm..code_arm];
assert!(text.contains("app::TextTower"), "{text}");
assert!(
!text.contains("app::CodeTower"),
"the code member was asked on a text request: {text}",
);
assert_eq!(
text.matches("try_from_shared(").count(),
1,
"the text arm crossed a member it does not reach: {text}",
);
let code = &emitted[code_arm..];
let code = &code[..code
.find("letquestion=&__ingraph_question;")
.unwrap_or(code.len())];
assert!(code.contains("app::CodeTower"), "{code}");
assert!(
!code.contains("app::TextTower"),
"the text member was asked on a code request: {code}",
);
assert_eq!(
code.matches("try_from_shared(").count(),
1,
"the code arm crossed a member it does not reach: {code}",
);
assert!(text.contains("TravelSearchTextSeat,"), "{text}");
assert!(code.contains("TravelSearchCodeSeat,"), "{code}");
}
#[test]
fn the_numbers_and_the_page_token_ride_both_shapes() {
let bare = tight(&dressed());
let chosen = tight(&choosing());
for (word, count) in [
("#[graphql(name=\"first\",default=", 4),
("#[graphql(name=\"depth\")]", 4),
("#[graphql(name=\"pageToken\")]", 2),
("#[graphql(name=\"nextPageToken\")]", 2),
] {
assert_eq!(bare.matches(word).count(), count, "{word} in {bare}");
assert_eq!(chosen.matches(word).count(), count, "{word} in {chosen}");
}
for road in [&bare, &chosen] {
assert_eq!(
road
.matches("__ingraph_broadcast:::core::option::Option<")
.count(),
1,
"the paged entry stopped taking an optional question: {road}",
);
}
}
#[test]
fn each_published_tier_hands_the_fold_its_own_name() {
let emitted = tight(&bound(SharedBridge::Total));
assert!(
emitted.contains("__ingraph_travel_search_ask(__async_graphql_ctx,\"travelSearchKeys\","),
"{emitted}"
);
assert!(
emitted.contains("__ingraph_travel_search_ask(__async_graphql_ctx,\"travelSearch\","),
"{emitted}"
);
}