"""Hermes read-only integration regressions; run on the Debian host.""" import importlib.util import json from pathlib import Path import subprocess import unittest from unittest.mock import patch ROOT = Path(__file__).resolve().parents[2] SPEC = importlib.util.spec_from_file_location("hermes_jeannie", ROOT / "infra/hermes/jeannie-readonly/__init__.py") MODULE = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(MODULE) class HermesJeannieTests(unittest.TestCase): def setUp(self): self.profile = {"profile": "k3s", "runtime": "k3s", "node": "opi5plus", "uid": "uid-1"} def test_header_resolves_current_selection_each_time(self): with patch.object(MODULE, "selected_profile", side_effect=[self.profile, dict(self.profile, profile="pimox", runtime="kubeadm")]): self.assertIn("Selected cluster: k3s (k3s)", MODULE.profile_header("answer")) self.assertIn("Selected cluster: pimox (kubeadm)", MODULE.profile_header("answer")) def test_registration_exposes_only_read_only_tools_and_response_hook(self): class Context: def __init__(self): self.tools = [] self.hooks = [] def register_tool(self, **kwargs): self.tools.append(kwargs["name"]) def register_hook(self, name, callback, **kwargs): self.hooks.append((name, kwargs)) context = Context() MODULE.register(context) self.assertEqual(context.tools, ["jeannie_status", "jeannie_runbooks"]) self.assertIn(("transform_llm_output", {"hold_stream": True}), context.hooks) def test_service_environment_drops_stale_selection(self): with patch.dict(MODULE.os.environ, {"LAB_CLUSTER_PROFILE": "pimox", "KUBECONFIG": "/stale", "JEANNIE_CLUSTER_PROFILE_PIN": "pimox"}): env = MODULE.clean_environment() self.assertNotIn("LAB_CLUSTER_PROFILE", env) self.assertNotIn("KUBECONFIG", env) self.assertNotIn("JEANNIE_CLUSTER_PROFILE_PIN", env) self.assertEqual(env["JEANNIE_SERVICE_CONTEXT"], "true") def test_status_checks_identity_and_preserves_unhealthy_report(self): report = {"cluster": {"profile": "k3s", "uid": "uid-1"}, "rows": [{"status": "fail"}]} completed = subprocess.CompletedProcess([], 1, stdout=json.dumps(report), stderr="") with patch.object(MODULE, "repository", return_value=ROOT), \ patch.object(MODULE, "selected_profile", return_value=self.profile), \ patch.object(MODULE.subprocess, "run", return_value=completed) as run: answer = json.loads(MODULE.jeannie_status({})) self.assertTrue(answer["ok"]) self.assertEqual(answer["status_exit_code"], 1) self.assertEqual(run.call_args.args[0][1:], ["status", "--fast", "--json"]) self.assertFalse(run.call_args.kwargs.get("shell", False)) def test_status_refuses_profile_change(self): report = {"cluster": {"profile": "k3s", "uid": "uid-1"}, "rows": []} with patch.object(MODULE, "repository", return_value=ROOT), \ patch.object(MODULE, "selected_profile", side_effect=[self.profile, dict(self.profile, uid="uid-2")]), \ patch.object(MODULE.subprocess, "run", return_value=subprocess.CompletedProcess([], 0, stdout=json.dumps(report))): self.assertFalse(json.loads(MODULE.jeannie_status({}))["ok"]) def test_runbook_question_is_one_argument_and_extra_fields_refused(self): completed = subprocess.CompletedProcess([], 0, stdout="docs/runbook.md:2 evidence", stderr="") with patch.object(MODULE, "repository", return_value=ROOT), \ patch.object(MODULE, "selected_profile", return_value=self.profile), \ patch.object(MODULE.subprocess, "run", return_value=completed) as run: result = json.loads(MODULE.jeannie_runbooks({"question": "gitea; touch /tmp/no"})) self.assertTrue(result["ok"]) self.assertEqual(run.call_args.args[0][1:], ["ask", "--citations", "gitea; touch /tmp/no"]) self.assertFalse(run.call_args.kwargs.get("shell", False)) self.assertFalse(json.loads(MODULE.jeannie_runbooks({"question": "gitea", "command": "x"}))["ok"])