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::FloorSpec;
use crate::ExpandOrigin;

/// **The floor's precedence, all three tiers in one walk**: a named override wins, the flavour
/// default answers where none was named, and the declaration's own is what the chain terminates in.
#[test]
fn a_named_floor_beats_the_flavour_default_beats_the_declaration() {
  let spec = FloorSpec::new().with_fulltext(0.4_f32).with_vector(0.7_f32);

  assert_eq!(
    spec.resolve_fulltext(Some(&0.9), &0.1),
    &0.9,
    "the override"
  );
  assert_eq!(spec.resolve_fulltext(None, &0.1), &0.4, "the flavour");
  assert_eq!(
    FloorSpec::<f32, f32>::new().resolve_fulltext(None, &0.1),
    &0.1,
    "the declaration's own"
  );
}

/// The two flavours are INDEPENDENT — setting one leaves the other resolving to its members'
/// declared floors, which is what *no blanket field* means when it is read off a value.
#[test]
fn the_two_flavours_do_not_reach_each_other() {
  let fulltext_only = FloorSpec::<f32, f32>::new().with_fulltext(0.4);

  assert_eq!(fulltext_only.fulltext(), Some(&0.4));
  assert_eq!(fulltext_only.vector(), None);
  assert_eq!(fulltext_only.resolve_vector(None, &0.65), &0.65);
}

/// The currencies are two TYPES, not one — a BM25 relevance and a cosine similarity are not the same
/// number, and the parameter pair is where that stops being a convention.
#[test]
fn the_two_flavours_may_be_scored_in_different_types() {
  let mixed: FloorSpec<f32, f64> = FloorSpec::new().with_fulltext(2.5).with_vector(0.82);

  assert_eq!(mixed.resolve_fulltext(None, &0.0_f32), &2.5_f32);
  assert_eq!(mixed.resolve_vector(None, &0.0_f64), &0.82_f64);
}

/// A spec that names nothing is the one a bare fused call carries: every member runs at its own
/// declared floor.
#[test]
fn the_empty_floor_spec_defers_to_every_declaration() {
  let bare = FloorSpec::<f32, f32>::default();

  assert_eq!(bare, FloorSpec::new());
  assert_eq!(bare.resolve_fulltext(None, &0.3), &0.3);
  assert_eq!(bare.resolve_vector(None, &0.8), &0.8);
}

/// **(E) THE OFFSET MOVES A MINTED ARM AND LEAVES THE ORIGINAL WHERE IT WAS.** The base is whatever
/// the precedence chain arrived at, so the two arms differ by exactly the offset and by nothing else.
#[test]
fn a_minted_arm_floors_one_offset_above_the_arm_the_caller_asked() {
  let spec = FloorSpec::<f32, f32>::new()
    .with_fulltext(0.4)
    .with_translated_fulltext_offset(0.1);
  let minted = ExpandOrigin::expander("translate");

  assert_eq!(
    spec.resolve_fulltext_at(&ExpandOrigin::Original, None, &0.2),
    0.4
  );
  assert_eq!(spec.resolve_fulltext_at(&minted, None, &0.2), 0.5);

  // WHICHEVER TIER WON THE BASE: an override is a base like any other, and the declaration's own is
  // too — the offset is applied after the chain terminates rather than inside it.
  assert_eq!(spec.resolve_fulltext_at(&minted, Some(&0.9), &0.2), 1.0);
  assert_eq!(
    FloorSpec::<f32, f32>::new()
      .with_translated_fulltext_offset(0.1)
      .resolve_fulltext_at(&minted, None, &0.2),
    0.3,
  );
}

/// A spec that names NO offset floors every arm the same, which is what the fan-free tower had and
/// what a deployment that says nothing keeps.
#[test]
fn an_unoffset_spec_floors_every_arm_at_one_number() {
  let spec = FloorSpec::<f32, f32>::new()
    .with_fulltext(0.4)
    .with_vector(0.7);

  for origin in [ExpandOrigin::Original, ExpandOrigin::expander("synonyms")] {
    assert_eq!(spec.resolve_fulltext_at(&origin, None, &0.2), 0.4);
    assert_eq!(spec.resolve_vector_at(&origin, None, &0.1), 0.7);
  }
}

/// The two flavours' offsets are as independent as their floors — a full-text offset does not reach
/// a vector member, which is *no blanket field* read at the second axis.
#[test]
fn the_two_flavours_offsets_do_not_reach_each_other() {
  let spec = FloorSpec::<f32, f32>::new().with_translated_fulltext_offset(0.1);
  let minted = ExpandOrigin::expander("translate");

  assert_eq!(spec.translated_fulltext_offset(), Some(&0.1));
  assert_eq!(spec.translated_vector_offset(), None);
  assert_eq!(spec.resolve_vector_at(&minted, None, &0.7), 0.7);
}

/// **TIER 3 OVER TIER 2, FIELD BY FIELD** — the request wins every number it named and leaves every
/// number it did not to the deployment. All four fields, so a later one cannot be forgotten.
#[test]
fn a_request_wins_the_floors_it_named_and_leaves_the_rest_to_the_deployment() {
  let deployment = FloorSpec::<f32, f32>::new()
    .with_fulltext(0.4)
    .with_vector(0.7)
    .with_translated_fulltext_offset(0.1)
    .with_translated_vector_offset(0.2);
  let request = FloorSpec::<f32, f32>::new()
    .with_vector(0.9)
    .with_translated_fulltext_offset(0.05);

  let effective = request.over(deployment.clone());

  assert_eq!(effective.vector(), Some(&0.9), "the request's own");
  assert_eq!(effective.translated_fulltext_offset(), Some(&0.05));
  assert_eq!(
    effective.fulltext(),
    Some(&0.4),
    "the deployment's, untouched"
  );
  assert_eq!(effective.translated_vector_offset(), Some(&0.2));

  // A request that names NOTHING is the identity over the deployment's whole spec — the absent tier
  // defers rather than replacing.
  assert_eq!(FloorSpec::new().over(deployment.clone()), deployment);
}