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::geometry::tests::{point, quad};

/// **THE WINDING ORDER, PINNED — this is the test the type's note points at.**
///
/// `avanalyze`'s `DocumentSegment` contract says a consumer reading the corners as `(TL, TR, BL,
/// BR)` builds a bow-tie out of every real document, and pins the order with a conformance suite.
/// This is the same pin one crate over: the four corners are DISTINGUISHABLE, so a transposition
/// anywhere in the constructor, the accessors or the array changes a value here rather than
/// rearranging equal ones.
///
/// The third corner is what tells the two readings apart. In WINDING order it is `bottom_right` —
/// diagonally opposite the first — and in raster order it would be `bottom_left`.
#[test]
fn the_corners_are_in_perimeter_winding_order_and_never_raster_order() {
  let page = quad();

  assert_eq!(page.top_left(), point(0.10, 0.11));
  assert_eq!(page.top_right(), point(0.90, 0.12));
  assert_eq!(page.bottom_right(), point(0.91, 0.88));
  assert_eq!(page.bottom_left(), point(0.13, 0.89));

  assert_eq!(
    page.corners(),
    [
      point(0.10, 0.11),
      point(0.90, 0.12),
      point(0.91, 0.88),
      point(0.13, 0.89),
    ],
    "the array walks the perimeter, so the third entry is diagonally opposite the first"
  );

  let third = page.corners()[2];
  assert_eq!(
    third,
    page.bottom_right(),
    "reading the third corner as `bottom_left` is the bow-tie this assertion exists to catch"
  );
}

/// The positional constructor takes its four arguments in the SAME winding order the accessors read
/// them back in, which is the half a doc comment alone cannot hold.
#[test]
fn the_constructor_takes_its_corners_in_the_order_the_accessors_return_them() {
  let corners = [
    point(0.1, 0.1),
    point(0.9, 0.1),
    point(0.9, 0.9),
    point(0.1, 0.9),
  ];
  let page = Quad::new(corners[0], corners[1], corners[2], corners[3]);

  assert_eq!(page.corners(), corners);
}

/// The unit-frame constructor is four [`Point2::try_unit`] calls and adds no rule of its own — a
/// quad has no sum to leave the frame with, every number in it being a corner.
#[test]
fn a_unit_quad_refuses_exactly_what_its_component_does() {
  assert!(Quad::try_unit((0.1, 0.1), (0.9, 0.1), (0.9, 0.9), (0.1, 0.9)).is_ok());
  assert!(Quad::try_unit((0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)).is_ok());

  assert!(Quad::try_unit((1.9, 0.1), (0.9, 0.1), (0.9, 0.9), (0.1, 0.9)).is_err());
  assert!(Quad::try_unit((0.1, 0.1), (0.9, 0.1), (0.9, 0.9), (0.1, f32::NAN)).is_err());
}

/// **A NON-CONVEX and a ROTATED quad are both admitted**, which is the ruling the type's note
/// states: a document seen at an angle can have its `top_left` below its `top_right`, so a check
/// enforcing the names would refuse exactly the observations this type exists to hold.
#[test]
fn a_rotated_quad_whose_corners_defy_their_names_is_still_admitted() {
  let tilted = Quad::try_unit((0.10, 0.40), (0.60, 0.10), (0.90, 0.60), (0.40, 0.90))
    .expect("a rotated document is a document");

  assert!(
    tilted.top_left().y() > tilted.top_right().y(),
    "the first corner sits BELOW the second, and nothing refuses it"
  );
}

/// **THE SEGMENT SHAPE**: four NESTED corners rather than eight flat components, so a column says
/// which corner it belongs to. Flattening would put four columns called `x` in one namespace.
#[test]
fn a_quad_nests_four_named_corners_of_two_columns_each() {
  let segments = <Quad<NormCoord> as ColumnKind>::SEGMENTS;
  assert_eq!(segments.len(), 4);

  let names: Vec<_> = segments
    .iter()
    .map(|segment| {
      segment
        .field()
        .expect("every corner is named")
        .declared()
        .as_str()
    })
    .collect();
  assert_eq!(
    names,
    ["top_left", "top_right", "bottom_right", "bottom_left"],
    "the segment order is the WINDING order, so the columns are written in it too"
  );

  for segment in segments {
    assert_eq!(crate::width(core::slice::from_ref(segment)), 2);
  }
}

