-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathtest_livekitapi.py
More file actions
586 lines (507 loc) · 21.1 KB
/
Copy pathtest_livekitapi.py
File metadata and controls
586 lines (507 loc) · 21.1 KB
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# Copyright 2026 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""API tests that drive the unified LiveKitAPI against the mock LiveKit server
(livekit/livekit cmd/test-server). Point them at a running instance with
LK_TEST_SERVER_URL (default http://127.0.0.1:9999); they skip when no server is
reachable.
Because the mock enforces the same per-method grants as the real server, a call
that succeeds also proves the SDK attached the right grants automatically. The
smoke tests fully populate each request so they double as a reference for a
complete call and exercise field serialization. Mock directives are passed via
the X-Lk-Mock header, set as a default header on the session (the public service
methods don't expose per-call headers).
"""
import asyncio
import contextlib
import json
import os
import urllib.request
from typing import Optional
import aiohttp
import pytest
from google.protobuf.duration_pb2 import Duration
import livekit.api as api
from livekit.api import SipCallError, ServerError
from livekit.protocol.rtc import SessionDescription
BASE = os.getenv("LK_TEST_SERVER_URL", "http://127.0.0.1:9999")
# devkey/secret match `livekit-server --dev`, which the mock verifies against.
KEY, SECRET = "devkey", "secret"
def _server_up() -> bool:
try:
with urllib.request.urlopen(f"{BASE}/settings/regions", timeout=1) as r:
return r.status == 200
except Exception:
return False
pytestmark = pytest.mark.skipif(
not _server_up(), reason=f"mock test server not reachable at {BASE}"
)
@contextlib.asynccontextmanager
async def _api(mock: Optional[dict] = None):
"""A LiveKitAPI whose session carries the given X-Lk-Mock directives (if any)
as a default header on every request."""
headers = {"X-Lk-Mock": json.dumps(mock)} if mock else None
async with aiohttp.ClientSession(headers=headers) as session:
yield api.LiveKitAPI(BASE, KEY, SECRET, session=session)
def _mp4(path: str) -> api.EncodedFileOutput:
return api.EncodedFileOutput(file_type=api.EncodedFileType.MP4, filepath=path)
# -- smoke: fully-populated calls across every service ------------------------
async def _room_smoke():
async with _api() as lk:
await lk.room.create_room(
api.CreateRoomRequest(
name="test-room",
empty_timeout=300,
departure_timeout=60,
max_participants=50,
metadata='{"scene":"lobby"}',
min_playout_delay=100,
max_playout_delay=2000,
sync_streams=True,
agents=[api.RoomAgentDispatch(agent_name="greeter", metadata='{"lang":"en"}')],
)
)
await lk.room.list_rooms(api.ListRoomsRequest(names=["test-room", "lobby"]))
await lk.room.delete_room(api.DeleteRoomRequest(room="test-room"))
await lk.room.list_participants(api.ListParticipantsRequest(room="test-room"))
await lk.room.get_participant(
api.RoomParticipantIdentity(room="test-room", identity="participant-42")
)
await lk.room.remove_participant(
api.RoomParticipantIdentity(room="test-room", identity="participant-42")
)
await lk.room.forward_participant(
api.ForwardParticipantRequest(
room="test-room", identity="participant-42", destination_room="overflow-room"
)
)
await lk.room.move_participant(
api.MoveParticipantRequest(
room="test-room", identity="participant-42", destination_room="breakout-room"
)
)
await lk.room.mute_published_track(
api.MuteRoomTrackRequest(
room="test-room", identity="participant-42", track_sid="TR_video1", muted=True
)
)
await lk.room.update_participant(
api.UpdateParticipantRequest(
room="test-room",
identity="participant-42",
name="Alice",
metadata='{"role":"host"}',
attributes={"seat": "1A"},
permission=api.ParticipantPermission(
can_subscribe=True,
can_publish=True,
can_publish_data=True,
can_publish_sources=[api.TrackSource.MICROPHONE, api.TrackSource.CAMERA],
can_update_metadata=True,
),
)
)
await lk.room.update_subscriptions(
api.UpdateSubscriptionsRequest(
room="test-room",
identity="participant-42",
track_sids=["TR_video1"],
subscribe=True,
participant_tracks=[
api.ParticipantTracks(participant_sid="PA_xyz789", track_sids=["TR_video1"])
],
)
)
await lk.room.update_room_metadata(
api.UpdateRoomMetadataRequest(room="test-room", metadata='{"scene":"intro"}')
)
await lk.room.send_data(
api.SendDataRequest(
room="test-room",
data=b"hello world",
kind=api.DataPacket.RELIABLE,
destination_identities=["participant-42"],
topic="chat",
)
)
async def _egress_smoke():
async with _api() as lk:
await lk.egress.start_room_composite_egress(
api.RoomCompositeEgressRequest(
room_name="test-room", layout="grid", file_outputs=[_mp4("room.mp4")]
)
)
await lk.egress.start_web_egress(
api.WebEgressRequest(
url="https://example.com/scene",
stream_outputs=[
api.StreamOutput(
protocol=api.StreamProtocol.RTMP, urls=["rtmps://a.example.com/live/key"]
)
],
)
)
await lk.egress.start_participant_egress(
api.ParticipantEgressRequest(
room_name="test-room",
identity="participant-42",
screen_share=True,
file_outputs=[_mp4("participant.mp4")],
)
)
await lk.egress.start_track_composite_egress(
api.TrackCompositeEgressRequest(
room_name="test-room",
audio_track_id="TR_audio1",
video_track_id="TR_video1",
file_outputs=[_mp4("track-composite.mp4")],
)
)
await lk.egress.start_track_egress(
api.TrackEgressRequest(
room_name="test-room",
track_id="TR_video1",
file=api.DirectFileOutput(filepath="track.mp4"),
)
)
await lk.egress.start_egress(
api.StartEgressRequest(
room_name="test-room",
web=api.WebSource(url="https://example.com/scene"),
outputs=[
api.Output(
file=api.FileOutput(
file_type=api.EncodedFileType.MP4, filepath="unified.mp4"
)
)
],
)
)
await lk.egress.update_layout(
api.UpdateLayoutRequest(egress_id="EG_abc123", layout="speaker")
)
await lk.egress.update_stream(
api.UpdateStreamRequest(
egress_id="EG_abc123",
add_output_urls=["rtmps://b.example.com/live/key"],
remove_output_urls=["rtmps://a.example.com/live/key"],
)
)
await lk.egress.list_egress(
api.ListEgressRequest(room_name="test-room", egress_id="EG_abc123", active=True)
)
await lk.egress.stop_egress(api.StopEgressRequest(egress_id="EG_abc123"))
async def _ingress_smoke():
async with _api() as lk:
await lk.ingress.create_ingress(
api.CreateIngressRequest(
input_type=api.IngressInput.RTMP_INPUT,
name="stream-input",
room_name="test-room",
participant_identity="ingress-bot",
participant_name="Live Stream",
participant_metadata='{"source":"rtmp"}',
enable_transcoding=True,
audio=api.IngressAudioOptions(
name="audio",
source=api.TrackSource.MICROPHONE,
preset=api.IngressAudioEncodingPreset.OPUS_STEREO_96KBPS,
),
video=api.IngressVideoOptions(
name="video",
source=api.TrackSource.CAMERA,
preset=api.IngressVideoEncodingPreset.H264_1080P_30FPS_3_LAYERS,
),
)
)
await lk.ingress.update_ingress(
api.UpdateIngressRequest(
ingress_id="IN_abc123",
name="stream-input-v2",
room_name="test-room",
participant_identity="ingress-bot",
participant_name="Live Stream",
enable_transcoding=True,
)
)
await lk.ingress.list_ingress(
api.ListIngressRequest(room_name="test-room", ingress_id="IN_abc123")
)
await lk.ingress.delete_ingress(api.DeleteIngressRequest(ingress_id="IN_abc123"))
async def _sip_smoke():
async with _api() as lk:
await lk.sip.create_inbound_trunk(
api.CreateSIPInboundTrunkRequest(
trunk=api.SIPInboundTrunkInfo(
name="inbound",
metadata='{"provider":"telco"}',
numbers=["+15105550100"],
allowed_addresses=["203.0.113.0/24"],
allowed_numbers=["+15105550111"],
auth_username="sip-user",
auth_password="sip-pass",
krisp_enabled=True,
)
)
)
await lk.sip.create_outbound_trunk(
api.CreateSIPOutboundTrunkRequest(
trunk=api.SIPOutboundTrunkInfo(
name="outbound",
address="sip.telco.example.com",
transport=api.SIPTransport.SIP_TRANSPORT_TLS,
destination_country="US",
numbers=["+15105550100"],
auth_username="sip-user",
auth_password="sip-pass",
)
)
)
await lk.sip.update_inbound_trunk(
"ST_abc123",
api.SIPInboundTrunkInfo(name="inbound-v2", numbers=["+15105550100"]),
)
await lk.sip.update_outbound_trunk(
"ST_abc123",
api.SIPOutboundTrunkInfo(
name="outbound-v2",
address="sip.telco.example.com",
transport=api.SIPTransport.SIP_TRANSPORT_TLS,
numbers=["+15105550100"],
),
)
# field-level (kwargs) update helpers
await lk.sip.update_inbound_trunk_fields(
"ST_abc123", name="inbound-v3", metadata="{}", numbers=["+15105550100"]
)
await lk.sip.update_outbound_trunk_fields(
"ST_abc123", name="outbound-v3", address="sip.telco.example.com"
)
await lk.sip.list_inbound_trunk(
api.ListSIPInboundTrunkRequest(trunk_ids=["ST_abc123"], numbers=["+15105550100"])
)
await lk.sip.list_outbound_trunk(api.ListSIPOutboundTrunkRequest(trunk_ids=["ST_abc123"]))
await lk.sip.delete_trunk(api.DeleteSIPTrunkRequest(sip_trunk_id="ST_abc123"))
await lk.sip.create_dispatch_rule(
api.CreateSIPDispatchRuleRequest(
dispatch_rule=api.SIPDispatchRuleInfo(
name="direct-to-support",
metadata='{"team":"support"}',
trunk_ids=["ST_abc123"],
rule=api.SIPDispatchRule(
dispatch_rule_direct=api.SIPDispatchRuleDirect(
room_name="support", pin="1234"
)
),
)
)
)
await lk.sip.update_dispatch_rule(
"SDR_abc123",
api.SIPDispatchRuleInfo(
name="individual-v2",
rule=api.SIPDispatchRule(
dispatch_rule_individual=api.SIPDispatchRuleIndividual(room_prefix="call-")
),
),
)
await lk.sip.update_dispatch_rule_fields("SDR_abc123", name="rule-v3", metadata="{}")
await lk.sip.list_dispatch_rule(
api.ListSIPDispatchRuleRequest(
dispatch_rule_ids=["SDR_abc123"], trunk_ids=["ST_abc123"]
)
)
await lk.sip.delete_dispatch_rule(
api.DeleteSIPDispatchRuleRequest(sip_dispatch_rule_id="SDR_abc123")
)
async def _connector_smoke():
offer = SessionDescription(type="offer", sdp="v=0\r\no=- 0 0 IN IP4 127.0.0.1\r\n")
answer = SessionDescription(type="answer", sdp="v=0\r\no=- 0 0 IN IP4 127.0.0.1\r\n")
async with _api() as lk:
await lk.connector.dial_whatsapp_call(
api.DialWhatsAppCallRequest(
whatsapp_phone_number_id="123456789012345",
whatsapp_to_phone_number="+15105550100",
whatsapp_api_key="wa-secret-key",
whatsapp_cloud_api_version="23.0",
room_name="test-room",
participant_identity="whatsapp-caller",
participant_name="WhatsApp Caller",
destination_country="US",
ringing_timeout=Duration(seconds=30),
)
)
await lk.connector.accept_whatsapp_call(
api.AcceptWhatsAppCallRequest(
whatsapp_phone_number_id="123456789012345",
whatsapp_api_key="wa-secret-key",
whatsapp_cloud_api_version="23.0",
whatsapp_call_id="wacid.HBgLABC",
sdp=answer,
room_name="test-room",
participant_identity="whatsapp-callee",
)
)
await lk.connector.connect_whatsapp_call(
api.ConnectWhatsAppCallRequest(whatsapp_call_id="wacid.HBgLABC", sdp=offer)
)
await lk.connector.disconnect_whatsapp_call(
api.DisconnectWhatsAppCallRequest(
whatsapp_call_id="wacid.HBgLABC",
whatsapp_api_key="wa-secret-key",
disconnect_reason=api.DisconnectWhatsAppCallRequest.BUSINESS_INITIATED,
)
)
await lk.connector.connect_twilio_call(
api.ConnectTwilioCallRequest(
twilio_call_direction=api.ConnectTwilioCallRequest.TWILIO_CALL_DIRECTION_INBOUND,
room_name="test-room",
participant_identity="twilio-caller",
destination_country="US",
)
)
async def _agent_dispatch_smoke():
async with _api() as lk:
await lk.agent_dispatch.create_dispatch(
api.CreateAgentDispatchRequest(
room="test-room", agent_name="inbound-agent", metadata='{"lang":"en"}'
)
)
await lk.agent_dispatch.get_dispatch("AD_abc123", "test-room")
await lk.agent_dispatch.list_dispatch("test-room")
await lk.agent_dispatch.delete_dispatch("AD_abc123", "test-room")
def test_room_smoke():
asyncio.run(_room_smoke())
def test_egress_smoke():
asyncio.run(_egress_smoke())
def test_ingress_smoke():
asyncio.run(_ingress_smoke())
def test_sip_smoke():
asyncio.run(_sip_smoke())
def test_connector_smoke():
asyncio.run(_connector_smoke())
def test_agent_dispatch_smoke():
asyncio.run(_agent_dispatch_smoke())
# -- deep: create_room round-trip + error propagation -------------------------
async def _create_room_echo():
async with _api() as lk:
req = api.CreateRoomRequest(
name="echo-room", metadata='{"scene":"lobby"}', empty_timeout=300, max_participants=50
)
room = await lk.room.create_room(req)
assert room.name == "echo-room"
assert room.metadata == '{"scene":"lobby"}'
assert room.empty_timeout == 300
assert room.max_participants == 50
assert room.sid != "" # placeholder assigned by the mock
def test_create_room_echoes_fields():
asyncio.run(_create_room_echo())
async def _create_room_error():
mock = {"failRegions": [0], "failStatus": 400, "failTwirpCode": "invalid_argument"}
async with _api(mock) as lk:
with pytest.raises(ServerError) as exc:
await lk.room.create_room(api.CreateRoomRequest(name="test-room"))
assert exc.value.code == "invalid_argument"
def test_create_room_propagates_twirp_error():
asyncio.run(_create_room_error())
# -- deep: SIP participant (delayMs:0 skips the mock's answer wait) ------------
async def _sip_participant():
async with _api() as lk:
p = await lk.sip.create_sip_participant(
api.CreateSIPParticipantRequest(
sip_trunk_id="ST_abc123",
sip_call_to="+15105550100",
room_name="test-room",
participant_identity="sip-caller",
participant_name="SIP Caller",
participant_metadata='{"source":"pstn"}',
dtmf="1234#",
play_dialtone=True,
max_call_duration=Duration(seconds=3600),
)
)
assert p.room_name == "test-room"
assert p.participant_identity == "sip-caller"
async with _api({"delayMs": 0}) as lk:
await lk.sip.create_sip_participant(
api.CreateSIPParticipantRequest(
sip_trunk_id="ST_abc123",
sip_call_to="+15105550100",
room_name="test-room",
wait_until_answered=True,
ringing_timeout=Duration(seconds=2),
)
)
await lk.sip.transfer_sip_participant(
api.TransferSIPParticipantRequest(
room_name="test-room",
participant_identity="sip-caller",
transfer_to="tel:+15105550122",
ringing_timeout=Duration(seconds=2),
)
)
def test_sip_participant():
asyncio.run(_sip_participant())
# -- cross-cutting: token auth ------------------------------------------------
async def _token_auth():
token = api.AccessToken(KEY, SECRET).with_grants(api.VideoGrants(room_create=True)).to_jwt()
async with api.LiveKitAPI.with_token(token, BASE) as lk:
room = await lk.room.create_room(api.CreateRoomRequest(name="token-room"))
assert room.name == "token-room"
def test_token_auth():
asyncio.run(_token_auth())
# -- cross-cutting: SIP call errors surface as SipCallError -------------------
async def _sip_call_error(sip: dict, *, wait: bool = False):
create = api.CreateSIPParticipantRequest(
sip_trunk_id="ST_abc123", sip_call_to="+15105550100", room_name="test-room"
)
if wait:
create.wait_until_answered = True
create.ringing_timeout.CopyFrom(Duration(seconds=2))
async with _api({"delayMs": 0, "sipStatus": sip}) as lk:
with pytest.raises(SipCallError) as exc:
await lk.sip.create_sip_participant(create)
return exc.value
def test_sip_busy():
err = asyncio.run(_sip_call_error({"code": 486, "status": "Busy Here"}))
assert isinstance(err, ServerError)
assert err.code == "resource_exhausted"
assert err.sip_status_code == 486
assert err.sip_status == "Busy Here"
# printable representation makes the failure clear
assert "486" in str(err) and "Busy Here" in str(err)
def test_sip_declined():
err = asyncio.run(_sip_call_error({"code": 603, "status": "Decline"}))
assert err.code == "permission_denied"
assert err.sip_status_code == 603
def test_sip_no_answer():
# Callee doesn't pick up within ringing_timeout -> SIP 408 Request Timeout.
err = asyncio.run(_sip_call_error({"code": 408, "status": "Request Timeout"}, wait=True))
assert err.code == "deadline_exceeded"
assert err.sip_status_code == 408
# -- cross-cutting: client-side dial timeout ----------------------------------
async def _sip_dial_timeout():
# ringing 1s -> ~3s dial budget; the mock delays the answer past it.
async with _api({"delayMs": 4000}) as lk:
await lk.sip.create_sip_participant(
api.CreateSIPParticipantRequest(
sip_trunk_id="ST_abc123",
sip_call_to="+15105550100",
room_name="test-room",
wait_until_answered=True,
ringing_timeout=Duration(seconds=1),
)
)
def test_sip_dial_timeout():
with pytest.raises((asyncio.TimeoutError, TimeoutError)):
asyncio.run(_sip_dial_timeout())