use std::sync::Arc;
use crate::{AsyncResource, Resources, Shared, shutdown_signal};
use super::Context;
struct Egemma {
dim: usize,
}
impl AsyncResource for Egemma {
type Options = usize;
type Error = core::convert::Infallible;
async fn build(_ctx: &Resources, dim: usize) -> Result<Self, Self::Error> {
Ok(Self { dim })
}
}
struct Whisper;
impl AsyncResource for Whisper {
type Options = ();
type Error = core::convert::Infallible;
async fn build(_ctx: &Resources, (): ()) -> Result<Self, Self::Error> {
Ok(Self)
}
}
fn shared() -> Arc<Resources> {
Arc::new(Resources::new().with(Shared::new(Egemma { dim: 768 })))
}
#[test]
fn a_window_carries_the_node_s_own_name() {
let ctx = Context::new("embed");
assert_eq!(ctx.node().as_str(), "embed");
assert_eq!(ctx.named().as_str(), "embed");
}
#[test]
fn a_window_wired_to_nothing_reaches_no_resource_and_never_reads_as_stopping() {
let ctx = Context::new("embed");
assert!(ctx.resources().is_none());
let refusal = ctx
.shared::<Egemma>()
.expect_err("no map was ever shared with this window");
assert!(refusal.to_string().contains("Egemma"));
assert!(!ctx.shutdown().is_shutdown());
}
#[test]
fn the_map_is_reached_by_type_and_a_resource_nothing_built_is_named() {
let ctx = Context::new("embed").sharing(shared());
assert_eq!(
ctx.shared::<Egemma>().expect("the launch built it").dim,
768
);
assert!(ctx.resources().is_some());
let refusal = ctx.shared::<Whisper>().expect_err("nothing built one");
assert!(refusal.to_string().contains("Whisper"));
assert_eq!(refusal.resource().as_str(), refusal.resource().as_str());
}
#[test]
fn the_wind_down_line_is_the_one_the_launch_says_the_stop_on() {
let (stopper, signal) = shutdown_signal();
let ctx = Context::new("decode").halting(signal);
assert!(!ctx.shutdown().is_shutdown());
assert!(stopper.announce());
assert!(ctx.shutdown().is_shutdown());
}
#[test]
fn halting_lets_go_of_the_private_line_it_replaced() {
let (stopper, signal) = shutdown_signal();
let ctx = Context::new("decode").halting(signal);
drop(stopper);
assert!(ctx.shutdown().is_shutdown());
}
#[test]
fn a_reopened_window_keeps_the_node_s_facts_and_takes_a_new_line() {
let (node_stopper, node_signal) = shutdown_signal();
let lane = Context::new("demux").sharing(shared()).halting(node_signal);
let (run_stopper, run_signal) = shutdown_signal();
let run = lane.reopened(run_signal);
assert_eq!(run.node().as_str(), "demux");
assert_eq!(run.shared::<Egemma>().expect("carried across").dim, 768);
assert!(run_stopper.announce());
assert!(run.shutdown().is_shutdown());
assert!(!lane.shutdown().is_shutdown());
drop(node_stopper);
}
#[test]
fn the_debug_reports_who_how_many_and_whether() {
let (stopper, signal) = shutdown_signal();
let rendered = format!(
"{:?}",
Context::new("embed").sharing(shared()).halting(signal)
);
assert!(rendered.contains("embed"), "{rendered}");
assert!(rendered.contains("Some(1)"), "{rendered}");
assert!(rendered.contains("shutdown: false"), "{rendered}");
drop(stopper);
}