use prism::pipeline::{ConstrainedTypeShape, EmptyCommitment, IntoBindingValue};
use uor_addr::common::{AddressInput, AddressLabel};
use uor_addr::json::address;
use uor_addr::json::verbs::VERB_TERMS_ADDRESS_INFERENCE;
use uor_addr::json::{canonicalize, JsonCarrier, JsonValue};
use uor_addr::{ADDRESS_LABEL_BYTES, ADDR_INLINE_BYTES};
#[test]
fn json_value_is_address_input() {
fn assert_address_input<'a, V: AddressInput<'a>>() {}
assert_address_input::<JsonCarrier<'_>>();
}
#[test]
fn address_input_supertraits_are_satisfied_by_json_value() {
fn requires_constrained_type_shape<V: ConstrainedTypeShape>() {}
fn requires_into_binding_value<'a, V: IntoBindingValue<'a>>() {}
requires_constrained_type_shape::<JsonCarrier<'_>>();
requires_into_binding_value::<JsonCarrier<'_>>();
}
#[test]
fn json_value_parse_accepts_well_formed_input() {
let raw = br#"{"foo":"bar"}"#;
let value = JsonValue::parse(raw).expect("parse succeeds");
assert!(!value.tagged_bytes().is_empty());
}
#[test]
fn json_value_parse_surfaces_typed_input_violations() {
let err = JsonValue::parse(b"not json").expect_err("must reject");
assert!(err.constraint_iri.ends_with("/validUtf8Json"));
}
#[test]
fn json_canonicalize_writes_jcs_nfc_canonical_bytes() {
let canonical = canonicalize(br#"{"b": 1, "a": 2}"#).expect("canonicalizes");
assert_eq!(
&canonical[..],
br#"{"a":2,"b":1}"#,
"JCS §3.2.3 re-orders keys lexicographically"
);
}
#[test]
fn common_verb_arena_composes_only_psi_term_variants() {
use prism::operation::Term;
let arena = VERB_TERMS_ADDRESS_INFERENCE::<{ ADDR_INLINE_BYTES }>();
assert!(!arena.is_empty(), "verb arena is non-empty");
let has_nerve = arena.iter().any(|t| matches!(t, Term::Nerve { .. }));
let has_postnikov = arena
.iter()
.any(|t| matches!(t, Term::PostnikovTower { .. }));
let has_homotopy = arena
.iter()
.any(|t| matches!(t, Term::HomotopyGroups { .. }));
let has_kinvariants = arena.iter().any(|t| matches!(t, Term::KInvariants { .. }));
assert!(
has_nerve && has_postnikov && has_homotopy && has_kinvariants,
"verb arena must contain ψ_1 + ψ_7 + ψ_8 + ψ_9"
);
let has_chain = arena.iter().any(|t| matches!(t, Term::ChainComplex { .. }));
let has_homology = arena
.iter()
.any(|t| matches!(t, Term::HomologyGroups { .. }));
let has_cochain = arena
.iter()
.any(|t| matches!(t, Term::CochainComplex { .. }));
let has_cohomology = arena
.iter()
.any(|t| matches!(t, Term::CohomologyGroups { .. }));
assert!(
!has_chain && !has_homology && !has_cochain && !has_cohomology,
"off-path ψ-stages must not appear in the verb body"
);
}
#[test]
fn common_output_shape_iri_matches_architectural_commitment() {
assert_eq!(
<AddressLabel as ConstrainedTypeShape>::IRI,
"https://uor.foundation/addr/AddressLabel/sha256"
);
}
#[test]
fn common_output_shape_site_count_matches_structural_formula_for_sha256() {
let identifier_len = b"sha256".len();
let separator_len = 1; let digest_bytes = 32; let hex_serialization_factor = 2;
let expected = identifier_len + separator_len + hex_serialization_factor * digest_bytes;
assert_eq!(expected, 71);
assert_eq!(
<AddressLabel as ConstrainedTypeShape>::SITE_COUNT,
ADDRESS_LABEL_BYTES,
"AddressLabel's SITE_COUNT matches the wire-format byte width"
);
assert_eq!(ADDRESS_LABEL_BYTES, expected);
}
#[test]
fn common_cost_model_default_is_empty_commitment() {
fn assert_empty_commitment_is_typed_commitment<C: prism::pipeline::TypedCommitment>() {}
assert_empty_commitment_is_typed_commitment::<EmptyCommitment>();
}
#[test]
fn end_to_end_kappa_derivation_via_common_surface() {
let raw = br#"{"hello":"world"}"#;
let outcome = address(raw).expect("well-formed input yields κ-label");
assert_eq!(outcome.address.len(), ADDRESS_LABEL_BYTES);
assert!(outcome.address.starts_with("sha256:"));
for &b in &outcome.address.as_bytes()[7..] {
assert!(
b.is_ascii_digit() || (b'a'..=b'f').contains(&b),
"hex digit at output is lowercase-hex ASCII"
);
}
}
#[test]
fn end_to_end_kappa_derivation_is_deterministic() {
let raw = br#"{"determinism":"check","array":[1,2,3]}"#;
let a = address(raw).expect("κ-label").address;
let b = address(raw).expect("κ-label").address;
let c = address(raw).expect("κ-label").address;
assert_eq!(a, b);
assert_eq!(b, c);
}
#[test]
fn end_to_end_kappa_derivation_distinguishes_typed_distinctions() {
let false_label = address(b"false").expect("κ-label").address;
let true_label = address(b"true").expect("κ-label").address;
let null_label = address(b"null").expect("κ-label").address;
let zero_label = address(b"0").expect("κ-label").address;
let empty_string_label = address(br#""""#).expect("κ-label").address;
assert_ne!(false_label, true_label);
assert_ne!(false_label, null_label);
assert_ne!(false_label, zero_label);
assert_ne!(false_label, empty_string_label);
assert_ne!(true_label, null_label);
assert_ne!(null_label, zero_label);
assert_ne!(zero_label, empty_string_label);
}