xerv-core 0.1.0

Workflow orchestration core: memory-mapped arena, write-ahead log, traits, and type system
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Event recording for test replay and debugging.
//!
//! Records events during test execution for later analysis, replay, or debugging.

use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

/// An event recorded during test execution.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum RecordedEvent {
    /// Clock time was read.
    ClockNow {
        /// Time in nanoseconds.
        nanos: u64,
    },

    /// System time was read.
    SystemTime {
        /// Time in milliseconds since UNIX epoch.
        millis: u64,
    },

    /// Sleep was requested.
    Sleep {
        /// Duration in milliseconds.
        duration_ms: u64,
    },

    /// Time was advanced (mock clock only).
    TimeAdvanced {
        /// Duration in milliseconds.
        duration_ms: u64,
    },

    /// UUID was generated.
    UuidGenerated {
        /// The generated UUID.
        uuid: String,
    },

    /// Random u64 was generated.
    RandomU64 {
        /// The generated value.
        value: u64,
    },

    /// Random f64 was generated.
    RandomF64 {
        /// The generated value.
        value: f64,
    },

    /// Random bytes were generated.
    RandomBytes {
        /// Number of bytes generated.
        count: usize,
    },

    /// HTTP request was made.
    HttpRequest {
        /// HTTP method.
        method: String,
        /// Request URL.
        url: String,
        /// Request headers.
        headers: HashMap<String, String>,
        /// Request body size.
        body_size: usize,
    },

    /// HTTP response was received.
    HttpResponse {
        /// HTTP status code.
        status: u16,
        /// Response body size.
        body_size: usize,
    },

    /// File was read.
    FileRead {
        /// File path.
        path: String,
        /// Whether the read succeeded.
        success: bool,
        /// Bytes read (if successful).
        bytes_read: Option<usize>,
    },

    /// File was written.
    FileWrite {
        /// File path.
        path: String,
        /// Whether the write succeeded.
        success: bool,
        /// Bytes written.
        bytes_written: usize,
    },

    /// Environment variable was read.
    EnvRead {
        /// Variable name.
        key: String,
        /// Value found (if any).
        value: Option<String>,
    },

    /// Environment variable was set.
    EnvSet {
        /// Variable name.
        key: String,
        /// New value.
        value: String,
    },

    /// Secret was read.
    SecretRead {
        /// Secret key.
        key: String,
        /// Whether the secret was found.
        found: bool,
    },

    /// Node execution started.
    NodeExecutionStart {
        /// Node ID.
        node_id: u32,
        /// Node type.
        node_type: String,
    },

    /// Node execution completed.
    NodeExecutionComplete {
        /// Node ID.
        node_id: u32,
        /// Output port.
        output_port: String,
        /// Whether execution succeeded.
        success: bool,
    },

    /// Custom event for application-specific recording.
    Custom {
        /// Event name.
        name: String,
        /// Event data as JSON.
        data: serde_json::Value,
    },
}

impl RecordedEvent {
    /// Create a custom event.
    pub fn custom(name: impl Into<String>, data: serde_json::Value) -> Self {
        Self::Custom {
            name: name.into(),
            data,
        }
    }

    /// Get the event type as a string.
    pub fn event_type(&self) -> &'static str {
        match self {
            Self::ClockNow { .. } => "clock_now",
            Self::SystemTime { .. } => "system_time",
            Self::Sleep { .. } => "sleep",
            Self::TimeAdvanced { .. } => "time_advanced",
            Self::UuidGenerated { .. } => "uuid_generated",
            Self::RandomU64 { .. } => "random_u64",
            Self::RandomF64 { .. } => "random_f64",
            Self::RandomBytes { .. } => "random_bytes",
            Self::HttpRequest { .. } => "http_request",
            Self::HttpResponse { .. } => "http_response",
            Self::FileRead { .. } => "file_read",
            Self::FileWrite { .. } => "file_write",
            Self::EnvRead { .. } => "env_read",
            Self::EnvSet { .. } => "env_set",
            Self::SecretRead { .. } => "secret_read",
            Self::NodeExecutionStart { .. } => "node_execution_start",
            Self::NodeExecutionComplete { .. } => "node_execution_complete",
            Self::Custom { .. } => "custom",
        }
    }
}

