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, timestamp};

/// The width is FOUR columns across three segments, and the middle one is the timebase's own answer
/// rather than a restatement of it.
#[test]
fn a_timestamp_spans_its_value_columns_and_its_derived_one() {
  let shapes = <MediaTimestamp as ColumnKind>::SEGMENTS;

  assert_eq!(shapes.len(), 3);
  assert!(matches!(shapes[1], ColumnShape::Composite { segments, .. }
    if core::ptr::eq(segments, <Timebase as ColumnKind>::SEGMENTS)));

  let names: Vec<_> = shapes
    .iter()
    .map(|shape| {
      shape
        .field()
        .expect("every segment names a field")
        .declared()
        .as_str()
    })
    .collect();
  assert_eq!(names, ["pts", "timebase", "nanos"]);
}

/// The cursor carries the VALUE and not the derived key, so two spellings of one instant stay two
/// boundaries — which is what a page resuming on the pair needs.
#[test]
fn the_cursor_carries_the_value_columns() {
  let mut bytes = Vec::new();
  timestamp(12_345, 1, 90_000).write_cursor(&mut bytes);

  assert_eq!(
    bytes.len(),
    3 * size_of::<i32>() + size_of::<i64>() - size_of::<i32>()
  );
  assert_eq!(
    bytes.len(),
    16,
    "eight bytes of count, eight of reduced timebase"
  );

  let read = <MediaTimestamp as CursorValue>::read_cursor(&bytes).expect("its own bytes");
  assert_eq!(read.get().pts(), 12_345);
  assert_eq!(read.get().timebase(), timebase(1, 90_000).get());
}

/// Round trip, including a negative count and the timebase reduction the pair carries.
#[test]
fn a_written_cursor_reads_back_as_the_value_it_came_from() {
  for (pts, num, den) in [
    (0, 1, 1),
    (-99, 1, 90_000),
    (i64::MAX, 1001, 30_000),
    (7, 2, 180_000),
  ] {
    let written = timestamp(pts, num, den);
    let mut bytes = Vec::new();
    written.write_cursor(&mut bytes);

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

    assert_eq!(read.get().pts(), pts, "{pts} @ {num}/{den}");
    assert_eq!(read, written, "{pts} @ {num}/{den}");
  }
}

/// A cursor a client forged is refused rather than read — the wrong width, and the right width
/// holding a timebase no value can have.
#[test]
fn a_forged_cursor_is_refused_rather_than_read() {
  for forged in [[0u8; 0].as_slice(), &[0; 8], &[0; 15], &[0; 17]] {
    assert_eq!(
      <MediaTimestamp as CursorValue>::read_cursor(forged),
      None,
      "{forged:?}"
    );
  }

  // Sixteen bytes whose timebase half is a zero denominator, which no `Timebase` can be.
  let zero_denominator = [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0];
  assert_eq!(
    <MediaTimestamp as CursorValue>::read_cursor(&zero_denominator),
    None
  );
}

/// **The derived key has no value for an instant past `i64` nanoseconds**, and the VALUE columns
/// still do — which is the whole of the overflow posture, seen from the type.
#[test]
fn an_unrepresentable_instant_keeps_its_value_and_loses_its_key() {
  // `i64::MAX` seconds is about 292 billion years; the nanosecond product leaves `i64` long before.
  let extreme = timestamp(i64::MAX, 1, 1);

  assert!(extreme.to_nanos().is_err(), "the key has no i64 to land in");
  assert_eq!(
    extreme.get().pts(),
    i64::MAX,
    "the count is exact regardless"
  );

  let mut bytes = Vec::new();
  extreme.write_cursor(&mut bytes);
  assert_eq!(
    <MediaTimestamp as CursorValue>::read_cursor(&bytes),
    Some(extreme),
    "and so is the cursor, which carries the value"
  );
}