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 width is TWO, and the two are the segments every other half of the crossing reads.
///
/// Asserted through [`ColumnKind`] rather than against the constant, because the trait is what a
/// holder's row actually asks: a `SEGMENTS` that stopped being wired to the impl would leave this
/// passing while a declaration read the default (a scalar's empty list) and stored one column.
#[test]
fn a_timebase_spans_the_two_columns_it_is_bound_and_read_through() {
  let names: Vec<_> = <Timebase as ColumnKind>::SEGMENTS
    .iter()
    .map(|shape| {
      shape
        .field()
        .expect("a plain segment names its column")
        .declared()
        .as_str()
    })
    .collect();

  assert_eq!(names, ["num", "den"]);
}

/// The cursor carries what the COLUMNS carry — the reduced pair — so a page resumes against values
/// a row can actually hold.
///
/// The two spellings of one timebase write the SAME eight bytes, which is the property that makes a
/// keyset boundary comparable at all: `2/180000` and `1/90000` are one row and must be one cursor.
#[test]
fn the_cursor_writes_the_pair_a_column_holds() {
  let mut declared = Vec::new();
  timebase(2, 180_000).write_cursor(&mut declared);

  let mut reduced = Vec::new();
  timebase(1, 90_000).write_cursor(&mut reduced);

  assert_eq!(declared, reduced);
  assert_eq!(declared.len(), 2 * size_of::<i32>());
  assert_eq!(declared, [0, 0, 0, 1, 0, 1, 95, 144]);
}

/// A cursor round-trips to the value the columns hold, and the DECLARED pair comes back reduced —
/// which is equality-preserving, `mediatime` comparing by value.
#[test]
fn a_written_cursor_reads_back_as_the_stored_value() {
  for (num, den) in [(0, 1), (1, 90_000), (2, 180_000), (30_000, 1001)] {
    let written = timebase(num, den);
    let mut bytes = Vec::new();
    written.write_cursor(&mut bytes);

    let read = <Timebase as CursorValue>::read_cursor(&bytes).expect("its own bytes");

    assert_eq!(read, written, "{num}/{den} survives the round trip");
    assert_eq!(
      read.reduced(),
      written.reduced(),
      "and lands on the pair a row holds"
    );
  }
}

/// A cursor is a string a CLIENT hands back, so bytes this type did not write are refused rather
/// than turned into a plausible timebase — the wrong WIDTH, and the right width holding a pair no
/// `Timebase` can exist as.
#[test]
fn a_forged_cursor_is_refused_rather_than_read() {
  for forged in [
    [0u8; 0].as_slice(),
    &[0, 0, 0, 1],
    &[0, 0, 0, 1, 0, 1, 95],
    &[0, 0, 0, 1, 0, 1, 95, 144, 0],
  ] {
    assert_eq!(
      <Timebase as CursorValue>::read_cursor(forged),
      None,
      "{forged:?} is not eight bytes of timebase"
    );
  }

  // Eight bytes, and the DOMAIN is what refuses them: a zero denominator and a negative half are
  // both writable as `i32`s and neither is a timebase.
  let zero_denominator = [0, 0, 0, 1, 0, 0, 0, 0];
  assert_eq!(
    <Timebase as CursorValue>::read_cursor(&zero_denominator),
    None
  );

  let negative_numerator = [255, 255, 255, 255, 0, 0, 0, 1];
  assert_eq!(
    <Timebase as CursorValue>::read_cursor(&negative_numerator),
    None
  );
}

/// The two column invariants are refused by the shared reader, which is the one both the decode and
/// the cursor go through — so a database and a page token cannot come to admit different values.
#[test]
fn the_column_reader_states_which_invariant_a_pair_broke() {
  assert_eq!(assembled(1, 0), Err(TimebaseColumnError::ZeroDenominator));
  assert_eq!(assembled(-1, 1), Err(TimebaseColumnError::NegativeHalf));
  assert_eq!(assembled(1, -2), Err(TimebaseColumnError::NegativeHalf));
  assert_eq!(assembled(0, 1), Ok(timebase(0, 1)));

  // Unreduced is not an error. A column holds whatever was written to it, and a write path that
  // stored the declared pair is telling the truth about a value equal to its reduction.
  assert_eq!(assembled(2, 180_000), Ok(timebase(1, 90_000)));
}