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::rc::Rc;

use super::{Fused, FusedScore, Rooted, Sighting};

/// A root that is neither `Copy` nor `Send`, and evidence that is neither — the seats stated as
/// types, so the accessors below prove what they ask rather than what the fixtures happen to allow.
#[derive(Debug, PartialEq)]
struct Heavy(Rc<str>);

fn score(value: f64) -> FusedScore {
  FusedScore::new(value).expect("not a NaN")
}

/// WHAT EACH MEMBER SAID, with the contribution set aside — so a claim about the evidence ORDER
/// reads as one list rather than as a list of pairs.
fn saw<R>(row: &Fused<R, &'static str>) -> Vec<&'static str> {
  row.evidence().iter().map(|held| *held.seen()).collect()
}

/// [`Rooted`]'s three members go in and come back out, none of them cloned.
#[test]
fn a_rooted_hit_decomposes_into_what_it_was_built_from() {
  let hit = Rooted::new(Heavy(Rc::from("m1")), 7, Heavy(Rc::from("transcript")));

  assert_eq!(hit.root_ref(), &Heavy(Rc::from("m1")));
  assert_eq!(hit.rank(), 7);
  assert_eq!(hit.evidence_ref(), &Heavy(Rc::from("transcript")));

  let (root, rank, evidence) = hit.into_components();
  assert_eq!(root, Heavy(Rc::from("m1")));
  assert_eq!(rank, 7);
  assert_eq!(evidence, Heavy(Rc::from("transcript")));
}

/// [`Fused`]'s three, the same way — and the evidence comes back as a SLICE, in the order it was
/// built with, because the constructor does not re-sort what a fold already ordered.
#[test]
fn a_fused_row_decomposes_and_keeps_its_evidence_order() {
  let row = Fused::new(
    score(0.032_522_474_881_015_34),
    Heavy(Rc::from("m2")),
    vec![
      Sighting::new(0.25, 1, "voiceprint"),
      Sighting::new(0.125, 4, "transcript"),
    ],
  );

  assert_eq!(row.score(), score(0.032_522_474_881_015_34));
  assert_eq!(row.root_ref(), &Heavy(Rc::from("m2")));
  assert_eq!(saw(&row), ["voiceprint", "transcript"]);
  // AND THE ENTRY IS A SIGHTING, carrying the term the fold computed beside what the member said.
  assert!((row.evidence()[0].contribution() - 0.25).abs() < f64::EPSILON);
  assert_eq!(row.evidence()[0].ordinal(), 1);

  let (fused, root, evidence) = row.into_components();
  assert_eq!(fused.get(), 0.032_522_474_881_015_34);
  assert_eq!(root, Heavy(Rc::from("m2")));
  assert_eq!(
    evidence,
    vec![
      Sighting::new(0.25, 1, "voiceprint"),
      Sighting::new(0.125, 4, "transcript"),
    ],
  );
}

/// The COPY accessor sits beside the borrow rather than instead of it, and the gate is on the METHOD
/// — so a root that cannot be copied still has a row, which the test above already spent.
#[test]
fn the_copy_accessor_answers_where_the_root_is_copy() {
  let row = Fused::new(
    score(0.015_625),
    "m4",
    vec![Sighting::new(0.5, 1, "voiceprint")],
  );

  assert_eq!(row.root(), "m4");
  assert_eq!(row.root_ref(), &"m4");
}

/// **The tier-B move**: a fused KEY becomes a fused ENTITY and the two other members ride across
/// unchanged. Written on the type for [`Ranked::map_node`](crate::Ranked::map_node)'s reason — a
/// caller assembling the row by hand can assemble it out of the wrong fold's numbers.
#[test]
fn mapping_the_root_carries_the_score_and_the_evidence() {
  let keyed = Fused::new(
    score(0.032_002_048_131_080_39),
    "m3",
    vec![Sighting::new(0.5, 2, "transcript")],
  );

  let hydrated = keyed.map_root(Heavy(Rc::from("the whole entity")));

  assert_eq!(hydrated.score(), score(0.032_002_048_131_080_39));
  assert_eq!(hydrated.root_ref(), &Heavy(Rc::from("the whole entity")));
  assert_eq!(saw(&hydrated), ["transcript"]);
  assert!(
    (hydrated.evidence()[0].contribution() - 0.5).abs() < f64::EPSILON,
    "the carry dropped the fold's own number",
  );
  assert_eq!(
    hydrated.evidence()[0].ordinal(),
    2,
    "the carry dropped the rank the fold computed from",
  );
}