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::*;
use crate::primitives::mediatime::tests::timebase;

/// The reduction is total over the whole constructible domain — `num` in `0..=i32::MAX`, `den` in
/// `1..=i32::MAX` — and lands on lowest terms, both extremes of each half included.
///
/// A pair already in lowest terms comes back as written, which is what makes reducing idempotent
/// and therefore safe to apply on both sides of the crossing.
#[test]
fn reduce_puts_every_timebase_in_lowest_terms() {
  for (declared, reduced) in [
    ((0, 1), (0, 1)),
    ((0, i32::MAX), (0, 1)),
    ((0, 90_000), (0, 1)),
    ((1, 1), (1, 1)),
    ((1, 90_000), (1, 90_000)),
    ((2, 180_000), (1, 90_000)),
    ((30_000, 1001), (30_000, 1001)),
    ((60_000, 2002), (30_000, 1001)),
    ((2, 4), (1, 2)),
    ((1 << 30, 2), (1 << 29, 1)),
    ((i32::MAX, 1), (i32::MAX, 1)),
    ((i32::MAX, i32::MAX), (1, 1)),
  ] {
    let (num, den) = timebase(declared.0, declared.1).reduced();

    assert_eq!(
      (num, den.get()),
      reduced,
      "{}/{} reduces",
      declared.0,
      declared.1
    );
  }
}

/// Reducing twice is reducing once, so a value that crosses the boundary more than once cannot
/// creep.
#[test]
fn reduce_is_idempotent() {
  for (num, den) in [(2, 180_000), (2, 4), (0, 90_000), (60_000, 2002), (8, 12)] {
    let (once_num, once_den) = timebase(num, den).reduced();
    let (twice_num, twice_den) = timebase(once_num, once_den.get()).reduced();

    assert_eq!((once_num, once_den), (twice_num, twice_den), "{num}/{den}");
  }
}

/// Timebases the domain calls equal reduce to ONE pair — which is what makes an index on the two
/// stored columns answer equality exactly.
#[test]
fn equal_timebases_reduce_to_the_same_pair() {
  let half = timebase(1, 2);
  let two_quarters = timebase(2, 4);

  assert_eq!(half, two_quarters, "mediatime compares timebases by value");
  assert_eq!(half.reduced(), two_quarters.reduced());
}

/// The three layers each do the right DIFFERENT thing, pinned together so no later change quietly
/// unifies them: the type compares by value, [`Display`](core::fmt::Display) preserves the pair the
/// stream declared, and the storage form canonicalises.
#[test]
fn the_rendering_keeps_what_the_reduction_discards() {
  let declared = timebase(60_000, 2002);

  assert_eq!(declared.to_string(), "60000/2002");
  assert_eq!(declared.reduced().0, 30_000);
  assert_eq!(declared.reduced().1.get(), 1001);
  assert_eq!(declared, timebase(30_000, 1001));
}

/// Every slot of all three facets is reachable through its getter, and the consuming and the
/// in-place setter reach the same filter.
#[test]
fn the_timebase_filter_projects_its_three_facets() {
  let numerator = ScalarFilter::new().with_gte(1);
  let denominator = ScalarFilter::new().with_eq(NonZeroI32::new(90_000).unwrap());

  let built = TimebaseFilter::new()
    .with_eq(timebase(1, 90_000))
    .with_ne(timebase(1, 1000))
    .with_in_(vec![timebase(30_000, 1001)])
    .with_not_in(vec![timebase(1, 24)])
    .with_starts_with(Utf8Bytes::from("1/"))
    .with_ends_with(Utf8Bytes::from("/90000"))
    .with_contains(Utf8Bytes::from("/9"))
    .with_numerator(numerator.clone())
    .with_denominator(denominator.clone());

  assert_eq!(built.eq(), Some(&timebase(1, 90_000)));
  assert_eq!(built.ne(), Some(&timebase(1, 1000)));
  assert_eq!(built.in_(), Some([timebase(30_000, 1001)].as_slice()));
  assert_eq!(built.not_in(), Some([timebase(1, 24)].as_slice()));
  assert_eq!(built.starts_with(), Some(&Utf8Bytes::from("1/")));
  assert_eq!(built.ends_with(), Some(&Utf8Bytes::from("/90000")));
  assert_eq!(built.contains(), Some(&Utf8Bytes::from("/9")));
  assert_eq!(built.numerator(), Some(&numerator));
  assert_eq!(built.denominator(), Some(&denominator));

  let mut in_place = TimebaseFilter::new();
  in_place
    .set_eq(timebase(1, 90_000))
    .set_ne(timebase(1, 1000))
    .set_in_(vec![timebase(30_000, 1001)])
    .set_not_in(vec![timebase(1, 24)])
    .set_starts_with(Utf8Bytes::from("1/"))
    .set_ends_with(Utf8Bytes::from("/90000"))
    .set_contains(Utf8Bytes::from("/9"))
    .set_numerator(numerator)
    .set_denominator(denominator);
  assert_eq!(in_place, built);
}

