use super::protected_fields::PROTECTED_PATCH_FIELDS;
use crate::passport::PASSPORT_WIRE_KEYS;
use crate::test_support::fully_populated_passport;
#[test]
fn every_protected_key_is_a_real_passport_field() {
let passport = fully_populated_passport();
let json = serde_json::to_value(&passport).expect("passport serialises");
let obj = json.as_object().expect("passport is a JSON object");
let stale: Vec<&str> = PROTECTED_PATCH_FIELDS
.iter()
.copied()
.filter(|k| !obj.contains_key(*k))
.collect();
assert!(
stale.is_empty(),
"PROTECTED_PATCH_FIELDS names keys `Passport` does not serialise to: {stale:?}\n\
Either the field was renamed and this list was not, or \
`fully_populated_passport` does not populate it. Both mean the field is \
unprotected."
);
}
#[test]
fn protected_keys_are_unique() {
let mut seen = std::collections::BTreeSet::new();
for key in PROTECTED_PATCH_FIELDS {
assert!(seen.insert(*key), "duplicate protected key: {key}");
}
}
const DELIBERATELY_PATCHABLE: &[&str] = &[
"co2ePerUnit",
"complianceResult",
"lintResult",
"productGroupData",
"productName",
"repairabilityScore",
];
#[test]
fn every_wire_key_is_classified() {
let unclassified: Vec<&str> = PASSPORT_WIRE_KEYS
.iter()
.copied()
.filter(|k| !PROTECTED_PATCH_FIELDS.contains(k) && !DELIBERATELY_PATCHABLE.contains(k))
.collect();
assert!(
unclassified.is_empty(),
"envelope keys are neither protected nor deliberately patchable: {unclassified:?}\n\
A new `Passport` field must be classified. Add it to PROTECTED_PATCH_FIELDS \
if a free-form patch must never reach it — which is the right answer for \
anything the state machine, the publish pipeline, the registry or record \
identity owns — or to DELIBERATELY_PATCHABLE, with the reason."
);
let both: Vec<&str> = DELIBERATELY_PATCHABLE
.iter()
.copied()
.filter(|k| PROTECTED_PATCH_FIELDS.contains(k))
.collect();
assert!(
both.is_empty(),
"keys are in both lists, so the classification contradicts itself: {both:?}"
);
}
#[test]
fn deliberately_patchable_keys_are_real_wire_keys() {
let stale: Vec<&str> = DELIBERATELY_PATCHABLE
.iter()
.copied()
.filter(|k| !PASSPORT_WIRE_KEYS.contains(k))
.collect();
assert!(
stale.is_empty(),
"DELIBERATELY_PATCHABLE names keys that are not `Passport` wire keys: {stale:?}"
);
}