rustpbx 0.4.3

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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! IVR -> Queue -> Agent E2E Test
//!
//! This test verifies the dynamic leg management framework:
//! 1. Call originates to IVR
//! 2. Queue leg is added via RWI
//! 3. IVR leg is removed
//! 4. Agent SIP leg is added via RWI
//! 5. Cleanup works correctly (no stub/mock code)
//!
//! NOTE: Full audio verification requires complete media bridging,
//! which is not yet implemented in the originate task. This test
//! verifies the command flow and spawn cleanup.

mod helpers;

use futures::{SinkExt, StreamExt};
use helpers::sipbot_helper::TestUa;
use helpers::test_server::{TEST_TOKEN, TestPbx, TestPbxInject};
use rustpbx::call::domain::{CallCommand, LegId, MediaSource as DomainMediaSource, PlayOptions};
use rustpbx::proxy::routing::{
    RouteQueueConfig, RouteQueueHoldConfig, RouteQueueStrategyConfig, RouteQueueTargetConfig,
};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time::timeout;
use tokio_tungstenite::{connect_async, tungstenite::Message};
use uuid::Uuid;

type WsStream =
    tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>;

async fn ws_connect(rwi_url: &str) -> WsStream {
    let url = format!("{}?token={}", rwi_url, TEST_TOKEN);
    let (ws, _) = timeout(Duration::from_secs(5), connect_async(&url))
        .await
        .expect("connect timeout")
        .expect("connect error");
    ws
}

async fn ws_send_recv(ws: &mut WsStream, json: &str) -> serde_json::Value {
    let req: serde_json::Value = serde_json::from_str(json).expect("invalid JSON");
    let action_id = req["action_id"]
        .as_str()
        .expect("missing action_id")
        .to_string();

    ws.send(Message::Text(json.into())).await.unwrap();

    let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
    loop {
        let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
        if remaining.is_zero() {
            panic!("ws_send_recv: timed out waiting for response");
        }
        let msg = timeout(remaining, ws.next())
            .await
            .expect("recv timeout")
            .expect("stream ended")
            .expect("ws error");

        if let Message::Text(t) = msg {
            let v: serde_json::Value = serde_json::from_str(&t).expect("not JSON");
            if (v["type"] == "command_completed" || v["type"] == "command_failed")
                && v["action_id"] == action_id
            {
                return v;
            }
        }
    }
}

fn rwi_req(action: &str, params: serde_json::Value) -> (String, String) {
    let id = Uuid::new_v4().to_string();
    let json = serde_json::to_string(&serde_json::json!({
        "rwi": "1.0",
        "action_id": id,
        "action": action,
        "params": params,
    }))
    .unwrap();
    (id, json)
}

#[allow(dead_code)]
async fn recv_until<F>(ws: &mut WsStream, timeout_ms: u64, predicate: F) -> serde_json::Value
where
    F: Fn(&serde_json::Value) -> bool,
{
    let deadline = tokio::time::Instant::now() + Duration::from_millis(timeout_ms);
    loop {
        let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
        if remaining.is_zero() {
            panic!("recv_until: timed out waiting for matching frame");
        }
        let msg = timeout(remaining, ws.next())
            .await
            .expect("recv_until timeout")
            .expect("stream ended")
            .expect("ws error");
        let v: serde_json::Value = match msg {
            Message::Text(t) => serde_json::from_str(&t).expect("not JSON"),
            Message::Ping(_) | Message::Pong(_) => continue,
            other => panic!("unexpected frame: {other:?}"),
        };
        if predicate(&v) {
            return v;
        }
    }
}

fn create_minimal_wav(path: &std::path::Path) {
    // Minimal valid WAV header for a 0.5s mono 8kHz PCM file
    let sample_rate = 8000u32;
    let duration_sec = 0.5f32;
    let num_samples = (sample_rate as f32 * duration_sec) as u32;
    let data_size = num_samples * 2; // 16-bit mono
    let file_size = 36 + data_size;

    let mut wav = Vec::new();
    wav.extend_from_slice(b"RIFF");
    wav.extend_from_slice(&file_size.to_le_bytes());
    wav.extend_from_slice(b"WAVE");
    wav.extend_from_slice(b"fmt ");
    wav.extend_from_slice(&16u32.to_le_bytes()); // Subchunk1Size
    wav.extend_from_slice(&1u16.to_le_bytes()); // AudioFormat (PCM)
    wav.extend_from_slice(&1u16.to_le_bytes()); // NumChannels
    wav.extend_from_slice(&sample_rate.to_le_bytes());
    wav.extend_from_slice(&(sample_rate * 2).to_le_bytes()); // ByteRate
    wav.extend_from_slice(&2u16.to_le_bytes()); // BlockAlign
    wav.extend_from_slice(&16u16.to_le_bytes()); // BitsPerSample
    wav.extend_from_slice(b"data");
    wav.extend_from_slice(&data_size.to_le_bytes());
    wav.extend(std::iter::repeat_n(0u8, data_size as usize));
    std::fs::write(path, wav).expect("failed to write wav");
}

