use super::super::tests::watch_ops;
use crate::mutation::{DeclaredError, ExchangeModel, HandlerModel, MutationModel};
fn tokens(model: &MutationModel) -> String {
model.emit().to_string().split_whitespace().collect()
}
fn rendered(model: &MutationModel) -> String {
prettyplease::unparse(&syn::parse2(model.emit()).expect("the expansion parses as a file"))
}
fn bare() -> MutationModel {
MutationModel {
graphql: Vec::new(),
..watch_ops()
}
}
fn answering_nothing() -> MutationModel {
MutationModel {
exchanges: vec![ExchangeModel {
field: syn::parse_quote!(reindex),
ty: syn::parse_quote!((Reindex, ())),
rename: None,
handler: HandlerModel {
path: syn::parse_quote!(crate::watch::reindex),
awaited: false,
context: Vec::new(),
error: None,
},
}],
..watch_ops()
}
}
#[test]
fn a_node_dressed_for_no_wire_gets_no_face() {
let model = bare();
let emitted = tokens(&model);
for absent in [
"async_graphql",
"if_async_graphql",
"Object",
"WatchOpsMutations",
] {
assert!(!emitted.contains(absent), "{absent}: {}", rendered(&model));
}
assert!(emitted.contains("pubenumWatchOpsCommand"), "{emitted}");
}
#[test]
fn the_unit_is_unconditional_and_the_face_is_gated() {
let model = watch_ops();
let emitted = tokens(&model);
let gate = emitted
.find("::ingraph::__private::gate::if_async_graphql!")
.expect("the face rides the binding gate");
let unit = emitted
.find("pubstructWatchOpsMutations;")
.expect("the unit is emitted");
assert!(unit < gate, "the unit is outside the gate: {emitted}");
assert!(
emitted[gate..].contains("::ingraph::__private::async_graphql::Object"),
"{}",
rendered(&model)
);
}
#[test]
fn every_exchange_is_its_own_root_field() {
let model = watch_ops();
let emitted = tokens(&model);
for (method, published) in [
("__async_graphql_add", "addWatch"),
("__async_graphql_remove", "remove"),
("__async_graphql_reindex", "reindex"),
] {
assert!(
emitted.contains(&format!("#[graphql(name=\"{published}\")]asyncfn{method}")),
"{published}: {}",
rendered(&model)
);
}
}
#[test]
fn the_face_is_async_and_the_handler_decides_the_await() {
let model = watch_ops();
let emitted = tokens(&model);
assert_eq!(emitted.matches("asyncfn__async_graphql_").count(), 3);
assert!(
emitted.contains("WatchOps::__ingraph_exchange_add(::ingraph::__private::resource::Shared::<crate::Catalog>::get(__ingraph_granted_0),__async_graphql_input).await"),
"{}",
rendered(&model)
);
assert!(
emitted.contains("WatchOps::__ingraph_exchange_remove(__async_graphql_input);"),
"{}",
rendered(&model)
);
}
#[test]
fn the_resolver_spends_the_one_cast_rather_than_making_a_second() {
let model = watch_ops();
let emitted = tokens(&model);
for (handler, call) in [
("crate::watch::add(", "__ingraph_exchange_add"),
("crate::watch::remove(", "__ingraph_exchange_remove"),
("crate::watch::reindex(", "__ingraph_exchange_reindex"),
] {
assert_eq!(
emitted.matches(handler).count(),
1,
"{handler}: {}",
rendered(&model)
);
let resolver = emitted
.rsplit_once("if_async_graphql!")
.expect("the gate")
.1;
assert!(
!resolver.contains(handler),
"the handler is named in the CALL and not in the resolver: {}",
rendered(&model)
);
assert!(
resolver.contains(&format!("WatchOps::{call}(")),
"{call}: {}",
rendered(&model)
);
}
}
#[test]
fn each_failure_renders_under_the_heading_its_declaration_wrote() {
let model = watch_ops();
let emitted = tokens(&model);
assert!(
emitted.contains(
"::ingraph::__private::graphql::refused_exchange(::core::option::Option::Some(\"ADD_FAILED\"),&__ingraph_refusal)"
),
"{}",
rendered(&model)
);
assert!(
emitted.contains(
"::ingraph::__private::graphql::refused_exchange(::core::option::Option::Some(\"NOT_OURS\"),\"thatwatchisnotours\")"
),
"{}",
rendered(&model)
);
}
#[test]
fn the_line_is_found_before_anything_has_an_effect() {
let model = watch_ops();
let emitted = tokens(&model);
let resolver = emitted
.split_once("asyncfn__async_graphql_add")
.expect("the first resolver")
.1;
let line = resolver.find("let__ingraph_line=").expect("the line");
let grant = resolver.find("let__ingraph_granted_0=").expect("the grant");
let mint = resolver.find("let__ingraph_command=").expect("the command");
let call = resolver
.find("WatchOps::__ingraph_exchange_add(")
.expect("the handler's own call");
let forward = resolver.find("say_async").expect("the forward");
assert!(
line < grant && grant < mint && mint < call && call < forward,
"{}",
rendered(&model)
);
}
#[test]
fn a_failure_returns_before_the_forward() {
let model = watch_ops();
let emitted = tokens(&model);
let resolver = emitted
.split_once("asyncfn__async_graphql_add")
.expect("the first resolver")
.1;
let refusal = resolver
.find("returnstatic::core::result::Result::Err")
.or_else(|| resolver.find("::core::result::Result::Err(__ingraph_refusal)"))
.expect("the failing arm");
assert!(
refusal < resolver.find("say_async").expect("the forward"),
"{}",
rendered(&model)
);
}
#[test]
fn the_command_is_minted_from_a_clone_of_the_request() {
let model = watch_ops();
let emitted = tokens(&model);
assert!(
emitted.contains(
"let__ingraph_command=WatchOpsCommand::Add(::core::clone::Clone::clone(&__async_graphql_input));"
),
"{}",
rendered(&model)
);
}
#[test]
fn an_absent_line_and_a_dead_one_are_one_refusal() {
let model = watch_ops();
let emitted = tokens(&model);
assert_eq!(
emitted
.matches(
"::ingraph::__private::graphql::refused(&::ingraph::__private::drive::QueryError::UnreachableSource)"
)
.count(),
6,
"{}",
rendered(&model)
);
}
#[test]
fn an_exchange_that_answers_nothing_publishes_a_boolean() {
let model = answering_nothing();
let emitted = tokens(&model);
assert!(
emitted.contains("->::ingraph::__private::async_graphql::Result<::core::primitive::bool>"),
"{}",
rendered(&model)
);
assert!(
emitted.contains("let()=__ingraph_answer;::core::result::Result::Ok(true)"),
"{}",
rendered(&model)
);
}
#[test]
fn an_exchange_that_answers_something_answers_its_own_response() {
let model = watch_ops();
let emitted = tokens(&model);
assert!(
emitted.contains(
"->::ingraph::__private::async_graphql::Result<<(AddWatch,AddResponse)as::ingraph::__private::source::Mutation>::Response>"
),
"{}",
rendered(&model)
);
assert_eq!(
emitted.matches("Result<::core::primitive::bool>").count(),
1,
"{}",
rendered(&model)
);
}
#[test]
fn an_ungranted_handler_takes_the_request_alone() {
let model = answering_nothing();
let emitted = tokens(&model);
assert!(
emitted.contains("WatchOps::__ingraph_exchange_reindex(__async_graphql_input)"),
"{}",
rendered(&model)
);
assert!(
emitted.contains(
"fn__ingraph_exchange_reindex(__ingraph_request:<(Reindex,())as::ingraph::__private::source::Mutation>::Request,)"
),
"{}",
rendered(&model)
);
assert!(!emitted.contains("__ingraph_granted_"), "{emitted}");
}
#[test]
fn the_face_names_the_shelf_at_the_attribute_too() {
let model = watch_ops();
let emitted = tokens(&model);
assert!(
emitted.contains(
"#[::ingraph::__private::async_graphql::Object(crate=\"::ingraph::__private::async_graphql\",name=\"WatchOpsMutations\")]"
),
"{}",
rendered(&model)
);
}
#[test]
fn a_renamed_crate_moves_the_faces_paths() {
let model = MutationModel {
krate: syn::parse_quote!(other::ingraph),
..watch_ops()
};
let emitted = tokens(&model);
assert!(
emitted.contains("other::ingraph::__private::gate::if_async_graphql!"),
"{emitted}"
);
assert!(
emitted.contains("crate=\"other::ingraph::__private::async_graphql\""),
"{emitted}"
);
assert_eq!(
emitted.matches("::ingraph::__private").count(),
emitted.matches("other::ingraph::__private").count(),
"{emitted}"
);
}
#[test]
fn the_published_word_is_the_bindings_decision() {
let model = watch_ops();
assert_eq!(model.exchanges[0].published(), "addWatch");
assert_eq!(model.exchanges[1].published(), "remove");
assert!(model.exchanges[0].rename.is_some());
assert!(model.exchanges[1].rename.is_none());
let cased = ExchangeModel {
field: syn::parse_quote!(add_watch_root),
ty: syn::parse_quote!((AddWatch, ())),
rename: None,
handler: HandlerModel {
path: syn::parse_quote!(crate::add),
awaited: false,
context: Vec::new(),
error: Some(DeclaredError::Canned {
message: syn::parse_quote!("no"),
code: None,
}),
},
};
assert_eq!(cased.published(), "addWatchRoot");
}