use async_graphql::{EmptyMutation, EmptySubscription, Object, Schema, SimpleObject};
use super::{Match, matches};
use crate::Ranked;
#[derive(Debug, Clone, PartialEq, SimpleObject)]
struct Segment {
media: String,
frame: i64,
}
impl Segment {
fn new(media: &str, frame: i64) -> Self {
Self {
media: media.to_owned(),
frame,
}
}
}
struct Root;
#[Object]
impl Root {
async fn search(&self) -> Vec<Match<Segment, f32>> {
matches([
Ranked::new(9.5_f32, 1, Segment::new("reel-a", 12)),
Ranked::new(4.25_f32, 4, Segment::new("reel-b", 3)),
Ranked::new(1.5_f32, 9, Segment::new("reel-c", 7)),
])
}
}
fn schema() -> Schema<Root, EmptyMutation, EmptySubscription> {
Schema::build(Root, EmptyMutation, EmptySubscription).finish()
}
fn sdl() -> String {
schema().sdl()
}
#[test]
fn the_published_name_is_the_node_s_own_plus_match() {
let sdl = sdl();
assert!(sdl.contains("type SegmentMatch"), "{sdl}");
assert!(sdl.contains("search: [SegmentMatch!]!"), "{sdl}");
}
#[test]
fn the_three_fields_are_non_null_and_in_the_ruled_order() {
let sdl = sdl();
let start = sdl
.find("type SegmentMatch")
.expect("the match type is published");
let body = &sdl[start..];
let end = body.find('}').expect("the type closes");
let body = &body[..end];
let score = body
.find("score: f32!")
.unwrap_or_else(|| panic!("a non-null score at its own width: {body}"));
assert!(!body.contains("score: Float!"), "{body}");
let rank = body
.find("rank: Int!")
.unwrap_or_else(|| panic!("a non-null rank: {body}"));
let node = body
.find("node: Segment!")
.unwrap_or_else(|| panic!("a non-null node: {body}"));
assert!(!body.contains("key:"), "the retired spelling: {body}");
assert!(score < rank, "score is written before rank: {body}");
assert!(rank < node, "rank is written before node: {body}");
}
#[test]
fn the_score_carries_its_non_comparability_label() {
let sdl = sdl();
assert!(
sdl.contains("Scores compare INSIDE one result list and nowhere else."),
"{sdl}"
);
assert!(sdl.contains("Not its position in this list"), "{sdl}");
}
#[test]
fn the_schema_carries_a_client_s_description_and_none_of_the_crate_s() {
let sdl = sdl();
assert!(
sdl.contains("One result of a search: what matched, how well, and where the engine put it."),
"{sdl}"
);
for internal in [
"E0592",
"E0034",
"duplicate definitions",
"multiple applicable items",
"PhantomData",
"OutputType",
] {
assert!(
!sdl.contains(internal),
"`{internal}` reached the published schema: {sdl}"
);
}
}
#[tokio::test]
async fn the_engine_s_ordinals_cross_unclosed() {
let answer = schema()
.execute("{ search { score rank node { media frame } } }")
.await;
assert!(answer.errors.is_empty(), "{:?}", answer.errors);
let rendered = answer.data.to_string();
assert!(rendered.contains("rank: 1"), "{rendered}");
assert!(rendered.contains("rank: 4"), "{rendered}");
assert!(rendered.contains("rank: 9"), "{rendered}");
assert!(!rendered.contains("rank: 2"), "{rendered}");
assert!(!rendered.contains("rank: 3"), "{rendered}");
}
#[test]
fn the_crossing_is_the_ranked_answer_itself() {
let ranked = Ranked::new(0.75_f32, 17, Segment::new("reel-z", 42));
let hit = Match::from(ranked.clone());
assert_eq!(hit.score_ref(), ranked.score_ref());
assert_eq!(hit.rank(), ranked.rank());
assert_eq!(hit.node_ref(), ranked.node_ref());
}
#[test]
fn the_list_crossing_keeps_the_engine_s_order() {
let crossed = matches([
Ranked::new(3_u32, 1, "a"),
Ranked::new(2_u32, 5, "b"),
Ranked::new(1_u32, 6, "c"),
]);
assert_eq!(
crossed.iter().map(Match::rank).collect::<Vec<_>>(),
vec![1, 5, 6]
);
assert_eq!(
crossed
.iter()
.map(|hit| *hit.node_ref())
.collect::<Vec<_>>(),
vec!["a", "b", "c"]
);
}