|
| 1 | +import pytest |
| 2 | +import asyncio |
| 3 | + |
| 4 | +from vocode.streaming.models.events import PhoneCallEndedEvent, EventType |
| 5 | +from vocode.streaming.utils.events_manager import EventsManager |
| 6 | + |
| 7 | +CONVERSATION_ID = "1" |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.asyncio |
| 11 | +async def test_initialization(): |
| 12 | + manager = EventsManager() |
| 13 | + assert manager.subscriptions == set() |
| 14 | + assert isinstance(manager.queue, asyncio.Queue) |
| 15 | + assert not manager.active |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.asyncio |
| 19 | +async def test_publish_event(): |
| 20 | + event = PhoneCallEndedEvent( |
| 21 | + conversation_id=CONVERSATION_ID, type=EventType.PHONE_CALL_ENDED |
| 22 | + ) # Replace with actual Event creation |
| 23 | + manager = EventsManager([EventType.PHONE_CALL_ENDED]) |
| 24 | + manager.publish_event(event) |
| 25 | + assert not manager.queue.empty() |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.asyncio |
| 29 | +async def test_handle_event_default_implementation(): |
| 30 | + event = PhoneCallEndedEvent( |
| 31 | + conversation_id=CONVERSATION_ID, type=EventType.PHONE_CALL_ENDED |
| 32 | + ) # Replace with actual Event creation |
| 33 | + manager = EventsManager([EventType.PHONE_CALL_ENDED]) |
| 34 | + await manager.handle_event(event) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.asyncio |
| 38 | +async def test_start_and_active_loop(): |
| 39 | + event = PhoneCallEndedEvent( |
| 40 | + conversation_id=CONVERSATION_ID, type=EventType.PHONE_CALL_ENDED |
| 41 | + ) # Replace with actual Event creation |
| 42 | + manager = EventsManager([EventType.PHONE_CALL_ENDED]) |
| 43 | + asyncio.create_task(manager.start()) |
| 44 | + manager.publish_event(event) |
| 45 | + await asyncio.sleep(0.1) |
| 46 | + manager.active = False |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.asyncio |
| 50 | +async def test_flush_method(): |
| 51 | + event = PhoneCallEndedEvent( |
| 52 | + conversation_id=CONVERSATION_ID, type=EventType.PHONE_CALL_ENDED |
| 53 | + ) |
| 54 | + manager = EventsManager([EventType.PHONE_CALL_ENDED]) |
| 55 | + for _ in range(5): |
| 56 | + manager.publish_event(event) |
| 57 | + await manager.flush(timeout=2) |
| 58 | + assert manager.queue.empty() |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_queue_empty_and_timeout(): |
| 63 | + manager = EventsManager([EventType.TRANSCRIPT]) |
| 64 | + await manager.flush(timeout=0) |
| 65 | + assert manager.queue.empty() |
0 commit comments