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
//! The VIDEO citizen's grammar, asserted where the grammar is.
//!
//! This file carries the claims that are about the DOOR — the fold, the totality, the
//! re-canonicalisation, the order — and it carries them for the house: the other two citizens are
//! the same macro over a different roster, so their own files assert what is theirs (the roster, the
//! separation) and leave the shared sentence here rather than restating it three times.

use core::str::FromStr as _;

use mediaframe::codec::VideoCodec as Foreign;

use super::VideoCodec;

/// **THE CANONICAL FORM IS THE LOWERCASE SLUG**, and it is what every face publishes.
#[test]
fn the_canonical_form_is_the_lowercase_slug() {
  assert_eq!(VideoCodec::new("h264").as_str(), "h264");
  assert_eq!(VideoCodec::new("H264").as_str(), "h264");
  assert_eq!(VideoCodec::new("prores").as_str(), "prores");
  assert_eq!(VideoCodec::new("AV1").as_str(), "av1");
}

/// **THE DOOR FOLDS EVERY SPELLING OF ONE CODEC ONTO ONE VALUE**, which is what makes a stored
/// column canonical and a query operand meet it without either side knowing the other's case.
#[test]
fn the_door_folds_every_spelling_of_one_codec() {
  // `prores` rather than `h264` because it is ALL letters: a slug that is mostly digits has only
  // two spellings to fold, which would leave the fold barely exercised.
  let canonical = VideoCodec::new("prores");

  for sent in ["prores", "ProRes", "PRORES", "pRoReS"] {
    assert_eq!(VideoCodec::new(sent), canonical, "`{sent}` is one value");
  }

  assert_eq!(VideoCodec::new("H264"), VideoCodec::new("h264"));
  assert_eq!(VideoCodec::new("hEvC"), VideoCodec::new("hevc"));
}

/// **A NAME THE ROSTER DOES NOT CARRY IS ADMITTED, AND NAMED AS SUCH** — the open vocabulary's
/// bargain, with [`is_registered`](VideoCodec::is_registered) as the question that is actually about
/// the table.
#[test]
fn a_name_the_roster_does_not_carry_is_admitted_and_named_as_such() {
  let known = VideoCodec::new("h264");
  assert!(known.is_registered());

  let unknown = VideoCodec::new("some_codec_9000");
  assert!(!unknown.is_registered());
  assert_eq!(
    unknown.as_str(),
    "some_codec_9000",
    "the escape is LOSSLESS — the name a muxer wrote is the name that comes back",
  );

  // And it folds on the way in like any other, so the escape is canonical too.
  assert_eq!(VideoCodec::new("SOME_CODEC_9000"), unknown);
}

/// **THE DOOR IS TOTAL** — every string is a codec, which is the property the whole house's
/// infallibility runs on.
///
/// The strings here are the ones a fallible door would have refused: empty, non-ASCII, punctuation,
/// and something no vocabulary would call a name.
#[test]
fn the_door_is_total_over_every_string() {
  for sent in ["", " ", "!!!", "日本語", "not a codec at all", "\u{0}"] {
    let codec = VideoCodec::new(sent);
    assert!(!codec.is_registered(), "`{sent}` names no roster member");
    assert_eq!(
      codec,
      VideoCodec::from_str(sent).expect("the parse is infallible"),
      "`{sent}` reaches one value through both doors",
    );
  }

  // The empty string is a value like any other, and it is NOT the roster's `und`-shaped hole: this
  // vocabulary has no such member, and an absent codec is a `NULL` column.
  assert_eq!(VideoCodec::new("").as_str(), "");
}

/// **THE FOREIGN VALUE IS RE-CANONICALISED ON THE WAY IN**, and this is the claim the conversion
/// exists to make rather than a restatement of the fold.
///
/// Upstream admits a value this house's door would never mint: `VideoCodec::other("h264")` is an
/// escape arm holding text a named variant already spells, and upstream's derived equality calls the
/// two DIFFERENT. If the conversion moved the bytes, that value would sit inside a citizen comparing
/// unequal to the named variant while [`Ord`](core::cmp::Ord) — which reads the slug — called them
/// equal, and the two rows would contradict each other.
#[test]
fn the_foreign_value_is_recanonicalised_on_the_way_in() {
  let sideways = Foreign::other("h264");
  assert_ne!(
    sideways,
    Foreign::H264,
    "upstream holds these apart, which is the premise this test is about",
  );

  let crossed = VideoCodec::from(sideways);
  assert_eq!(
    crossed,
    VideoCodec::new("h264"),
    "the door re-runs the slug table, so only the named spelling exists in here",
  );
  assert!(crossed.is_registered());
  assert_eq!(crossed.into_inner(), Foreign::H264);

  // A name that truly is not on the roster crosses unchanged, escape and all.
  let genuine = Foreign::other("some_codec_9000");
  assert_eq!(
    VideoCodec::from(genuine.clone()).into_inner(),
    genuine,
    "re-canonicalising is not the same as flattening — an unknown name stays itself",
  );
}

