# -*- coding: utf-8 -*- """Kontakte-Badge-Reopen: Office-SSO-Starter + Lifecycle statt autonomer Login-URL.""" from __future__ import annotations import inspect import os import tempfile import unittest from pathlib import Path from unittest.mock import MagicMock, patch import aza_empfang_shell_surface as surface import aza_empfang_webview as ew from aza_empfang_desktop_core import EmpfangDesktopCoreMixin class FocusUsesOfficeHandoffTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.focus_src = inspect.getsource(ew.EmpfangWebviewApi.focus_empfang_chat_shell) def test_exact_title_match_kept(self): self.assertIn("exact_titles=(chat_title", self.focus_src) def test_no_prefix_match_on_desktop(self): self.assertNotIn("_focus_other_empfang_host_window(prefixes", self.focus_src) def test_no_autonomous_spawn_in_focus_path(self): self.assertNotIn("_spawn_empfang_chat_shell_process()", self.focus_src) self.assertNotIn("_build_default_empfang_chat_shell_url", self.focus_src) def test_office_ipc_handoff_without_main_window_focus(self): self.assertIn("request_office_open_empfang_chat_shell_ipc", self.focus_src) self.assertNotIn("_focus_office_main_window()", self.focus_src) class OfficePollHandlerTests(unittest.TestCase): @classmethod def setUpClass(cls): import basis14 cls.poll_src = inspect.getsource(basis14.KGDesktopApp._poll_office_update_request_from_hull) def test_poll_consumes_chat_open_flag(self): self.assertIn("consume_office_open_empfang_chat_shell_request_flag", self.poll_src) def test_poll_uses_office_singleton_starter(self): self.assertIn("_empfang_open_webview_singleton", self.poll_src) def test_poll_touches_peer_refresh_before_open(self): self.assertIn("touch_shell_peer_refresh_signal", self.poll_src) class OfficeSingletonStarterTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.singleton_src = inspect.getsource(EmpfangDesktopCoreMixin._empfang_open_webview_singleton) def test_shell_session_post(self): self.assertIn("_fetch_empfang_shell_session_dict", self.singleton_src) def test_launch_token_url(self): self.assertIn("/empfang/shell/launch?token=", self.singleton_src) self.assertIn("target=empfang_chat_shell", self.singleton_src) def test_proc_tracked_for_lifecycle(self): self.assertIn("_empfang_webview_proc", self.singleton_src) self.assertIn("_aza_track_external_pid", self.singleton_src) class ShellSurfaceIpcRoundtripTests(unittest.TestCase): def test_request_and_consume_flag(self): with tempfile.TemporaryDirectory() as td: flag = Path(td) / surface._OFFICE_EMPFANG_CHAT_SHELL_OPEN_REQUEST with patch.object(surface, "_office_shell_ipc_path", return_value=flag): self.assertFalse(surface.consume_office_open_empfang_chat_shell_request_flag()) surface.request_office_open_empfang_chat_shell_ipc() self.assertTrue(flag.is_file()) self.assertTrue(surface.consume_office_open_empfang_chat_shell_request_flag()) self.assertFalse(flag.is_file()) self.assertFalse(surface.consume_office_open_empfang_chat_shell_request_flag()) class FocusEmpfangChatShellBehaviorTests(unittest.TestCase): @patch.object(ew, "_focus_other_empfang_host_window", return_value=False) @patch.object(surface, "request_office_open_empfang_chat_shell_ipc") @patch.object(surface, "touch_shell_peer_refresh_signal") def test_closed_shell_requests_office_not_spawn( self, _touch, req_ipc, _focus_win ): api = ew.EmpfangWebviewApi(mode=ew._MODE_KONTAKT_PANEL) r = api.focus_empfang_chat_shell() self.assertEqual(r, {"ok": True}) import time time.sleep(0.15) req_ipc.assert_called_once() @patch.object(ew, "_focus_other_empfang_host_window", return_value=True) @patch.object(surface, "request_office_open_empfang_chat_shell_ipc") @patch.object(surface, "touch_shell_peer_refresh_signal") def test_existing_shell_focus_only(self, _touch, req_ipc, _focus_win): api = ew.EmpfangWebviewApi(mode=ew._MODE_KONTAKT_PANEL) api.focus_empfang_chat_shell() import time time.sleep(0.15) req_ipc.assert_not_called() class OfficePollIntegrationTests(unittest.TestCase): def test_poll_opens_via_singleton(self): import basis14 host = MagicMock() host._shutdown_in_progress = False host._empfang_open_webview_singleton = MagicMock() flag = Path(tempfile.mkdtemp()) / surface._OFFICE_EMPFANG_CHAT_SHELL_OPEN_REQUEST flag.write_text("1", encoding="utf-8") try: with patch.object(surface, "_office_shell_ipc_path", return_value=flag): with patch.object( surface, "touch_shell_peer_refresh_signal" ) as touch: basis14.KGDesktopApp._poll_office_update_request_from_hull(host) touch.assert_called_once_with(source="office_kontakt_open") host._empfang_open_webview_singleton.assert_called_once_with( silent_status=True, offer_tk_on_shell_fail=False, ) finally: if flag.is_file(): flag.unlink() if __name__ == "__main__": unittest.main()