use async_graphql::{EmptyMutation, EmptySubscription, Object, Schema, SimpleObject};
use super::{FoldedMatch, folded_matches};
use crate::retrieve::{Folded, StreamBest};
#[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,
}
}
}
#[derive(Debug, Clone, PartialEq, SimpleObject)]
struct SegmentEntity {
media: String,
frame: i64,
caption: String,
}
fn walked() -> Folded<Segment, f32> {
Folded::new(
9.0_f32,
Box::new([
Some(StreamBest::new(6.5_f32, 1)),
None,
Some(StreamBest::new(2.5_f32, 7)),
]),
Segment::new("reel-a", 12),
)
}
struct Root;
#[Object]
impl Root {
async fn set_search(&self) -> Vec<FoldedMatch<Segment, f32>> {
folded_matches([walked()])
}
async fn hydrated_set_search(&self) -> Vec<FoldedMatch<SegmentEntity, f32>> {
folded_matches([walked().map_node(SegmentEntity {
media: "reel-a".to_owned(),
frame: 12,
caption: "a caption the address does not carry".to_owned(),
})])
}
}
fn schema() -> Schema<Root, EmptyMutation, EmptySubscription> {
Schema::build(Root, EmptyMutation, EmptySubscription).finish()
}
fn sdl() -> String {
schema().sdl()
}
fn body(sdl: &str, ty: &str) -> String {
let start = sdl
.find(ty)
.unwrap_or_else(|| panic!("`{ty}` is published: {sdl}"));
let rest = &sdl[start..];
let end = rest.find("\n}").expect("the type closes on its own line");
rest[..end].to_owned()
}
#[test]
fn the_published_name_is_the_node_s_own_plus_folded_match() {
let sdl = sdl();
assert!(sdl.contains("type SegmentFoldedMatch"), "{sdl}");
assert!(sdl.contains("setSearch: [SegmentFoldedMatch!]!"), "{sdl}");
}
#[test]
fn the_three_rows_are_the_ruled_ones_and_there_is_no_answer_wide_rank() {
let sdl = sdl();
let body = body(&sdl, "type SegmentFoldedMatch");
let score = body
.find("score: f32!")
.unwrap_or_else(|| panic!("a non-null folded score at its own width: {body}"));
assert!(!body.contains("score: Float!"), "{body}");
let streams = body
.find("streams: [SegmentStreamSighting]!")
.unwrap_or_else(|| panic!("a non-null list of nullable slots: {body}"));
let node = body
.find("node: Segment!")
.unwrap_or_else(|| panic!("a non-null node: {body}"));
assert!(score < streams, "score is written before streams: {body}");
assert!(streams < node, "streams is written before node: {body}");
assert!(!body.contains("rank:"), "an answer-wide rank: {body}");
assert!(!body.contains("key:"), "the retired spelling: {body}");
}
#[test]
fn the_slot_list_is_positional_and_its_elements_are_nullable() {
let sdl = sdl();
let body = body(&sdl, "type SegmentFoldedMatch");
assert!(body.contains("streams: [SegmentStreamSighting]!"), "{body}");
assert!(
!body.contains("[SegmentStreamSighting!]!"),
"a stream that saw nothing would have no slot: {body}"
);
}
#[test]
fn the_slot_publishes_the_pre_fold_score_and_the_stream_s_own_ordinal() {
let sdl = sdl();
let body = body(&sdl, "type SegmentStreamSighting");
assert!(body.contains("score: f32!"), "{body}");
assert!(!body.contains("score: Float!"), "{body}");
assert!(body.contains("ordinal: Int!"), "{body}");
assert!(!body.contains("contribution"), "{body}");
}
#[test]
fn the_hydrated_twin_differs_in_one_row() {
let sdl = sdl();
assert!(
sdl.contains("hydratedSetSearch: [SegmentEntityFoldedMatch!]!"),
"{sdl}"
);
let body = body(&sdl, "type SegmentEntityFoldedMatch");
assert!(body.contains("node: SegmentEntity!"), "{body}");
assert!(!body.contains("node: Segment!"), "{body}");
assert!(!body.contains("key:"), "{body}");
assert!(!body.contains("entity:"), "the retired spelling: {body}");
assert!(body.contains("score: f32!"), "{body}");
assert!(
body.contains("streams: [SegmentEntityStreamSighting]!"),
"{body}"
);
}
#[test]
fn both_scores_carry_their_own_reading() {
let sdl = sdl();
assert!(
sdl.contains("This list's order is this number's order, and the number compares nowhere else."),
"{sdl}"
);
assert!(
sdl.contains("The number BEFORE any folding"),
"the slot's pre-fold reading is missing: {sdl}"
);
assert!(
sdl.contains("There is no rank for the result as a whole"),
"the missing-rank law is not published: {sdl}"
);
}
#[test]
fn the_schema_carries_a_client_s_description_and_none_of_the_crate_s() {
let sdl = sdl();
for internal in ["E0592", "E0034", "PhantomData", "OutputType", "MEASURED"] {
assert!(
!sdl.contains(internal),
"`{internal}` reached the published schema: {sdl}"
);
}
}
#[tokio::test]
async fn the_slots_render_in_asked_order_with_the_absent_one_null() {
let answer = schema()
.execute("{ setSearch { score streams { score ordinal } node { frame } } }")
.await;
assert!(answer.errors.is_empty(), "{:?}", answer.errors);
let rendered = answer.data.to_string();
assert!(rendered.contains("score: 6.5"), "{rendered}");
assert!(rendered.contains("ordinal: 1"), "{rendered}");
assert!(rendered.contains("score: 2.5"), "{rendered}");
assert!(rendered.contains("ordinal: 7"), "{rendered}");
assert!(
rendered.contains("null"),
"the absent stream was dropped: {rendered}"
);
assert!(rendered.contains("score: 9"), "{rendered}");
}
#[test]
fn the_crossing_is_the_folded_answer_itself() {
let folded = walked();
let hit = FoldedMatch::from(folded.clone());
assert_eq!(hit.score_ref(), folded.score_ref());
assert_eq!(hit.node_ref(), folded.node_ref());
assert_eq!(hit.streams().len(), folded.streams().len());
for (crossed, slot) in hit.streams().iter().zip(folded.streams()) {
match (crossed, slot) {
(Some(crossed), Some(slot)) => {
assert_eq!(crossed.score_ref(), slot.score_ref());
assert_eq!(crossed.ordinal(), slot.ordinal());
}
(None, None) => {}
_ => panic!("a slot changed presence across the crossing"),
}
}
}