use core::num::{NonZeroU32, NonZeroUsize};
use super::{Route, route_depth};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
struct Knobs {
fold: Option<u8>,
}
fn depth(n: usize) -> NonZeroUsize {
NonZeroUsize::new(n).expect("the fixture numbers are not zero")
}
fn ceiling(n: u32) -> NonZeroU32 {
NonZeroU32::new(n).expect("the fixture numbers are not zero")
}
#[test]
fn the_unconfigured_edge_names_no_depth() {
assert_eq!(Route::<Knobs>::new().depth(), None);
assert_eq!(Route::<Knobs>::default().depth(), None);
}
#[test]
fn the_depth_builder_leaves_the_other_edge_facts_alone() {
let route = Route::new()
.with_weight(2.5)
.with_enabled(false)
.with_depth(depth(50))
.with_options(Knobs { fold: Some(3) });
assert_eq!(route.depth(), Some(depth(50)));
assert!((route.weight() - 2.5).abs() < f64::EPSILON);
assert!(!route.enabled());
assert_eq!(route.options(), Some(&Knobs { fold: Some(3) }));
}
#[test]
fn the_in_place_form_both_names_a_depth_and_takes_one_away() {
let mut route = Route::<Knobs>::new();
route.set_depth(Some(depth(7)));
assert_eq!(route.depth(), Some(depth(7)));
route.set_depth(None);
assert_eq!(
route.depth(),
None,
"a depth that cannot be withdrawn would make the deferral one-way",
);
}
#[test]
fn the_declared_ceiling_caps_the_edge_it_used_to_lose_to() {
assert_eq!(
route_depth(Some(depth(50)), Some(&ceiling(20)), depth(10)),
depth(20),
);
assert_eq!(
route_depth(Some(depth(500_000)), Some(&ceiling(100)), depth(10)),
depth(100),
);
}
#[test]
fn an_undepthed_ask_is_the_budget_capped_by_the_wall() {
assert_eq!(route_depth(None, Some(&ceiling(20)), depth(100)), depth(20));
assert_eq!(route_depth(None, Some(&ceiling(200)), depth(10)), depth(10));
}
#[test]
fn no_depth_and_no_ceiling_falls_to_the_callers_budget() {
assert_eq!(route_depth(None, None, depth(10)), depth(10));
}
#[test]
fn a_depth_below_the_budget_is_handed_back_untouched() {
assert_eq!(route_depth(Some(depth(5)), None, depth(10)), depth(5));
assert_eq!(
route_depth(Some(depth(5)), Some(&ceiling(20)), depth(10)),
depth(5),
);
}
#[test]
fn the_declared_ceiling_crosses_into_the_doors_currency_whole() {
assert_eq!(
route_depth(None, Some(&ceiling(u32::MAX)), depth(10)),
depth(10),
);
assert_eq!(
route_depth(
Some(depth(u32::MAX as usize)),
Some(&ceiling(u32::MAX)),
depth(10)
),
depth(u32::MAX as usize),
);
}
#[cfg(feature = "async-graphql")]
mod wire {
use core::num::NonZeroUsize;
use async_graphql::{EmptyMutation, EmptySubscription, InputObject, InputType, Object, Schema};
use super::super::Route;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, InputObject)]
struct TranscriptFulltextOptions {
fold: Option<u32>,
}
struct Query;
#[Object]
impl Query {
async fn fused(&self, route: Option<Route<TranscriptFulltextOptions>>) -> bool {
route.is_some_and(|route| route.depth().is_some())
}
}
fn sdl() -> String {
Schema::build(Query, EmptyMutation, EmptySubscription)
.finish()
.sdl()
}
#[test]
fn the_edge_publishes_a_defaultless_depth_beside_its_two_constants() {
let sdl = sdl();
assert!(sdl.contains("depth: Int\n"), "{sdl}");
assert!(
!sdl.contains("depth: Int ="),
"a deferral must not publish a literal: {sdl}",
);
assert!(sdl.contains("weight: Float = 1"), "{sdl}");
assert!(sdl.contains("enabled: Boolean = true"), "{sdl}");
}
#[test]
fn a_written_depth_parses_and_an_absent_one_defers() {
let mut object = async_graphql::indexmap::IndexMap::new();
object.insert(
async_graphql::Name::new("depth"),
async_graphql::Value::from(50_u64),
);
let named = <Route<TranscriptFulltextOptions> as InputType>::parse(Some(
async_graphql::Value::Object(object),
))
.expect("one field is a route");
assert_eq!(named.depth(), NonZeroUsize::new(50));
let bare = <Route<TranscriptFulltextOptions> as InputType>::parse(Some(
async_graphql::Value::Object(async_graphql::indexmap::IndexMap::new()),
))
.expect("an empty object is a route");
assert_eq!(bare.depth(), None);
}
#[test]
fn a_zero_depth_is_an_input_error_rather_than_an_empty_answer() {
let mut object = async_graphql::indexmap::IndexMap::new();
object.insert(
async_graphql::Name::new("depth"),
async_graphql::Value::from(0_u64),
);
let refused = <Route<TranscriptFulltextOptions> as InputType>::parse(Some(
async_graphql::Value::Object(object),
));
assert!(refused.is_err(), "a zero depth was accepted");
}
}