import subprocess import unittest from unittest.mock import patch from copienator_gui.syncthing import SERVICE, control_service class SyncthingTests(unittest.TestCase): def test_active_service_is_not_restarted(self): with patch("copienator_gui.syncthing.service_status", return_value="active"), \ patch("copienator_gui.syncthing.subprocess.run") as run: self.assertEqual(control_service("ensure"), "active") run.assert_not_called() def test_inactive_service_is_started_and_verified(self): with patch("copienator_gui.syncthing.service_status", side_effect=["inactive", "active"]), \ patch("copienator_gui.syncthing.subprocess.run", return_value=subprocess.CompletedProcess([], 0)) as run: self.assertEqual(control_service("ensure"), "active") self.assertEqual(run.call_args.args[0], ["systemctl", "--no-ask-password", "start", SERVICE]) def test_failed_start_reports_error(self): with patch("copienator_gui.syncthing.service_status", return_value="inactive"), \ patch("copienator_gui.syncthing.subprocess.run", return_value=subprocess.CompletedProcess([], 1, stderr="Access denied")): with self.assertRaisesRegex(RuntimeError, "Access denied"): control_service("ensure") def test_stop_targets_system_instance(self): with patch("copienator_gui.syncthing.service_status", return_value="inactive"), \ patch("copienator_gui.syncthing.subprocess.run", return_value=subprocess.CompletedProcess([], 0)) as run: self.assertEqual(control_service("stop"), "inactive") self.assertEqual(run.call_args.args[0], ["systemctl", "--no-ask-password", "stop", SERVICE])