diff options
| author | Yuval Adam <_@yuv.al> | 2026-07-24 12:24:07 +0200 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2026-07-24 12:24:07 +0200 |
| commit | 7f99c1957f29d57c006a11a02226ecab030a1d4c (patch) | |
| tree | b9fc555d6bfd603847eb066ed71acbe5d21c09b3 /tests/test_controller.py | |
| parent | df594d42ad1206fcb246bedb1945949527d7e039 (diff) | |
Add D-Bus daemon and Arch user service packaging
Diffstat (limited to 'tests/test_controller.py')
| -rw-r--r-- | tests/test_controller.py | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/tests/test_controller.py b/tests/test_controller.py new file mode 100644 index 0000000..61c291e --- /dev/null +++ b/tests/test_controller.py @@ -0,0 +1,88 @@ +from pathlib import Path +import tempfile +import threading +import unittest + +from parley.config import Config +from parley.controller import Controller +from parley.state import State + + +class FakeRecorder: + def __init__(self) -> None: + self.path = None + self.cancelled = False + + def start(self, path: Path) -> None: + self.path = path + path.write_bytes(b"RIFF") + + def stop(self) -> Path: + return self.path + + def cancel(self) -> None: + self.cancelled = True + if self.path: + self.path.unlink(missing_ok=True) + + +class FakeTranscriber: + def transcribe(self, wav_path: Path, workspace: Path) -> str: + return "hello from controller" + + def cancel(self) -> None: + pass + + +class ControllerTests(unittest.TestCase): + def setUp(self) -> None: + self.temporary = tempfile.TemporaryDirectory() + root = Path(self.temporary.name) + ffmpeg = root / "ffmpeg" + runtime = root / "transcribe-cli" + model = root / "model.gguf" + for path in (ffmpeg, runtime): + path.write_text("#!/bin/sh\n") + path.chmod(0o755) + model.touch() + self.config = Config(ffmpeg, runtime, model, root / "transcripts") + + def tearDown(self) -> None: + self.temporary.cleanup() + + def test_successful_operation_persists_before_callback(self) -> None: + ready = threading.Event() + received = [] + controller = Controller( + self.config, + on_transcript=lambda text, path: (received.append((text, path)), ready.set()), + ) + controller.recorder = FakeRecorder() + controller.transcriber = FakeTranscriber() + try: + controller.start() + self.assertEqual(controller.state, State.RECORDING) + controller.stop_and_transcribe() + self.assertTrue(ready.wait(2), "transcription callback was not delivered") + self.assertEqual(controller.state, State.IDLE) + text, path = received[0] + self.assertEqual(text, "hello from controller") + self.assertEqual(path.read_text(), "hello from controller\n") + finally: + controller.close() + + def test_cancel_recording_discards_operation(self) -> None: + controller = Controller(self.config) + recorder = FakeRecorder() + controller.recorder = recorder + try: + controller.start() + controller.cancel() + self.assertEqual(controller.state, State.IDLE) + self.assertTrue(recorder.cancelled) + finally: + controller.close() + + +if __name__ == "__main__": + unittest.main() |
