use super::{DeclaredError, ExchangeModel, HandlerModel, MutationModel};
pub(super) fn watch_ops() -> MutationModel {
MutationModel {
krate: syn::parse_quote!(::ingraph),
name: syn::parse_quote!(WatchOps),
vis: syn::parse_quote!(pub),
graphql: vec![crate::model::GraphqlKind::AsyncGraphql],
exchanges: vec![
ExchangeModel {
field: syn::parse_quote!(add),
ty: syn::parse_quote!((AddWatch, AddResponse)),
rename: Some(syn::parse_quote!("addWatch")),
handler: HandlerModel {
path: syn::parse_quote!(crate::watch::add),
awaited: true,
context: vec![syn::parse_quote!(crate::Catalog)],
error: Some(DeclaredError::Rich {
ty: syn::parse_quote!(crate::AddWatchError),
code: Some(syn::parse_quote!("ADD_FAILED")),
}),
},
},
ExchangeModel {
field: syn::parse_quote!(remove),
ty: syn::parse_quote!((RemoveWatch, RemoveResponse)),
rename: None,
handler: HandlerModel {
path: syn::parse_quote!(crate::watch::remove),
awaited: false,
context: Vec::new(),
error: Some(DeclaredError::Canned {
message: syn::parse_quote!("that watch is not ours"),
code: Some(syn::parse_quote!("NOT_OURS")),
}),
},
},
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,
},
},
],
}
}
fn compact(model: &MutationModel) -> String {
rendered(model).split_whitespace().collect()
}
fn rendered(model: &MutationModel) -> String {
prettyplease::unparse(&syn::parse2(model.emit()).expect("the expansion parses as a file"))
}
fn tokens(model: &MutationModel) -> String {
model.emit().to_string().split_whitespace().collect()
}
#[test]
fn the_command_enum_has_one_arm_per_exchange() {
let model = watch_ops();
let emitted = tokens(&model);
assert!(
emitted.contains("pubenumWatchOpsCommand"),
"{}",
rendered(&model)
);
for (variant, ty) in [
("Add", "(AddWatch,AddResponse)"),
("Remove", "(RemoveWatch,RemoveResponse)"),
("Reindex", "(Reindex,())"),
] {
let arm = format!("{variant}(<{ty}as::ingraph::__private::source::Mutation>::Request)");
assert!(
emitted.contains(&arm),
"the `{variant}` arm: {}",
rendered(&model)
);
}
}
#[test]
fn the_arms_payload_is_the_projection_and_never_a_written_type() {
let model = watch_ops();
let file: syn::File = syn::parse2(model.emit()).expect("the expansion parses as a file");
let command = file
.items
.iter()
.find_map(|item| match item {
syn::Item::Enum(command) => Some(command),
_ => None,
})
.expect("the command vocabulary");
let arms: String = quote::quote!(#command)
.to_string()
.split_whitespace()
.collect();
assert!(
!arms.contains("Add(AddWatch)"),
"the request type is never written directly: {}",
rendered(&model)
);
assert_eq!(
arms
.matches("as::ingraph::__private::source::Mutation>::Request")
.count(),
3,
"one projection per field: {}",
rendered(&model)
);
}
#[test]
fn the_node_row_hands_the_vocabulary_to_a_source() {
let model = watch_ops();
assert!(
compact(&model).contains(
"impl::ingraph::__private::source::MutationNodeforWatchOps{typeCommand=WatchOpsCommand;}"
),
"{}",
rendered(&model)
);
}
#[test]
fn the_record_says_what_the_declaration_said() {
let model = watch_ops();
let emitted = compact(&model);
assert!(
emitted.contains("impl::ingraph::__private::source::DeclaredMutationforWatchOps"),
"{}",
rendered(&model)
);
assert!(
emitted.contains("constALL_SYNC:bool=false;"),
"{}",
rendered(&model)
);
assert!(
emitted.contains(
"::ingraph::__private::source::Exchange::of(\"add\",\"crate::watch::add\")\
.renamed(\"addWatch\")\
.awaited()\
.granting(&[\"crate::Catalog\"])\
.failing(\
::ingraph::__private::source::Fallibility::Rich{\
named:\"crate::AddWatchError\",\
code:::core::option::Option::Some(\"ADD_FAILED\"),\
})"
),
"{}",
rendered(&model)
);
assert!(
emitted.contains(
"::ingraph::__private::source::Exchange::of(\"remove\",\"crate::watch::remove\")\
.failing(\
::ingraph::__private::source::Fallibility::Canned{\
message:\"thatwatchisnotours\",\
code:::core::option::Option::Some(\"NOT_OURS\"),\
})"
),
"{}",
rendered(&model)
);
}
#[test]
fn an_unwritten_word_leaves_no_call_behind() {
let model = watch_ops();
let emitted = compact(&model);
assert!(
emitted.contains(
"::ingraph::__private::source::Exchange::of(\"reindex\",\"crate::watch::reindex\"),"
),
"the plain exchange is the bare constructor and nothing else: {}",
rendered(&model)
);
assert_eq!(
emitted.matches(".renamed(").count(),
1,
"one field wrote a rename: {}",
rendered(&model)
);
assert_eq!(
emitted.matches(".awaited()").count(),
1,
"one field wrote the face word: {}",
rendered(&model)
);
assert_eq!(
emitted.matches(".granting(").count(),
1,
"one field wrote a context: {}",
rendered(&model)
);
assert_eq!(
emitted.matches(".failing(").count(),
2,
"two fields can fail: {}",
rendered(&model)
);
}
#[test]
fn a_node_of_blocking_handlers_is_all_sync() {
let mut model = watch_ops();
model.exchanges[0].handler.awaited = false;
assert!(model.all_sync());
assert!(compact(&model).contains("constALL_SYNC:bool=true;"));
}
#[test]
fn a_failure_with_no_code_says_none_rather_than_nothing() {
let mut model = watch_ops();
model.exchanges[1].handler.error = Some(DeclaredError::Canned {
message: syn::parse_quote!("that watch is not ours"),
code: None,
});
assert!(
compact(&model).contains("code:::core::option::Option::None,"),
"{}",
rendered(&model)
);
}
#[test]
fn every_framework_path_goes_through_the_shelf() {
let model = watch_ops();
let emitted = compact(&model);
for named in [
"Mutation",
"MutationNode",
"DeclaredMutation",
"Exchange",
"Fallibility",
] {
assert!(
!emitted.contains(&format!("::ingraph::{named}")),
"`{named}` is reached through `__private`: {}",
rendered(&model)
);
}
}
#[test]
fn a_renamed_crate_moves_every_path() {
let mut model = watch_ops();
model.krate = syn::parse_quote!(crate::shim);
let emitted = compact(&model);
assert!(!emitted.contains("::ingraph::"), "{}", rendered(&model));
assert!(
emitted.contains("crate::shim::__private::source::MutationNode"),
"{}",
rendered(&model)
);
}
#[test]
fn the_vocabulary_is_as_visible_as_the_node() {
let mut model = watch_ops();
model.vis = syn::parse_quote!(pub(crate));
assert!(
compact(&model).contains("pub(crate)enumWatchOpsCommand"),
"{}",
rendered(&model)
);
}
#[test]
fn a_raw_field_name_is_unrawed_in_both_places() {
let mut model = watch_ops();
model.exchanges[2].field = syn::parse_quote!(r#type);
let emitted = compact(&model);
assert!(emitted.contains("Type(<"), "{}", rendered(&model));
assert!(
emitted.contains("Exchange::of(\"type\","),
"{}",
rendered(&model)
);
}
#[test]
fn a_multiword_field_names_one_arm() {
let mut model = watch_ops();
model.exchanges[2].field = syn::parse_quote!(rebuild_index);
assert!(
compact(&model).contains("RebuildIndex(<"),
"{}",
rendered(&model)
);
}
#[test]
fn the_declaration_side_is_the_nodes_rust_surface_and_the_wire_stays_in_the_gate() {
let model = watch_ops();
let rendered = rendered(&model);
let tops: Vec<&str> = rendered
.lines()
.filter(|line| line.starts_with("impl "))
.collect();
assert_eq!(
tops,
vec![
"impl ::ingraph::__private::source::MutationNode for WatchOps {",
"impl ::ingraph::__private::source::DeclaredMutation for WatchOps {",
"impl ::core::fmt::Debug for WatchOpsResponse {",
"impl ::core::fmt::Display for WatchOpsError {",
"impl ::core::fmt::Debug for WatchOpsError {",
"impl ::core::error::Error for WatchOpsError {}",
"impl WatchOpsError {",
"impl WatchOps {",
"impl WatchOps {",
],
"{rendered}"
);
assert!(!rendered.contains("Resolver"), "{rendered}");
let gate = rendered
.find("if_async_graphql!")
.expect("the binding gate");
let gated: String = rendered[gate..].split_whitespace().collect();
assert!(gated.contains("implWatchOpsMutations{"), "{rendered}");
}