///|
/// Recompute one receipt from its original Delta input and this trusted prior
/// session. Compare progress, duplicates, complete output and complete logical
/// checkpoint, including untouched streams and capacity. On mismatch no next
/// session is returned. This proves correspondence to the supplied inputs, not
/// their authenticity, downstream delivery, or an unprovided recording prefix.
///
/// ```mbt check
/// test {
///   let s = @metricstitch.ReplaySession::new(@metricstitch.State::new())
///   let line = "{\"resourceMetrics\":[]}"
///   let record = s.apply_line(line)
///   let verified = s.verify_record(line, record.receipt())
///   assert_eq(verified.checkpoint(), record.session.checkpoint())
///   assert_eq(s.record_count(), 0L)
/// }
/// ```
pub fn ReplaySession::verify_record(
  self : ReplaySession,
  line : String,
  receipt : Json,
) -> ReplaySession raise StitchError {
  let expected = self.apply_line(line)
  let path = "/records/" + expected.ordinal.to_string()
  let actual = decode_replay_record(receipt) catch {
    Invalid(message) => raise Invalid(path + ": " + message)
  }
  if actual.ordinal != expected.ordinal {
    raise Invalid(path + "/ordinal: receipt does not follow the prior session")
  }
  if actual.duplicates != expected.duplicates {
    raise Invalid(path + "/duplicates: receipt differs from recomputed input")
  }
  if actual.request != expected.request {
    raise Invalid(
      path + "/request: receipt differs from recomputed cumulative output",
    )
  }
  if actual.session.checkpoint() != expected.session.checkpoint() {
    raise Invalid(
      path + "/session/state: receipt differs from recomputed checkpoint",
    )
  }
  expected.session
}