rustpbx 0.4.7

A SIP PBX implementation 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
//! CDR (Call Detail Record) capture utilities for E2E testing

use crate::callrecord::CallRecordSender;
use crate::callrecord::{CallRecord, CallRecordHangupReason};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tracing::{debug, warn};

/// Captures CDR records during testing
pub struct CdrCapture {
    records: Arc<RwLock<Vec<CallRecord>>>,
    sender: CallRecordSender,
    _receiver_handle: tokio::task::JoinHandle<()>,
}

impl CdrCapture {
    /// Create a new CDR capture with a channel
    pub fn new() -> (Self, CallRecordSender) {
        let (sender, mut receiver) = tokio::sync::mpsc::channel::<CallRecord>(
            crate::callrecord::CALL_RECORD_CHANNEL_CAPACITY,
        );
        let records = Arc::new(RwLock::new(Vec::new()));
        let records_clone = records.clone();

        let _receiver_handle = crate::utils::spawn(async move {
            while let Some(record) = receiver.recv().await {
                debug!(
                    call_id = %record.call_id,
                    status = %record.details.status,
                    "CDR captured"
                );
                records_clone.write().await.push(record);
            }
            debug!("CDR capture receiver closed");
        });

        (
            Self {
                records,
                sender: sender.clone(),
                _receiver_handle,
            },
            sender,
        )
    }

    /// Wait for a CDR record with specific call_id
    pub async fn wait_for_record(&self, call_id: &str, timeout: Duration) -> Option<CallRecord> {
        let start = tokio::time::Instant::now();

        while start.elapsed() < timeout {
            let records = self.records.read().await;
            if let Some(record) = records.iter().find(|r| r.call_id == call_id) {
                return Some(record.clone());
            }
            drop(records);

            tokio::time::sleep(Duration::from_millis(100)).await;
        }

        warn!(call_id, "Timeout waiting for CDR record");
        None
    }

    /// Get all captured records
    pub async fn get_all_records(&self) -> Vec<CallRecord> {
        self.records.read().await.clone()
    }

    /// Find record by call_id
    pub async fn find_by_call_id(&self, call_id: &str) -> Option<CallRecord> {
        self.records
            .read()
            .await
            .iter()
            .find(|r| r.call_id == call_id)
            .cloned()
    }

    /// Clear all records
    pub async fn clear(&self) {
        self.records.write().await.clear();
    }

    /// Get the sender for use in server configuration
    pub fn sender(&self) -> CallRecordSender {
        self.sender.clone()
    }
}

impl Default for CdrCapture {
    fn default() -> Self {
        let (capture, sender) = Self::new();
        // Keep sender alive
        std::mem::forget(sender);
        capture
    }
}

/// CDR expectation for validation
#[derive(Debug, Clone, Default)]
pub struct CdrExpectation {
    pub expected_direction: Option<String>,
    pub expected_status: Option<String>,
    pub expect_recording: Option<bool>,
    pub expected_hangup_reason: Option<CallRecordHangupReason>,
    pub min_duration_secs: Option<i64>,
    pub max_duration_secs: Option<i64>,
    pub expected_caller: Option<String>,
    pub expected_callee: Option<String>,
}

impl CdrExpectation {
    pub fn with_direction(mut self, direction: impl Into<String>) -> Self {
        self.expected_direction = Some(direction.into());
        self
    }

    pub fn with_status(mut self, status: impl Into<String>) -> Self {
        self.expected_status = Some(status.into());
        self
    }

    pub fn with_recording(mut self, has_recording: bool) -> Self {
        self.expect_recording = Some(has_recording);
        self
    }

    pub fn with_hangup_reason(mut self, reason: CallRecordHangupReason) -> Self {
        self.expected_hangup_reason = Some(reason);
        self
    }

    pub fn with_duration_range(mut self, min: i64, max: i64) -> Self {
        self.min_duration_secs = Some(min);
        self.max_duration_secs = Some(max);
        self
    }

    pub fn with_caller(mut self, caller: impl Into<String>) -> Self {
        self.expected_caller = Some(caller.into());
        self
    }

    pub fn with_callee(mut self, callee: impl Into<String>) -> Self {
        self.expected_callee = Some(callee.into());
        self
    }
}

/// CDR validation result
#[derive(Debug, Clone)]
pub struct CdrValidationResult {
    pub is_valid: bool,
    pub errors: Vec<String>,
}

impl CdrValidationResult {
    pub fn new() -> Self {
        Self {
            is_valid: true,
            errors: Vec::new(),
        }
    }

    pub fn add_error(&mut self, error: impl Into<String>) {
        self.errors.push(error.into());
        self.is_valid = false;
    }
}

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

