From 3d62b9c203b6ddc8faf829e6a050d00404071af1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Mon, 8 Dec 2025 18:42:38 -0800 Subject: [PATCH] tests: added user turn start strategies unit tests --- tests/test_interruption_strategies.py | 2 +- tests/test_user_turn_start_strategy.py | 106 +++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 tests/test_user_turn_start_strategy.py diff --git a/tests/test_interruption_strategies.py b/tests/test_interruption_strategies.py index aa1bd7625..7700f77c8 100644 --- a/tests/test_interruption_strategies.py +++ b/tests/test_interruption_strategies.py @@ -9,7 +9,7 @@ import unittest from pipecat.audio.interruptions.min_words_interruption_strategy import MinWordsInterruptionStrategy -class TestInterruptionStrategy(unittest.IsolatedAsyncioTestCase): +class TestMinWordsInterruptionStrategy(unittest.IsolatedAsyncioTestCase): async def test_min_words(self): strategy = MinWordsInterruptionStrategy(min_words=2) await strategy.append_text("Hello") diff --git a/tests/test_user_turn_start_strategy.py b/tests/test_user_turn_start_strategy.py new file mode 100644 index 000000000..144dd15cc --- /dev/null +++ b/tests/test_user_turn_start_strategy.py @@ -0,0 +1,106 @@ +# +# Copyright (c) 2024-2025 Daily +# +# SPDX-License-Identifier: BSD 2-Clause License +# + +import unittest + +from pipecat.frames.frames import ( + InterimTranscriptionFrame, + TranscriptionFrame, + VADUserStartedSpeakingFrame, + VADUserStoppedSpeakingFrame, +) +from pipecat.turns.user.min_words_user_turn_start_strategy import MinWordsUserTurnStartStrategy +from pipecat.turns.user.vad_user_turn_start_strategy import VADUserTurnStartStrategy + + +class TestMinWordsInterruptionStrategy(unittest.IsolatedAsyncioTestCase): + async def test_only_transcriptions(self): + strategy = MinWordsUserTurnStartStrategy(min_words=2) + + should_start = None + + @strategy.event_handler("on_user_turn_started") + async def on_user_turn_started(strategy): + nonlocal should_start + should_start = True + + await strategy.process_frame(TranscriptionFrame(text="Hello", user_id="cat", timestamp="")) + self.assertFalse(should_start) + + await strategy.process_frame( + TranscriptionFrame(text=" there!", user_id="cat", timestamp="") + ) + self.assertTrue(should_start) + + # Reset and check again + should_start = None + await strategy.reset() + + await strategy.process_frame(TranscriptionFrame(text="Hello!", user_id="cat", timestamp="")) + self.assertFalse(should_start) + + await strategy.process_frame( + TranscriptionFrame(text="How are you?", user_id="cat", timestamp="") + ) + self.assertTrue(should_start) + + async def test_only_interim_transcriptions(self): + strategy = MinWordsUserTurnStartStrategy(min_words=2) + + should_start = None + + @strategy.event_handler("on_user_turn_started") + async def on_user_turn_started(strategy): + nonlocal should_start + should_start = True + + await strategy.process_frame( + InterimTranscriptionFrame(text="Hello", user_id="cat", timestamp="") + ) + self.assertFalse(should_start) + + await strategy.process_frame( + InterimTranscriptionFrame(text="Hello there!", user_id="cat", timestamp="") + ) + self.assertTrue(should_start) + + async def test_all_transcriptions(self): + strategy = MinWordsUserTurnStartStrategy(min_words=2) + + should_start = None + + @strategy.event_handler("on_user_turn_started") + async def on_user_turn_started(strategy): + nonlocal should_start + should_start = True + + await strategy.process_frame( + InterimTranscriptionFrame(text="Hello", user_id="cat", timestamp="") + ) + self.assertFalse(should_start) + + await strategy.process_frame( + TranscriptionFrame(text="Hello there!", user_id="cat", timestamp="") + ) + self.assertTrue(should_start) + + +class TestVADUserTurnStartStrategy(unittest.IsolatedAsyncioTestCase): + async def test_vad_strategy(self): + strategy = VADUserTurnStartStrategy() + + should_start = None + + @strategy.event_handler("on_user_turn_started") + async def on_user_turn_started(strategy): + nonlocal should_start + should_start = True + + await strategy.process_frame(VADUserStoppedSpeakingFrame()) + self.assertFalse(should_start) + + await strategy.process_frame(VADUserStartedSpeakingFrame()) + self.assertTrue(should_start)