zapretgui/tests/test_show_window_bridge.py
loop-uh 7910e578ed
All checks were successful
Zapret source guards / validate (push) Successful in 8s
Platform layer: privileges and single instance behind platforms.current()
Second and third seams of the multi-platform project. platforms.current()
now also offers privileges (is_elevated, relaunch_elevated, and
gui_needs_elevation: true on Windows, false on Linux where a root helper
service will do the privileged work) and single_instance (Windows: mutex +
named event as before; Linux: flock lock file + Unix socket carrying 'show the
window', bound before Qt starts so an early signal is not lost).

shell_bootstrap, the show-window bridge and every IsUserAnAdmin call site
(system state, program settings, winws health diagnostics, Defender manager,
NSSM diagnostics) go through the platform. startup/admin_check.py (its two
restart helpers had no callers) and startup/single_instance.py are deleted.
The process-hacker diagnostics test now runs on Linux too. Windows behaviour
is unchanged. The Windows API ratchet shrinks from 72 to 66 files.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 12:42:13 -05:00

107 lines
3.7 KiB
Python

from __future__ import annotations
import sys
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock, patch
from PyQt6.QtCore import QCoreApplication
PROJECT_SRC = Path(__file__).resolve().parents[1] / "src"
if str(PROJECT_SRC) not in sys.path:
sys.path.insert(0, str(PROJECT_SRC))
class ShowWindowBridgeTests(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls._app = QCoreApplication.instance() or QCoreApplication([])
def test_show_request_prefers_tray_manager(self) -> None:
from startup.show_window_bridge import ShowWindowBridge
tray_manager = Mock()
window = SimpleNamespace(
visual_state=SimpleNamespace(tray_manager=tray_manager),
)
bridge = ShowWindowBridge(window)
with patch("ui.window_adapter.show_window") as show_window:
bridge._handle_show_window()
tray_manager.show_window.assert_called_once_with()
show_window.assert_not_called()
def test_show_request_uses_common_window_adapter_without_tray_manager(self) -> None:
from startup.show_window_bridge import ShowWindowBridge
window = SimpleNamespace(
visual_state=SimpleNamespace(tray_manager=None),
showNormal=Mock(),
activateWindow=Mock(),
raise_=Mock(),
)
bridge = ShowWindowBridge(window)
with patch("ui.window_adapter.show_window") as show_window:
bridge._handle_show_window()
show_window.assert_called_once_with(window)
window.showNormal.assert_not_called()
window.activateWindow.assert_not_called()
window.raise_.assert_not_called()
def test_signal_emission_reaches_handler(self) -> None:
from startup.show_window_bridge import ShowWindowBridge
tray_manager = Mock()
window = SimpleNamespace(
visual_state=SimpleNamespace(tray_manager=tray_manager),
)
bridge = ShowWindowBridge(window)
bridge.show_requested.connect(bridge._handle_show_window)
bridge.show_requested.emit()
tray_manager.show_window.assert_called_once_with()
def test_start_wires_watcher_to_signal(self) -> None:
from startup.show_window_bridge import ShowWindowBridge
tray_manager = Mock()
window = SimpleNamespace(visual_state=SimpleNamespace(tray_manager=tray_manager))
bridge = ShowWindowBridge(window)
single_instance = Mock()
single_instance.start_show_watcher.return_value = Mock()
start_watcher = single_instance.start_show_watcher
with patch(
"startup.show_window_bridge.current_platform",
return_value=SimpleNamespace(single_instance=single_instance),
):
self.assertTrue(bridge.start())
start_watcher.assert_called_once()
# Переданный наблюдателю callback должен доводить сигнал до обработчика
(watcher_callback,) = start_watcher.call_args.args
watcher_callback()
tray_manager.show_window.assert_called_once_with()
def test_start_reports_failure_when_watcher_not_started(self) -> None:
from startup.show_window_bridge import ShowWindowBridge
window = SimpleNamespace(visual_state=SimpleNamespace(tray_manager=Mock()))
bridge = ShowWindowBridge(window)
single_instance = Mock()
single_instance.start_show_watcher.return_value = None
with patch(
"startup.show_window_bridge.current_platform",
return_value=SimpleNamespace(single_instance=single_instance),
):
self.assertFalse(bridge.start())
if __name__ == "__main__":
unittest.main()