/// Validate a CDR record against expectations
pub fn validate_cdr(record: &CallRecord, expected: &CdrExpectation) -> CdrValidationResult {
    let mut result = CdrValidationResult::new();

    // Validate direction
    if let Some(ref expected_dir) = expected.expected_direction
        && record.details.direction != *expected_dir
    {
        result.add_error(format!(
            "Direction mismatch: expected '{}', got '{}'",
            expected_dir, record.details.direction
        ));
    }

    // Validate status
    if let Some(ref expected_status) = expected.expected_status
        && record.details.status != *expected_status
    {
        result.add_error(format!(
            "Status mismatch: expected '{}', got '{}'",
            expected_status, record.details.status
        ));
    }

    // Validate recording
    if let Some(expect_recording) = expected.expect_recording {
        let has_recording = !record.recorder.is_empty() || record.details.recording_url.is_some();
        if has_recording != expect_recording {
            result.add_error(format!(
                "Recording mismatch: expected recording={}, got recording={}",
                expect_recording, has_recording
            ));
        }
    }

    // Validate hangup reason
    if let Some(ref expected_reason) = expected.expected_hangup_reason {
        match &record.hangup_reason {
            Some(actual) if actual == expected_reason => {}
            Some(actual) => {
                result.add_error(format!(
                    "Hangup reason mismatch: expected '{:?}', got '{:?}'",
                    expected_reason, actual
                ));
            }
            None => {
                result.add_error(format!(
                    "Hangup reason missing: expected '{:?}'",
                    expected_reason
                ));
            }
        }
    }

    // Validate duration
    let duration = (record.end_time - record.start_time).num_seconds();
    if let Some(min) = expected.min_duration_secs
        && duration < min
    {
        result.add_error(format!(
            "Duration too short: expected >= {}s, got {}s",
            min, duration
        ));
    }
    if let Some(max) = expected.max_duration_secs
        && duration > max
    {
        result.add_error(format!(
            "Duration too long: expected <= {}s, got {}s",
            max, duration
        ));
    }

    // Validate caller
    if let Some(ref expected_caller) = expected.expected_caller
        && !record.caller.contains(expected_caller)
    {
        result.add_error(format!(
            "Caller mismatch: expected containing '{}', got '{}'",
            expected_caller, record.caller
        ));
    }

    // Validate callee
    if let Some(ref expected_callee) = expected.expected_callee
        && !record.callee.contains(expected_callee)
    {
        result.add_error(format!(
            "Callee mismatch: expected containing '{}', got '{}'",
            expected_callee, record.callee
        ));
    }

    // Basic completeness checks
    if record.call_id.is_empty() {
        result.add_error("Call ID is empty");
    }
    if record.start_time.timestamp() == 0 {
        result.add_error("Start time is not set");
    }
    if record.end_time.timestamp() == 0 {
        result.add_error("End time is not set");
    }

    result
}

/// Assertion helpers for tests
pub mod assertions {
    use super::*;

    /// Assert that CDR record matches expectations
    pub fn assert_cdr_matches(record: &CallRecord, expected: &CdrExpectation) {
        let result = validate_cdr(record, expected);
        if !result.is_valid {
            panic!("CDR validation failed:\n{}", result.errors.join("\n"));
        }
    }

    /// Assert that call was completed normally
    pub fn assert_call_completed(record: &CallRecord) {
        let expected = CdrExpectation::default()
            .with_status("completed")
            .with_hangup_reason(CallRecordHangupReason::ByCaller);
        assert_cdr_matches(record, &expected);
    }

    /// Assert that call was rejected
    pub fn assert_call_rejected(record: &CallRecord, expected_status_code: u16) {
        assert_eq!(
            record.status_code, expected_status_code,
            "Expected status code {}, got {}",
            expected_status_code, record.status_code
        );

        match record.hangup_reason {
            Some(CallRecordHangupReason::Rejected) | Some(CallRecordHangupReason::Canceled) => {}
            ref other => {
                panic!("Expected rejected/canceled reason, got {:?}", other);
            }
        }
    }
}

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

    fn create_test_record() -> CallRecord {
        CallRecord {
            call_id: "test-call-123".to_string(),
            start_time: Utc::now(),
            ring_time: Some(Utc::now()),
            answer_time: Some(Utc::now()),
            end_time: Utc::now() + chrono::Duration::seconds(10),
            caller: "sip:alice@example.com".to_string(),
            callee: "sip:bob@example.com".to_string(),
            status_code: 200,
            hangup_reason: Some(CallRecordHangupReason::ByCaller),
            hangup_messages: vec![],
            recorder: vec![],
            sip_leg_roles: Default::default(),
            leg_timeline: Default::default(),
            details: crate::callrecord::CallDetails {
                direction: "outbound".to_string(),
                status: "completed".to_string(),
                from_number: Some("alice".to_string()),
                to_number: Some("bob".to_string()),
                caller_name: None,
                agent_name: None,
                queue: None,
                department_id: None,
                extension_id: None,
                sip_trunk_id: None,
                route_id: None,
                sip_gateway: None,
                recording_url: None,
                recording_duration_secs: None,
                has_transcript: false,
                transcript_status: None,
                transcript_language: None,
                tags: None,
                rewrite: Default::default(),
                last_error: None,
                metadata: None,
            },
            extensions: http::Extensions::new(),
        }
    }

    #[tokio::test]
    async fn test_cdr_capture() {
        let (capture, sender) = CdrCapture::new();

        let mut record = create_test_record();
        record.call_id = "test-456".to_string();

        sender.send(record.clone()).await.unwrap();

        let found = capture
            .wait_for_record("test-456", Duration::from_secs(1))
            .await;
        assert!(found.is_some());
        assert_eq!(found.unwrap().call_id, "test-456");
    }

    #[test]
    fn test_validate_cdr_success() {
        let record = create_test_record();
        let expected = CdrExpectation::default()
            .with_direction("outbound")
            .with_status("completed")
            .with_hangup_reason(CallRecordHangupReason::ByCaller)
            .with_caller("alice")
            .with_callee("bob")
            .with_duration_range(5, 15);

        let result = validate_cdr(&record, &expected);
        assert!(result.is_valid, "Errors: {:?}", result.errors);
    }

    #[test]
    fn test_validate_cdr_failure() {
        let record = create_test_record();
        let expected = CdrExpectation::default()
            .with_direction("inbound") // Wrong direction
            .with_status("failed"); // Wrong status

        let result = validate_cdr(&record, &expected);
        assert!(!result.is_valid);
        assert_eq!(result.errors.len(), 2);
    }
}