#[cfg(feature = "serde")]
#[cfg(test)]
mod tests {
use data_encoding::BASE64;
use scte35::*;
#[test]
fn test_complete_message_serialization() {
let base64_message = "/DAWAAAAAAAAAP/wBQb+Qjo1vQAAuwxz9A==";
let buffer = BASE64.decode(base64_message.as_bytes()).unwrap();
let section = parse_splice_info_section(&buffer).unwrap();
let json = serde_json::to_string_pretty(§ion).unwrap();
println!("JSON: {}", json);
assert!(json.contains("\"table_id\": 252"));
assert!(json.contains("\"type\": \"TimeSignal\""));
assert!(json.contains("\"splice_time\""));
assert!(json.contains("\"pts_time\""));
assert!(json.contains("\"time_specified_flag\""));
assert!(json.contains("\"duration_info\""));
assert!(json.contains("\"ticks\""));
assert!(json.contains("\"human_readable\""));
let deserialized: SpliceInfoSection = serde_json::from_str(&json).unwrap();
assert_eq!(section.table_id, deserialized.table_id);
assert_eq!(
section.splice_command_type,
deserialized.splice_command_type
);
assert_eq!(section.crc_32, deserialized.crc_32);
}
#[test]
fn test_segmentation_descriptor_json() {
let base64_message = "/DAvAAAAAAAA///wBQb+dGKQoAAZAhdDVUVJSAAAjn+fCAgAAAAALKChijUCAKnMZ1g=";
let buffer = BASE64.decode(base64_message.as_bytes()).unwrap();
let section = parse_splice_info_section(&buffer).unwrap();
let json = serde_json::to_string_pretty(§ion).unwrap();
println!("Segmentation JSON: {}", json);
assert!(json.contains("\"splice_descriptors\""));
assert!(json.contains("\"descriptor_type\": \"Segmentation\""));
assert!(json.contains("\"segmentation_event_id\""));
assert!(json.contains("\"segmentation_type\""));
assert!(json.contains("\"segmentation_type_id\": 53")); assert!(json.contains("\"description\": \"Provider Placement Opportunity End\""));
assert!(json.contains("\"segmentation_upid\":"));
}
#[test]
fn test_binary_fields_base64() {
use scte35::types::PrivateCommand;
let private_cmd = PrivateCommand {
private_command_id: 0xABCD,
private_command_length: 4,
private_bytes: vec![0xDE, 0xAD, 0xBE, 0xEF],
};
let json = serde_json::to_string(&private_cmd).unwrap();
assert!(json.contains("\"private_bytes\":\"3q2+7w==\""));
let deserialized: PrivateCommand = serde_json::from_str(&json).unwrap();
assert_eq!(private_cmd.private_bytes, deserialized.private_bytes);
}
#[test]
fn test_enum_serialization() {
let seg_type = SegmentationType::ProviderAdvertisementStart;
let json = serde_json::to_string(&seg_type).unwrap();
assert_eq!(
json,
"{\"id\":48,\"description\":\"Provider Advertisement Start\"}"
);
let upid_type = SegmentationUpidType::AdID;
let json = serde_json::to_string(&upid_type).unwrap();
assert_eq!(json, "{\"value\":3,\"description\":\"Ad Identifier\"}");
}
#[test]
fn test_mpu_upid_serialization() {
let base64_message = "/DAsAAAAAAAAAP/wBQb+7YaD1QAWAhRDVUVJAADc8X+/DAVPVkxZSSIAAJ6Gk2Q=";
let buffer = BASE64.decode(base64_message.as_bytes()).unwrap();
let section = parse_splice_info_section(&buffer).unwrap();
let json = serde_json::to_string_pretty(§ion).unwrap();
println!("MPU UPID JSON: {}", json);
assert!(json.contains("\"segmentation_upid_type\": {"));
assert!(json.contains("\"value\": 12")); assert!(json.contains("\"description\": \"MPU (Media Processing Unit)\""));
assert!(json.contains("\"segmentation_upid\": \"T1ZMWUk=\""));
assert!(json.contains("\"upid_string\": \"OVLYI\""));
assert!(json.contains("\"segmentation_type_id\": 34")); assert!(json.contains("\"description\": \"Break Start\""));
let deserialized: scte35::SpliceInfoSection = serde_json::from_str(&json).unwrap();
assert_eq!(section.crc_32, deserialized.crc_32);
}
}