import importlib.util import io import json import os import plistlib from contextlib import redirect_stdout from pathlib import Path import subprocess import tempfile import unittest from unittest.mock import patch spec = importlib.util.spec_from_file_location("setup_native", Path(__file__).with_name("setup-native.py")) setup_native = importlib.util.module_from_spec(spec) spec.loader.exec_module(setup_native) manage_spec = importlib.util.spec_from_file_location("pipelines_manage", Path(__file__).with_name("manage.py")) manage = importlib.util.module_from_spec(manage_spec) manage_spec.loader.exec_module(manage) TOKEN = "tk_" + "a" * 29 NTFY_VERSION = "2.11.0" NTFY_MODULE = "heckel.io/ntfy/v2" def resolution(): """The shape resolve_ntfy returns: an exact tag to build the server from.""" return {"repository": "binwiederhier/ntfy", "module": NTFY_MODULE, "version": NTFY_VERSION, "tag": "v" + NTFY_VERSION} class Recorder: def __init__(self, token=TOKEN, volumes=(), image="pipelines-pipelines:latest", fail_create=False, fail_copy=False, fail_volume_ls=False, fail_build=False): self.calls = [] self.token = token self.volumes = volumes self.image = image self.fail_create = fail_create self.fail_copy = fail_copy self.fail_volume_ls = fail_volume_ls self.fail_build = fail_build self._module_cache = None self.kwargs = [] def module_cache(self): """A stand-in Go module cache holding the pinned ntfy module source.""" if self._module_cache is None: self._module_cache = tempfile.mkdtemp(prefix="gomodcache-") source = Path(self._module_cache) / f"{NTFY_MODULE}@{resolution()['tag']}" source.mkdir(parents=True) (source / "go.mod").write_text(f"module {NTFY_MODULE}\n") return self._module_cache def __call__(self, command, **kwargs): command = list(command) self.calls.append(command) self.kwargs.append(kwargs) stdout, code = "", 0 if command[0].endswith("node") and command[1:] == ["--version"]: stdout = "v22.19.0\n" if command[0] == "docker": if command[1:3] == ["volume", "ls"]: if self.fail_volume_ls: code = 1 else: stdout = "\n".join(self.volumes) elif command[1:3] == ["image", "ls"]: stdout = self.image + "\n" if self.image else "" elif command[1] == "create" and self.fail_create: code = 1 elif command[1] == "cp": if self.fail_copy: code = 1 else: Path(command[3]).write_bytes(b"migrated") elif command[0] == "go" and command[1:3] == ["env", "GOMODCACHE"]: stdout = self.module_cache() + "\n" elif command[0] == "go" and command[1:2] == ["build"]: if self.fail_build: code = 1 elif "-o" in command: # Only the ntfy build is inspected by the installer; its output # is created so the atomic install has a real file to move. output = Path(command[command.index("-o") + 1]) if output.name == "ntfy": output.write_bytes(b"#!/bin/sh\n") output.chmod(0o755) if command[0].endswith("ntfy") and "token" in command: stdout = f"token {self.token} created for pipeline-publisher\n" return subprocess.CompletedProcess(command, code, stdout=stdout, stderr="") def has(self, *parts): return any(all(part in call for part in parts) for call in self.calls) class SetupNativeTests(unittest.TestCase): def setUp(self): # All LaunchAgent files, including full-run tests, stay in scratch HOME. self.home = tempfile.TemporaryDirectory() self.addCleanup(self.home.cleanup) home = patch.object(setup_native.Path, "home", return_value=Path(self.home.name)) home.start() self.addCleanup(home.stop) def installer(self, directory, recorder): # A disposable XDG config home keeps the client default out of the real # user configuration during tests. return setup_native.Setup(directory, prefix=Path(directory) / "prefix", runner=recorder, resolver=resolution, environ={"XDG_CONFIG_HOME": str(Path(directory) / "config")}) @staticmethod def which_all(opencode="/usr/bin/opencode", pi=None, cc="/usr/bin/cc"): mapping = {"go": "/usr/bin/go", "python3": "/usr/bin/python3", "git": "/usr/bin/git", "curl": "/usr/bin/curl", "node": "/usr/bin/node", "launchctl": "/bin/launchctl", "systemctl": "/usr/bin/systemctl"} if opencode: mapping["opencode"] = opencode if pi: mapping["pi"] = pi if cc: mapping["cc"] = cc return lambda name: mapping.get(name) def test_require_tools_reports_each_missing_tool_without_installing(self): with tempfile.TemporaryDirectory() as directory: recorder = Recorder() installer = self.installer(directory, recorder) mapping = {"go": "/usr/bin/go", "python3": "/usr/bin/python3", "git": None, "curl": None} with patch.object(setup_native.shutil, "which", side_effect=lambda name: mapping.get(name)): with self.assertRaises(ValueError) as raised: installer.require_tools() message = str(raised.exception) self.assertIn("git", message) self.assertIn("curl", message) self.assertIn("node", message) self.assertIn("never installs tools", message) self.assertEqual(recorder.calls, []) def test_registration_uses_native_supervisor_on_both_platforms(self): for system in ("Darwin", "Linux"): with self.subTest(system=system), tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) prefix = Path(directory) / "prefix with spaces" manifest = {"prefix": str(prefix), "python": "/usr/bin/python3", "path": "/bin"} with patch.object(setup_native.platform, "system", return_value=system), \ patch.object(setup_native.shutil, "which", side_effect=self.which_all()): path = installer.service_path() installer.register_service(path, manifest) self.assertEqual(manifest["registration"], str(path)) self.assertEqual(path.stat().st_mode & 0o777, 0o600) if system == "Darwin": data = plistlib.loads(path.read_bytes()) self.assertEqual(data["ProgramArguments"], ["/usr/bin/python3", str(prefix / "lib/manage.py"), "supervise", "--prefix", str(prefix)]) self.assertTrue(data["RunAtLoad"]) else: text = path.read_text() self.assertIn('"supervise" "--prefix"', text) self.assertIn('"' + str(prefix) + '"', text) self.assertIn("WantedBy=default.target", text) def test_unmanaged_service_is_never_replaced(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) with patch.object(setup_native.platform, "system", return_value="Linux"), \ patch.object(setup_native.shutil, "which", side_effect=self.which_all()): path = installer.service_path() path.parent.mkdir(parents=True) path.write_text("operator service") with self.assertRaisesRegex(ValueError, "unmanaged"): installer.service_path() self.assertEqual(path.read_text(), "operator service") def test_missing_service_manager_fails_before_build_or_config(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) with patch.object(setup_native.platform, "system", return_value="Linux"), \ patch.object(setup_native.shutil, "which", return_value=None): with self.assertRaisesRegex(ValueError, "systemctl is required"): installer.service_path() self.assertFalse((Path(directory) / "prefix").exists()) def test_web_install_copies_assets_and_preserves_credentials(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) prefix = Path(directory) / "prefix" installer.prepare_layout(prefix) with patch.object(setup_native.shutil, "which", side_effect=self.which_all()): web = installer.configure_web(prefix, setup_native.arguments([])) auth = Path(web["auth_file"]) before = auth.read_bytes() installer.configure_web(prefix, setup_native.arguments([])) self.assertEqual(auth.read_bytes(), before) self.assertEqual(auth.stat().st_mode & 0o777, 0o600) self.assertGreaterEqual(len(json.loads(before)["password"]), 8) self.assertTrue(Path(web["script"]).is_file()) self.assertTrue((prefix / "lib/web/src/proxySecurity.js").is_file()) self.assertEqual(web["port"], 8082) def test_invalid_web_auth_is_retained(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) auth = Path(directory) / "auth.json" auth.write_text('{"password":"short"}') installer.environ["PIPELINES_WEB_AUTH_FILE"] = str(auth) with patch.object(setup_native.shutil, "which", side_effect=self.which_all()): with self.assertRaisesRegex(ValueError, "Invalid existing web auth"): installer.configure_web(Path(directory) / "prefix", setup_native.arguments([])) self.assertEqual(auth.read_text(), '{"password":"short"}') def test_old_node_is_rejected(self): with tempfile.TemporaryDirectory() as directory: def old_node(command, **kwargs): return subprocess.CompletedProcess(command, 0, stdout="v16.0.0\n") installer = self.installer(directory, old_node) with patch.object(setup_native.shutil, "which", side_effect=self.which_all()): with self.assertRaisesRegex(ValueError, "Node.js 18"): installer.configure_web(Path(directory) / "prefix", setup_native.arguments([])) def test_build_installs_only_server_and_cli_with_trimpath(self): with tempfile.TemporaryDirectory() as directory: recorder = Recorder() installer = self.installer(directory, recorder) prefix = Path(directory) / "prefix" installer.prepare_layout(prefix) installer.build(prefix) builds = [call for call in recorder.calls if call[:2] == ["go", "build"]] self.assertEqual(len(builds), 2) for call in builds: self.assertIn("-trimpath", call) self.assertTrue(any(call[-1] == "./cmd/pipelines-server" for call in builds)) self.assertTrue(any(call[-1] == "./cmd/pipelines" for call in builds)) self.assertFalse(any("pipelines-tui" in part for call in recorder.calls for part in call)) def test_layout_creates_private_prefix_and_copies_manage(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) prefix = Path(directory) / "prefix" installer.prepare_layout(prefix) for name in ("bin", "lib", "data", "logs", "secrets"): self.assertTrue((prefix / name).is_dir(), name) self.assertEqual((prefix / "secrets").stat().st_mode & 0o777, 0o700) self.assertTrue((prefix / "lib/manage.py").is_file()) def test_prefix_honours_pipelines_prefix_environment(self): with tempfile.TemporaryDirectory() as directory: custom = Path(directory) / "custom" installer = setup_native.Setup(directory, runner=Recorder(), environ={"PIPELINES_PREFIX": str(custom)}) self.assertEqual(installer.resolve_prefix(), custom.resolve()) def test_authoring_role_defaults_only_fresh_install_to_enabled(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) prefix = Path(directory) / "prefix" self.assertTrue(installer.authoring_role(prefix, None)) prefix.mkdir() for previous, expected in (({}, False), ({"allow_authoring": False}, False), ({"allow_authoring": True}, True)): with self.subTest(previous=previous): manifest = prefix / setup_native.MARKER manifest.write_text(json.dumps(previous)) before = manifest.read_bytes() self.assertIs(installer.authoring_role(prefix, None), expected) self.assertTrue(installer.authoring_role(prefix, True)) self.assertFalse(installer.authoring_role(prefix, False)) self.assertEqual(manifest.read_bytes(), before) manifest.write_text('{"allow_authoring":"true"}') with self.assertRaisesRegex(ValueError, "Invalid existing authoring role"): installer.authoring_role(prefix, None) def test_authoring_flags_are_explicit_optional_overrides(self): self.assertIsNone(setup_native.arguments([]).allow_authoring) self.assertTrue(setup_native.arguments(["--allow-authoring"]).allow_authoring) self.assertFalse(setup_native.arguments(["--no-authoring"]).allow_authoring) def test_full_run_keeps_existing_execution_only_and_accepts_fresh_opt_out(self): for previous, arguments in (({}, []), ({"allow_authoring": False}, []), (None, ["--no-authoring"])): with self.subTest(previous=previous), tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) prefix = Path(directory) / "prefix" if previous is not None: prefix.mkdir() (prefix / setup_native.MARKER).write_text(json.dumps(previous)) with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \ patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \ patch.object(setup_native.configure, "host_timezone", return_value="UTC"), \ redirect_stdout(io.StringIO()): installer.run(setup_native.arguments(arguments)) self.assertFalse(manage.load(prefix)["allow_authoring"]) def test_missing_opencode_fails_actionably(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) with patch.object(setup_native.shutil, "which", side_effect=self.which_all(opencode=None)): with self.assertRaisesRegex(ValueError, "OpenCode was not found"): installer.harness() def test_full_run_writes_manifest_tokens_and_config_without_harness_install(self): with tempfile.TemporaryDirectory() as directory: recorder = Recorder() installer = self.installer(directory, recorder) prefix = Path(directory) / "prefix" with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \ patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \ patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"): installer.run(setup_native.arguments([])) config = manage.load(prefix) self.assertEqual(config["format"], 1) self.assertTrue(config["allow_authoring"]) self.assertEqual(config["prefix"], str(prefix.resolve())) self.assertEqual(config["server_port"], 8080) self.assertEqual(config["bind_address"], "127.0.0.1") self.assertEqual(config["ntfy"]["mode"], "native") self.assertEqual(config["topic"], "pipelines") self.assertEqual(config["opencode"], "/usr/bin/opencode") self.assertEqual(config["pi"], "") self.assertTrue(Path(config["registration"]).is_file()) self.assertTrue(recorder.has("start", "--prefix", str(prefix.resolve()))) self.assertEqual((prefix / setup_native.MARKER).stat().st_mode & 0o777, 0o600) token_path = prefix / "secrets/publisher-token" self.assertEqual(token_path.read_text(), TOKEN + "\n") self.assertEqual(token_path.stat().st_mode & 0o777, 0o600) text = (prefix / "data/ntfy/server.yml").read_text() for needle in ('listen-http:', 'base-url: "http://192.168.1.20:8090"', 'cache-file:', 'auth-file:', 'auth-default-access: "deny-all"', 'enable-login: true', 'attachment-cache-dir:', 'attachment-expiry-duration:', 'attachment-total-size-limit:', 'attachment-file-size-limit:'): self.assertIn(needle, text) self.assertNotIn("topic", text) self.assertTrue(recorder.has(str(prefix.resolve() / "bin/pipelines"), "skill", "install")) self.assertTrue(recorder.has("access", "pipeline-publisher", "pipelines", "write-only")) self.assertTrue(recorder.has("access", "phone-reader", "pipelines", "read-only")) self.assertFalse(any("opencode" in part for call in recorder.calls for part in call)) self.assertFalse(any("pipelines-tui" in part for call in recorder.calls for part in call)) self.assertFalse(any(TOKEN in part for call in recorder.calls for part in call)) def test_reinstall_keeps_existing_publisher_token(self): with tempfile.TemporaryDirectory() as directory: recorder = Recorder() installer = self.installer(directory, recorder) prefix = Path(directory) / "prefix" with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \ patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \ patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"): installer.run(setup_native.arguments([])) before = (prefix / "secrets/publisher-token").read_text() def refuse_resolution(): raise AssertionError("re-resolved ntfy despite a verified installed binary") installer.resolver = refuse_resolution recorder.calls.clear() installer.run(setup_native.arguments([])) self.assertEqual((prefix / "secrets/publisher-token").read_text(), before) self.assertFalse(any("token" in call for call in recorder.calls), "reinstall re-created the token") def test_ntfy_is_built_from_the_pinned_module(self): with tempfile.TemporaryDirectory() as directory: recorder = Recorder() installer = self.installer(directory, recorder) prefix = Path(directory) / "prefix" installer.prepare_layout(prefix) with patch.object(setup_native.shutil, "which", side_effect=self.which_all()): path = installer.build_ntfy(prefix, resolution()) self.assertTrue(path.is_file()) self.assertTrue(path.stat().st_mode & 0o100) self.assertTrue(recorder.has("go", "mod", "download", f"{NTFY_MODULE}@v{NTFY_VERSION}")) build = next(call for call in recorder.calls if call[:2] == ["go", "build"] and call[-1] == ".") self.assertIn("-trimpath", build) self.assertIn(setup_native.NTFY_BUILD_TAGS, build) self.assertTrue(any(f"-X main.version=v{NTFY_VERSION}" in part for part in build)) self.assertTrue(build[build.index("-o") + 1].endswith("/ntfy")) kwargs = next(kw for call, kw in zip(recorder.calls, recorder.kwargs) if call[:2] == ["go", "build"] and call[-1] == ".") self.assertEqual(kwargs.get("env", {}).get("CGO_ENABLED"), "1") def test_ntfy_build_is_reused_for_the_same_version(self): with tempfile.TemporaryDirectory() as directory: recorder = Recorder() installer = self.installer(directory, recorder) prefix = Path(directory) / "prefix" installer.prepare_layout(prefix) with patch.object(setup_native.shutil, "which", side_effect=self.which_all()): installer.build_ntfy(prefix, resolution()) installer.write_manifest(prefix, {"ntfy": {"version": "v" + NTFY_VERSION}}) recorder.calls.clear() installer.build_ntfy(prefix, resolution()) self.assertFalse(any(call[:2] == ["go", "build"] for call in recorder.calls), "rebuilt ntfy for an already installed version") def test_missing_c_compiler_fails_actionably(self): installer = setup_native.Setup("/tmp", runner=Recorder(), resolver=resolution, environ={}) with patch.object(setup_native.shutil, "which", side_effect=self.which_all(cc=None)): with self.assertRaisesRegex(ValueError, "No C compiler found"): installer.require_compiler() def test_ntfy_build_failure_is_reported(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder(fail_build=True)) prefix = Path(directory) / "prefix" installer.prepare_layout(prefix) with patch.object(setup_native.shutil, "which", side_effect=self.which_all()): with self.assertRaisesRegex(ValueError, "build failed"): installer.build_ntfy(prefix, resolution()) def test_publisher_token_is_never_overwritten(self): with tempfile.TemporaryDirectory() as directory: prefix = Path(directory) / "prefix" (prefix / "secrets").mkdir(parents=True) path = prefix / "secrets/publisher-token" setup_native.tokens.save_token(f"token {TOKEN} created", path, 0o600) self.assertEqual(path.stat().st_mode & 0o777, 0o600) with self.assertRaises(ValueError): setup_native.tokens.save_token(f"token {TOKEN} created", path, 0o600) self.assertEqual(path.read_text(), TOKEN + "\n") def test_migration_copies_docker_data_only_when_absent(self): with tempfile.TemporaryDirectory() as directory: prefix = Path(directory) / "prefix" (prefix / "data/ntfy").mkdir(parents=True) recorder = Recorder(volumes=("pipelines_pipelines-data", "pipelines_ntfy-data")) installer = self.installer(directory, recorder) self.assertTrue(installer.migrate_docker(prefix, replace=False)) self.assertEqual((prefix / "data/pipelines.db").read_bytes(), b"migrated") self.assertTrue(any(call[:2] == ["docker", "cp"] and call[2].endswith("-wal") for call in installer.runner.calls), "the SQLite write-ahead log sidecar must be copied with the database") self.assertEqual((prefix / "data/ntfy/auth.db").read_bytes(), b"migrated") def test_migration_keeps_existing_native_data_without_opt_in(self): with tempfile.TemporaryDirectory() as directory: prefix = Path(directory) / "prefix" (prefix / "data").mkdir(parents=True) (prefix / "data/pipelines.db").write_bytes(b"native") recorder = Recorder(volumes=("pipelines_pipelines-data",)) installer = self.installer(directory, recorder) self.assertTrue(installer.migrate_docker(prefix, replace=False)) self.assertEqual((prefix / "data/pipelines.db").read_bytes(), b"native") self.assertFalse(any(call[1:2] == ["cp"] for call in recorder.calls)) def test_absent_docker_volumes_is_a_noop(self): with tempfile.TemporaryDirectory() as directory: recorder = Recorder(volumes=()) installer = self.installer(directory, recorder) self.assertFalse(installer.migrate_docker(Path(directory) / "prefix", replace=False)) self.assertFalse(any(call[0] == "docker" and call[1] == "create" for call in recorder.calls)) def test_uninspectable_docker_volumes_abort_instead_of_skipping_migration(self): with tempfile.TemporaryDirectory() as directory: recorder = Recorder(fail_volume_ls=True) installer = self.installer(directory, recorder) with self.assertRaisesRegex(ValueError, "Could not inspect Docker volumes"): installer.migrate_docker(Path(directory) / "prefix", replace=False) def test_missing_docker_cli_is_a_noop_not_a_failure(self): def no_docker(command, **kwargs): raise FileNotFoundError(command[0]) with tempfile.TemporaryDirectory() as directory: installer = setup_native.Setup(directory, prefix=Path(directory) / "prefix", runner=no_docker, resolver=resolution, environ={}) self.assertEqual(installer.docker_volumes(), ()) def test_requested_migration_failure_aborts(self): with tempfile.TemporaryDirectory() as directory: prefix = Path(directory) / "prefix" (prefix / "data").mkdir(parents=True) recorder = Recorder(volumes=("pipelines_pipelines-data",), fail_copy=True) installer = self.installer(directory, recorder) with self.assertRaisesRegex(ValueError, "install aborted"): installer.migrate_docker(prefix, replace=False) def test_full_run_migrates_docker_auth_store_before_native_accounts(self): with tempfile.TemporaryDirectory() as directory: recorder = Recorder(volumes=("pipelines_ntfy-data",)) installer = self.installer(directory, recorder) prefix = Path(directory) / "prefix" with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \ patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \ patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"): installer.run(setup_native.arguments([])) # The migrated store must survive bootstrap instead of being shadowed. self.assertEqual((prefix / "data/ntfy/auth.db").read_bytes(), b"migrated") copy_index = next(i for i, call in enumerate(recorder.calls) if call[0] == "docker" and call[1:2] == ["cp"]) add_index = next(i for i, call in enumerate(recorder.calls) if "pipeline-publisher" in call and "user" in call) self.assertLess(copy_index, add_index, "auth store migrated after native accounts were created") def test_missing_local_image_aborts_migration(self): with tempfile.TemporaryDirectory() as directory: prefix = Path(directory) / "prefix" (prefix / "data").mkdir(parents=True) recorder = Recorder(volumes=("pipelines_pipelines-data",), image=None) installer = self.installer(directory, recorder) with self.assertRaisesRegex(ValueError, "no local Pipelines image"): installer.migrate_docker(prefix, replace=False) def test_configure_harnesses_writes_host_profiles_for_detected_harness(self): with tempfile.TemporaryDirectory() as directory: recorder = Recorder() installer = self.installer(directory, recorder) prefix = Path(directory) / "prefix" installer.configure_harnesses({"opencode": "/usr/bin/opencode", "pi": ""}, "fixture/harness") path = installer.harness_config_path() self.assertEqual(path, Path(directory) / "config/pipelines/harnesses.json") config = json.loads(path.read_text()) self.assertEqual(config["default"], "opencode") profile = config["profiles"]["opencode"] self.assertEqual(profile["executable"], "/usr/bin/opencode") self.assertNotIn("docker_directory", profile) self.assertTrue(installer._valid_model(profile["model"])) self.assertTrue(installer._valid_model(profile["pipeline_model"])) self.assertEqual(profile["model"], "fixture/harness") # Detection-only: no harness install/upgrade/download was invoked. self.assertFalse(any("opencode" in part for call in recorder.calls for part in call)) def test_configure_harnesses_skips_without_a_valid_model(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) path = installer.harness_config_path() result = installer.configure_harnesses({"opencode": "/usr/bin/opencode", "pi": ""}) self.assertIsNone(result) self.assertFalse(path.exists(), "an invalid profile was written") def test_configure_harnesses_migrates_stale_docker_link_and_default(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) path = installer.harness_config_path() path.parent.mkdir(parents=True) path.write_text(json.dumps({ "default": "pi", "future": "preserve", "profiles": { "opencode": {"docker_directory": "/checkout", "executable": "opencode", "model": "fixture/interactive", "pipeline_model": "fixture/stages"}, "pi": {"docker_directory": "/checkout", "executable": "pi", "model": "provider/interactive", "pipeline_model": "provider/stages"}, }, })) installer.configure_harnesses({"opencode": "/usr/bin/opencode", "pi": "/usr/bin/pi"}) saved = json.loads(path.read_text()) self.assertEqual(saved["default"], "opencode") self.assertEqual(saved["future"], "preserve") for name in ("opencode", "pi"): self.assertNotIn("docker_directory", saved["profiles"][name], name) self.assertTrue(installer._valid_model(saved["profiles"][name]["model"])) self.assertEqual(saved["profiles"]["opencode"]["executable"], "/usr/bin/opencode") def test_configure_harnesses_preserves_configured_host_default(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) path = installer.harness_config_path() path.parent.mkdir(parents=True) profiles = {name: {"model": "fixture/interactive", "pipeline_model": "fixture/stages"} for name in ("opencode", "pi")} profiles["pi"]["future"] = "preserve" path.write_text(json.dumps({"default": "pi", "profiles": profiles})) installer.configure_harnesses({"opencode": "/usr/bin/opencode", "pi": "/usr/bin/pi"}) saved = json.loads(path.read_text()) self.assertEqual(saved["default"], "pi") self.assertEqual(saved["profiles"]["pi"]["model"], "fixture/interactive") self.assertEqual(saved["profiles"]["pi"]["future"], "preserve") def _fake_pipelines(self, path, version, commit): path.parent.mkdir(parents=True, exist_ok=True) path.write_text(f'#!/bin/sh\necho \'{{"version":"{version}","commit":"{commit}"}}\'\n') path.chmod(0o755) return path def test_check_path_shadow_warns_naming_path_and_both_versions(self): with tempfile.TemporaryDirectory() as directory: directory = Path(directory) prefix = directory / "prefix" installed = self._fake_pipelines(prefix / "bin/pipelines", "new-version", "bbbb") shadow_dir = directory / "earlier" shadow = self._fake_pipelines(shadow_dir / "pipelines", "old-version", "aaaa") before = shadow.read_bytes() installer = setup_native.Setup(directory, prefix=prefix, runner=subprocess.run, resolver=resolution, environ={}) with patch.object(setup_native.shutil, "which", side_effect=lambda name: str(shadow) if name == "pipelines" else None): output = io.StringIO() with redirect_stdout(output): result = installer.check_path_shadow(prefix) warning = output.getvalue() self.assertEqual(result, shadow.resolve()) self.assertIn("WARNING", warning) self.assertIn(str(shadow), warning) self.assertIn("old-version", warning) self.assertIn("new-version", warning) # The shadowing binary is only read; it is never modified or replaced. self.assertEqual(shadow.read_bytes(), before) self.assertEqual(installed.read_bytes().count(b"new-version"), 1) def test_check_path_shadow_is_silent_when_prefix_binary_wins(self): with tempfile.TemporaryDirectory() as directory: directory = Path(directory) prefix = directory / "prefix" installed = self._fake_pipelines(prefix / "bin/pipelines", "same-version", "cccc") installer = setup_native.Setup(directory, prefix=prefix, runner=subprocess.run, resolver=resolution, environ={}) with patch.object(setup_native.shutil, "which", return_value=str(installed)): output = io.StringIO() with redirect_stdout(output): result = installer.check_path_shadow(prefix) self.assertIsNone(result) self.assertEqual(output.getvalue(), "") def test_check_path_shadow_is_silent_without_a_path_binary(self): with tempfile.TemporaryDirectory() as directory: installer = setup_native.Setup(directory, prefix=Path(directory) / "prefix", runner=subprocess.run, resolver=resolution, environ={}) with patch.object(setup_native.shutil, "which", return_value=None): output = io.StringIO() with redirect_stdout(output): result = installer.check_path_shadow(Path(directory) / "prefix") self.assertIsNone(result) self.assertEqual(output.getvalue(), "") def test_check_path_shadow_reads_each_binary_at_most_once(self): class VersionRunner: def __init__(self): self.commands = [] def __call__(self, command, **kwargs): command = list(command) self.commands.append(command) version = "old-version" if "earlier" in command[0] else "new-version" return subprocess.CompletedProcess(command, 0, stdout=f'{{"version":"{version}","commit":"ab"}}', stderr="") with tempfile.TemporaryDirectory() as directory: directory = Path(directory) prefix = directory / "prefix" installed = self._fake_pipelines(prefix / "bin/pipelines", "new-version", "bbbb") shadow = self._fake_pipelines(directory / "earlier/pipelines", "old-version", "aaaa") runner = VersionRunner() installer = setup_native.Setup(directory, prefix=prefix, runner=runner, resolver=resolution, environ={}) with patch.object(setup_native.shutil, "which", return_value=str(shadow)): output = io.StringIO() with redirect_stdout(output): installer.check_path_shadow(prefix) # A detected shadow costs exactly one bounded, read-only `version` # read per binary (the shadow and the freshly installed CLI) so both # versions can be named; no other subprocess runs. self.assertEqual(len(runner.commands), 2) self.assertTrue(all(command[1:] == ["version", "--json"] for command in runner.commands)) self.assertIn("old-version", output.getvalue()) self.assertIn("new-version", output.getvalue()) self.assertEqual(installed.read_bytes().count(b"new-version"), 1) def test_full_run_migrates_docker_harness_config_to_host(self): with tempfile.TemporaryDirectory() as directory: recorder = Recorder() installer = self.installer(directory, recorder) prefix = Path(directory) / "prefix" path = installer.harness_config_path() path.parent.mkdir(parents=True) path.write_text(json.dumps({ "default": "pi", "profiles": { "opencode": {"docker_directory": "/checkout", "executable": "opencode", "model": "fixture/interactive", "pipeline_model": "fixture/stages"}, "pi": {"docker_directory": "/checkout", "executable": "pi", "model": "provider/interactive", "pipeline_model": "provider/stages"}, }, })) with patch.object(setup_native.shutil, "which", side_effect=self.which_all(opencode="/usr/bin/opencode", pi="/usr/bin/pi")), \ patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \ patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"): installer.run(setup_native.arguments([])) saved = json.loads(path.read_text()) self.assertEqual(saved["default"], "opencode", "stale Docker-era pi default survived") self.assertNotIn("docker_directory", saved["profiles"]["opencode"]) self.assertTrue(saved["profiles"]["opencode"]["executable"].endswith("opencode")) self.assertTrue(installer._valid_model(saved["profiles"]["opencode"]["model"])) # Detection-only: no harness install/upgrade/download command recorded. self.assertFalse(any("opencode" in part for call in recorder.calls for part in call)) self.assertFalse(any(part == "pi" for call in recorder.calls for part in call)) self.assertFalse(any("npm" == part for call in recorder.calls for part in call)) if __name__ == "__main__": unittest.main()