/// Recorder for capturing events during test execution.
///
/// Thread-safe and can be shared across async tasks.
///
/// # Example
///
/// ```
/// use xerv_core::testing::{EventRecorder, RecordedEvent};
///
/// let recorder = EventRecorder::new();
///
/// recorder.record(RecordedEvent::ClockNow { nanos: 1000 });
/// recorder.record(RecordedEvent::UuidGenerated { uuid: "test-uuid".to_string() });
///
/// let events = recorder.events();
/// assert_eq!(events.len(), 2);
///
/// let json = recorder.to_json();
/// assert!(json.contains("clock_now"));
/// ```
pub struct EventRecorder {
    events: RwLock<Vec<RecordedEvent>>,
    enabled: std::sync::atomic::AtomicBool,
}

impl EventRecorder {
    /// Create a new event recorder.
    pub fn new() -> Self {
        Self {
            events: RwLock::new(Vec::new()),
            enabled: std::sync::atomic::AtomicBool::new(true),
        }
    }

    /// Record an event.
    pub fn record(&self, event: RecordedEvent) {
        if self.enabled.load(std::sync::atomic::Ordering::SeqCst) {
            self.events.write().push(event);
        }
    }

    /// Get all recorded events.
    pub fn events(&self) -> Vec<RecordedEvent> {
        self.events.read().clone()
    }

    /// Get the number of recorded events.
    pub fn len(&self) -> usize {
        self.events.read().len()
    }

    /// Check if no events have been recorded.
    pub fn is_empty(&self) -> bool {
        self.events.read().is_empty()
    }

    /// Clear all recorded events.
    pub fn clear(&self) {
        self.events.write().clear();
    }

    /// Enable or disable recording.
    pub fn set_enabled(&self, enabled: bool) {
        self.enabled
            .store(enabled, std::sync::atomic::Ordering::SeqCst);
    }

    /// Check if recording is enabled.
    pub fn is_enabled(&self) -> bool {
        self.enabled.load(std::sync::atomic::Ordering::SeqCst)
    }

    /// Convert recorded events to JSON.
    pub fn to_json(&self) -> String {
        serde_json::to_string_pretty(&*self.events.read()).expect("Failed to serialize events")
    }

    /// Convert recorded events to compact JSON.
    pub fn to_json_compact(&self) -> String {
        serde_json::to_string(&*self.events.read()).expect("Failed to serialize events")
    }

    /// Load events from JSON.
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        let events: Vec<RecordedEvent> = serde_json::from_str(json)?;
        Ok(Self {
            events: RwLock::new(events),
            enabled: std::sync::atomic::AtomicBool::new(true),
        })
    }

    /// Filter events by type.
    pub fn events_of_type(&self, event_type: &str) -> Vec<RecordedEvent> {
        self.events
            .read()
            .iter()
            .filter(|e| e.event_type() == event_type)
            .cloned()
            .collect()
    }

    /// Find events matching a predicate.
    pub fn find<F>(&self, predicate: F) -> Vec<RecordedEvent>
    where
        F: Fn(&RecordedEvent) -> bool,
    {
        self.events
            .read()
            .iter()
            .filter(|e| predicate(e))
            .cloned()
            .collect()
    }

    /// Assert that an event of the given type was recorded.
    pub fn assert_recorded(&self, event_type: &str) -> bool {
        self.events
            .read()
            .iter()
            .any(|e| e.event_type() == event_type)
    }

    /// Assert that a specific HTTP request was made.
    pub fn assert_http_request(&self, method: &str, url_pattern: &str) -> bool {
        let re = regex::Regex::new(url_pattern).expect("Invalid URL pattern");
        self.events.read().iter().any(|e| {
            if let RecordedEvent::HttpRequest { method: m, url, .. } = e {
                m.eq_ignore_ascii_case(method) && re.is_match(url)
            } else {
                false
            }
        })
    }

    /// Get all HTTP requests.
    pub fn http_requests(&self) -> Vec<(String, String)> {
        self.events
            .read()
            .iter()
            .filter_map(|e| {
                if let RecordedEvent::HttpRequest { method, url, .. } = e {
                    Some((method.clone(), url.clone()))
                } else {
                    None
                }
            })
            .collect()
    }

    /// Get all generated UUIDs.
    pub fn generated_uuids(&self) -> Vec<Uuid> {
        self.events
            .read()
            .iter()
            .filter_map(|e| {
                if let RecordedEvent::UuidGenerated { uuid } = e {
                    Uuid::parse_str(uuid).ok()
                } else {
                    None
                }
            })
            .collect()
    }
}

