import sys import unittest import importlib import tempfile from pathlib import Path from unittest import mock sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) sys.modules.pop("cthulhu.speech", None) speech = importlib.import_module("cthulhu.speech") class PresentationInterruptSoundRegressionTests(unittest.TestCase): def test_speech_stop_also_stops_sound_playback(self): speechServer = mock.Mock() echoServer = mock.Mock() soundPlayer = mock.Mock() with ( mock.patch.object(speech, "_speechserver", speechServer), mock.patch.object(speech, "_echoSpeechserver", echoServer), mock.patch.object(speech.sound, "getPlayer", return_value=soundPlayer), ): speech.stop() speechServer.stop.assert_called_once_with() echoServer.stop.assert_called_once_with() soundPlayer.stop.assert_called_once_with() def test_speech_icons_interrupt_previous_sound_when_speech_is_queued(self): soundPlayer = mock.Mock() with tempfile.TemporaryDirectory() as temporaryDirectory: soundPath = Path(temporaryDirectory) / "button.wav" soundPath.write_bytes(b"RIFF") icon = speech.Icon(temporaryDirectory, "button.wav") with mock.patch.object(speech.sound, "getPlayer", return_value=soundPlayer): speech.speak([icon], interrupt=False) soundPlayer.play.assert_called_once_with(icon, interrupt=True) if __name__ == "__main__": unittest.main()