videosdk-server-sdk 0.1.0

Rust server SDK for the VideoSDK v2 REST APIs
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! End-to-end tests for the Phase 2 resources, against a mock HTTP server.

use futures_util::StreamExt as _;
use serde_json::{json, Value};
use wiremock::matchers::{body_json, method, path, query_param};
use wiremock::{Mock, MockServer, ResponseTemplate};

use crate::pagination::ListParams;
use crate::resources::rooms::{
    AutoCloseConfigInput, RoomCreateParams, RoomListParams, RoomWebhookInput,
};
use crate::resources::sessions::{
    SessionEndParams, SessionListParams, SessionRemoveParticipantParams, SessionStatus,
};
use crate::test_support::{body, client, only_request, requests};
use crate::Region;

fn room_json(room_id: &str) -> Value {
    json!({"id": "db-1", "roomId": room_id})
}

/// Mounts a catch-all returning `response`.
async fn mount(server: &MockServer, verb: &str, route: &str, response: Value) {
    Mock::given(method(verb))
        .and(path(route))
        .respond_with(ResponseTemplate::new(200).set_body_json(response))
        .mount(server)
        .await;
}

/* ----------------------------------- rooms ---------------------------------- */

#[tokio::test]
async fn create_room_posts_the_mapped_wire_body() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v2/rooms"))
        .and(body_json(json!({
            "customRoomId": "my-room",
            "geoFence": "in002",
            "webhook": {"endPoint": "https://example.com/hook", "events": ["session*"]},
            "autoCloseConfig": {"type": "session-ends", "duration": 300},
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(room_json("abcd-efgh-ijkl")))
        .expect(1)
        .mount(&server)
        .await;

    let room = client(&server)
        .rooms()
        .create(RoomCreateParams {
            custom_room_id: Some("my-room".into()),
            geo_fence: Some(Region::IN002),
            webhook: Some(RoomWebhookInput {
                url: "https://example.com/hook".into(),
                events: vec!["session*".into()],
            }),
            auto_close_config: Some(AutoCloseConfigInput {
                after_inactive_seconds: Some(300),
                kind: None,
            }),
            ..Default::default()
        })
        .await
        .unwrap();

    assert_eq!(room.room_id, "abcd-efgh-ijkl");
}

#[tokio::test]
async fn get_room_escapes_the_path_segment() {
    let server = MockServer::start().await;
    mount(&server, "GET", "/v2/rooms/a%2Fb", room_json("a/b")).await;

    client(&server).rooms().get("a/b").await.unwrap();
    assert_eq!(only_request(&server).await.url.path(), "/v2/rooms/a%2Fb");
}

#[tokio::test]
async fn validate_reports_a_valid_room() {
    let server = MockServer::start().await;
    mount(
        &server,
        "GET",
        "/v2/rooms/validate/my-room",
        room_json("abcd-efgh-ijkl"),
    )
    .await;

    let validation = client(&server).rooms().validate("my-room").await.unwrap();
    assert!(validation.valid);
    // The resolved roomId is echoed back, not the customRoomId that was passed.
    assert_eq!(validation.room_id, "abcd-efgh-ijkl");
    assert!(validation.room.is_some());
}

#[tokio::test]
async fn validate_treats_400_402_and_404_as_invalid_rather_than_an_error() {
    for status in [400, 402, 404] {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .respond_with(ResponseTemplate::new(status))
            .mount(&server)
            .await;

        let validation = client(&server)
            .rooms()
            .validate("nope")
            .await
            .unwrap_or_else(|e| panic!("status {status} should not error: {e}"));
        assert!(!validation.valid);
        assert_eq!(validation.room_id, "nope");
        assert!(validation.room.is_none());
    }
}

#[tokio::test]
async fn validate_propagates_other_errors() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .respond_with(ResponseTemplate::new(401))
        .mount(&server)
        .await;

    let err = client(&server).rooms().validate("x").await.unwrap_err();
    assert!(err.is_authentication());
}

#[tokio::test]
async fn end_room_posts_to_deactivate() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v2/rooms/deactivate"))
        .and(body_json(json!({"roomId": "abcd-efgh-ijkl"})))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": "db-1", "roomId": "abcd-efgh-ijkl", "disabled": true,
        })))
        .expect(1)
        .mount(&server)
        .await;

    let room = client(&server).rooms().end("abcd-efgh-ijkl").await.unwrap();
    assert!(room.disabled);
}