impl Default for EventRecorder {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn record_and_retrieve() {
        let recorder = EventRecorder::new();

        recorder.record(RecordedEvent::ClockNow { nanos: 1000 });
        recorder.record(RecordedEvent::UuidGenerated {
            uuid: "test-uuid".to_string(),
        });

        let events = recorder.events();
        assert_eq!(events.len(), 2);

        assert!(matches!(events[0], RecordedEvent::ClockNow { nanos: 1000 }));
    }

    #[test]
    fn json_serialization() {
        let recorder = EventRecorder::new();
        recorder.record(RecordedEvent::RandomU64 { value: 42 });

        let json = recorder.to_json();
        assert!(json.contains("random_u64"));
        assert!(json.contains("42"));

        let loaded = EventRecorder::from_json(&json).unwrap();
        assert_eq!(loaded.len(), 1);
    }

    #[test]
    fn filter_by_type() {
        let recorder = EventRecorder::new();
        recorder.record(RecordedEvent::ClockNow { nanos: 100 });
        recorder.record(RecordedEvent::UuidGenerated {
            uuid: "a".to_string(),
        });
        recorder.record(RecordedEvent::ClockNow { nanos: 200 });

        let clock_events = recorder.events_of_type("clock_now");
        assert_eq!(clock_events.len(), 2);
    }

    #[test]
    fn disable_recording() {
        let recorder = EventRecorder::new();

        recorder.record(RecordedEvent::ClockNow { nanos: 100 });
        assert_eq!(recorder.len(), 1);

        recorder.set_enabled(false);
        recorder.record(RecordedEvent::ClockNow { nanos: 200 });
        assert_eq!(recorder.len(), 1); // Still 1, recording was disabled

        recorder.set_enabled(true);
        recorder.record(RecordedEvent::ClockNow { nanos: 300 });
        assert_eq!(recorder.len(), 2);
    }

    #[test]
    fn assert_http_request() {
        let recorder = EventRecorder::new();
        recorder.record(RecordedEvent::HttpRequest {
            method: "POST".to_string(),
            url: "https://api.example.com/users".to_string(),
            headers: HashMap::new(),
            body_size: 0,
        });

        assert!(recorder.assert_http_request("POST", r"api\.example\.com/users"));
        assert!(!recorder.assert_http_request("GET", r"api\.example\.com/users"));
        assert!(!recorder.assert_http_request("POST", r"other\.com"));
    }

    #[test]
    fn custom_events() {
        let recorder = EventRecorder::new();
        recorder.record(RecordedEvent::custom(
            "my_event",
            serde_json::json!({"key": "value"}),
        ));

        let events = recorder.events_of_type("custom");
        assert_eq!(events.len(), 1);

        if let RecordedEvent::Custom { name, data } = &events[0] {
            assert_eq!(name, "my_event");
            assert_eq!(data["key"], "value");
        } else {
            panic!("Expected Custom event");
        }
    }
}