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
//! What the WIRE-ERASED DOOR says: a key crosses through text both ways, an unparseable one is
//! `Idle` on the read side and [`KeyDoesNotParse`] on the write side, and every other door is
//! [`Registry`]'s own, taken through a string.

use core::fmt;

use super::{DynRegistry, KeyDoesNotParse};
use crate::{Registry, RegistryOptions, RegistryOverride, RetireOrigin, RootState};

/// A root key that can name itself on the wire — the bound [`DynRegistry`]'s blanket impl asks for,
/// beyond [`super::RootKey`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct MediaId(u64);

impl fmt::Display for MediaId {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    write!(f, "{}", self.0)
  }
}

impl core::str::FromStr for MediaId {
  type Err = core::num::ParseIntError;

  fn from_str(s: &str) -> Result<Self, Self::Err> {
    s.parse().map(MediaId)
  }
}

/// A fresh, empty table, running the shipped defaults.
fn table() -> Registry<MediaId> {
  Registry::new(RegistryOptions::new())
}

/// **A KEY NOTHING IS TRUE OF READS `Idle` THROUGH THE WIRE DOOR** — [`Registry::state`]'s own
/// total answer, crossed.
#[test]
fn an_unheard_of_key_is_idle_through_the_wire_door() {
  let table = table();
  assert_eq!(DynRegistry::state(&table, "7"), RootState::Idle);
}

/// **A KEY THAT DOES NOT EVEN PARSE IS ALSO `Idle`** — total on the SAME reasoning
/// [`Registry::state`] is total on: nothing can be true of a key that is not this family's own root
/// type in any spelling.
#[test]
fn an_unparseable_key_is_idle_rather_than_a_refusal() {
  let table = table();
  assert_eq!(DynRegistry::state(&table, "not-a-number"), RootState::Idle);
  assert_eq!(DynRegistry::state(&table, ""), RootState::Idle);
}

/// **A DISPATCH TAKEN THROUGH THE TYPED DOOR IS VISIBLE THROUGH THE WIRE ONE** — the two doors read
/// the SAME table, which is the whole point of a second FACE rather than a second STORE.
#[test]
fn a_typed_dispatch_is_visible_through_the_wire_door() {
  let table = table();
  let key = MediaId(7);
  let stamp = crate::Stamp::now();
  let _advance = table.dispatched(&key, stamp);

  assert!(matches!(
    DynRegistry::state(&table, "7"),
    RootState::Analyzing { inflight: 1, .. }
  ));
}

/// **RETIRE, THROUGH THE WIRE DOOR** — lands the same state the typed door would, and answers it
/// back rather than leaving a caller to ask a second time.
#[test]
fn retire_through_the_wire_door_lands_and_answers() {
  let table = table();
  let landed =
    DynRegistry::retire(&table, "7", RetireOrigin::Vanished).expect("7 parses as a MediaId");
  assert!(matches!(
    landed,
    RootState::Retired {
      origin: RetireOrigin::Vanished,
      ..
    }
  ));
  assert_eq!(table.state(&MediaId(7)), landed, "the typed door agrees");
}

/// **RETIRE ON A KEY THAT DOES NOT PARSE IS `KeyDoesNotParse`** — a mistake worth surfacing rather
/// than a no-op worth hiding; see the module note.
#[test]
fn retire_on_an_unparseable_key_refuses_by_name() {
  let table = table();
  assert_eq!(
    DynRegistry::retire(&table, "nope", RetireOrigin::User),
    Err(KeyDoesNotParse)
  );
}

/// **RESTORE, THROUGH THE WIRE DOOR** — a retired key comes back, and the wire door sees the same
/// `Restoring` the typed one would.
#[test]
fn restore_through_the_wire_door_lands_and_answers() {
  let table = table();
  let key = MediaId(7);
  let _ = table.retire(&key, RetireOrigin::User, crate::Stamp::now());
  assert!(matches!(table.state(&key), RootState::Retired { .. }));

  let landed = DynRegistry::restore(&table, "7").expect("7 parses");
  assert_eq!(landed, RootState::Restoring);
  assert_eq!(table.state(&key), RootState::Restoring);
}

/// **REANALYZE IS AN INTAKE UNDER A FRIENDLIER NAME** — an idle key reached this way starts
/// `Analyzing`, exactly as a debounced burst would have taken it.
#[test]
fn reanalyze_through_the_wire_door_is_an_intake() {
  let table = table();
  let landed = DynRegistry::reanalyze(&table, "7").expect("7 parses");
  assert!(matches!(landed, RootState::Analyzing { inflight: 0, .. }));
  assert_eq!(table.state(&MediaId(7)), landed);
}

/// **COUNTS CROSS WHOLE** — the wire door's tally is the typed door's, key for key.
#[test]
fn counts_cross_through_the_wire_door() {
  let table = table();
  let stamp = crate::Stamp::now();
  let _ = table.intake(&MediaId(1), stamp);
  let _ = table.intake(&MediaId(2), stamp);
  let _ = table.retire(&MediaId(3), RetireOrigin::Vanished, stamp);

  let counts = DynRegistry::counts(&table);
  assert_eq!(counts.analyzing, 2);
  assert_eq!(counts.retired, 1);
  assert_eq!(counts.retiring, 0);
}

/// **RECENT RENDERS EVERY HELD KEY AS TEXT**, up to the limit asked for.
#[test]
fn recent_renders_held_keys_as_text() {
  let table = table();
  let stamp = crate::Stamp::now();
  let _ = table.intake(&MediaId(1), stamp);
  let _ = table.intake(&MediaId(2), stamp);

  let mut recent = DynRegistry::recent(&table, 10);
  recent.sort_by(|a, b| a.0.cmp(&b.0));
  assert_eq!(recent.len(), 2);
  assert_eq!(recent[0].0.as_str(), "1");
  assert_eq!(recent[1].0.as_str(), "2");

  assert_eq!(
    DynRegistry::recent(&table, 1).len(),
    1,
    "the limit is honoured"
  );
}

/// **`apply_knobs` THROUGH THE WIRE DOOR TURNS THE SAME LIVE DIAL** the typed door reads —
/// [`Registry::apply_knobs`] itself, crossed.
#[test]
fn apply_knobs_through_the_wire_door_is_apply_knobs() {
  let table = table();
  let quiet = core::time::Duration::from_millis(9);
  DynRegistry::apply_knobs(&table, &RegistryOverride::new().with_debounce(quiet));
  assert_eq!(DynRegistry::options(&table).debounce(), quiet);
  assert_eq!(table.options().debounce(), quiet, "the typed door agrees");
}