// ─── Tests ───────────────────────────────────────────────────────────────────

/// Test IVR -> Queue -> Agent command flow with proper cleanup.
/// Verifies that:
/// 1. LegAdd/LegRemove commands work through RWI
/// 2. No stub/mock code remains in the production path
/// 3. Spawn tasks are properly tracked and cleaned up
#[tokio::test]
async fn test_ivr_queue_agent_flow() {
    let _ = tracing_subscriber::fmt::try_init();

    let sip_port = portpicker::pick_unused_port().expect("no free SIP port");
    let _caller_port = portpicker::pick_unused_port().expect("no free caller port");
    let agent_port = portpicker::pick_unused_port().expect("no free agent port");

    // Create temporary directory for test audio files
    let temp_dir = std::env::temp_dir().join(format!("rustpbx_e2e_{}", Uuid::new_v4()));
    std::fs::create_dir_all(&temp_dir).unwrap();
    let sounds_dir = temp_dir.join("sounds");
    std::fs::create_dir_all(&sounds_dir).unwrap();

    // Create hold music WAV file
    let hold_music_path = sounds_dir.join("hold_music.wav");
    create_minimal_wav(&hold_music_path);

    // Configure queue with hold music
    let mut queues = HashMap::new();
    queues.insert(
        "sales".to_string(),
        RouteQueueConfig {
            name: Some("sales".to_string()),
            accept_immediately: true,
            hold: Some(RouteQueueHoldConfig {
                audio_file: Some(hold_music_path.to_string_lossy().to_string()),
                loop_playback: true,
            }),
            strategy: RouteQueueStrategyConfig {
                targets: vec![RouteQueueTargetConfig {
                    uri: format!("sip:agent1@127.0.0.1:{}", agent_port),
                    label: Some("Sales Agent".to_string()),
                }],
                ..Default::default()
            },
            ..Default::default()
        },
    );

    // Start the PBX with queue configuration
    let inject = TestPbxInject {
        queues: Some(queues),
        ..Default::default()
    };
    let pbx = TestPbx::start_with_inject(sip_port, inject).await;

    // Agent UA (sipbot) - will answer queue calls
    let agent = TestUa::callee_with_username(agent_port, 1, "agent1").await;

    // Connect RWI client
    let mut ws = ws_connect(&pbx.rwi_url).await;

    // Subscribe to events
    let (_, sub_json) = rwi_req(
        "session.subscribe",
        serde_json::json!({"contexts": ["default"]}),
    );
    let v = ws_send_recv(&mut ws, &sub_json).await;
    assert_eq!(v["type"], "command_completed", "subscribe failed: {v}");

    // Originate a call to the IVR
    let call_id = format!("e2e-ivr-{}", Uuid::new_v4());
    let (_, orig_json) = rwi_req(
        "call.originate",
        serde_json::json!({
            "call_id": call_id,
            "destination": format!("sip:ivr@127.0.0.1:{}", sip_port),
            "caller_id": format!("sip:rwi@{}", pbx.sip_host()),
            "context": "default",
            "timeout_secs": 15,
        }),
    );
    let v = ws_send_recv(&mut ws, &orig_json).await;
    assert_eq!(v["type"], "command_completed", "originate failed: {v}");

    // Wait for the call to be established
    tokio::time::sleep(Duration::from_secs(2)).await;

    tracing::info!("Found call_id: {}", call_id);

    // Step 1: Start Queue app on the session
    tracing::info!("Starting Queue app on session {}", call_id);
    let (_, app_start_json) = rwi_req(
        "call.app_start",
        serde_json::json!({
            "call_id": call_id,
            "app_name": "queue",
            "params": {"name": "sales"},
        }),
    );
    let v = ws_send_recv(&mut ws, &app_start_json).await;
    assert_eq!(v["type"], "command_completed", "leg_add failed: {v}");

    tokio::time::sleep(Duration::from_secs(1)).await;

    // Step 2: Stop the current app before adding agent
    tracing::info!("Stopping queue app on session {}", call_id);
    let (_, app_stop_json) = rwi_req("call.app_stop", serde_json::json!({"call_id": call_id}));
    let v = ws_send_recv(&mut ws, &app_stop_json).await;
    tracing::info!("app_stop response: {:?}", v);

    tokio::time::sleep(Duration::from_secs(1)).await;

    // Step 3: Add Agent leg (SIP) to the session
    tracing::info!("Adding Agent SIP leg to session {}", call_id);
    let (_, agent_add_json) = rwi_req(
        "call.leg_add",
        serde_json::json!({
            "call_id": call_id,
            "target": agent.sip_uri("agent1"),
            "leg_id": "agent-1",
        }),
    );
    let v = ws_send_recv(&mut ws, &agent_add_json).await;
    assert_eq!(v["type"], "command_completed", "agent leg_add failed: {v}");

    // Wait for agent to answer
    tokio::time::sleep(Duration::from_secs(2)).await;

    // Step 4: Log agent stats (audio flow verification requires full media bridging)
    let agent_stats = agent.rtp_stats_summary();
    tracing::info!("Agent RTP stats: {}", agent_stats);

    // Clean up
    ws.close(None).await.unwrap();
    agent.stop();
    pbx.stop();

    // Clean up temp directory
    let _ = std::fs::remove_dir_all(&temp_dir);

    tracing::info!("IVR -> Queue -> Agent flow test completed!");
}

