Skip to content

Commit c980a45

Browse files
authored
refactor: improve catalyst id parse test coverage (#311)
1 parent cb8f3de commit c980a45

File tree

1 file changed

+21
-0
lines changed
  • rust/catalyst-types/src/catalyst_id

1 file changed

+21
-0
lines changed

rust/catalyst-types/src/catalyst_id/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ impl TryFrom<&[u8]> for CatalystId {
678678

679679
#[cfg(test)]
680680
mod tests {
681+
use chrono::{DateTime, Utc};
681682
use ed25519_dalek::SigningKey;
682683
use rand::rngs::OsRng;
683684

@@ -699,8 +700,28 @@ mod tests {
699700
/// Tests that deserialization and re-serialization round trip correctly
700701
fn test_catalyst_id_from_str() {
701702
for id_string in CATALYST_ID_TEST_VECTOR {
703+
let username = id_string.split_once('@').map(|s| s.0);
704+
let (username, nonce) = username
705+
.and_then(|s| s.split_once(':').map(|(u, n)| (u, Some(n))))
706+
.or_else(|| username.map(|u| (u, None)))
707+
.unzip();
708+
709+
let nonce = nonce.flatten();
710+
let username = username.map(String::from);
711+
let nonce = nonce
712+
.map(|n| n.parse::<i64>().unwrap())
713+
.map(|n| DateTime::<Utc>::from_timestamp(n, 0).unwrap());
714+
715+
let encryption =
716+
id_string.contains(format!("#{}", CatalystId::ENCRYPTION_FRAGMENT).as_str());
717+
702718
let id = id_string.parse::<CatalystId>().unwrap();
719+
703720
assert_eq!(format!("{id}"), id_string);
721+
assert_eq!(username, id.username());
722+
assert_eq!(nonce, id.nonce());
723+
assert!(id.is_signature_key() ^ encryption);
724+
assert!(id.is_encryption_key() ^ !encryption);
704725
}
705726
}
706727

0 commit comments

Comments
 (0)