import importlib.util import io import os from pathlib import Path import signal import subprocess import tempfile import unittest from contextlib import redirect_stdout from unittest.mock import MagicMock, patch spec = importlib.util.spec_from_file_location("manage_native", Path(__file__).with_name("manage.py")) manage = importlib.util.module_from_spec(spec) spec.loader.exec_module(manage) setup_spec = importlib.util.spec_from_file_location("setup_native_service", Path(__file__).with_name("setup-native.py")) setup_native = importlib.util.module_from_spec(setup_spec) setup_spec.loader.exec_module(setup_native) class FakeProcess: def __init__(self, pid=4321): self.pid = pid def poll(self): return None class ManageNativeTests(unittest.TestCase): def service_config(self, home, system): config = self.config(home / "prefix") config["opencode_env"] = {"XDG_CONFIG_HOME": str(home / ".config")} path = (home / "Library/LaunchAgents/net.a-wan.pipelines.plist" if system == "Darwin" else home / ".config/systemd/user/net.a-wan.pipelines.service") setup_native.Setup(home, environ={}).register_service(path, config) return config def test_native_service_preview_is_read_only_on_both_platforms(self): for system in ("Darwin", "Linux"): with self.subTest(system=system), tempfile.TemporaryDirectory() as directory, \ patch.object(manage.Path, "home", return_value=Path(directory)), \ patch.object(manage.platform, "system", return_value=system): config = self.service_config(Path(directory), system) path = Path(config["registration"]) before = path.read_bytes() with patch.object(manage, "run", side_effect=AssertionError("preview invoked OS manager")), \ patch.object(manage, "start", side_effect=AssertionError("preview started services")), \ redirect_stdout(io.StringIO()) as output: manage.service_command(config, ["enable", "--print"]) self.assertEqual(path.read_bytes(), before) self.assertEqual(output.getvalue(), before.decode()) self.assertNotIn("publisher-token", output.getvalue()) def test_native_service_enable_and_disable_use_existing_registration(self): for system in ("Darwin", "Linux"): with self.subTest(system=system), tempfile.TemporaryDirectory() as directory, \ patch.object(manage.Path, "home", return_value=Path(directory)), \ patch.object(manage.platform, "system", return_value=system): config = self.service_config(Path(directory), system) before = Path(config["registration"]).read_bytes() with patch.object(manage, "start") as start, redirect_stdout(io.StringIO()): manage.service_command(config, ["enable"]) start.assert_called_once_with(config) calls = [] with patch.object(manage, "run", side_effect=lambda args, **kw: calls.append(args)), \ patch.object(manage, "stop", side_effect=lambda cfg: calls.append("stop")), \ redirect_stdout(io.StringIO()): manage.service_command(config, ["disable"]) expected = (["launchctl", "disable", f"gui/{os.getuid()}/net.a-wan.pipelines"] if system == "Darwin" else ["systemctl", "--user", "disable", "net.a-wan.pipelines.service"]) self.assertEqual(calls, [expected, "stop"]) self.assertEqual(Path(config["registration"]).read_bytes(), before) def test_native_service_status_does_not_print_raw_manager_output(self): for system in ("Darwin", "Linux"): with self.subTest(system=system), tempfile.TemporaryDirectory() as directory, \ patch.object(manage.Path, "home", return_value=Path(directory)), \ patch.object(manage.platform, "system", return_value=system): config = self.service_config(Path(directory), system) def manager(args, **kwargs): self.assertTrue(kwargs["quiet"]) return subprocess.CompletedProcess(args, 0, stdout='"net.a-wan.pipelines" => true\nprivate-output') with patch.object(manage, "run", side_effect=manager), redirect_stdout(io.StringIO()) as output: manage.service_command(config, ["status"]) self.assertNotIn("private-output", output.getvalue()) self.assertIn("Autostart: " + ("disabled" if system == "Darwin" else "enabled"), output.getvalue()) def test_native_service_rejects_unmanaged_symlink_and_wrong_prefix_definitions(self): for variant in ("unmanaged", "symlink", "wrong-prefix", "wrong-label"): with self.subTest(variant=variant), tempfile.TemporaryDirectory() as directory, \ patch.object(manage.Path, "home", return_value=Path(directory)), \ patch.object(manage.platform, "system", return_value="Linux"): config = self.service_config(Path(directory), "Linux") path = Path(config["registration"]) if variant == "unmanaged": path.write_text("Managed by pipelines service\nExecStart=developer-server") elif variant == "symlink": target = path.with_suffix(".other") path.rename(target) path.symlink_to(target) elif variant == "wrong-prefix": config["prefix"] += "-other" else: config["label"] = "pipelines" with patch.object(manage, "run", side_effect=AssertionError("unsafe OS manager invocation")), \ patch.object(manage, "start", side_effect=AssertionError("unsafe startup")): with self.assertRaises(ValueError): manage.service_command(config, ["enable"]) def test_native_service_rejects_standalone_database_flags(self): with redirect_stdout(io.StringIO()), patch("sys.stderr", new_callable=io.StringIO): with self.assertRaises(SystemExit) as raised: manage.service_command({}, ["enable", "--db", "/other.db"]) self.assertEqual(raised.exception.code, 2) def test_manager_dispatches_internal_service_verb_without_forwarding_to_go(self): config = {"prefix": "/scratch/native"} with patch("sys.argv", ["manage.py", "--prefix", "/scratch/native", "native-service", "enable", "--print"]), \ patch.object(manage, "load", return_value=config), \ patch.object(manage, "service_command") as service, \ patch.object(manage.os, "execve", side_effect=AssertionError("recursive Go dispatch")): manage.main() service.assert_called_once_with(config, ["enable", "--print"]) def test_supervisor_authoring_flag_requires_explicit_manifest_true(self): for role in (True, False, None, "true"): with self.subTest(role=role), tempfile.TemporaryDirectory() as directory: root = Path(directory) (root / "logs").mkdir() config = self.config(root) if role is not None: config["allow_authoring"] = role ntfy, server = MagicMock(), MagicMock() ntfy.poll.return_value = None server.poll.return_value = 0 server.returncode = 0 with patch.object(manage, "ntfy_start", return_value=ntfy), \ patch.object(manage, "wait_health"), patch.object(manage.signal, "signal"), \ patch.object(manage.subprocess, "Popen", return_value=server) as popen: manage.supervise(config) command = popen.call_args.args[0] self.assertEqual(command[:5], [str(root / "bin/pipelines-server"), "-addr", "127.0.0.1:8080", "-db", str(root / "data/pipelines.db")]) self.assertEqual("-allow-authoring" in command, role is True) def config(self, root, mode="native"): return { "format": 1, "prefix": str(root), "python": "python3", "server_port": 8080, "ntfy_port": 8090, "opencode": "opencode", "opencode_env": {}, "path": "", "topic": "pipelines", "public_url": "http://192.168.1.20:8090", "username": "phone-reader", "workspace": str(root), "launcher": str(root / "bin/pipelines"), "registration": None, "label": "net.a-wan.pipelines", "docker": "docker", "project": "pipelines", "timezone": "UTC", "ntfy": {"mode": mode, "bin": str(root / "bin/ntfy"), "config": str(root / "data/ntfy/server.yml"), "base_url": "http://192.168.1.20:8090"}, } def test_mode_selection_defaults_to_docker(self): self.assertEqual(manage.ntfy_mode({}), "docker") self.assertEqual(manage.ntfy_mode({"ntfy": {}}), "docker") self.assertEqual(manage.ntfy_mode({"ntfy": {"mode": "native"}}), "native") self.assertEqual(manage.ntfy_health_url({"ntfy_port": 8090}), "http://127.0.0.1:8090/v1/health") def test_registered_start_commands_on_both_platforms(self): for system in ("Darwin", "Linux"): with self.subTest(system=system), tempfile.TemporaryDirectory() as directory: config = self.config(Path(directory)) config["registration"] = str(Path(directory) / "service") with patch.object(manage.platform, "system", return_value=system), \ patch.object(manage, "run") as run, patch.object(manage, "wait_health"): manage.start(config) calls = [call.args[0] for call in run.call_args_list] if system == "Linux": self.assertEqual(calls, [["systemctl", "--user", "daemon-reload"], ["systemctl", "--user", "enable", "--now", "net.a-wan.pipelines.service"]]) else: self.assertEqual(calls[0], ["launchctl", "enable", f"gui/{os.getuid()}/net.a-wan.pipelines"]) self.assertIn(["launchctl", "kickstart", f"gui/{os.getuid()}/net.a-wan.pipelines"], calls) def test_supervisor_spawns_authenticated_web_and_cleans_up_on_exit(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) (root / "logs").mkdir() config = self.config(root) config["web"] = {"node": "/bin/node", "script": "/prefix/lib/web/serve.mjs", "host": "0.0.0.0", "port": 8082, "auth_file": "/private/auth.json"} ntfy, server, web = MagicMock(), MagicMock(), MagicMock() ntfy.poll.return_value = None server.poll.return_value = None web.poll.side_effect = [1, None] with patch.object(manage, "ntfy_start", return_value=ntfy), \ patch.object(manage, "wait_health"), patch.object(manage.signal, "signal"), \ patch.dict(os.environ, {"PIPELINES_WEB_AUTH": "disabled"}), \ patch.object(manage.subprocess, "Popen", side_effect=[server, web]) as popen: with self.assertRaisesRegex(RuntimeError, "web proxy exited"): manage.supervise(config) call = popen.call_args_list[1] self.assertEqual(call.args[0], ["/bin/node", "/prefix/lib/web/serve.mjs", "--port", "8082", "--host", "0.0.0.0", "--pipelines", "127.0.0.1:8080"]) self.assertNotIn("PIPELINES_WEB_AUTH", call.kwargs["env"]) self.assertEqual(call.kwargs["env"]["PIPELINES_WEB_AUTH_FILE"], "/private/auth.json") web.terminate.assert_called_once() server.terminate.assert_called_once() ntfy.terminate.assert_called_once() def test_start_checks_web_login_readiness(self): with tempfile.TemporaryDirectory() as directory: config = self.config(Path(directory)) config["registration"] = "/scratch/service" config["web"] = {"host": "0.0.0.0", "port": 8123} response = MagicMock() response.__enter__.return_value.status = 200 with patch.object(manage.platform, "system", return_value="Linux"), \ patch.object(manage, "run"), patch.object(manage, "wait_health"), \ patch.object(manage.urllib.request, "urlopen", return_value=response) as open_url: manage.start(config) open_url.assert_called_once_with("http://127.0.0.1:8123/login", timeout=2) def test_server_address_uses_the_manifest_bind_and_defaults_to_loopback(self): self.assertEqual(manage.server_address({"server_port": 8080, "bind_address": "0.0.0.0"}), "0.0.0.0:8080") self.assertEqual(manage.server_address({"server_port": 8080, "bind_address": "10.0.0.75"}), "10.0.0.75:8080") # A manifest written before the setting existed must stay loopback-only. self.assertEqual(manage.server_address({"server_port": 8080}), "127.0.0.1:8080") self.assertEqual(manage.server_address({"server_port": 8080, "bind_address": ""}), "127.0.0.1:8080") def test_ntfy_start_launches_binary_with_generated_config(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) (root / "bin").mkdir() (root / "logs").mkdir() config = self.config(root) with patch.object(manage.subprocess, "Popen", return_value=FakeProcess(pid=4321)) as popen: child = manage.ntfy_start(config) self.assertEqual(popen.call_args[0][0], [config["ntfy"]["bin"], "serve", "--config", config["ntfy"]["config"]]) self.assertEqual(child.pid, 4321) self.assertEqual((root / "ntfy.pid").read_text(), "4321") def test_ntfy_stop_terminates_managed_process_and_removes_pid(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) (root / "bin").mkdir() (root / "bin/ntfy").write_text("") (root / "ntfy.pid").write_text(str(os.getpid())) config = self.config(root) killed = [] def fake_kill(pid, sig): if sig == 0: raise ProcessLookupError killed.append((pid, sig)) with patch.object(manage.subprocess, "check_output", return_value=str(root / "bin/ntfy") + " serve --config x"), \ patch.object(manage.os, "kill", side_effect=fake_kill), \ patch.object(manage, "compose", side_effect=AssertionError("compose used for native ntfy")): manage.ntfy_stop(config) self.assertIn((os.getpid(), signal.SIGTERM), killed) self.assertFalse((root / "ntfy.pid").exists()) def test_native_stop_never_calls_compose(self): with tempfile.TemporaryDirectory() as directory: config = self.config(Path(directory)) with patch.object(manage, "registration", return_value=None), \ patch.object(manage, "managed_pid", return_value=None), \ patch.object(manage, "ntfy_stop") as stop, \ patch.object(manage, "compose", side_effect=AssertionError("compose used for native ntfy")): manage.stop(config) stop.assert_called_once_with(config) def test_uninstall_native_skips_compose_down(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) (root / "bin").mkdir() config = self.config(root) with patch.object(manage, "stop"), \ patch.object(manage, "registration", return_value=None), \ patch.object(manage, "compose", side_effect=AssertionError("compose used for native ntfy")): manage.uninstall(config, purge=False, yes=True) self.assertTrue((root / manage.MARKER).is_file()) if __name__ == "__main__": unittest.main()