/// Empty exactly while EVERY slot of every facet is, so no slot can be set without the whole filter
/// admitting it. A slot missing from the chain would leave a set filter claiming to be empty, and a
/// lowering that trusts the claim emits no predicate at all.
///
/// One case per slot rather than one per facet: the chain is a conjunction of nine terms, and a
/// dropped term is invisible to a case that sets a sibling slot of the same facet.
#[test]
fn the_timebase_filter_is_empty_only_while_every_slot_is() {
  assert!(TimebaseFilter::new().is_empty());

  for filter in [
    TimebaseFilter::new().with_eq(timebase(1, 90_000)),
    TimebaseFilter::new().with_ne(timebase(1, 90_000)),
    TimebaseFilter::new().with_in_(vec![timebase(1, 90_000)]),
    TimebaseFilter::new().with_not_in(vec![timebase(1, 90_000)]),
    TimebaseFilter::new().with_starts_with(Utf8Bytes::from("1/")),
    TimebaseFilter::new().with_ends_with(Utf8Bytes::from("/90000")),
    TimebaseFilter::new().with_contains(Utf8Bytes::from("/9")),
    TimebaseFilter::new().with_numerator(ScalarFilter::new().with_eq(1)),
    TimebaseFilter::new()
      .with_denominator(ScalarFilter::new().with_eq(NonZeroI32::new(90_000).unwrap())),
  ] {
    assert!(!filter.is_empty(), "{filter:?}");
  }

  // An EMPTY membership list is an operand rather than an absence — it asks for no rows — so a
  // filter holding one is not empty.
  assert!(!TimebaseFilter::new().with_in_(Vec::new()).is_empty());
}

/// `rational`'s operand is the canonical `num/den` spelling, and it names exactly the timebases
/// that exist — every other string is refused rather than rounded into a timebase nobody wrote.
#[test]
fn the_rational_form_names_exactly_the_timebases_that_exist() {
  assert_eq!("1/90000".parse(), Ok(timebase(1, 90_000)));
  assert_eq!("0/1".parse(), Ok(timebase(0, 1)));
  assert_eq!("30000/1001".parse(), Ok(timebase(30_000, 1001)));

  // A zero denominator is not a timebase, and `mediatime` refuses a negative half on either side.
  assert_eq!(
    "1/0".parse::<Timebase>(),
    Err(ParseTimebaseError::ZeroDenominator)
  );
  assert_eq!(
    "-1/2".parse::<Timebase>(),
    Err(ParseTimebaseError::NegativeHalf)
  );
  assert_eq!(
    "1/-2".parse::<Timebase>(),
    Err(ParseTimebaseError::NegativeHalf)
  );

  // A bare number names a numerator and leaves the denominator unsaid; it is not `1/1`.
  assert_eq!(
    "1".parse::<Timebase>(),
    Err(ParseTimebaseError::MissingSeparator)
  );
  assert_eq!(
    "".parse::<Timebase>(),
    Err(ParseTimebaseError::MissingSeparator)
  );

  // Each half is named separately, so a client is told WHICH one it got wrong.
  assert_eq!(
    "a/b".parse::<Timebase>(),
    Err(ParseTimebaseError::Numerator)
  );
  assert_eq!(
    "1/b".parse::<Timebase>(),
    Err(ParseTimebaseError::Denominator)
  );
  assert_eq!(
    "1/2/3".parse::<Timebase>(),
    Err(ParseTimebaseError::Denominator),
    "the split takes the FIRST separator, so the rest is one malformed denominator"
  );
}

/// The rendered form parses back to the timebase that rendered it, with both halves as written.
///
/// Compares the NUMERATOR AND DENOMINATOR rather than the timebases, for [`Timebase`]'s
/// value-based equality: a rendering that reduced would satisfy an `assert_eq!` on the values while
/// losing the timebase the stream declared.
#[test]
fn the_rational_form_round_trips_both_halves() {
  assert_eq!(timebase(1, 90_000).to_string(), "1/90000");
  assert_eq!(timebase(0, i32::MAX).to_string(), format!("0/{}", i32::MAX));

  for (num, den) in [(0, 1), (1, 90_000), (30_000, 1001), (i32::MAX, i32::MAX)] {
    let recovered = timebase(num, den)
      .to_string()
      .parse::<Timebase>()
      .expect("a rendered timebase always parses");

    assert_eq!(recovered.get().num(), num, "numerator of {num}/{den}");
    assert_eq!(
      recovered.get().den().get(),
      den,
      "denominator of {num}/{den}"
    );
  }
}