diff --git a/nym-gateway-probe/src/mode/mod.rs b/nym-gateway-probe/src/mode/mod.rs index 6992e09413..7048977c77 100644 --- a/nym-gateway-probe/src/mode/mod.rs +++ b/nym-gateway-probe/src/mode/mod.rs @@ -99,3 +99,162 @@ impl std::str::FromStr for TestMode { } } } + +#[cfg(test)] +mod tests { + use super::*; + + // ============ from_flags() tests ============ + + #[test] + fn test_from_flags_default_is_mixnet() { + // All flags false -> Mixnet (default) + assert_eq!( + TestMode::from_flags(false, false, false, false), + TestMode::Mixnet + ); + } + + #[test] + fn test_from_flags_only_wireguard_is_mixnet() { + // only_wireguard still uses mixnet path (WG via authenticator) + assert_eq!( + TestMode::from_flags(true, false, false, false), + TestMode::Mixnet + ); + } + + #[test] + fn test_from_flags_only_lp_registration() { + // only_lp_registration -> LpOnly (takes priority) + assert_eq!( + TestMode::from_flags(false, true, false, false), + TestMode::LpOnly + ); + // Even with other flags set, only_lp_registration wins + assert_eq!( + TestMode::from_flags(true, true, true, true), + TestMode::LpOnly + ); + } + + #[test] + fn test_from_flags_test_lp_wg_single_hop() { + // test_lp_wg without exit gateway -> SingleHop + assert_eq!( + TestMode::from_flags(false, false, true, false), + TestMode::SingleHop + ); + } + + #[test] + fn test_from_flags_test_lp_wg_two_hop() { + // test_lp_wg with exit gateway -> TwoHop + assert_eq!( + TestMode::from_flags(false, false, true, true), + TestMode::TwoHop + ); + } + + #[test] + fn test_from_flags_has_exit_gateway_alone_is_mixnet() { + // has_exit_gateway alone doesn't change mode + assert_eq!( + TestMode::from_flags(false, false, false, true), + TestMode::Mixnet + ); + } + + // ============ Helper method tests ============ + + #[test] + fn test_needs_mixnet() { + assert!(TestMode::Mixnet.needs_mixnet()); + assert!(!TestMode::SingleHop.needs_mixnet()); + assert!(!TestMode::TwoHop.needs_mixnet()); + assert!(!TestMode::LpOnly.needs_mixnet()); + } + + #[test] + fn test_uses_lp() { + assert!(!TestMode::Mixnet.uses_lp()); + assert!(TestMode::SingleHop.uses_lp()); + assert!(TestMode::TwoHop.uses_lp()); + assert!(TestMode::LpOnly.uses_lp()); + } + + #[test] + fn test_tests_wireguard() { + assert!(TestMode::Mixnet.tests_wireguard()); + assert!(TestMode::SingleHop.tests_wireguard()); + assert!(TestMode::TwoHop.tests_wireguard()); + assert!(!TestMode::LpOnly.tests_wireguard()); + } + + #[test] + fn test_needs_exit_gateway() { + assert!(!TestMode::Mixnet.needs_exit_gateway()); + assert!(!TestMode::SingleHop.needs_exit_gateway()); + assert!(TestMode::TwoHop.needs_exit_gateway()); + assert!(!TestMode::LpOnly.needs_exit_gateway()); + } + + // ============ Display tests ============ + + #[test] + fn test_display() { + assert_eq!(TestMode::Mixnet.to_string(), "mixnet"); + assert_eq!(TestMode::SingleHop.to_string(), "single-hop"); + assert_eq!(TestMode::TwoHop.to_string(), "two-hop"); + assert_eq!(TestMode::LpOnly.to_string(), "lp-only"); + } + + // ============ FromStr tests ============ + + #[test] + fn test_from_str_canonical() { + assert_eq!("mixnet".parse::().unwrap(), TestMode::Mixnet); + assert_eq!("single-hop".parse::().unwrap(), TestMode::SingleHop); + assert_eq!("two-hop".parse::().unwrap(), TestMode::TwoHop); + assert_eq!("lp-only".parse::().unwrap(), TestMode::LpOnly); + } + + #[test] + fn test_from_str_alternate_formats() { + // snake_case + assert_eq!("single_hop".parse::().unwrap(), TestMode::SingleHop); + assert_eq!("two_hop".parse::().unwrap(), TestMode::TwoHop); + assert_eq!("lp_only".parse::().unwrap(), TestMode::LpOnly); + + // no separator + assert_eq!("singlehop".parse::().unwrap(), TestMode::SingleHop); + assert_eq!("twohop".parse::().unwrap(), TestMode::TwoHop); + assert_eq!("lponly".parse::().unwrap(), TestMode::LpOnly); + } + + #[test] + fn test_from_str_case_insensitive() { + assert_eq!("MIXNET".parse::().unwrap(), TestMode::Mixnet); + assert_eq!("Single-Hop".parse::().unwrap(), TestMode::SingleHop); + assert_eq!("TWO_HOP".parse::().unwrap(), TestMode::TwoHop); + assert_eq!("LpOnly".parse::().unwrap(), TestMode::LpOnly); + } + + #[test] + fn test_from_str_invalid() { + assert!("invalid".parse::().is_err()); + assert!("".parse::().is_err()); + assert!("mix".parse::().is_err()); + } + + // ============ Roundtrip test ============ + + #[test] + fn test_display_fromstr_roundtrip() { + for mode in [TestMode::Mixnet, TestMode::SingleHop, TestMode::TwoHop, TestMode::LpOnly] { + let s = mode.to_string(); + let parsed: TestMode = s.parse().unwrap(); + assert_eq!(mode, parsed); + } + } +}