use core::{future::Future, num::NonZeroUsize};
use async_graphql::{Context, EmptyMutation, EmptySubscription, Object, Schema};
use super::{FoldDoor, installed_fold};
use crate::{
AsyncVectorRead, AsyncVectorWrite, Connected, FoldSemantics, Hits, Max, Sum, VectorColumn,
store::{
VectorEngine,
column::testkit::{Col, Fallback, FallbackRead, Refusal},
},
};
macro_rules! scoring_engine {
($engine:ident) => {
#[derive(Clone, Default)]
struct $engine;
impl VectorEngine for $engine {
type Address = Vec<Col>;
type Connection = ();
type Element = f32;
type Error = Refusal;
type Options = ();
type Score = f32;
type SearchOptions = ();
type WriteBuffer = Fallback;
type ReadBuffer = FallbackRead;
fn buffer() -> Self::WriteBuffer {
Fallback::default()
}
fn finalize(buf: Self::WriteBuffer) -> Self::Address {
buf.0
}
fn open(address: Self::Address) -> Self::ReadBuffer {
Fallback(address).read()
}
}
impl AsyncVectorWrite for $engine {
type Doc = ();
type WriteContext<'a> = ();
fn upsert(
&self,
_ctx: Connected<'_, Self::Connection, Self::WriteContext<'_>>,
_doc: Self::Doc,
) -> impl Future<Output = Result<(), Self::Error>> + Send {
core::future::ready(Ok(()))
}
}
impl AsyncVectorRead for $engine {
type ReadContext<'a> = ();
fn knn(
&self,
_ctx: Connected<'_, Self::Connection, Self::ReadContext<'_>>,
_column: VectorColumn,
_query: &[Self::Element],
_k: NonZeroUsize,
_options: Option<&Self::SearchOptions>,
) -> impl Future<Output = Result<Hits<Self::Address, Self::Score>, Self::Error>> + Send {
core::future::ready(Ok(Vec::new()))
}
fn get(
&self,
_ctx: Connected<'_, Self::Connection, Self::ReadContext<'_>>,
_column: VectorColumn,
_address: &Self::Address,
) -> impl Future<Output = Result<Vec<Vec<Self::Element>>, Self::Error>> + Send {
core::future::ready(Ok(Vec::new()))
}
}
};
}
scoring_engine!(Toy);
scoring_engine!(Other);
struct FramesByClip;
struct FramesByCaption;
struct Leading;
impl FoldSemantics<f32> for Leading {
fn score(&self, bests: &[Option<f32>]) -> f32 {
bests.iter().flatten().copied().next().unwrap_or_default()
}
fn name(&self) -> &str {
"leading"
}
}
struct Root;
#[Object]
impl Root {
async fn clip_fold(&self, ctx: &Context<'_>) -> String {
installed_fold::<Toy, FramesByClip>(ctx)
.map_or_else(|| "none".to_owned(), |door| FoldDoor::name(door).to_owned())
}
async fn caption_fold(&self, ctx: &Context<'_>) -> String {
installed_fold::<Toy, FramesByCaption>(ctx)
.map_or_else(|| "none".to_owned(), |door| FoldDoor::name(door).to_owned())
}
async fn other_engine_fold(&self, ctx: &Context<'_>) -> String {
installed_fold::<Other, FramesByClip>(ctx)
.map_or_else(|| "none".to_owned(), |door| FoldDoor::name(door).to_owned())
}
}
async fn asked(schema: &Schema<Root, EmptyMutation, EmptySubscription>, field: &str) -> String {
let answer = schema.execute(format!("{{ {field} }}")).await;
assert!(answer.errors.is_empty(), "{:?}", answer.errors);
answer.data.to_string()
}
#[tokio::test]
async fn an_uninstalled_pairing_answers_nothing() {
let schema = Schema::build(Root, EmptyMutation, EmptySubscription).finish();
assert!(asked(&schema, "clipFold").await.contains("none"));
}
#[tokio::test]
async fn a_downstream_fold_installs_and_names_itself() {
let schema = FoldDoor::<Toy, FramesByClip>::installed(Leading)
.install(Schema::build(Root, EmptyMutation, EmptySubscription))
.finish();
assert!(asked(&schema, "clipFold").await.contains("leading"));
}
#[tokio::test]
async fn each_pairing_reaches_its_own_fold_and_no_other() {
let schema = FoldDoor::<Toy, FramesByClip>::installed(Max::new())
.install(
FoldDoor::<Toy, FramesByCaption>::installed(Sum::new()).install(Schema::build(
Root,
EmptyMutation,
EmptySubscription,
)),
)
.finish();
assert!(asked(&schema, "clipFold").await.contains("max"));
assert!(asked(&schema, "captionFold").await.contains("sum"));
assert!(asked(&schema, "otherEngineFold").await.contains("none"));
}
#[test]
fn the_debug_line_names_the_installed_fold() {
let door = FoldDoor::<Toy, FramesByClip>::installed(Max::new());
let rendered = format!("{door:?}");
assert!(rendered.contains("max"), "{rendered}");
assert!(rendered.contains("FoldDoor"), "{rendered}");
}