Files
aza/AzA march 2026/_test_translator_transcription_popup_ack.py
2026-06-13 22:47:31 +02:00

143 lines
5.2 KiB
Python

# -*- coding: utf-8 -*-
"""Gezielte Tests: Uebersetzer-Singleton, Transkriptions-Retry, Popup-Ack."""
from __future__ import annotations
import inspect
import os
import tempfile
import time
import unittest
from unittest.mock import MagicMock, patch
import basis14
import translate
from aza_audio import wait_for_audio_file_stable
class TestTranslatorSingleton(unittest.TestCase):
def tearDown(self):
translate._clear_main_root()
def test_focus_existing_returns_none_without_window(self):
self.assertIsNone(translate.focus_existing_translator())
def test_open_uebersetzer_frozen_uses_embedded_main(self):
src = inspect.getsource(basis14.KGDesktopApp._open_uebersetzer)
self.assertIn("translate.main(parent=self)", src)
self.assertNotIn("daemon=True", src)
self.assertIn("focus_existing_translator", src)
def test_translate_spec_hiddenimport_present(self):
spec_path = os.path.join(os.path.dirname(__file__), "aza_desktop.spec")
with open(spec_path, encoding="utf-8") as fh:
body = fh.read()
self.assertIn('"translate"', body)
class TestTranscriptionRetry(unittest.TestCase):
def test_retry_delays_configured(self):
delays = basis14.KGDesktopApp._TRANSCRIBE_RETRY_DELAYS
self.assertEqual(delays, (0.5, 1.0, 2.0))
def test_empty_transcript_raises_runtime_error(self):
app = MagicMock()
app.get_backend_url.return_value = "https://example.test"
app.get_backend_token.return_value = "tok"
app.get_practice_id.return_value = "pid"
app._get_consent_user_id.return_value = "u1"
app._get_transcribe_prompt.return_value = ""
app._get_transcribe_domain.return_value = "medical"
app._autotext_data = {"user_specialty_default": "dermatology"}
app._calc_transcribe_timeout.return_value = 30
app._debug_log = MagicMock()
app.after = MagicMock()
resp = MagicMock()
resp.ok = True
resp.status_code = 200
resp.text = '{"success": true, "transcript": " "}'
resp.json.return_value = {"success": True, "transcript": " "}
health = MagicMock()
health.ok = True
health.status_code = 200
health.text = "ok"
session = MagicMock()
session.get.return_value = health
session.post.return_value = resp
session.__enter__ = MagicMock(return_value=session)
session.__exit__ = MagicMock(return_value=False)
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
tmp.write(b"RIFF")
path = tmp.name
try:
with patch("basis14.requests.Session", return_value=session), patch(
"basis14._get_or_create_device_id", return_value="dev"
), patch("aza_audio.wait_for_audio_file_stable", return_value=True):
real = basis14.KGDesktopApp._transcribe_single_attempt
with self.assertRaises(RuntimeError) as ctx:
real(app, path, 1)
self.assertIn("leeres Transkript", str(ctx.exception))
finally:
os.unlink(path)
def test_wait_for_audio_file_stable_detects_stable_size(self):
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
tmp.write(b"abcd")
path = tmp.name
try:
self.assertTrue(wait_for_audio_file_stable(path, timeout=1.0))
finally:
os.unlink(path)
class TestPopupAck(unittest.TestCase):
def test_quit_notification_calls_ack_and_dismiss(self):
class _AppStub:
def __init__(self):
self._empfang_dismissed_notification_ids = set()
self._empfang_ack_message = MagicMock()
_dismiss_empfang_notification = basis14.KGDesktopApp._dismiss_empfang_notification
app = _AppStub()
basis14.KGDesktopApp._empfang_quit_notification(app, "msg-1", external_dm=True)
self.assertIn("msg-1", app._empfang_dismissed_notification_ids)
app._empfang_ack_message.assert_called_once_with("msg-1", external_dm=True)
def test_popup_close_uses_quit_notification(self):
src = open(
os.path.join(os.path.dirname(__file__), "aza_empfang_incoming_popup.py"),
encoding="utf-8",
).read()
self.assertIn("_empfang_quit_notification", src)
self.assertIn("skip_ack=True", src)
def test_incoming_skips_chat_ack_messages(self):
app = MagicMock()
app._empfang_self_user_id.return_value = "me"
app._empfang_native_alert_message_targets_me.return_value = True
app._empfang_incoming_sender_is_other.return_value = True
fn = basis14.KGDesktopApp._empfang_native_alert_is_incoming
m = {
"status": "offen",
"extras": {"chat_ack": True, "sender_user_id": "other"},
}
self.assertFalse(fn(app, m))
class TestPopupCentering(unittest.TestCase):
def test_raise_popup_uses_vertical_center(self):
src = open(
os.path.join(os.path.dirname(__file__), "aza_empfang_incoming_popup.py"),
encoding="utf-8",
).read()
self.assertIn("vertical_center=True", src)
self.assertIn("flash_ms=1500", src)
if __name__ == "__main__":
unittest.main()