use newt_interaction::{
InteractionDefinition, DEFINITION_SCHEMA_V1, INSTANCE_SCHEMA_V1, RESPONSE_SCHEMA_V1,
};
mod fixtures;
use fixtures::{definition, instance, response};
#[test]
fn the_schema_tag_is_bound_into_identity() {
use content_addressable::{canonical, ContentAddressable, ContentId};
let def = definition();
let bytes = canonical::to_canonical_dagcbor(&def).unwrap();
let id = def.content_id().unwrap();
assert_eq!(ContentId::from_canonical_bytes(&bytes), id);
let at = bytes
.windows(DEFINITION_SCHEMA_V1.len())
.position(|w| w == DEFINITION_SCHEMA_V1.as_bytes())
.expect("the tag is in the encoding");
let mut bumped = bytes.clone();
bumped[at + DEFINITION_SCHEMA_V1.len() - 1] = b'2';
assert_ne!(
ContentId::from_canonical_bytes(&bumped),
id,
"bumping the schema tag must re-address the record"
);
}
#[test]
fn a_freshly_built_record_carries_the_current_tag() {
let def = definition();
assert_eq!(def.schema_tag(), DEFINITION_SCHEMA_V1);
assert_eq!(instance(&def).schema_tag(), INSTANCE_SCHEMA_V1);
assert_eq!(
response(&def, &instance(&def)).schema_tag(),
RESPONSE_SCHEMA_V1
);
}
#[test]
fn the_three_tags_are_distinct() {
let tags = [DEFINITION_SCHEMA_V1, INSTANCE_SCHEMA_V1, RESPONSE_SCHEMA_V1];
let unique: std::collections::BTreeSet<_> = tags.iter().collect();
assert_eq!(unique.len(), tags.len(), "schema tags collide: {tags:?}");
for tag in tags {
assert!(
tag.starts_with("newt.interaction."),
"{tag} breaks convention"
);
assert!(tag.ends_with("/v1"), "{tag} is not versioned");
}
}
#[test]
fn a_definition_round_trips_through_json_with_its_tag() {
let def = definition();
let json = serde_json::to_string(&def).unwrap();
assert!(json.contains(DEFINITION_SCHEMA_V1));
let back: InteractionDefinition = serde_json::from_str(&json).unwrap();
assert_eq!(back, def);
assert_eq!(back.definition_id().unwrap(), def.definition_id().unwrap());
}