product-os-command-control 0.0.27

Product OS : Command and Control provides a set of tools for running command and control across a distributed set of Product OS : Servers.
Documentation
// Unit tests for registry module - Node struct

use product_os_command_control::Node;
use std::collections::BTreeMap;

mod test_helpers;
use test_helpers::{create_test_certificates, create_test_configuration};

#[test]
fn test_node_creation() {
    let config = create_test_configuration();
    let certs = create_test_certificates();
    
    let node = Node::default(&config, certs);
    
    assert!(!node.get_identifier().is_empty());
    assert_eq!(node.get_protocol(), "https");
    assert_eq!(node.get_failures(), 0);
}

#[test]
fn test_node_identifier_is_uuid() {
    let config = create_test_configuration();
    let certs = create_test_certificates();
    
    let node = Node::default(&config, certs);
    let identifier = node.get_identifier();
    
    // UUID should be 36 characters with dashes
    assert_eq!(identifier.len(), 36);
    assert_eq!(identifier.matches('-').count(), 4);
}

#[test]
fn test_node_process_id() {
    let config = create_test_configuration();
    let certs = create_test_certificates();
    
    let node = Node::default(&config, certs);
    let pid = node.get_process_id();
    
    // Process ID should be non-zero (current process)
    assert!(pid > 0);
}

#[test]
fn test_node_certificate() {
    let config = create_test_configuration();
    let certs = create_test_certificates();
    let cert_data = certs.certificates[0].clone();
    
    let node = Node::default(&config, certs);
    let node_cert = node.get_certificate();
    
    assert_eq!(node_cert, cert_data);
}

#[test]
fn test_node_match_feature() {
    let config = create_test_configuration();
    let certs = create_test_certificates();
    
    let node = Node::default(&config, certs);
    
    // Node doesn't have any features initially, so should not match
    let matched = node.match_node("feature", "test-feature");
    assert!(!matched);
}

#[test]
fn test_node_match_capability() {
    let config = create_test_configuration();
    let certs = create_test_certificates();
    
    let node = Node::default(&config, certs);
    
    // Node doesn't have capabilities initially - empty vec means it matches anything
    // (This is a logic bug in the current implementation that will be fixed later)
    let matched = node.match_node("capability", "test-capability");
    assert!(matched); // Current behavior: empty capabilities => matches
}

#[test]
fn test_node_match_query_empty() {
    let config = create_test_configuration();
    let certs = create_test_certificates();
    
    let node = Node::default(&config, certs);
    let query = BTreeMap::new();
    
    // Empty query should match
    let matched = node.match_node_query(&query);
    assert!(matched);
}

#[test]
fn test_node_match_query_with_criteria() {
    let config = create_test_configuration();
    let certs = create_test_certificates();
    
    let node = Node::default(&config, certs);
    
    let mut query = BTreeMap::new();
    query.insert("feature", "nonexistent");
    
    // Should not match since feature doesn't exist
    let matched = node.match_node_query(&query);
    assert!(!matched);
}

#[test]
fn test_node_timestamps() {
    let config = create_test_configuration();
    let certs = create_test_certificates();
    
    let node = Node::default(&config, certs);
    
    let created = node.get_created_at();
    let updated = node.get_last_updated_at();
    
    // Timestamps should be set
    assert!(created <= updated);
}

#[test]
fn test_node_features_empty() {
    let config = create_test_configuration();
    let certs = create_test_certificates();
    
    let node = Node::default(&config, certs);
    let features = node.get_features();
    
    // Should have features collection (check it exists)
    // We can't check len() directly, so just verify we can get the reference
    assert!(std::ptr::addr_of!(*features) as usize > 0);
}

#[test]
fn test_node_services_empty() {
    let config = create_test_configuration();
    let certs = create_test_certificates();
    
    let node = Node::default(&config, certs);
    let services = node.get_services();
    
    // Should have services collection
    assert_eq!(services.list().len(), 0);
}

#[test]
fn test_node_address_parsing() {
    let config = create_test_configuration();
    let certs = create_test_certificates();
    
    let node = Node::default(&config, certs);
    let address = node.get_address();
    
    assert_eq!(address.scheme().map(|s| s.as_str()), Some("https"));
    assert_eq!(address.host(), Some("localhost"));
}