#[tokio::test]
async fn list_rooms_sends_filters_and_omits_absent_ones() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v2/rooms"))
        .and(query_param("page", "2"))
        .and(query_param("query", "my-room"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"data": [room_json("r-1")]})))
        .expect(1)
        .mount(&server)
        .await;

    let page = client(&server)
        .rooms()
        .list(RoomListParams {
            page: Some(2),
            query: Some("my-room".into()),
            ..Default::default()
        })
        .await
        .unwrap();

    assert_eq!(page.data.len(), 1);
    let url = only_request(&server).await.url;
    assert!(!url.query().unwrap().contains("userId"));
}

#[tokio::test]
async fn list_rooms_streams_across_pages() {
    let server = MockServer::start().await;
    let page = |current: u32, id: &str| {
        json!({
            "pageInfo": {"currentPage": current, "perPage": 1, "lastPage": 2, "total": 2},
            "data": [room_json(id)],
        })
    };
    Mock::given(method("GET"))
        .and(query_param("page", "2"))
        .respond_with(ResponseTemplate::new(200).set_body_json(page(2, "r-2")))
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .respond_with(ResponseTemplate::new(200).set_body_json(page(1, "r-1")))
        .mount(&server)
        .await;

    let client = client(&server);
    let ids: Vec<String> = client
        .rooms()
        .list_stream(RoomListParams::default())
        .map(|room| room.unwrap().room_id)
        .collect()
        .await;
    assert_eq!(ids, ["r-1", "r-2"]);
}

#[tokio::test]
async fn a_list_stream_surfaces_the_first_fetch_error() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .respond_with(ResponseTemplate::new(403))
        .mount(&server)
        .await;

    let client = client(&server);
    let results: Vec<_> = client
        .rooms()
        .list_stream(RoomListParams::default())
        .collect()
        .await;
    assert_eq!(results.len(), 1);
    assert!(results[0].as_ref().unwrap_err().is_permission());
}

/* ---------------------------------- sessions -------------------------------- */

#[tokio::test]
async fn list_sessions_sends_every_filter() {
    let server = MockServer::start().await;
    mount(&server, "GET", "/v2/sessions", json!({"data": []})).await;

    client(&server)
        .sessions()
        .list(SessionListParams {
            room_id: Some("r-1".into()),
            custom_room_id: Some("my-room".into()),
            user_id: Some("u-1".into()),
            start_date: Some(1_700_000_000_000),
            end_date: Some(1_800_000_000_000),
            status: Some(SessionStatus::ENDED),
            ..Default::default()
        })
        .await
        .unwrap();

    let url = only_request(&server).await.url;
    let query: std::collections::HashMap<_, _> = url.query_pairs().collect();
    assert_eq!(query["roomId"], "r-1");
    assert_eq!(query["customRoomId"], "my-room");
    assert_eq!(query["userId"], "u-1");
    assert_eq!(query["startDate"], "1700000000000");
    assert_eq!(query["endDate"], "1800000000000");
    assert_eq!(query["status"], "ended");
}

#[tokio::test]
async fn end_session_folds_the_meeting_id_alias_and_derives_status() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v2/sessions/end"))
        .and(body_json(
            json!({"roomId": "abcd-efgh-ijkl", "vsdkForceKill": true}),
        ))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": "s-1",
            "meetingId": "abcd-efgh-ijkl",
            "userMeetingId": "my-room",
            "end": "2026-01-01T00:00:00Z",
        })))
        .expect(1)
        .mount(&server)
        .await;

    let session = client(&server)
        .sessions()
        .end(SessionEndParams {
            // `meeting_id` is folded into `roomId` on the wire.
            meeting_id: Some("abcd-efgh-ijkl".into()),
            force: Some(true),
            ..Default::default()
        })
        .await
        .unwrap();

    assert_eq!(session.room_id, "abcd-efgh-ijkl");
    assert_eq!(session.custom_room_id.as_deref(), Some("my-room"));
    assert_eq!(session.status, Some(SessionStatus::ENDED));
}

