use core::num::NonZeroU32;
use super::{
ElectionBackend, FRAMEWORK_LIMIT, FieldNumbers, IndexBackend, IndexGraphql, IndexModel, Numbers,
tests::{document, two_elections},
};
use crate::model::GraphqlKind;
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn n(count: u32) -> Option<NonZeroU32> {
Some(NonZeroU32::new(count).expect("the fixture numbers are not zero"))
}
fn shots() -> IndexModel {
two_elections()
}
fn clip() -> syn::Ident {
ident("caption_embedding")
}
fn tight(model: &IndexModel) -> String {
model.emit().to_string().split_whitespace().collect()
}
fn wrote(field: &str, numbers: Numbers, first: Option<NonZeroU32>) -> FieldNumbers {
FieldNumbers {
field: ident(field),
numbers,
skins: match first {
Some(_) => vec![ElectionBackend {
first,
..ElectionBackend::of(GraphqlKind::AsyncGraphql)
}],
None => Vec::new(),
},
}
}
fn dressed(first: Option<NonZeroU32>) -> IndexGraphql {
IndexGraphql {
backends: vec![IndexBackend {
first,
..IndexBackend::of(GraphqlKind::AsyncGraphql)
}],
}
}
#[test]
fn an_unwritten_declaration_publishes_the_framework_number() {
let model = shots();
assert_eq!(
model.wire_first(GraphqlKind::AsyncGraphql, Some(&clip())),
FRAMEWORK_LIMIT,
);
assert_eq!(FRAMEWORK_LIMIT.get(), 20);
}
#[test]
fn each_tier_beats_the_one_below_it() {
let kind = GraphqlKind::AsyncGraphql;
let mut model = shots();
model.numbers.limit = n(7);
assert_eq!(model.wire_first(kind, Some(&clip())), n(7).unwrap());
model.graphql = Some(dressed(n(9)));
assert_eq!(model.wire_first(kind, Some(&clip())), n(9).unwrap());
model.field_numbers = vec![wrote(
"caption_embedding",
Numbers {
limit: n(11),
..Numbers::default()
},
None,
)];
assert_eq!(model.wire_first(kind, Some(&clip())), n(11).unwrap());
model.field_numbers = vec![wrote(
"caption_embedding",
Numbers {
limit: n(11),
..Numbers::default()
},
n(13),
)];
assert_eq!(model.wire_first(kind, Some(&clip())), n(13).unwrap());
}
#[test]
fn a_fields_number_does_not_reach_the_field_beside_it() {
let kind = GraphqlKind::AsyncGraphql;
let mut model = shots();
model.numbers.limit = n(7);
model.field_numbers = vec![wrote(
"caption_embedding",
Numbers {
limit: n(11),
..Numbers::default()
},
None,
)];
assert_eq!(model.wire_first(kind, Some(&clip())), n(11).unwrap());
assert_eq!(
model.wire_first(kind, Some(&ident("image_embedding"))),
n(7).unwrap(),
"a sibling election took the number the other one wrote",
);
}
#[test]
fn a_skins_number_belongs_to_the_backend_that_wrote_it() {
let mut model = shots();
model.numbers.limit = n(7);
model.graphql = Some(dressed(n(9)));
assert_eq!(
model.wire_first(GraphqlKind::AsyncGraphql, Some(&clip())),
n(9).unwrap(),
);
assert!(
model
.graphql
.as_ref()
.and_then(|block| block.skin(GraphqlKind::AsyncGraphql))
.is_some_and(|skin| skin.first == n(9)),
);
}
#[test]
fn an_unwritten_depth_is_the_whole_limit_chain() {
let kind = GraphqlKind::AsyncGraphql;
let mut model = shots();
model.numbers.limit = n(7);
assert_eq!(model.pool_depth(kind, Some(&clip())), n(7).unwrap());
model.graphql = Some(dressed(n(9)));
assert_eq!(
model.pool_depth(kind, Some(&clip())),
n(9).unwrap(),
"the fall-through reads the WHOLE chain rather than the bones alone",
);
}
#[test]
fn a_written_depth_wins_at_the_tier_that_wrote_it() {
let kind = GraphqlKind::AsyncGraphql;
let mut model = shots();
model.numbers.limit = n(7);
model.numbers.depth = n(200);
assert_eq!(model.pool_depth(kind, Some(&clip())), n(200).unwrap());
model.field_numbers = vec![wrote(
"caption_embedding",
Numbers {
depth: n(50),
..Numbers::default()
},
None,
)];
assert_eq!(model.pool_depth(kind, Some(&clip())), n(50).unwrap());
assert_eq!(
model.pool_depth(kind, Some(&ident("image_embedding"))),
n(200).unwrap(),
);
}
#[test]
fn the_wall_caps_the_published_default_and_does_not_replace_it() {
let kind = GraphqlKind::AsyncGraphql;
let mut model = shots();
model.numbers.limit = n(500);
model.numbers.ceiling = n(100);
assert_eq!(model.wire_first(kind, Some(&clip())), n(100).unwrap());
model.numbers.limit = n(10);
assert_eq!(
model.wire_first(kind, Some(&clip())),
n(10).unwrap(),
"the wall replaced the chain instead of capping it",
);
}
#[test]
fn the_wall_caps_a_written_depth() {
let kind = GraphqlKind::AsyncGraphql;
let mut model = shots();
model.numbers.depth = n(500_000);
model.numbers.ceiling = n(100);
assert_eq!(model.pool_depth(kind, Some(&clip())), n(100).unwrap());
}
#[test]
fn the_wall_has_two_tiers_and_no_framework_floor() {
let mut model = shots();
assert_eq!(model.declared_ceiling(Some(&clip())), None);
model.numbers.ceiling = n(1_000);
assert_eq!(model.declared_ceiling(Some(&clip())), n(1_000));
model.field_numbers = vec![wrote(
"caption_embedding",
Numbers {
ceiling: n(100),
..Numbers::default()
},
None,
)];
assert_eq!(model.declared_ceiling(Some(&clip())), n(100));
assert_eq!(
model.declared_ceiling(Some(&ident("image_embedding"))),
n(1_000),
"a sibling election took the wall the other one wrote",
);
}
#[test]
fn a_declaration_that_writes_no_wall_still_passes_none() {
let emitted = tight(&shots());
assert!(
emitted.contains("route_depth("),
"the fixture resolves its fetches through the law: {emitted}",
);
assert!(
!emitted.contains("NonZeroU32::new("),
"a wall nobody declared reached an emitted call: {emitted}",
);
}
#[test]
fn a_declared_wall_reaches_the_law_and_no_other_surface() {
let mut model = shots();
model.numbers.ceiling = n(100);
let emitted = tight(&model);
assert!(
emitted.contains("route_depth(::core::option::Option::and_then(__ingraph_route,"),
"{emitted}",
);
assert!(
emitted
.contains("::core::option::Option::as_ref(&::core::num::NonZeroU32::new(100u32)),limit,"),
"the declared wall did not arrive at the law's second argument: {emitted}",
);
assert!(
!emitted.contains("\"ceiling\""),
"a protective number reached a wire surface: {emitted}",
);
}
#[test]
fn each_route_is_capped_by_its_own_elections_wall() {
let mut model = shots();
model.numbers.ceiling = n(1_000);
model.field_numbers = vec![wrote(
"caption_embedding",
Numbers {
ceiling: n(100),
..Numbers::default()
},
None,
)];
let emitted = tight(&model);
assert!(emitted.contains("NonZeroU32::new(100u32)"), "{emitted}");
assert!(
emitted.contains("NonZeroU32::new(1000u32)"),
"the second election kept the container's wall: {emitted}",
);
}
#[test]
fn the_unelected_subject_is_capped_by_the_container() {
let mut model = document();
model.numbers.ceiling = n(64);
let emitted = tight(&model);
assert!(emitted.contains("NonZeroU32::new(64u32)"), "{emitted}");
}