How can I test session.say? If I do session.run fo...
# ask-ai
b
How can I test session.say? If I do session.run for tests it seems to ignore it exists
t
Thanks for reaching out, miguel. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To test
session.say
effectively and ensure it works as expected during tests, follow these steps: • Mock Components: Set up your test environment with mocked STT, LLM, and TTS components to isolate the behavior of
session.say
. • Invoke `session.say`: Call
session.say()
with your test message in your test method. • Track Playback Events: Use event listeners to capture
PlaybackFinishedEvent
and verify that the message was played. • Verify Chat Context: Ensure the message is added to the chat context if
add_to_chat_ctx=True
. If
session.run
seems to ignore
session.say
, consider the following: • Asynchronous Execution: Ensure you
await
session.say()
to allow it to complete before
session.run
proceeds. • TTS Configuration: Verify that TTS is correctly configured in the session, as
session.say
won’t play messages without it. • Event Handling: Set up event listeners to capture speech events indicating playback completion. • SpeechHandle Management: Use
SpeechHandle
to wait for speech completion, ensuring
session.run
doesn’t skip it. Here’s an example test code snippet:
Copy code
async def test_session_say():
    speed = 5.0
    actions = FakeActions()
    actions.add_llm("Hello, test!")
    actions.add_tts(2.0)

    session = create_session(actions, speed_factor=speed)
    agent = MyAgent()

    playback_finished_events = []
    session.output.audio.on("playback_finished", playback_finished_events.append)

    await session.say("Test message")

    assert len(playback_finished_events) == 1
    assert not playback_finished_events[0].interrupted
By following these steps, you can ensure
session.say
is tested properly and works seamlessly with
session.run
.