#[tokio::test]
async fn end_session_requires_a_room_or_meeting_id() {
    let server = MockServer::start().await;
    let err = client(&server)
        .sessions()
        .end(SessionEndParams::default())
        .await
        .unwrap_err();

    assert!(err.is_validation());
    assert!(err.to_string().contains("room_id"));
    // The precondition fails before any request is issued.
    assert!(requests(&server).await.is_empty());
}

#[tokio::test]
async fn list_session_participants_lifts_them_out_of_the_session_envelope() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v2/sessions/s-1/participants"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "pageInfo": {"currentPage": 1, "perPage": 10, "lastPage": 1, "total": 2},
            "data": {"id": "s-1", "participants": [
                {"participantId": "p-1", "name": "Ada"},
                {"participantId": "p-2"},
            ]},
        })))
        .mount(&server)
        .await;

    let page = client(&server)
        .sessions()
        .list_participants("s-1", ListParams::default())
        .await
        .unwrap();

    assert_eq!(page.data.len(), 2);
    assert_eq!(page.data[0].participant_id, "p-1");
    assert_eq!(page.data[0].name, "Ada");
    assert_eq!(page.data[1].name, "", "an absent name defaults to empty");
    assert_eq!(page.total(), 2);
}

#[tokio::test]
async fn list_active_session_participants_hits_the_active_route() {
    let server = MockServer::start().await;
    mount(
        &server,
        "GET",
        "/v2/sessions/s-1/participants/active",
        json!({"data": {"participants": []}}),
    )
    .await;

    let page = client(&server)
        .sessions()
        .list_active_participants("s-1", ListParams::default())
        .await
        .unwrap();
    assert!(page.data.is_empty());
}

#[tokio::test]
async fn remove_participant_requires_a_room_or_session_id() {
    let server = MockServer::start().await;
    let err = client(&server)
        .sessions()
        .remove_participant(SessionRemoveParticipantParams {
            participant_id: "p-1".into(),
            ..Default::default()
        })
        .await
        .unwrap_err();

    assert!(err.is_validation());
    assert!(requests(&server).await.is_empty());
}

#[tokio::test]
async fn remove_participant_returns_the_confirmation_message() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v2/sessions/participants/remove"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!("Participant removed")))
        .mount(&server)
        .await;

    let message = client(&server)
        .sessions()
        .remove_participant(SessionRemoveParticipantParams {
            participant_id: "p-1".into(),
            session_id: Some("s-1".into()),
            ..Default::default()
        })
        .await
        .unwrap();

    assert_eq!(message, "Participant removed");
    assert_eq!(
        body(&only_request(&server).await),
        json!({"participantId": "p-1", "sessionId": "s-1"})
    );
}

#[tokio::test]
async fn session_stats_are_returned_untyped() {
    let server = MockServer::start().await;
    mount(
        &server,
        "GET",
        "/v2/sessions/s-1/stats",
        json!({"score": 4.8, "nested": {"a": 1}}),
    )
    .await;

    let stats = client(&server).sessions().get_stats("s-1").await.unwrap();
    assert_eq!(stats["score"], json!(4.8));
}

#[tokio::test]
async fn participant_stats_use_the_singular_participant_path() {
    let server = MockServer::start().await;
    mount(
        &server,
        "GET",
        "/v2/sessions/s-1/participant/p-1/stats",
        json!({}),
    )
    .await;

    client(&server)
        .sessions()
        .get_participant_stats("s-1", "p-1")
        .await
        .unwrap();
}

/* -------------------------------- participants ------------------------------ */

