|
| 1 | +"""Tests for registry-backed local-agent CLI adapter.""" |
| 2 | + |
| 3 | +import io |
| 4 | +import json |
| 5 | +import pathlib |
| 6 | +import sys |
| 7 | +import unittest |
| 8 | +from contextlib import redirect_stdout, redirect_stderr |
| 9 | +from unittest import mock |
| 10 | + |
| 11 | +_REPO_ROOT = pathlib.Path(__file__).parent.parent |
| 12 | +sys.path.insert(0, str(_REPO_ROOT)) |
| 13 | + |
| 14 | +from sourceosctl.commands import local_agent |
| 15 | +from sourceosctl.commands import local_agent_registry_cli |
| 16 | + |
| 17 | + |
| 18 | +class TestLocalAgentRegistryCli(unittest.TestCase): |
| 19 | + def test_load_registry_into_cli_populates_default_agents(self): |
| 20 | + agents = local_agent_registry_cli.load_registry_into_cli() |
| 21 | + self.assertIn("node-commander", agents) |
| 22 | + self.assertIn("node-commander", local_agent.DEFAULT_AGENTS) |
| 23 | + self.assertEqual( |
| 24 | + local_agent.DEFAULT_AGENTS["node-commander"].runtime_image, |
| 25 | + "localhost/socioprophet/node-commander:dev-boot", |
| 26 | + ) |
| 27 | + |
| 28 | + def test_json_list(self): |
| 29 | + out = io.StringIO() |
| 30 | + with redirect_stdout(out): |
| 31 | + rc = local_agent_registry_cli.main(["list", "--json"]) |
| 32 | + self.assertEqual(rc, 0) |
| 33 | + payload = json.loads(out.getvalue()) |
| 34 | + self.assertIn("agents", payload) |
| 35 | + self.assertEqual(payload["agents"][0]["name"], "node-commander") |
| 36 | + |
| 37 | + def test_json_status_with_mocked_checks(self): |
| 38 | + out = io.StringIO() |
| 39 | + check = local_agent.Check("agent", "pass", "ok") |
| 40 | + with mock.patch.object(local_agent, "collect_checks", return_value=[check]): |
| 41 | + with redirect_stdout(out): |
| 42 | + rc = local_agent_registry_cli.main(["status", "node-commander", "--json"]) |
| 43 | + self.assertEqual(rc, 0) |
| 44 | + payload = json.loads(out.getvalue()) |
| 45 | + self.assertEqual(payload["agent"]["name"], "node-commander") |
| 46 | + self.assertTrue(payload["ok"]) |
| 47 | + self.assertEqual(payload["checks"][0]["name"], "agent") |
| 48 | + |
| 49 | + def test_json_status_unknown_agent(self): |
| 50 | + err = io.StringIO() |
| 51 | + with redirect_stderr(err): |
| 52 | + rc = local_agent_registry_cli.main(["status", "missing", "--json"]) |
| 53 | + self.assertEqual(rc, 2) |
| 54 | + self.assertIn("unknown local agent", err.getvalue()) |
| 55 | + |
| 56 | + def test_non_json_delegates_to_existing_cli(self): |
| 57 | + with mock.patch.object(local_agent, "main", return_value=0) as mocked: |
| 58 | + rc = local_agent_registry_cli.main(["list"]) |
| 59 | + self.assertEqual(rc, 0) |
| 60 | + mocked.assert_called_once_with(["list"]) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + unittest.main() |
0 commit comments