avmnif-rs 0.4.1

Safe NIF toolkit for AtomVM written in Rust
Documentation
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Test utilities for port communication functionality

#[cfg(test)]
use alloc::{format, string::String, string::ToString, vec, vec::Vec};
use crate::atom::AtomTableOps;
use crate::testing::mocks::*;
use crate::term::{Term, TermValue, PortId, ProcessId, NifResult, NifError};

#[cfg(test)]
/// Simple message type for testing
#[derive(Debug, Clone, PartialEq)]
pub enum TestMessage {
    Command(String),
    Data(Vec<u8>),
    Error(String),
}

#[cfg(test)]
/// Test implementation of port data for testing purposes
pub struct TestPortData {
    pub port_id: u32,
    pub active: bool,
    pub messages: Vec<TestMessage>,
    pub last_command: Option<String>,
    pub error_count: u32,
}

#[cfg(test)]
impl TestPortData {
    pub fn new() -> Self {
        Self {
            port_id: 0,
            active: false,
            messages: Vec::new(),
            last_command: None,
            error_count: 0,
        }
    }

    pub fn with_port_id(port_id: u32) -> Self {
        Self {
            port_id,
            active: false,
            messages: Vec::new(),
            last_command: None,
            error_count: 0,
        }
    }

    pub fn add_message(&mut self, message: TestMessage) {
        self.messages.push(message);
    }

    pub fn activate(&mut self) {
        self.active = true;
    }

    pub fn deactivate(&mut self) {
        self.active = false;
    }

    pub fn process_messages(&mut self, _atom_table: &MockAtomTable) {
        while let Some(message) = self.messages.pop() {
            match message {
                TestMessage::Command(cmd) => {
                    self.last_command = Some(cmd);
                }
                TestMessage::Data(_) => {
                    // Handle data messages
                }
                TestMessage::Error(_) => {
                    self.error_count += 1;
                }
            }
        }
    }

    pub fn port_id(&self) -> u32 {
        self.port_id
    }

    pub fn is_active(&self) -> bool {
        self.active
    }

    pub fn message_count(&self) -> usize {
        self.messages.len()
    }
}

#[cfg(test)]
pub fn create_ok_reply(atom_table: &MockAtomTable, data: TermValue) -> TermValue {
    let ok_atom = atom_table.ensure_atom_str("ok").unwrap();
    TermValue::tuple(vec![TermValue::Atom(ok_atom), data])
}

