use super::super::tests::watch_ops;
use crate::mutation::{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 wholly_blocking() -> MutationModel {
let mut model = watch_ops();
model.exchanges[0].handler.awaited = false;
model
}
fn cannot_fail() -> 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()
}
}
fn twice_granted() -> MutationModel {
let mut model = watch_ops();
model.exchanges[1].handler.context = vec![syn::parse_quote!(crate::Catalog)];
model.exchanges[2].handler.context = vec![syn::parse_quote!(crate::watch::Ledger)];
model
}
#[test]
fn every_assembled_name_is_derived_from_the_nodes() {
let model = watch_ops();
assert_eq!(model.response_name(), "WatchOpsResponse");
assert_eq!(model.error_name(), "WatchOpsError");
assert_eq!(model.face_name(), "handle_watch_ops");
}
#[test]
fn the_face_is_the_or_of_its_handlers() {
let awaited = watch_ops();
assert!(awaited.awaited(), "one awaited handler");
assert!(
tokens(&awaited).contains("pubasyncfnhandle_watch_ops("),
"{}",
rendered(&awaited)
);
let blocking = wholly_blocking();
assert!(blocking.all_sync(), "every handler blocking");
assert!(
tokens(&blocking).contains("pubfnhandle_watch_ops("),
"{}",
rendered(&blocking)
);
assert!(
!tokens(&blocking).contains("asyncfnhandle_watch_ops"),
"{}",
rendered(&blocking)
);
}
#[test]
fn the_records_all_sync_and_the_faces_mode_are_one_fact() {
for model in [watch_ops(), wholly_blocking(), cannot_fail()] {
assert_eq!(
model.all_sync(),
!model.awaited(),
"one reading, two spellings"
);
let emitted = tokens(&model);
let stated = format!("constALL_SYNC:bool={};", model.all_sync());
assert!(emitted.contains(&stated), "{}", rendered(&model));
}
}
#[test]
fn the_response_sum_has_one_arm_per_exchange() {
let model = watch_ops();
let emitted = tokens(&model);
assert!(emitted.contains("pubenumWatchOpsResponse{"), "{emitted}");
for (variant, ty) in [
("Add", "(AddWatch,AddResponse)"),
("Remove", "(RemoveWatch,RemoveResponse)"),
("Reindex", "(Reindex,())"),
] {
assert!(
emitted.contains(&format!(
"{variant}(<{ty}as::ingraph::__private::source::Mutation>::Response)"
)),
"{variant}: {}",
rendered(&model)
);
}
}
#[test]
fn both_sums_debug_their_arm_and_never_its_payload() {
let model = watch_ops();
let emitted = tokens(&model);
for sum in ["WatchOpsResponse", "WatchOpsError"] {
assert!(
emitted.contains(&format!("impl::core::fmt::Debugfor{sum}{{")),
"{sum}: {}",
rendered(&model)
);
}
assert!(
emitted.contains("Self::Add(_)=>::core::fmt::Formatter::write_str(__ingraph_f,\"Add\")"),
"{}",
rendered(&model)
);
}
#[test]
fn the_error_sum_carries_the_rich_type_and_nothing_for_a_canned_failure() {
let model = watch_ops();
let emitted = tokens(&model);
let sum = emitted
.split_once("pubenumWatchOpsError{")
.expect("the error sum")
.1
.split_once('}')
.expect("its arms")
.0;
assert!(sum.contains("Add(crate::AddWatchError)"), "{sum}");
assert!(sum.contains("Remove"), "{sum}");
assert!(
!sum.contains("Reindex"),
"an exchange that cannot fail contributes no way to fail: {sum}"
);
}
#[test]
fn a_node_that_cannot_fail_gets_an_uninhabited_error() {
let model = cannot_fail();
let emitted = tokens(&model);
assert!(emitted.contains("pubenumWatchOpsError{}"), "{emitted}");
assert_eq!(
emitted.matches("match*self{}").count(),
3,
"{}",
rendered(&model)
);
}
#[test]
fn the_error_sum_renders_under_the_heading_its_declaration_wrote() {
let model = watch_ops();
let emitted = tokens(&model);
assert!(
emitted.contains(
"Self::Add(__ingraph_refusal)=>{::core::fmt::Formatter::write_str(__ingraph_f,\"ADD_FAILED:\
\")?;::core::fmt::Display::fmt(__ingraph_refusal,__ingraph_f)}"
),
"{}",
rendered(&model)
);
assert!(
emitted.contains(
"Self::Remove=>::core::fmt::Formatter::write_str(__ingraph_f,\"NOT_OURS:thatwatchisnotours\")"
),
"{}",
rendered(&model)
);
}
#[test]
fn the_error_sum_answers_the_code_its_declaration_wrote() {
let model = watch_ops();
let emitted = tokens(&model);
assert!(
emitted.contains("pubconstfncode(&self)->::core::option::Option<&'staticstr>"),
"{}",
rendered(&model)
);
assert!(
emitted.contains("Self::Add(_)=>::core::option::Option::Some(\"ADD_FAILED\")"),
"{}",
rendered(&model)
);
}
#[test]
fn one_call_is_emitted_per_exchange_and_both_faces_spend_it() {
let model = watch_ops();
let emitted = tokens(&model);
for (call, tier) in [
(
"__ingraph_exchange_add",
"->::core::result::Result<<(AddWatch,AddResponse)as::ingraph::__private::source::Mutation>::\
Response,crate::AddWatchError>",
),
(
"__ingraph_exchange_remove",
"->::core::result::Result<<(RemoveWatch,RemoveResponse)as::ingraph::__private::source::\
Mutation>::Response,()>",
),
(
"__ingraph_exchange_reindex",
"-><(Reindex,())as::ingraph::__private::source::Mutation>::Response",
),
] {
assert_eq!(
emitted.matches(&format!("fn{call}(")).count(),
1,
"{call}: {}",
rendered(&model)
);
assert!(emitted.contains(tier), "{call}: {}", rendered(&model));
assert_eq!(
emitted.matches(&format!("Self::{call}(")).count(),
1,
"{call}: {}",
rendered(&model)
);
assert_eq!(
emitted.matches(&format!("WatchOps::{call}(")).count(),
1,
"{call}: {}",
rendered(&model)
);
}
for handler in [
"crate::watch::add(",
"crate::watch::remove(",
"crate::watch::reindex(",
] {
assert_eq!(
emitted.matches(handler).count(),
1,
"{handler}: {}",
rendered(&model)
);
}
}
#[test]
fn the_face_is_emitted_without_any_wire_word() {
let model = MutationModel {
graphql: Vec::new(),
..watch_ops()
};
let emitted = tokens(&model);
assert!(emitted.contains("pubasyncfnhandle_watch_ops("), "{emitted}");
assert!(!emitted.contains("async_graphql"), "{emitted}");
assert_eq!(emitted.matches("Self::__ingraph_exchange_add(").count(), 1);
assert_eq!(
emitted.matches("WatchOps::__ingraph_exchange_add(").count(),
0
);
}
#[test]
fn the_face_takes_the_union_of_what_its_exchanges_were_granted() {
let model = twice_granted();
let emitted = tokens(&model);
assert!(
emitted.contains(
"pubasyncfnhandle_watch_ops(catalog:&crate::Catalog,ledger:&crate::watch::Ledger,\
__ingraph_cmd:WatchOpsCommand,)"
),
"{}",
rendered(&model)
);
}
#[test]
fn an_ungranted_node_takes_the_command_alone() {
let model = cannot_fail();
let emitted = tokens(&model);
assert!(
emitted.contains("pubfnhandle_watch_ops(__ingraph_cmd:WatchOpsCommand,)"),
"{}",
rendered(&model)
);
}
#[test]
fn each_command_arm_answers_and_refuses_under_its_own_name() {
let model = watch_ops();
let emitted = tokens(&model);
assert!(
emitted.contains(
"WatchOpsCommand::Add(__ingraph_request)=>matchSelf::__ingraph_exchange_add(catalog,\
__ingraph_request).await{"
),
"{}",
rendered(&model)
);
assert!(
emitted.contains("::core::result::Result::Ok(WatchOpsResponse::Add(__ingraph_answer))"),
"{}",
rendered(&model)
);
assert!(
emitted.contains("::core::result::Result::Err(WatchOpsError::Add(__ingraph_refusal))"),
"{}",
rendered(&model)
);
assert!(
emitted.contains(
"::core::result::Result::Err(())=>::core::result::Result::Err(WatchOpsError::Remove)"
),
"{}",
rendered(&model)
);
assert!(
emitted.contains(
"WatchOpsCommand::Reindex(__ingraph_request)=>::core::result::Result::Ok(WatchOpsResponse::\
Reindex(Self::__ingraph_exchange_reindex(__ingraph_request)))"
),
"{}",
rendered(&model)
);
}
#[test]
fn a_renamed_crate_moves_the_faces_paths() {
let model = MutationModel {
krate: syn::parse_quote!(other::ingraph),
graphql: Vec::new(),
..watch_ops()
};
let emitted = tokens(&model);
assert_eq!(
emitted.matches("::ingraph::__private").count(),
emitted.matches("other::ingraph::__private").count(),
"{emitted}"
);
}