diff --git a/crates/node/config/src/execution.rs b/crates/node/config/src/execution.rs index 55d77b4..17efdfb 100644 --- a/crates/node/config/src/execution.rs +++ b/crates/node/config/src/execution.rs @@ -33,3 +33,58 @@ const fn default_gas_limit() -> u64 { const fn default_block_time() -> u64 { DEFAULT_BLOCK_TIME } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_default_execution_config() { + let config = ExecutionConfig::default(); + assert_eq!(config.gas_limit, DEFAULT_GAS_LIMIT); + assert_eq!(config.block_time, DEFAULT_BLOCK_TIME); + } + + #[test] + fn test_execution_config_serde_roundtrip() { + let config = ExecutionConfig { gas_limit: 50_000_000, block_time: 5 }; + let serialized = serde_json::to_string(&config).expect("serialize"); + let deserialized: ExecutionConfig = serde_json::from_str(&serialized).expect("deserialize"); + assert_eq!(config, deserialized); + } + + #[test] + fn test_execution_config_toml_roundtrip() { + let config = ExecutionConfig { gas_limit: 15_000_000, block_time: 1 }; + let serialized = toml::to_string(&config).expect("serialize toml"); + let deserialized: ExecutionConfig = toml::from_str(&serialized).expect("deserialize toml"); + assert_eq!(config, deserialized); + } + + #[test] + fn test_execution_config_serde_defaults() { + let config: ExecutionConfig = serde_json::from_str("{}").expect("deserialize"); + assert_eq!(config.gas_limit, DEFAULT_GAS_LIMIT); + assert_eq!(config.block_time, DEFAULT_BLOCK_TIME); + } + + #[test] + fn test_execution_config_partial_defaults() { + let config: ExecutionConfig = + serde_json::from_str(r#"{"gas_limit": 10000000}"#).expect("deserialize"); + assert_eq!(config.gas_limit, 10_000_000); + assert_eq!(config.block_time, DEFAULT_BLOCK_TIME); + + let config: ExecutionConfig = + serde_json::from_str(r#"{"block_time": 10}"#).expect("deserialize"); + assert_eq!(config.gas_limit, DEFAULT_GAS_LIMIT); + assert_eq!(config.block_time, 10); + } + + #[test] + fn test_execution_config_clone_and_eq() { + let config = ExecutionConfig { gas_limit: 999, block_time: 42 }; + assert_eq!(config, config.clone()); + assert_ne!(config, ExecutionConfig::default()); + } +} diff --git a/crates/node/config/src/network.rs b/crates/node/config/src/network.rs index 3df1afe..497b165 100644 --- a/crates/node/config/src/network.rs +++ b/crates/node/config/src/network.rs @@ -35,3 +35,76 @@ impl Default for NetworkConfig { fn default_listen_addr() -> String { DEFAULT_LISTEN_ADDR.to_string() } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_default_network_config() { + let config = NetworkConfig::default(); + assert_eq!(config.listen_addr, DEFAULT_LISTEN_ADDR); + assert!(config.dialable_addr.is_none()); + assert!(config.bootstrap_peers.is_empty()); + } + + #[test] + fn test_network_config_serde_roundtrip() { + let config = NetworkConfig { + listen_addr: "127.0.0.1:9000".to_string(), + dialable_addr: Some("1.2.3.4:9000".to_string()), + bootstrap_peers: vec!["peer1:30303".to_string()], + }; + let serialized = serde_json::to_string(&config).expect("serialize"); + let deserialized: NetworkConfig = serde_json::from_str(&serialized).expect("deserialize"); + assert_eq!(config, deserialized); + } + + #[test] + fn test_network_config_toml_roundtrip() { + let config = NetworkConfig { + listen_addr: "0.0.0.0:8080".to_string(), + dialable_addr: None, + bootstrap_peers: vec!["node1.example.com:30303".to_string()], + }; + let serialized = toml::to_string(&config).expect("serialize toml"); + let deserialized: NetworkConfig = toml::from_str(&serialized).expect("deserialize toml"); + assert_eq!(config, deserialized); + } + + #[test] + fn test_network_config_serde_defaults() { + let config: NetworkConfig = serde_json::from_str("{}").expect("deserialize"); + assert_eq!(config.listen_addr, DEFAULT_LISTEN_ADDR); + assert!(config.dialable_addr.is_none()); + assert!(config.bootstrap_peers.is_empty()); + } + + #[test] + fn test_network_config_dialable_addr_skip_serializing_when_none() { + let config = NetworkConfig::default(); + let serialized = serde_json::to_string(&config).expect("serialize"); + assert!(!serialized.contains("dialable_addr")); + } + + #[test] + fn test_network_config_dialable_addr_serialized_when_some() { + let config = NetworkConfig { + dialable_addr: Some("1.2.3.4:30303".to_string()), + ..Default::default() + }; + let serialized = serde_json::to_string(&config).expect("serialize"); + assert!(serialized.contains("dialable_addr")); + } + + #[test] + fn test_network_config_clone_and_eq() { + let config = NetworkConfig { + listen_addr: "10.0.0.1:5555".to_string(), + dialable_addr: Some("external.host:5555".to_string()), + bootstrap_peers: vec!["a".to_string()], + }; + assert_eq!(config, config.clone()); + assert_ne!(config, NetworkConfig::default()); + } +} diff --git a/crates/node/config/src/rpc.rs b/crates/node/config/src/rpc.rs index e4d5ebc..53bb469 100644 --- a/crates/node/config/src/rpc.rs +++ b/crates/node/config/src/rpc.rs @@ -33,3 +33,65 @@ fn default_http_addr() -> String { fn default_ws_addr() -> String { DEFAULT_WS_ADDR.to_string() } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_default_rpc_config() { + let config = RpcConfig::default(); + assert_eq!(config.http_addr, DEFAULT_HTTP_ADDR); + assert_eq!(config.ws_addr, DEFAULT_WS_ADDR); + } + + #[test] + fn test_rpc_config_serde_roundtrip() { + let config = RpcConfig { + http_addr: "127.0.0.1:8080".to_string(), + ws_addr: "127.0.0.1:8081".to_string(), + }; + let serialized = serde_json::to_string(&config).expect("serialize"); + let deserialized: RpcConfig = serde_json::from_str(&serialized).expect("deserialize"); + assert_eq!(config, deserialized); + } + + #[test] + fn test_rpc_config_toml_roundtrip() { + let config = RpcConfig { + http_addr: "0.0.0.0:9545".to_string(), + ws_addr: "0.0.0.0:9546".to_string(), + }; + let serialized = toml::to_string(&config).expect("serialize toml"); + let deserialized: RpcConfig = toml::from_str(&serialized).expect("deserialize toml"); + assert_eq!(config, deserialized); + } + + #[test] + fn test_rpc_config_serde_defaults() { + let config: RpcConfig = serde_json::from_str("{}").expect("deserialize"); + assert_eq!(config.http_addr, DEFAULT_HTTP_ADDR); + assert_eq!(config.ws_addr, DEFAULT_WS_ADDR); + } + + #[test] + fn test_rpc_config_partial_defaults() { + let config: RpcConfig = + serde_json::from_str(r#"{"http_addr": "1.2.3.4:8545"}"#).expect("deserialize"); + assert_eq!(config.http_addr, "1.2.3.4:8545"); + assert_eq!(config.ws_addr, DEFAULT_WS_ADDR); + + let config: RpcConfig = + serde_json::from_str(r#"{"ws_addr": "5.6.7.8:8546"}"#).expect("deserialize"); + assert_eq!(config.http_addr, DEFAULT_HTTP_ADDR); + assert_eq!(config.ws_addr, "5.6.7.8:8546"); + } + + #[test] + fn test_rpc_config_clone_and_eq() { + let config = + RpcConfig { http_addr: "custom:1111".to_string(), ws_addr: "custom:2222".to_string() }; + assert_eq!(config, config.clone()); + assert_ne!(config, RpcConfig::default()); + } +}