File size: 4,288 Bytes
224e773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/// A routing node represents an executable expert in the routing pipeline
#[derive(Clone, Debug)]
pub struct RoutingNode {
    pub id: u64,
    pub name: String,
    pub weight: f64,
    pub enabled: bool,
}

impl RoutingNode {
    /// Create a new routing node with id, name, and weight
    pub fn new(id: u64, name: String, weight: f64) -> Self {
        Self {
            id,
            name,
            weight,
            enabled: weight > 0.0,
        }
    }

    /// Execute this routing node on the given input
    pub fn execute(&self, input: &str) -> hyperkitty_core::Result<String> {
        if !self.enabled {
            return Err(hyperkitty_core::Error::InvalidRoute);
        }

        // Apply weighted transformation
        let weighted_input = format!(
            "{}(weight: {:.4}, input: '{}' )",
            self.name, self.weight, input
        );

        Ok(weighted_input)
    }

    /// Check if this node is valid for routing
    pub fn is_valid(&self) -> bool {
        self.enabled && self.weight > 0.0 && !self.name.is_empty()
    }

    /// Set the enabled state
    pub fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_routing_node_creation() {
        let node = RoutingNode::new(42, "test_node".to_string(), 0.75);
        assert_eq!(node.id, 42);
        assert_eq!(node.name, "test_node");
        assert_eq!(node.weight, 0.75);
        assert!(node.enabled);
    }

    #[test]
    fn test_routing_node_disabled_when_weight_zero() {
        let node = RoutingNode::new(1, "node".to_string(), 0.0);
        assert!(!node.enabled);
    }

    #[test]
    fn test_routing_node_execute_produces_output() {
        let node = RoutingNode::new(100, "router".to_string(), 0.5);
        let result = node.execute("test input");

        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(output.contains("router"));
        assert!(output.contains("0.5"));
    }

    #[test]
    fn test_routing_node_execute_rejects_disabled() {
        let mut node = RoutingNode::new(1, "node".to_string(), 0.5);
        node.set_enabled(false);

        let result = node.execute("input");
        assert!(result.is_err());
    }

    #[test]
    fn test_routing_node_execute_preserves_input() {
        let node = RoutingNode::new(1, "node".to_string(), 1.0);
        let result = node.execute("hello world");

        assert!(result.is_ok());
        assert!(result.unwrap().contains("hello world"));
    }

    #[test]
    fn test_routing_node_is_valid_enabled() {
        let node = RoutingNode::new(1, "valid".to_string(), 0.8);
        assert!(node.is_valid());
    }

    #[test]
    fn test_routing_node_is_valid_zero_weight() {
        let node = RoutingNode::new(1, "invalid".to_string(), 0.0);
        assert!(!node.is_valid());
    }

    #[test]
    fn test_routing_node_is_valid_empty_name() {
        let node = RoutingNode::new(1, String::new(), 0.5);
        assert!(!node.is_valid());
    }

    #[test]
    fn test_routing_node_set_enabled() {
        let mut node = RoutingNode::new(1, "node".to_string(), 0.5);
        assert!(node.enabled);

        node.set_enabled(false);
        assert!(!node.enabled);

        node.set_enabled(true);
        assert!(node.enabled);
    }

    #[test]
    fn test_routing_node_high_weight() {
        let node = RoutingNode::new(1, "high_weight".to_string(), 0.99);
        assert!(node.is_valid());
        assert!(node.weight > 0.9);
    }

    #[test]
    fn test_routing_node_low_weight() {
        let node = RoutingNode::new(1, "low_weight".to_string(), 0.01);
        assert!(node.is_valid());
    }

    #[test]
    fn test_routing_node_cloneable() {
        let node = RoutingNode::new(42, "original".to_string(), 0.5);
        let cloned = node.clone();

        assert_eq!(cloned.id, node.id);
        assert_eq!(cloned.name, node.name);
        assert_eq!(cloned.weight, node.weight);
    }

    #[test]
    fn test_routing_node_execute_weight_precision() {
        let node = RoutingNode::new(1, "node".to_string(), 0.3333);
        let result = node.execute("test");

        let output = result.unwrap();
        assert!(output.contains("0.3333"));
    }
}