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 super::Route;

/// A route's interior, standing in for an emitted package.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
struct Knobs {
  ceiling: Option<u32>,
}

/// **THE DE-OPTION LAW, read off the bare half.** `weight` and `enabled` defer to nobody and are
/// BARE with their constants; `options` defers to the declaration's own knobs and is an [`Option`],
/// as `depth` is beside it (proved in [`depth`](super::depth)). An unconfigured edge is therefore
/// fully specified in exactly the two fields that defer to nothing.
#[test]
fn the_unconfigured_edge_is_the_two_constants_and_no_package() {
  let route = Route::<Knobs>::new();

  assert!((route.weight() - 1.0).abs() < f64::EPSILON);
  assert!(route.enabled());
  assert_eq!(route.options(), None);
}

/// The constants are the LAW rather than a taste, so the assertion is on the numbers: a change to
/// either has to fail a test before it fails a caller.
#[test]
fn the_shipped_edge_constants_are_one_and_walked() {
  assert!((Route::<Knobs>::DEFAULT_WEIGHT - 1.0).abs() < f64::EPSILON);
  // A CONST BLOCK, which is the stronger pin as well as the one clippy asks for: the law is checked
  // where it is written rather than when a test happens to run.
  const { assert!(Route::<Knobs>::DEFAULT_ENABLED) }
}

/// `Default` is [`Route::new`] and not a derive — a derived body would have demanded `T: Default` of
/// a package whose ABSENCE is the point.
#[test]
fn the_default_edge_is_the_unconfigured_one() {
  assert_eq!(Route::<Knobs>::default(), Route::<Knobs>::new());
}

/// The three builders compose, and each leaves the other two alone — the options-pattern chain a
/// tier package's own `new()` feeds.
#[test]
fn the_builders_compose_without_disturbing_each_other() {
  let route = Route::new()
    .with_weight(2.5)
    .with_enabled(false)
    .with_options(Knobs { ceiling: Some(64) });

  assert!((route.weight() - 2.5).abs() < f64::EPSILON);
  assert!(!route.enabled());
  assert_eq!(route.options(), Some(&Knobs { ceiling: Some(64) }));
}

/// **A ROUTE'S PACKAGE IS THE ROUTE'S, and the edge facts are the PARENT'S.** A standalone door
/// takes the interior and never sees this wrapper, which is what the split buys: asserted by
/// unwrapping to exactly the value that went in.
#[test]
fn the_interior_comes_back_out_untouched() {
  let knobs = Knobs { ceiling: Some(7) };

  assert_eq!(Route::new().with_options(knobs).options(), Some(&knobs));
}

/// A CHILD TIER and a SLOT wear the same wrapper, which is what makes the tree fractal rather than
/// two shapes that happen to nest — asserted by wrapping a wrapper.
#[cfg(test)]
#[test]
fn a_route_of_routes_is_the_same_type_one_level_down() {
  let nested: Route<Route<Knobs>> = Route::new().with_options(Route::new().with_weight(0.5));

  let inner = nested.options().expect("the child tier's edge");
  assert!((inner.weight() - 0.5).abs() < f64::EPSILON);
  assert!(inner.enabled(), "the inner edge kept its own default");
}

#[cfg(feature = "async-graphql")]
mod wire {
  use async_graphql::{EmptyMutation, EmptySubscription, InputObject, InputType, Object, Schema};

  use super::super::Route;

  /// An emitted package's stand-in, with a wire face of its own.
  #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, InputObject)]
  struct TranscriptFulltextOptions {
    ceiling: Option<u32>,
  }

  struct Query;

  #[Object]
  impl Query {
    /// The one resolver, whose ARGUMENT is what makes the route reachable from the schema root —
    /// async-graphql builds its registry by walking from the roots.
    async fn fused(&self, route: Option<Route<TranscriptFulltextOptions>>) -> bool {
      route.is_some_and(|route| route.enabled())
    }
  }

  fn sdl() -> String {
    Schema::build(Query, EmptyMutation, EmptySubscription)
      .finish()
      .sdl()
  }

  /// **THE NAME IS DERIVED, and the derivation is the modifier convention.** `<package>Route`, so
  /// two distinct package names give two distinct route names with no hand-maintained table.
  #[test]
  fn the_wire_name_is_the_packages_own_under_a_route_suffix() {
    assert_eq!(
      <Route<TranscriptFulltextOptions> as InputType>::type_name(),
      "TranscriptFulltextOptionsRoute",
    );
  }

  /// The edge renders as `{ weight, enabled, options }`, and the two bare fields publish their Rust
  /// constants as the SCHEMA's defaults — which is what makes *omit the field* and *write the
  /// default* one request.
  #[test]
  fn the_edge_publishes_its_constants_as_the_schemas_own_defaults() {
    let sdl = sdl();

    assert!(
      sdl.contains("input TranscriptFulltextOptionsRoute {"),
      "{sdl}",
    );
    // NULLABLE *and* defaulted, which is the pair the law needs: nullable makes the field
    // omissible, the default makes an omission and an explicit write the same request.
    assert!(sdl.contains("weight: Float = 1"), "{sdl}");
    assert!(sdl.contains("enabled: Boolean = true"), "{sdl}");
    assert!(
      sdl.contains("options: TranscriptFulltextOptions"),
      "the interior is nullable and wholly omissible:\n{sdl}",
    );
  }

  /// The whole edge is omissible, and an EMPTY object is the unconfigured edge — the wire's half of
  /// the de-Option law.
  #[test]
  fn an_empty_object_parses_as_the_unconfigured_edge() {
    let parsed = <Route<TranscriptFulltextOptions> as InputType>::parse(Some(
      async_graphql::Value::Object(async_graphql::indexmap::IndexMap::new()),
    ))
    .expect("an empty object is a route");

    assert_eq!(parsed, Route::new());
  }

  /// A written field beats the constant, and the two that were not written keep theirs.
  #[test]
  fn a_written_field_beats_the_constant_and_leaves_the_others() {
    let mut object = async_graphql::indexmap::IndexMap::new();
    object.insert(
      async_graphql::Name::new("weight"),
      async_graphql::Value::from(3.0_f64),
    );

    let parsed = <Route<TranscriptFulltextOptions> as InputType>::parse(Some(
      async_graphql::Value::Object(object),
    ))
    .expect("one field is a route");

    assert!((parsed.weight() - 3.0).abs() < f64::EPSILON);
    assert!(parsed.enabled(), "the field nobody wrote kept its constant");
    assert_eq!(parsed.options(), None);
  }
}