/// Mocks the active-session lookup that the room-keyed participant view does first.
async fn mount_active_session(server: &MockServer, session_id: Option<&str>) {
    let data = match session_id {
        Some(id) => json!([{ "id": id }]),
        None => json!([]),
    };
    Mock::given(method("GET"))
        .and(path("/v2/sessions"))
        .and(query_param("status", "ongoing"))
        .and(query_param("roomId", "r-1"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "data": data })))
        .mount(server)
        .await;
}

#[tokio::test]
async fn room_participants_resolve_the_active_session_first() {
    let server = MockServer::start().await;
    mount_active_session(&server, Some("s-9")).await;
    mount(
        &server,
        "GET",
        "/v2/sessions/s-9/participants/active",
        json!({"data": {"participants": [{"participantId": "p-1"}]}}),
    )
    .await;

    let page = client(&server)
        .participants()
        .list("r-1", ListParams::default())
        .await
        .unwrap();
    assert_eq!(page.data[0].participant_id, "p-1");
}

#[tokio::test]
async fn room_participants_are_an_empty_page_with_no_live_session() {
    let server = MockServer::start().await;
    mount_active_session(&server, None).await;

    let page = client(&server)
        .participants()
        .list("r-1", ListParams::default())
        .await
        .unwrap();
    assert!(page.data.is_empty());
    assert!(!page.has_next_page());
    // Only the session lookup was issued; no participants request followed.
    assert_eq!(requests(&server).await.len(), 1);
}

#[tokio::test]
async fn room_participant_stream_yields_nothing_with_no_live_session() {
    let server = MockServer::start().await;
    mount_active_session(&server, None).await;

    let client = client(&server);
    let items: Vec<_> = client
        .participants()
        .list_stream("r-1", ListParams::default())
        .collect()
        .await;
    assert!(items.is_empty());
}

#[tokio::test]
async fn room_participant_stream_walks_pages() {
    let server = MockServer::start().await;
    mount_active_session(&server, Some("s-9")).await;
    let page = |current: u32, id: &str| {
        json!({
            "pageInfo": {"currentPage": current, "perPage": 1, "lastPage": 2, "total": 2},
            "data": {"participants": [{"participantId": id}]},
        })
    };
    Mock::given(method("GET"))
        .and(path("/v2/sessions/s-9/participants/active"))
        .and(query_param("page", "2"))
        .respond_with(ResponseTemplate::new(200).set_body_json(page(2, "p-2")))
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/v2/sessions/s-9/participants/active"))
        .respond_with(ResponseTemplate::new(200).set_body_json(page(1, "p-1")))
        .mount(&server)
        .await;

    let client = client(&server);
    let ids: Vec<String> = client
        .participants()
        .list_stream("r-1", ListParams::default())
        .map(|p| p.unwrap().participant_id)
        .collect()
        .await;
    assert_eq!(ids, ["p-1", "p-2"]);
}

#[tokio::test]
async fn getting_a_room_participant_without_a_live_session_is_not_found() {
    let server = MockServer::start().await;
    mount_active_session(&server, None).await;

    let err = client(&server)
        .participants()
        .get("r-1", "p-1")
        .await
        .unwrap_err();
    assert!(err.is_not_found());
    assert!(err.to_string().contains("no active session"));
    // A client-side not-found carries no HTTP status.
    assert_eq!(err.status(), None);
}

#[tokio::test]
async fn removing_a_room_participant_lets_the_server_resolve_the_session() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v2/sessions/participants/remove"))
        .respond_with(ResponseTemplate::new(200).set_body_string("Removed"))
        .mount(&server)
        .await;

    let message = client(&server)
        .participants()
        .remove("r-1", "p-1")
        .await
        .unwrap();
    assert_eq!(message, "Removed");
    // No active-session lookup: the server resolves it from roomId.
    let request = only_request(&server).await;
    assert_eq!(
        body(&request),
        json!({"roomId": "r-1", "participantId": "p-1"})
    );
}