# -*- coding: utf-8 -*- """Gezielte Tests: Kontakte-Nachrichten-Badge -> Chat-Huelle (offen/geschlossen) + Scroll.""" from __future__ import annotations import inspect import re import unittest from pathlib import Path ROOT = Path(__file__).resolve().parent HTML = (ROOT / "web" / "empfang.html").read_text(encoding="utf-8-sig") WEBVIEW = (ROOT / "aza_empfang_webview.py").read_text(encoding="utf-8") def _func_body(src: str, name: str) -> str: marker = f"function {name}" idx = src.find(marker) if idx < 0: return "" rest = src[idx + len(marker) :] nxt = rest.find("\nfunction ") return rest[:nxt] if nxt >= 0 else rest class ContactsMessageBadgeOpenShellTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.html = HTML cls.kp_msg = _func_body(cls.html, "kontaktPanelOpenMessagesBadgeInHuelle") cls.nav = _func_body(cls.html, "navigateFromMessagesBadgeClick") cls.contact_nav = _func_body(cls.html, "navigateFromContactMessagesBadgeClick") cls.refresh = _func_body(cls.html, "refreshDesktopShellContextFromServer") cls.peer_sig = _func_body(cls.html, "empfangShellMaybeConsumePeerRefreshSignal") cls.focus_src = inspect.getsource( __import__("aza_empfang_webview", fromlist=["EmpfangWebviewApi"]).EmpfangWebviewApi.focus_empfang_chat_shell ) def test_kontakt_panel_awaits_dm_open_before_focus(self): self.assertIn("await apiFetch", self.kp_msg) self.assertIn("dm_open_peer_user_id", self.kp_msg) self.assertIn("dm_open_msg_id", self.kp_msg) self.assertIn("focus_empfang_chat_shell", self.kp_msg) dm_idx = self.kp_msg.find("await apiFetch") focus_idx = self.kp_msg.find("focus_empfang_chat_shell") self.assertGreater(focus_idx, dm_idx) def test_header_badge_uses_shared_kontakt_helper(self): self.assertIn("kontaktPanelOpenMessagesBadgeInHuelle", self.nav) self.assertIn("findFirstPeerWithPendingAck", self.nav) self.assertIn("findFirstUnreadMessageIdForPeer", self.nav) self.assertNotIn("apiFetch(API_BASE + '/shell/dm-open'", self.nav) def test_contact_row_badge_uses_peer_user_id(self): self.assertIn("navigateFromContactMessagesBadgeClick", self.html) self.assertIn("sb-msg-badge-click", self.html) self.assertIn("kontaktPanelOpenMessagesBadgeInHuelle", self.contact_nav) self.assertIn("isKontaktPanelMode()", self.contact_nav) contact_click = _func_body(self.html, "navigateFromContactMessagesBadgeClick") self.assertIn("findFirstUnreadMessageIdForPeer", contact_click) def test_broker_skips_message_badge_clicks(self): broker = _func_body(self.html, "bindKontaktPanelBroker") self.assertIn("sb-msg-badge-click", broker) def test_chat_internal_badge_path_unchanged(self): idx = self.nav.find("if (isKontaktPanelMode())") self.assertGreater(idx, 0) after_kontakt = self.nav[self.nav.find("return;", idx) + len("return;") :] self.assertIn("armEmpfangBadgeScrollToUnread", after_kontakt) self.assertIn("selectDmPeer", after_kontakt) self.assertNotIn("kontaktPanelOpenMessagesBadgeInHuelle", after_kontakt) def test_force_reapply_on_peer_refresh_signal(self): self.assertIn("__empfangForceDmOpenApply", self.peer_sig) self.assertIn("__empfangForceDmOpenApply", self.refresh) self.assertIn("forceNav", self.refresh) def test_focus_uses_office_starter_not_autonomous_spawn(self): self.assertIn("exact_titles=(chat_title", self.focus_src) self.assertIn("request_office_open_empfang_chat_shell_ipc", self.focus_src) self.assertNotIn("_spawn_empfang_chat_shell_process()", self.focus_src) self.assertNotIn("_MODE_DESKTOP_SHELL", self.focus_src) def test_stale_hwnd_process_check(self): self.assertIn("_hwnd_process_alive", WEBVIEW) focus_fn = inspect.getsource( __import__("aza_empfang_webview", fromlist=["_focus_other_empfang_host_window"])._focus_other_empfang_host_window ) self.assertIn("_hwnd_process_alive", focus_fn) def test_task_badges_unchanged(self): task_nav = _func_body(self.html, "navigateFromTaskBadgeClick") self.assertIn("kontaktPanelNavigateFromBadge", task_nav) self.assertNotIn("kontaktPanelOpenMessagesBadgeInHuelle", task_nav) def test_scroll_helpers_present(self): self.assertIn("armEmpfangBadgeScrollToUnread", self.refresh) self.assertIn("scrollMessengerToMessageIdWithRetry", self.html) self.assertIn("consumeEmpfangBadgeScrollForActivePeer", self.html) def test_kontakt_panel_does_not_consume_shell_context(self): self.assertIn("if (isKontaktPanelMode()) return;", self.refresh) def test_no_name_only_peer_identification_in_kontakt_badge(self): self.assertIn("dm_open_peer_user_id: uid", self.kp_msg) self.assertNotRegex(self.kp_msg, r"dm_open_peer_user_id:\s*displayName") class FocusEmpfangChatShellSpawnTests(unittest.TestCase): def test_focus_returns_ok_immediately(self): from aza_empfang_webview import EmpfangWebviewApi api = EmpfangWebviewApi(mode="kontakt_panel") r = api.focus_empfang_chat_shell() self.assertTrue(r.get("ok")) if __name__ == "__main__": unittest.main()