"""Тесты автозапуска Chromium (kiosk boot).""" from __future__ import annotations import json import os import tempfile import unittest from pathlib import Path from app.services.kiosk_boot_service import ( _DESKTOP_ENTRY_NAME, _LABWC_MARKER, _chromium_flag_list, _kiosk_launch_url, _kiosk_target_url, build_chromium_desktop_content, build_labwc_autostart_content, get_kiosk_boot_status, set_kiosk_boot, ) class KioskBootServiceTests(unittest.TestCase): def test_chromium_flags_disable_translate(self) -> None: home = Path("/home/komton") flags = " ".join(_chromium_flag_list({}, home=home)) self.assertIn("Translate,", flags) self.assertIn("--host-rules=", flags) self.assertIn("translate.googleapis.com", flags) self.assertIn("--user-data-dir=", flags) self.assertIn("wesp-kiosk-chromium", flags) self.assertNotIn("--incognito", flags) def test_launch_url_startup_dark(self) -> None: cfg = {"WESP_KIOSK_TARGET_URL": "http://localhost/scales"} url = _kiosk_launch_url(cfg) self.assertIn("/starting?", url) self.assertIn("next=/scales", url) self.assertIn("theme=dark", url) self.assertEqual(_kiosk_target_url(cfg), "http://localhost/scales") def test_build_desktop_contains_url_and_kiosk(self) -> None: home = Path("/home/komton") cfg = {} text = build_chromium_desktop_content( chromium="/usr/bin/chromium", url="http://localhost/starting?next=/scales", app_config=cfg, home=home, ) self.assertIn("--kiosk", text) self.assertIn("/starting", text) def test_build_labwc_minimal(self) -> None: home = Path("/home/komton") text = build_labwc_autostart_content( chromium="/usr/bin/chromium", url="http://localhost/starting?next=/scales&theme=dark", app_config={}, home=home, ) self.assertIn(_LABWC_MARKER, text) self.assertIn("/usr/bin/kanshi", text) self.assertNotIn("pcmanfm", text) self.assertIn("curl", text) self.assertIn("127.0.0.1/starting", text) self.assertIn("--app=", text) def test_system_labwc_patch_roundtrip(self) -> None: with tempfile.TemporaryDirectory() as tmp: home = Path(tmp) / "komton" home.mkdir() system = Path(tmp) / "labwc-autostart" system.write_text( "/usr/bin/lwrespawn /usr/bin/pcmanfm-pi &\n", encoding="utf-8", ) system.chmod(0o755) policy = Path(tmp) / "policy.json" fake_chromium = Path(tmp) / "chromium" fake_chromium.write_text("#!/bin/sh\n", encoding="utf-8") fake_chromium.chmod(0o755) cfg = { "WESP_KIOSK_GUI_USER": "komton", "WESP_KIOSK_TARGET_URL": "http://localhost/scales", "WESP_KIOSK_CHROMIUM": str(fake_chromium), "WESP_KIOSK_SYSTEM_LABWC_AUTOSTART": str(system), "WESP_KIOSK_CHROMIUM_POLICY_PATH": str(policy), } import app.services.kiosk_boot_service as mod orig_home = mod._home_dir mod._home_dir = lambda _user: home # type: ignore[assignment] try: res = set_kiosk_boot(True, cfg, hide_desktop=True) self.assertTrue(res.get("ok"), res) body = system.read_text(encoding="utf-8") self.assertIn(_LABWC_MARKER, body) self.assertIn("/starting", body) self.assertIn("user-data-dir", body) prefs = home / ".config" / "wesp-kiosk-chromium" / "Default" / "Preferences" self.assertTrue(prefs.is_file()) prefs_data = json.loads(prefs.read_text(encoding="utf-8")) self.assertFalse(prefs_data["translate"]["enabled"]) self.assertTrue(policy.is_file()) st = get_kiosk_boot_status(cfg) self.assertEqual(st["kiosk_mode"], "system_labwc") res2 = set_kiosk_boot(False, cfg) self.assertTrue(res2.get("ok")) self.assertIn("pcmanfm", system.read_text(encoding="utf-8")) finally: mod._home_dir = orig_home if __name__ == "__main__": unittest.main()