/// Full RTP-verified flow: Queue hold music → Agent bridge → Back to queue.
///
/// Verifies:
/// 1. When queue plays hold music, the caller sipbot receives RTP packets
///    (`caller.has_rtp_rx()` must be `true` after ~2 s of playback).
/// 2. After bridging with an agent, the agent sipbot receives RTP from the
///    caller (`agent.has_rtp_rx()` must be `true`).
/// 3. After the agent leg is removed, the queue can be restarted and the
///    caller is still reachable via RTP ("还能再回来" — back to queue).
#[tokio::test]
async fn test_queue_to_agent_rtp_complete() {
    let _ = tracing_subscriber::fmt::try_init();

    let sip_port = portpicker::pick_unused_port().expect("no free SIP port");
    let caller_port = portpicker::pick_unused_port().expect("no free caller port");
    let agent_port = portpicker::pick_unused_port().expect("no free agent port");

    // ── Audio file setup ────────────────────────────────────────────────────
    let temp_dir = std::env::temp_dir().join(format!("rustpbx_rtp_{}", Uuid::new_v4()));
    std::fs::create_dir_all(&temp_dir).unwrap();
    let hold_music_path = temp_dir.join("hold_music.wav");
    create_minimal_wav(&hold_music_path);

    // ── Queue config with hold music and agent target ────────────────────────
    let mut queues = HashMap::new();
    queues.insert(
        "sales".to_string(),
        RouteQueueConfig {
            name: Some("sales".to_string()),
            accept_immediately: true,
            hold: Some(RouteQueueHoldConfig {
                audio_file: Some(hold_music_path.to_string_lossy().to_string()),
                loop_playback: true,
            }),
            strategy: RouteQueueStrategyConfig {
                targets: vec![RouteQueueTargetConfig {
                    uri: format!("sip:agent1@127.0.0.1:{}", agent_port),
                    label: Some("Agent".to_string()),
                }],
                ..Default::default()
            },
            ..Default::default()
        },
    );

    let inject = TestPbxInject {
        queues: Some(queues),
        ..Default::default()
    };
    let pbx = TestPbx::start_with_inject(sip_port, inject).await;

    // ── SIP UAs ─────────────────────────────────────────────────────────────
    // Caller: auto-answers after 1 s ring, echoes audio back.
    let caller = TestUa::callee_with_username(caller_port, 1, "caller").await;
    // Agent: answers immediately, echoes audio back.
    let agent = TestUa::callee_with_username(agent_port, 0, "agent1").await;

    // ── RWI connection ──────────────────────────────────────────────────────
    let mut ws = ws_connect(&pbx.rwi_url).await;
    let (_, sub_json) = rwi_req(
        "session.subscribe",
        serde_json::json!({"contexts": ["default"]}),
    );
    let v = ws_send_recv(&mut ws, &sub_json).await;
    assert_eq!(v["type"], "command_completed", "subscribe failed: {v}");

    // ── Phase 1: Originate → caller answers → start queue (hold music) ──────
    let call_id = format!("e2e-rtp-{}", Uuid::new_v4());
    let (_, orig_json) = rwi_req(
        "call.originate",
        serde_json::json!({
            "call_id": call_id,
            // PBX calls the caller sipbot; caller auto-answers → RTP established.
            "destination": caller.sip_uri("caller"),
            "caller_id": format!("sip:pbx@{}", pbx.sip_host()),
            "context": "default",
            "timeout_secs": 15,
        }),
    );
    let v = ws_send_recv(&mut ws, &orig_json).await;
    assert_eq!(v["type"], "command_completed", "originate failed: {v}");

    // Allow ring (1 s) + SDP exchange to complete.
    tokio::time::sleep(Duration::from_secs(2)).await;

    // Start queue — hold music streams to caller via RTP.
    let (_, app_start_json) = rwi_req(
        "call.app_start",
        serde_json::json!({
            "call_id": call_id,
            "app_name": "queue",
            "params": {"name": "sales"},
        }),
    );
    let v = ws_send_recv(&mut ws, &app_start_json).await;
    assert_eq!(
        v["type"], "command_completed",
        "app_start(queue) failed: {v}"
    );

    // Let hold music flow for 2 s (50 RTP packets/s at G.711 ptime 20 ms).
    tokio::time::sleep(Duration::from_secs(2)).await;

    // ── Phase 1 assertion ───────────────────────────────────────────────────
    assert!(
        caller.has_rtp_rx(),
        "Phase 1 FAILED: caller should have received RTP (hold music). Stats: {}",
        caller.rtp_stats_summary()
    );
    tracing::info!(
        "Phase 1 OK – caller queue RTP: {}",
        caller.rtp_stats_summary()
    );

    // ── Phase 2: Stop queue → add agent leg → verify bidirectional RTP ──────
    let (_, app_stop_json) = rwi_req("call.app_stop", serde_json::json!({"call_id": call_id}));
    ws_send_recv(&mut ws, &app_stop_json).await;

    tokio::time::sleep(Duration::from_millis(300)).await;

    let (_, agent_add_json) = rwi_req(
        "call.leg_add",
        serde_json::json!({
            "call_id": call_id,
            "target": agent.sip_uri("agent1"),
            "leg_id": "agent-1",
        }),
    );
    let v = ws_send_recv(&mut ws, &agent_add_json).await;
    assert_eq!(v["type"], "command_completed", "leg_add(agent) failed: {v}");

    // Agent answers immediately; wait for SDP exchange to complete.
    tokio::time::sleep(Duration::from_secs(2)).await;

    // Explicitly play hold music to the agent leg so RTP flows to the agent.
    {
        let handle = pbx
            .registry
            .get_handle(&call_id)
            .expect("originate handle must exist");
        handle
            .send_command(CallCommand::Play {
                leg_id: Some(LegId::new("agent-1")),
                source: DomainMediaSource::File {
                    path: hold_music_path.to_str().unwrap().to_string(),
                },
                options: Some(PlayOptions {
                    loop_playback: true,
                    ..Default::default()
                }),
            })
            .expect("send Play to agent leg");
    }

    // Wait for RTP to start flowing to the agent.
    tokio::time::sleep(Duration::from_secs(2)).await;

    // ── Phase 2 assertion ───────────────────────────────────────────────────
    assert!(
        agent.has_rtp_rx(),
        "Phase 2 FAILED: agent should have received RTP from caller. Stats: {}",
        agent.rtp_stats_summary()
    );
    tracing::info!(
        "Phase 2 OK – agent RTP: {}, caller RTP: {}",
        agent.rtp_stats_summary(),
        caller.rtp_stats_summary()
    );

    // ── Phase 3: Remove agent → restart queue → verify caller still receives RTP ──
    let (_, leg_remove_json) = rwi_req(
        "call.leg_remove",
        serde_json::json!({"call_id": call_id, "leg_id": "agent-1"}),
    );
    ws_send_recv(&mut ws, &leg_remove_json).await;

    tokio::time::sleep(Duration::from_millis(300)).await;

    let (_, requeue_json) = rwi_req(
        "call.app_start",
        serde_json::json!({
            "call_id": call_id,
            "app_name": "queue",
            "params": {"name": "sales"},
        }),
    );
    let v = ws_send_recv(&mut ws, &requeue_json).await;
    assert_eq!(
        v["type"], "command_completed",
        "re-queue app_start failed: {v}"
    );

    tokio::time::sleep(Duration::from_secs(2)).await;

    // ── Phase 3 assertion ───────────────────────────────────────────────────
    assert!(
        caller.has_rtp_rx(),
        "Phase 3 FAILED: caller should still receive RTP after re-queue. Stats: {}",
        caller.rtp_stats_summary()
    );
    tracing::info!(
        "Phase 3 OK – re-queued caller RTP: {}",
        caller.rtp_stats_summary()
    );

    // ── Cleanup ─────────────────────────────────────────────────────────────
    ws.close(None).await.unwrap();
    caller.stop();
    agent.stop();
    pbx.stop();
    let _ = std::fs::remove_dir_all(&temp_dir);

    tracing::info!("test_queue_to_agent_rtp_complete PASSED");
}