2121import asyncio
2222import os
2323import uuid
24- from typing import Callable , TypeVar
24+ from typing import Any , Callable , Dict , List , TypeVar
2525import numpy as np
26- import pytest
26+ import pytest # type: ignore[import-not-found]
2727
2828from livekit import rtc , api
29- from livekit .rtc .utils import sine_wave_generator
29+ from livekit .rtc .utils import sine_wave_generator # type: ignore[attr-defined]
3030
3131
3232SAMPLE_RATE = 48000
@@ -55,7 +55,7 @@ async def assert_eventually(
5555 raise AssertionError (f"{ message } (last result: { last_result } )" )
5656
5757
58- def skip_if_no_credentials ():
58+ def skip_if_no_credentials () -> Any :
5959 required_vars = ["LIVEKIT_URL" , "LIVEKIT_API_KEY" , "LIVEKIT_API_SECRET" ]
6060 missing = [var for var in required_vars if not os .getenv (var )]
6161 return pytest .mark .skipif (
@@ -82,12 +82,13 @@ def unique_room_name(base: str) -> str:
8282 return f"{ base } -{ uuid .uuid4 ().hex [:8 ]} "
8383
8484
85- @pytest .mark .asyncio
86- @skip_if_no_credentials ()
87- async def test_publish_track ():
85+ @pytest .mark .asyncio # type: ignore[misc]
86+ @skip_if_no_credentials () # type: ignore[misc]
87+ async def test_publish_track () -> None :
8888 """Test that a published track can be subscribed by another participant"""
8989 room_name = unique_room_name ("test-publish-track" )
9090 url = os .getenv ("LIVEKIT_URL" )
91+ assert url is not None
9192
9293 publisher_room = rtc .Room ()
9394 subscriber_room = rtc .Room ()
@@ -102,15 +103,15 @@ async def test_publish_track():
102103 @subscriber_room .on ("track_published" )
103104 def on_track_published (
104105 publication : rtc .RemoteTrackPublication , participant : rtc .RemoteParticipant
105- ):
106+ ) -> None :
106107 track_published_event .set ()
107108
108109 @subscriber_room .on ("track_subscribed" )
109110 def on_track_subscribed (
110111 track : rtc .Track ,
111112 publication : rtc .RemoteTrackPublication ,
112113 participant : rtc .RemoteParticipant ,
113- ):
114+ ) -> None :
114115 nonlocal subscribed_track
115116 if track .kind == rtc .TrackKind .KIND_AUDIO :
116117 subscribed_track = track
@@ -140,12 +141,13 @@ def on_track_subscribed(
140141 await subscriber_room .disconnect ()
141142
142143
143- @pytest .mark .asyncio
144- @skip_if_no_credentials ()
145- async def test_audio_stream_subscribe ():
144+ @pytest .mark .asyncio # type: ignore[misc]
145+ @skip_if_no_credentials () # type: ignore[misc]
146+ async def test_audio_stream_subscribe () -> None :
146147 """Test that published audio can be consumed and has similar energy levels"""
147148 room_name = unique_room_name ("test-audio-stream" )
148149 url = os .getenv ("LIVEKIT_URL" )
150+ assert url is not None
149151
150152 publisher_room = rtc .Room ()
151153 subscriber_room = rtc .Room ()
@@ -161,7 +163,7 @@ def on_track_subscribed(
161163 track : rtc .Track ,
162164 publication : rtc .RemoteTrackPublication ,
163165 participant : rtc .RemoteParticipant ,
164- ):
166+ ) -> None :
165167 nonlocal subscribed_track
166168 if track .kind == rtc .TrackKind .KIND_AUDIO :
167169 subscribed_track = track
@@ -178,9 +180,9 @@ def on_track_subscribed(
178180 await publisher_room .local_participant .publish_track (track , options )
179181 target_duration = 5.0
180182
181- published_energy = []
183+ published_energy : List [ Any ] = []
182184
183- async def publish_audio ():
185+ async def publish_audio () -> None :
184186 async for frame in sine_wave_generator (440 , target_duration , SAMPLE_RATE ):
185187 data = np .frombuffer (frame .data .tobytes (), dtype = np .int16 )
186188 energy = np .mean (np .abs (data .astype (np .float32 )))
@@ -235,20 +237,21 @@ async def publish_audio():
235237 await subscriber_room .disconnect ()
236238
237239
238- @pytest .mark .asyncio
239- @skip_if_no_credentials ()
240- async def test_room_lifecycle_events ():
240+ @pytest .mark .asyncio # type: ignore[misc]
241+ @skip_if_no_credentials () # type: ignore[misc]
242+ async def test_room_lifecycle_events () -> None :
241243 """Test that room lifecycle and track events are fired properly"""
242244 room_name = unique_room_name ("test-lifecycle-events" )
243245 url = os .getenv ("LIVEKIT_URL" )
246+ assert url is not None
244247
245248 room1 = rtc .Room ()
246249 room2 = rtc .Room ()
247250
248251 token1 = create_token ("participant-1" , room_name )
249252 token2 = create_token ("participant-2" , room_name )
250253
251- events = {
254+ events : Dict [ str , List [ str ]] = {
252255 "disconnected" : [],
253256 "participant_connected" : [],
254257 "participant_disconnected" : [],
@@ -263,51 +266,51 @@ async def test_room_lifecycle_events():
263266 }
264267
265268 @room1 .on ("disconnected" )
266- def on_room1_disconnected (reason ) :
269+ def on_room1_disconnected (reason : Any ) -> None :
267270 events ["disconnected" ].append ("room1" )
268271
269272 @room1 .on ("participant_connected" )
270- def on_room1_participant_connected (participant : rtc .RemoteParticipant ):
273+ def on_room1_participant_connected (participant : rtc .RemoteParticipant ) -> None :
271274 events ["participant_connected" ].append (f"room1-{ participant .identity } " )
272275
273276 @room1 .on ("participant_disconnected" )
274- def on_room1_participant_disconnected (participant : rtc .RemoteParticipant ):
277+ def on_room1_participant_disconnected (participant : rtc .RemoteParticipant ) -> None :
275278 events ["participant_disconnected" ].append (f"room1-{ participant .identity } " )
276279
277280 @room1 .on ("local_track_published" )
278- def on_room1_local_track_published (publication : rtc .LocalTrackPublication , track ) :
281+ def on_room1_local_track_published (publication : rtc .LocalTrackPublication , track : Any ) -> None :
279282 events ["local_track_published" ].append (f"room1-{ publication .sid } " )
280283
281284 @room1 .on ("local_track_unpublished" )
282- def on_room1_local_track_unpublished (publication : rtc .LocalTrackPublication ):
285+ def on_room1_local_track_unpublished (publication : rtc .LocalTrackPublication ) -> None :
283286 events ["local_track_unpublished" ].append (f"room1-{ publication .sid } " )
284287
285288 @room1 .on ("room_updated" )
286- def on_room1_room_updated ():
289+ def on_room1_room_updated () -> None :
287290 events ["room_updated" ].append ("room1" )
288291
289292 @room1 .on ("connection_state_changed" )
290- def on_room1_connection_state_changed (state : rtc .ConnectionState ):
293+ def on_room1_connection_state_changed (state : rtc .ConnectionState ) -> None :
291294 events ["connection_state_changed" ].append (f"room1-{ state } " )
292295
293296 @room2 .on ("track_published" )
294297 def on_room2_track_published (
295298 publication : rtc .RemoteTrackPublication , participant : rtc .RemoteParticipant
296- ):
299+ ) -> None :
297300 events ["track_published" ].append (f"room2-{ publication .sid } " )
298301
299302 @room2 .on ("track_subscribed" )
300303 def on_room2_track_subscribed (
301304 track : rtc .Track ,
302305 publication : rtc .RemoteTrackPublication ,
303306 participant : rtc .RemoteParticipant ,
304- ):
307+ ) -> None :
305308 events ["track_subscribed" ].append (f"room2-{ publication .sid } " )
306309
307310 @room2 .on ("track_unpublished" )
308311 def on_room2_track_unpublished (
309312 publication : rtc .RemoteTrackPublication , participant : rtc .RemoteParticipant
310- ):
313+ ) -> None :
311314 events ["track_unpublished" ].append (f"room2-{ publication .sid } " )
312315
313316 try :
@@ -388,20 +391,21 @@ def on_room2_track_unpublished(
388391 await room2 .disconnect ()
389392
390393
391- @pytest .mark .asyncio
392- @skip_if_no_credentials ()
393- async def test_connection_state_transitions ():
394+ @pytest .mark .asyncio # type: ignore[misc]
395+ @skip_if_no_credentials () # type: ignore[misc]
396+ async def test_connection_state_transitions () -> None :
394397 """Test that connection state transitions work correctly"""
395398 room_name = unique_room_name ("test-connection-state" )
396399 url = os .getenv ("LIVEKIT_URL" )
400+ assert url is not None
397401
398402 room = rtc .Room ()
399403 token = create_token ("state-test" , room_name )
400404
401- states = []
405+ states : List [ rtc . ConnectionState ] = []
402406
403407 @room .on ("connection_state_changed" )
404- def on_state_changed (state : rtc .ConnectionState ):
408+ def on_state_changed (state : rtc .ConnectionState ) -> None :
405409 states .append (state )
406410
407411 try :
@@ -414,7 +418,7 @@ def on_state_changed(state: rtc.ConnectionState):
414418 message = "Room did not reach CONN_CONNECTED state" ,
415419 )
416420 await assert_eventually (
417- lambda : rtc .ConnectionState .CONN_CONNECTED in states ,
421+ lambda : rtc .ConnectionState .CONN_CONNECTED in states , # type: ignore[comparison-overlap]
418422 message = "CONN_CONNECTED state not in state change events" ,
419423 )
420424
0 commit comments