#[cfg(test)]
pub fn create_error_reply(atom_table: &MockAtomTable, reason: TermValue) -> TermValue {
    let error_atom = atom_table.ensure_atom_str("error").unwrap();
    TermValue::tuple(vec![TermValue::Atom(error_atom), reason])
}

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

    #[test]
    fn test_create_ok_reply() {
        let atom_table = MockAtomTable::new();
        let ok_atom = atom_table.ensure_atom_str("ok").unwrap();
        let data = TermValue::SmallInt(42);
        
        let reply = create_ok_reply(&atom_table, data);
        
        // Verify the reply structure
        if let Some(elements) = reply.as_tuple() {
            assert_eq!(elements.len(), 2);
            assert_eq!(elements[0], TermValue::Atom(ok_atom));
            assert_eq!(elements[1], TermValue::SmallInt(42));
        } else {
            panic!("Expected tuple reply");
        }
    }

    #[test]
    fn test_create_error_reply() {
        let atom_table = MockAtomTable::new();
        let error_atom = atom_table.ensure_atom_str("error").unwrap();
        let reason_atom = atom_table.ensure_atom_str("invalid_input").unwrap();
        let reason = TermValue::Atom(reason_atom);
        
        let reply = create_error_reply(&atom_table, reason);
        
        // Verify the error reply structure
        if let Some(elements) = reply.as_tuple() {
            assert_eq!(elements.len(), 2);
            assert_eq!(elements[0], TermValue::Atom(error_atom));
            assert_eq!(elements[1], TermValue::Atom(reason_atom));
        } else {
            panic!("Expected tuple reply");
        }
    }

    #[test]
    fn test_port_data_trait_defaults() {
        let mut test_data = TestPortData::new();
        
        // Test default implementations
        assert_eq!(test_data.port_id(), 0);
        assert_eq!(test_data.message_count(), 0);
        
        // Test activation - should start inactive
        assert!(!test_data.is_active());
        test_data.activate();
        assert!(test_data.is_active());
        
        // Test deactivation
        test_data.deactivate();
        assert!(!test_data.is_active());
    }

    #[test]
    fn test_generic_port_data_message_handling() {
        let mut port_data = TestPortData::new();
        let atom_table = MockAtomTable::new();
        
        // Initially no messages
        assert_eq!(port_data.message_count(), 0);
        
        // Add a message
        let msg = TestMessage::Command("test".to_string());
        port_data.add_message(msg);
        assert_eq!(port_data.message_count(), 1);
        
        // Process messages (this should consume the message)
        port_data.process_messages(&atom_table);
        assert_eq!(port_data.message_count(), 0);  // Should be 0 after processing
        
        // Verify processing worked
        assert!(port_data.last_command.is_some());
        assert_eq!(port_data.last_command.as_ref().unwrap(), "test");
    }

    #[test]
    fn test_port_data_with_many_messages() {
        let mut port_data = TestPortData::new();
        let atom_table = MockAtomTable::new();
        
        // Add multiple messages
        for i in 0..5 {
            let msg = TestMessage::Command(format!("command_{}", i));
            port_data.add_message(msg);
        }
        
        assert_eq!(port_data.message_count(), 5);
        
        // Process all messages (should consume all of them)
        port_data.process_messages(&atom_table);
        assert_eq!(port_data.message_count(), 0);  // All messages should be consumed
        
        // Verify last command was processed (LIFO order)
        assert!(port_data.last_command.is_some());
        assert_eq!(port_data.last_command.as_ref().unwrap(), "command_0");
    }

    #[test]
    fn test_port_data_lifecycle() {
        let mut port_data = TestPortData::with_port_id(123);
        
        assert_eq!(port_data.port_id(), 123);
        assert!(!port_data.is_active());
        
        port_data.activate();
        assert!(port_data.is_active());
        
        port_data.deactivate();
        assert!(!port_data.is_active());
    }

    #[test]
    fn test_port_data_cleanup() {
        let mut port_data = TestPortData::new();
        
        // Add some messages
        port_data.add_message(TestMessage::Command("cmd1".to_string()));
        port_data.add_message(TestMessage::Command("cmd2".to_string()));
        
        assert_eq!(port_data.message_count(), 2);
        
        // Clear messages
        port_data.messages.clear();
        assert_eq!(port_data.message_count(), 0);
    }

    #[test]
    fn test_port_data_state_transitions() {
        let mut port_data = TestPortData::new();
        let atom_table = MockAtomTable::new();
        
        // Test state transitions with message processing
        assert!(!port_data.is_active());
        
        port_data.activate();
        assert!(port_data.is_active());
        
        // Add and process a message while active
        port_data.add_message(TestMessage::Command("active_cmd".to_string()));
        port_data.process_messages(&atom_table);
        
        assert_eq!(port_data.last_command.as_ref().unwrap(), "active_cmd");
        assert!(port_data.is_active()); // Should remain active
        
        port_data.deactivate();
        assert!(!port_data.is_active());
    }

    #[test]
    fn test_port_data_macro_generated_types() {
        // Test that our TestPortData works with macro-generated scenarios
        let mut port_data = TestPortData::new();
        let atom_table = MockAtomTable::new();
        
        // Simulate macro-generated port operations
        let commands = vec![
            "initialize",
            "configure", 
            "start",
            "process",
            "stop"
        ];
        
        for cmd in commands {
            port_data.add_message(TestMessage::Command(cmd.to_string()));
        }
        
        assert_eq!(port_data.message_count(), 5);
        
        port_data.process_messages(&atom_table);
        assert_eq!(port_data.message_count(), 0);
        assert_eq!(port_data.last_command.as_ref().unwrap(), "initialize"); // Last processed (LIFO)
    }

    #[test]
    fn test_port_op_result() {
        let atom_table = MockAtomTable::new();
        
        // Test successful operation using TermValue
        let ok_result: Result<TermValue, NifError> = Ok(TermValue::SmallInt(42));
        match ok_result {
            Ok(term) => assert_eq!(term, TermValue::SmallInt(42)),
            _ => panic!("Expected Ok result"),
        }
        
        // Test error operation
        let error_result: Result<TermValue, NifError> = Err(NifError::BadArg);
        match error_result {
            Err(NifError::BadArg) => assert!(true),
            _ => panic!("Expected BadArg error"),
        }
    }

    #[test]
    fn test_port_result_values() {
        // Test various port operation results
        #[derive(Debug, PartialEq)]
        enum TestPortResult {
            Continue,
            Stop,
            Reply(TermValue),
        }
        
        let results = vec![
            TestPortResult::Continue,
            TestPortResult::Stop,
            TestPortResult::Reply(TermValue::SmallInt(123)),
        ];
        
        for result in results {
            match result {
                TestPortResult::Continue => {
                    // Test continue case
                    assert!(true);
                }
                TestPortResult::Stop => {
                    // Test stop case  
                    assert!(true);
                }
                TestPortResult::Reply(term) => {
                    assert_eq!(term, TermValue::SmallInt(123));
                }
            }
        }
    }

    #[test]
    fn test_port_error_handling() {
        let mut port_data = TestPortData::new();
        let atom_table = MockAtomTable::new();
        
        // Test error message handling
        let error_msg = TestMessage::Error("test error".to_string());
        port_data.add_message(error_msg);
        
        assert_eq!(port_data.error_count, 0);
        port_data.process_messages(&atom_table);
        assert_eq!(port_data.error_count, 1);
    }

    #[test]
    fn test_port_error_conversion() {
        let atom_table = MockAtomTable::new();
        
        // Test converting various error types to terms
        let error_atom = atom_table.ensure_atom_str("conversion_error").unwrap();
        let error_term = TermValue::Atom(error_atom);
        
        // Verify error atoms are created correctly
        assert!(atom_table.atom_equals_str(error_atom, "conversion_error"));
    }

    #[test]
    fn test_term_to_pid() {
        let atom_table = MockAtomTable::new();
        
        // Test PID term creation and validation
        let pid_atom = atom_table.ensure_atom_str("pid").unwrap();
        let node_atom = atom_table.ensure_atom_str("node@host").unwrap();
        let creation_atom = atom_table.ensure_atom_str("creation").unwrap();
        
        // Create a PID term
        let pid_term = TermValue::Pid(ProcessId(123));
        
        // Verify PID-related atoms are handled correctly
        assert!(atom_table.atom_equals_str(pid_atom, "pid"));
        assert!(atom_table.atom_equals_str(node_atom, "node@host"));
        assert!(atom_table.atom_equals_str(creation_atom, "creation"));
        
        // Verify PID term
        if let Some(ProcessId(id)) = pid_term.as_pid() {
            assert_eq!(id, 123);
        } else {
            panic!("Expected PID term");
        }
    }

    #[test]
    fn test_standard_message_commands() {
        let mut port_data = TestPortData::new();
        let atom_table = MockAtomTable::new();
        
        // Test standard Erlang port commands
        let standard_commands = vec![
            "open", "close", "command", "connect", "disconnect"
        ];
        
        for cmd in standard_commands {
            port_data.add_message(TestMessage::Command(cmd.to_string()));
        }
        
        port_data.process_messages(&atom_table);
        
        // Should have processed all commands
        assert_eq!(port_data.message_count(), 0);
        assert!(port_data.last_command.is_some());
    }
}

// Add helper method to TermValue for PID extraction
impl TermValue {
    pub fn as_pid(&self) -> Option<ProcessId> {
        match self {
            TermValue::Pid(pid) => Some(*pid),
            _ => None,
        }
    }
}