From 14e644d6024cfb7bb88ed4280186f6e78ddcce5e Mon Sep 17 00:00:00 2001 From: Derek Sorken Date: Tue, 17 Mar 2026 18:27:36 -0600 Subject: [PATCH] add updated tests --- .../p2p/src/gossipsub/lean/topics.rs | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) diff --git a/crates/networking/p2p/src/gossipsub/lean/topics.rs b/crates/networking/p2p/src/gossipsub/lean/topics.rs index 4b0a7560c..7f96c4cf2 100644 --- a/crates/networking/p2p/src/gossipsub/lean/topics.rs +++ b/crates/networking/p2p/src/gossipsub/lean/topics.rs @@ -115,3 +115,149 @@ impl std::fmt::Display for LeanGossipTopicKind { } } } + +#[cfg(test)] +mod tests { + use libp2p::gossipsub::TopicHash; + + use super::{ + ENCODING_POSTFIX, LEAN_BLOCK_TOPIC, LeanGossipTopic, LeanGossipTopicKind, TOPIC_PREFIX, + }; + use crate::gossipsub::error::GossipsubError; + + #[test] + fn test_gossip_topic_creation() { + let topic = LeanGossipTopic { + kind: LeanGossipTopicKind::Block, + fork: "0x12345678".to_string(), + }; + + assert_eq!( + topic, + LeanGossipTopic { + kind: LeanGossipTopicKind::Block, + fork: "0x12345678".to_string(), + } + ); + assert_eq!( + topic.to_string(), + "/leanconsensus/0x12345678/block/ssz_snappy" + ); + } + + #[test] + fn test_gossip_topic_from_string() { + let topic_str = "/leanconsensus/0x12345678/block/ssz_snappy"; + let topic = TopicHash::from_raw(topic_str); + + assert_eq!( + LeanGossipTopic::from_topic_hash(&topic).unwrap(), + LeanGossipTopic { + kind: LeanGossipTopicKind::Block, + fork: "0x12345678".to_string(), + } + ); + } + + #[test] + fn test_parse_topic_string() { + let topic = TopicHash::from_raw(format!( + "/{TOPIC_PREFIX}/0x12345678/{LEAN_BLOCK_TOPIC}/{ENCODING_POSTFIX}", + )); + + let parsed = LeanGossipTopic::from_topic_hash(&topic).unwrap(); + + assert_eq!(parsed.fork, "0x12345678"); + assert_eq!(parsed.kind, LeanGossipTopicKind::Block); + } + + #[test] + fn test_invalid_topic_string() { + let invalid_topic = TopicHash::from_raw("/invalid/topic"); + let wrong_prefix = TopicHash::from_raw("/wrongprefix/0x123/block/ssz_snappy"); + + let invalid_topic_err = LeanGossipTopic::from_topic_hash(&invalid_topic).unwrap_err(); + assert!(matches!(invalid_topic_err, GossipsubError::InvalidTopic(_))); + assert!( + invalid_topic_err + .to_string() + .contains("Invalid topic format") + ); + + let wrong_prefix_err = LeanGossipTopic::from_topic_hash(&wrong_prefix).unwrap_err(); + assert!(matches!(wrong_prefix_err, GossipsubError::InvalidTopic(_))); + assert!( + wrong_prefix_err + .to_string() + .contains("Invalid topic format") + ); + } + + #[test] + fn test_topic_kind_enum() { + assert_eq!(LeanGossipTopicKind::Block.to_string(), "block"); + assert_eq!( + LeanGossipTopicKind::AttestationSubnet(0).to_string(), + "attestation_0" + ); + } + + #[test] + fn test_validate_fork_success() { + let topic = LeanGossipTopic { + kind: LeanGossipTopicKind::Block, + fork: "0x12345678".to_string(), + }; + + let expected_fork = "0x12345678"; + + assert_eq!(topic.fork, expected_fork); + } + + #[test] + fn test_validate_fork_raises_on_mismatch() { + let topic = LeanGossipTopic { + kind: LeanGossipTopicKind::Block, + fork: "0x12345678".to_string(), + }; + + let expected_fork = "0xtesttopic"; + + assert_ne!(topic.fork, expected_fork); + } + + #[test] + fn test_from_string_validated_success() { + let topic = TopicHash::from_raw("/leanconsensus/0x12345678/block/ssz_snappy"); + + let parsed = LeanGossipTopic::from_topic_hash(&topic).unwrap(); + + assert_eq!( + parsed, + LeanGossipTopic { + kind: LeanGossipTopicKind::Block, + fork: "0x12345678".to_string(), + } + ); + assert_eq!(parsed.fork, "0x12345678"); + } + + #[test] + fn test_from_string_validated_raises_on_mismatch() { + let topic = TopicHash::from_raw("/leanconsensus/0x12345678/block/ssz_snappy"); + + let parsed = LeanGossipTopic::from_topic_hash(&topic).unwrap(); + + assert_ne!(parsed.fork, "0xtesttopic"); + } + + #[test] + fn test_from_string_validated_raises_on_invalid_topic() { + let topic = TopicHash::from_raw("/invalid/topic"); + + let err = LeanGossipTopic::from_topic_hash(&topic).unwrap_err(); + + assert!(matches!(err, GossipsubError::InvalidTopic(_))); + assert!(err.to_string().contains("Invalid topic format")); + } +}