use rust_network_mgr::{config, network::NetworkMonitor, nftables::NftablesManager, socket::SocketHandler, types::{ControlCommand, NetworkEvent}};
use std::collections::HashMap;
use std::io::Write;
use std::net::IpAddr;
use tempfile::NamedTempFile;
use tokio::sync::mpsc;
fn create_dummy_config_file() -> NamedTempFile {
let yaml = r#"
interfaces:
- name: lo
dhcp: false
address: 127.0.0.1/8
nftables_zone: local
- name: eth_test
dhcp: true
nftables_zone: wan
socket_path: /tmp/rust-net-test.sock
nftables_rules_path: /tmp/dummy_rules.nft
"#;
let mut file = NamedTempFile::new().unwrap();
writeln!(file, "{}", yaml).unwrap();
file
}
#[tokio::test]
async fn test_config_loading_integration() {
let config_file = create_dummy_config_file();
let result = config::load_config(Some(config_file.path()));
assert!(result.is_ok());
let config = result.unwrap();
assert_eq!(config.interfaces.len(), 2);
assert_eq!(config.interfaces[0].name, "lo");
assert_eq!(config.interfaces[1].nftables_zone, Some("wan".to_string()));
assert!(config::validate_config(&config).is_ok());
}
#[tokio::test]
async fn test_component_instantiation() {
let (network_tx, _network_rx) = mpsc::channel::<NetworkEvent>(1);
let (control_tx, _control_rx) = mpsc::channel::<ControlCommand>(1);
let config_file = create_dummy_config_file();
let config = config::load_config(Some(config_file.path())).expect("Failed to load dummy config");
let _monitor = NetworkMonitor::new(network_tx);
let _nft_manager = NftablesManager::new(config.clone());
let socket_result = SocketHandler::new(config.socket_path.as_deref(), control_tx).await;
if let Some(path) = config.socket_path {
let _ = std::fs::remove_file(path); }
assert!(socket_result.is_ok(), "SocketHandler creation failed: {:?}", socket_result.err());
println!("Basic component instantiation successful.");
}