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::timestamp;

#[test]
fn nanos_convert_a_pts_through_its_timebase() {
  assert_eq!(timestamp(90_000, 1, 90_000).to_nanos(), Ok(1_000_000_000));
  assert_eq!(timestamp(-99, 1, 90_000).to_nanos(), Ok(-1_100_000));
  assert_eq!(timestamp(3, 1, 1).to_nanos(), Ok(3_000_000_000));
  assert_eq!(timestamp(0, 0, 1).to_nanos(), Ok(0));
}

/// Equal instants in different timebases produce the same nanoseconds — the property that makes an
/// ordering operator over the stored column mean what the domain filter says.
#[test]
fn equal_instants_agree_on_nanoseconds_across_timebases() {
  let milliseconds = timestamp(1_000, 1, 1_000);
  let ninety_kilohertz = timestamp(90_000, 1, 90_000);

  assert_eq!(milliseconds, ninety_kilohertz);
  assert_eq!(milliseconds.to_nanos(), ninety_kilohertz.to_nanos());
  assert_eq!(milliseconds.to_nanos(), Ok(1_000_000_000));
}

/// Rounding is toward zero on both sides of the origin, matching `mediatime`'s own rescaling.
#[test]
fn nanos_truncate_toward_zero() {
  assert_eq!(timestamp(1, 1, 3).to_nanos(), Ok(333_333_333));
  assert_eq!(timestamp(-1, 1, 3).to_nanos(), Ok(-333_333_333));
}

/// The `i64` ceiling is reachable exactly, and the first instant past it is refused rather than
/// wrapped, saturated or panicked.
#[test]
fn nanos_pin_the_upper_boundary() {
  assert_eq!(
    timestamp(i64::MAX, 1, 1_000_000_000).to_nanos(),
    Ok(i64::MAX),
    "one nanosecond per tick puts the ceiling exactly in reach"
  );

  // Two nanoseconds per tick: 2^62 ticks is i64::MAX + 1, the smallest instant that does not fit.
  let overflow = timestamp(1 << 62, 2, 1_000_000_000)
    .to_nanos()
    .expect_err("i64::MAX + 1 nanoseconds must not fit");
  assert_eq!(overflow.nanos(), 9_223_372_036_854_775_808);
  assert_eq!(
    overflow.to_string(),
    "9223372036854775808 nanoseconds does not fit an i64"
  );
}

/// The floor, the same way round.
#[test]
fn nanos_pin_the_lower_boundary() {
  assert_eq!(
    timestamp(i64::MIN, 1, 1_000_000_000).to_nanos(),
    Ok(i64::MIN)
  );
  assert_eq!(
    timestamp(-(1 << 62), 2, 1_000_000_000).to_nanos(),
    Ok(i64::MIN)
  );

  let overflow = timestamp(-(1 << 62) - 1, 2, 1_000_000_000)
    .to_nanos()
    .expect_err("below i64::MIN must not fit");
  assert_eq!(overflow.nanos(), -9_223_372_036_854_775_810);
}

/// The widest product the conversion can be handed — `i64::MIN` ticks of `i32::MAX / 1` seconds —
/// is computed exactly rather than overflowing the `i128` it is computed in.
///
/// The worst case is ATTAINED, not bounded: `|pts|` cannot exceed `2⁶³`, `num` cannot exceed
/// `i32::MAX`, and `den` cannot go below `1`, so the literal below IS the largest magnitude
/// `to_nanos` can ever produce. That is the point of pinning it — a change that widens either
/// operand eats the `i128` headroom, and it fails HERE rather than wrapping in production.
#[test]
fn the_widest_product_stays_inside_i128() {
  let overflow = timestamp(i64::MIN, i32::MAX, 1)
    .to_nanos()
    .expect_err("this is far outside i64");

  assert_eq!(
    overflow.nanos(),
    -19_807_040_619_342_712_361_531_211_776_000_000_000
  );
  assert!(overflow.nanos() > i128::MIN, "the i128 headroom is real");
}

/// The consuming and in-place halves of the quartet reach the same value, and the `maybe`/`update`
/// halves can put a slot back to absent — which `with`/`set` cannot express.
#[test]
fn the_accessor_quartet_agrees_with_itself() {
  let instant = timestamp(7, 1, 90_000);

  let mut in_place = MediaTimestampFilter::new();
  in_place
    .set_gt(MediaInstantBound::new(instant))
    .set_in_(vec![instant]);
  assert_eq!(
    in_place,
    MediaTimestampFilter::new()
      .with_gt(MediaInstantBound::new(instant))
      .with_in_(vec![instant])
  );

  let cleared = in_place
    .clone()
    .maybe_gt(None)
    .maybe_in_(None::<Vec<MediaTimestamp>>);
  assert_eq!(cleared, MediaTimestampFilter::new());
  assert!(cleared.is_empty());

  let mut updated = in_place.clone();
  updated
    .update_gt(None)
    .update_in_(None::<Vec<MediaTimestamp>>);
  assert_eq!(updated, cleared);
}

/// A list operator takes anything that becomes a boxed slice, so the caller keeps the choice of
/// whether a conversion allocates.
#[test]
fn list_operators_accept_any_into_boxed_slice() {
  let instant = timestamp(7, 1, 90_000);
  let from_vec = MediaTimestampFilter::new().with_in_(vec![instant]);
  let from_array = MediaTimestampFilter::new().with_in_([instant]);
  let from_boxed = MediaTimestampFilter::new().with_in_(vec![instant].into_boxed_slice());

  assert_eq!(from_vec, from_array);
  assert_eq!(from_vec, from_boxed);
}