/// **THE CURSOR ROUND TRIP**, in winding order, with the forged-length refusal beside it.
#[test]
fn a_quad_round_trips_through_a_cursor_and_refuses_a_forged_one() {
  let mut bytes = Vec::new();
  let written = quad();
  crate::CursorValue::write_cursor(&written, &mut bytes);

  assert_eq!(
    <Quad<NormCoord> as crate::CursorValue>::read_cursor(&bytes),
    Some(written)
  );

  assert_eq!(
    <Quad<NormCoord> as crate::CursorValue>::read_cursor(&bytes[..bytes.len() - 1]),
    None
  );
  assert_eq!(
    <Quad<NormCoord> as crate::CursorValue>::read_cursor(&[]),
    None
  );
}

/// EMPTINESS descends through the CORNER's filter into the component's — a corner holding an empty
/// position filter asks nothing, which is the recursion this face is built on.
#[test]
fn a_filter_is_empty_until_a_component_under_some_corner_is_set() {
  let bare = QuadFilter::<crate::ScalarFilter<NormCoord>>::new();
  assert!(crate::GraphqlFilter::is_empty(&bare));

  let emptily =
    QuadFilter::<crate::ScalarFilter<NormCoord>>::new().with_top_left(Point2Filter::new());
  assert!(
    crate::GraphqlFilter::is_empty(&emptily),
    "a corner holding an empty position filter asks nothing"
  );

  let asked =
    QuadFilter::<crate::ScalarFilter<NormCoord>>::new()
      .with_bottom_right(Point2Filter::new().with_x(
        crate::ScalarFilter::new().with_gt(crate::primitives::geometry::tests::coord(0.5)),
      ));
  assert!(!crate::GraphqlFilter::is_empty(&asked));
}

/// **THE TIGHT BOX AROUND A PHOTOGRAPHED PAGE** — the reduction the quad exists to be more precise
/// than, taken deliberately.
///
/// The fixture's corners are the family's own DISTINGUISHABLE four, so every one of the four extreme
/// searches lands on a different corner: the leftmost `x` is the top-left's, the rightmost is the
/// bottom-right's, the topmost `y` is the top-left's and the bottommost is the bottom-left's. A
/// reduction that read the wrong corner changes a number here rather than rearranging equal ones.
#[test]
fn the_bounds_are_the_tightest_box_the_four_corners_span() {
  let page = Quad::new(
    Point2::new(0.10_f32, 0.11),
    Point2::new(0.90, 0.12),
    Point2::new(0.91, 0.88),
    Point2::new(0.13, 0.89),
  );

  let around = page.bounds();
  assert_eq!(around.x(), 0.10, "the leftmost corner, which is the FIRST");
  assert_eq!(
    around.y(),
    0.11,
    "the topmost corner, which is also the first"
  );
  assert_eq!(around.width(), 0.91 - 0.10, "…out to the THIRD corner's x");
  assert_eq!(
    around.height(),
    0.89 - 0.11,
    "…down to the FOURTH corner's y"
  );
}

/// **AN AXIS-ALIGNED QUAD REDUCES TO EXACTLY ITSELF**, which is the identity a lossy projection still
/// owes: nothing is lost where there was no angle to lose.
#[test]
fn a_rectangular_quad_bounds_to_the_rectangle_it_already_is() {
  let square = Quad::new(
    Point2::new(0.2_f32, 0.3),
    Point2::new(0.8, 0.3),
    Point2::new(0.8, 0.7),
    Point2::new(0.2, 0.7),
  );

  let around = square.bounds();
  assert_eq!((around.x(), around.y()), (0.2, 0.3));
  assert!((around.width() - 0.6).abs() < 1e-6);
  assert!((around.height() - 0.4).abs() < 1e-6);
}

/// **THE REDUCTION IS THE COMPONENT'S ARITHMETIC**, at a width whose subtraction is exact — the
/// raster world, where a box around four pixel corners is four integers.
#[test]
fn the_bounds_are_taken_in_whatever_the_component_measures_in() {
  let pixels = Quad::new(
    Point2::new(12_u32, 40),
    Point2::new(212, 44),
    Point2::new(208, 190),
    Point2::new(10, 186),
  );

  let around = pixels.bounds();
  assert_eq!((around.x(), around.y()), (10, 40));
  assert_eq!((around.width(), around.height()), (202, 150));
}