# -*- coding: utf-8 -*- """Tests: Startpanel Praxis Chat Office-SSO (Block B).""" from __future__ import annotations import unittest from pathlib import Path from unittest.mock import MagicMock, patch import aza_start_panel as sp class TestStartpanelChatOfficeSso(unittest.TestCase): def test_start_praxischat_office_running_uses_ipc(self) -> None: with patch( "aza_chat_desktop_host._office_desktop_running", return_value=True ), patch( "aza_empfang_shell_surface.request_office_open_empfang_chat_shell_ipc" ) as ipc, patch.object(sp, "_launch_praxischat_process") as launch: ok, msg = sp.start_praxischat() self.assertTrue(ok) ipc.assert_called_once() launch.assert_not_called() self.assertIn("Office", msg) def test_start_praxischat_profile_token_handoff(self) -> None: with patch( "aza_chat_desktop_host._office_desktop_running", return_value=False ), patch.object(sp, "_startpanel_fetch_shell_token", return_value="tok-abc"), patch.object( sp, "_launch_praxischat_process", return_value=(True, "ok") ) as launch: ok, _msg = sp.start_praxischat() self.assertTrue(ok) launch.assert_called_once_with(["--handoff-token=tok-abc"]) def test_start_praxischat_no_profile_falls_back(self) -> None: with patch( "aza_chat_desktop_host._office_desktop_running", return_value=False ), patch.object(sp, "_startpanel_fetch_shell_token", return_value=None), patch.object( sp, "_launch_praxischat_process", return_value=(True, "bare") ) as launch: ok, msg = sp.start_praxischat() self.assertTrue(ok) launch.assert_called_once_with() self.assertEqual(msg, "bare") def test_fetch_shell_token_requires_practice_and_user(self) -> None: with patch("aza_persistence.load_user_profile", return_value={"practice_id": "p1"}): self.assertIsNone(sp._startpanel_fetch_shell_token()) def test_fetch_shell_token_success(self) -> None: resp = MagicMock(status_code=200) resp.json.return_value = {"shell_token": "shell-xyz"} with patch( "aza_persistence.load_user_profile", return_value={"practice_id": "p1", "empfang_user_id": "u1"}, ), patch.object(sp, "_startpanel_backend_url", return_value="https://api.test"), patch.object( sp, "_startpanel_backend_token", return_value="api-tok" ), patch("requests.post", return_value=resp) as post: tok = sp._startpanel_fetch_shell_token() self.assertEqual(tok, "shell-xyz") post.assert_called_once() _args, kwargs = post.call_args self.assertIn("/empfang/shell/session", _args[0]) hdrs = kwargs["headers"] self.assertEqual(hdrs["X-Practice-Id"], "p1") self.assertEqual(hdrs["X-AzA-Empfang-User-Id"], "u1") def test_source_contains_ipc_and_handoff(self) -> None: src = Path(sp.__file__).read_text(encoding="utf-8") self.assertIn("request_office_open_empfang_chat_shell_ipc", src) self.assertIn("--handoff-token=", src) self.assertIn("_startpanel_fetch_shell_token", src) if __name__ == "__main__": unittest.main()