ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
use std::sync::Arc;

use crate::{AsyncResource, Resources, Shared, shutdown_signal};

use super::Context;

/// A resource with one readable fact, so a lookup that reaches the right map can prove it.
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 })
  }
}

/// A resource nothing ever builds, so the refusal has a type to name.
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());
  // The SAME refusal a launch gives for a resource nothing built — never a panic, and never a
  // silent `None`.
  let refusal = ctx
    .shared::<Egemma>()
    .expect_err("no map was ever shared with this window");
  assert!(refusal.to_string().contains("Egemma"));

  // The line's other half is held here, so nobody can say the stop on it — which is the honest
  // answer for a body no launch is winding down.
  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);
  // The window kept no half of its own, so the deployment's stopper is the only voice — and
  // dropping it is the disconnect road onto the same fact.
  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);

  // The two lines are SEPARATE: this delivery's stop is this delivery's.
  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);
}