/// **ORDERING IS ALPHABETICAL OVER THE CANONICAL SPELLING**, and says nothing about the codecs.
#[test]
fn ordering_is_alphabetical_over_the_canonical_spelling() {
  let mut codecs = [
    VideoCodec::new("prores"),
    VideoCodec::new("av1"),
    VideoCodec::new("h264"),
    VideoCodec::new("vp9"),
  ];
  codecs.sort();

  let order: Vec<&str> = codecs.iter().map(VideoCodec::as_str).collect();
  assert_eq!(order, ["av1", "h264", "prores", "vp9"]);

  // The case a caller sent does not move a value in the order, the fold having happened first.
  assert!(VideoCodec::new("AV1") < VideoCodec::new("h264"));
}

/// **THE ORDER AND THE EQUALITY AGREE OVER THE WHOLE VALUE SPACE**, which is the consistency
/// contract [`Ord`](core::cmp::Ord) carries and the one the door's fold is what secures.
///
/// The pairs are chosen to hit the case that could break it: a named variant against the escape arm
/// spelling the same name. Inside a citizen that second value cannot exist, so `cmp` answering
/// `Equal` and `eq` answering `true` is not a coincidence — it is the door.
#[test]
fn the_order_and_the_equality_agree_over_the_whole_value_space() {
  let pairs = [
    ("h264", "h264"),
    ("H264", "h264"),
    ("h264", "av1"),
    ("some_codec_9000", "some_codec_9000"),
    ("some_codec_9000", "h264"),
    ("", ""),
    ("", "h264"),
  ];

  for (left, right) in pairs {
    let left = VideoCodec::new(left);
    let right = VideoCodec::new(right);

    assert_eq!(
      left == right,
      left.cmp(&right).is_eq(),
      "`{left}` and `{right}` — equality and order must call the same pairs equal",
    );
  }

  // And the crossing from upstream cannot smuggle a disagreeing value in either.
  let sideways = VideoCodec::from(Foreign::other("h264"));
  let named = VideoCodec::new("h264");
  assert!(sideways == named && sideways.cmp(&named).is_eq());
}

/// **EVERY ROSTER MEMBER IS A FIXPOINT OF THE DOOR** — the whole vendored table, walked.
///
/// It is the assertion that keeps the fold and the slug table in step for real rather than for the
/// dozen names a test would otherwise have picked: a codec whose `as_str` did not parse back to
/// itself would be a value that stored and read back as something else.
#[test]
fn every_roster_member_is_a_fixpoint_of_the_door() {
  assert!(
    !Foreign::ROSTER.is_empty(),
    "the vendored table is not empty"
  );

  for member in Foreign::ROSTER {
    let citizen = VideoCodec::from(member.clone());

    assert_eq!(
      citizen.get(),
      member,
      "`{}` crosses into the citizen unchanged",
      member.as_str(),
    );
    assert!(
      citizen.is_registered(),
      "`{}` is on the roster and must say so",
      member.as_str(),
    );
    assert_eq!(
      VideoCodec::new(citizen.as_str()),
      citizen,
      "`{}` is a fixpoint of the door",
      member.as_str(),
    );
    assert_eq!(
      VideoCodec::new(&member.as_str().to_ascii_uppercase()),
      citizen,
      "`{}` folds from its uppercase spelling",
      member.as_str(),
    );
  }
}

/// **THE RENDERING AND THE PARSE ARE INVERSE**, in both directions and through both text faces.
#[test]
fn the_rendering_and_the_parse_are_inverse() {
  for sent in ["h264", "H264", "av1", "some_codec_9000", ""] {
    let codec = VideoCodec::new(sent);
    let rendered = codec.to_string();

    assert_eq!(VideoCodec::new(&rendered), codec, "`{sent}` round trips");
    assert_eq!(rendered, codec.as_str(), "`Display` IS the canonical slug");
  }
}

/// **THE DEBUG FACE PRINTS THE SLUG** — quoted, and with no type name or variant path around it.
#[test]
fn the_debug_face_prints_the_slug() {
  assert_eq!(format!("{:?}", VideoCodec::new("H264")), "\"h264\"");
  assert_eq!(
    format!("{:?}", VideoCodec::new("some_codec_9000")),
    "\"some_codec_9000\"",
    "the escape arm prints as its text and not as `Other(…)`",
  );
}

/// **THE TEXT SEAT CONVERTS BOTH WAYS**, and the upstream seat does too.
#[test]
fn the_text_seat_converts_both_ways() {
  let codec = VideoCodec::new("H264");

  assert_eq!(AsRef::<str>::as_ref(&codec), "h264");
  assert_eq!(String::from(codec.clone()), String::from("h264"));
  assert_eq!(Foreign::from(codec.clone()), Foreign::H264);
  assert_eq!(VideoCodec::from(Foreign::H264), codec);
}