generated from coulomb/repo-seed
Implements the full BRIDGE-WP-0003 workplan: 188 tests passing, 0 lint errors. ## What's added **Capability registry** (`src/bridge/capabilities.py`): - 10 capabilities with required_access_modes (cli/mcp/skill) - Single source of truth for what OpsBridge does and where **MCP server** (`src/bridge/mcp_server/server.py`): - 10 FastMCP tools: bridge_up/down/restart/status/logs + 5 catalog_* tools - 3 resources: bridge://status, catalog://domains, catalog://targets - `.mcp.json` for project-scope auto-registration - `scripts/register_mcp.py` for user-scope machine-global registration **Skill** (`~/.claude/plugins/ops-bridge/bridge-status.md`): - /bridge-status: health table with emoji indicators + remediation advice **Cross-mode test coverage enforcement**: - `tests/conftest.py`: capability/access_mode marks + collect_capability_coverage() - `tests/test_mcp.py`: 31 FastMCP in-process client tests (Client(mcp) pattern) - `tests/test_skill.py`: static skill lint against capability registry - `tests/test_coverage_completeness.py`: meta-test that fails if any required (capability × mode) pair lacks a test; also validates CLI commands and MCP tools are registered in the capability registry **ADR** (`architecture/adr-001-cross-mode-capability-registry.md`): - Documents the registry pattern and FastMCP 3.x testing approach Key implementation note: FastMCP 3.x in-process results are in result.content[0].text (JSON string), not result.data directly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
210 lines
6.7 KiB
Python
210 lines
6.7 KiB
Python
"""Tests for CLI commands."""
|
|
import json
|
|
import textwrap
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
from typer.testing import CliRunner
|
|
|
|
from bridge.cli import app
|
|
|
|
|
|
VALID_CONFIG = textwrap.dedent("""\
|
|
tunnels:
|
|
test-tunnel:
|
|
host: host.local
|
|
remote_port: 18000
|
|
local_port: 8000
|
|
ssh_user: ubuntu
|
|
ssh_key: ~/.ssh/id_ops
|
|
actor: operator.bernd
|
|
actors:
|
|
operator.bernd:
|
|
class: human
|
|
description: Bernd
|
|
""")
|
|
|
|
runner = CliRunner()
|
|
|
|
|
|
@pytest.fixture
|
|
def config_file(tmp_path):
|
|
f = tmp_path / "tunnels.yaml"
|
|
f.write_text(VALID_CONFIG)
|
|
return f
|
|
|
|
|
|
@pytest.fixture
|
|
def state_dir(tmp_path):
|
|
return tmp_path / "state"
|
|
|
|
|
|
@pytest.fixture
|
|
def env(config_file, state_dir):
|
|
return {"BRIDGE_CONFIG": str(config_file), "BRIDGE_STATE_DIR": str(state_dir)}
|
|
|
|
|
|
class TestHelpCommand:
|
|
def test_app_help(self):
|
|
result = runner.invoke(app, ["--help"])
|
|
assert result.exit_code == 0
|
|
assert "bridge" in result.output.lower() or "Usage" in result.output
|
|
|
|
def test_up_help(self):
|
|
result = runner.invoke(app, ["up", "--help"])
|
|
assert result.exit_code == 0
|
|
|
|
def test_down_help(self):
|
|
result = runner.invoke(app, ["down", "--help"])
|
|
assert result.exit_code == 0
|
|
|
|
def test_status_help(self):
|
|
result = runner.invoke(app, ["status", "--help"])
|
|
assert result.exit_code == 0
|
|
|
|
def test_logs_help(self):
|
|
result = runner.invoke(app, ["logs", "--help"])
|
|
assert result.exit_code == 0
|
|
|
|
def test_restart_help(self):
|
|
result = runner.invoke(app, ["restart", "--help"])
|
|
assert result.exit_code == 0
|
|
|
|
|
|
class TestStatusCommand:
|
|
@pytest.mark.capability("bridge_status")
|
|
@pytest.mark.access_mode("cli")
|
|
def test_status_shows_tunnels(self, env, state_dir):
|
|
result = runner.invoke(app, ["status"], env=env)
|
|
assert result.exit_code == 0
|
|
assert "test-tunnel" in result.output
|
|
|
|
def test_status_json_flag(self, env, state_dir):
|
|
result = runner.invoke(app, ["status", "--json"], env=env)
|
|
assert result.exit_code == 0
|
|
data = json.loads(result.output)
|
|
assert isinstance(data, list)
|
|
assert len(data) == 1
|
|
assert data[0]["tunnel"] == "test-tunnel"
|
|
assert "state" in data[0]
|
|
assert "actor" in data[0]
|
|
assert "host" in data[0]
|
|
|
|
def test_status_shows_state(self, env, state_dir):
|
|
result = runner.invoke(app, ["status"], env=env)
|
|
assert result.exit_code == 0
|
|
assert "stopped" in result.output.lower()
|
|
|
|
def test_status_unknown_config_exit_1(self, tmp_path):
|
|
result = runner.invoke(app, ["status"], env={"BRIDGE_CONFIG": str(tmp_path / "no.yaml")})
|
|
assert result.exit_code == 1
|
|
|
|
|
|
class TestUpCommand:
|
|
def test_up_unknown_tunnel_exit_1(self, env):
|
|
result = runner.invoke(app, ["up", "nonexistent"], env=env)
|
|
assert result.exit_code == 1
|
|
assert "nonexistent" in result.output
|
|
|
|
@pytest.mark.capability("bridge_up")
|
|
@pytest.mark.access_mode("cli")
|
|
def test_up_calls_manager_start(self, env, state_dir):
|
|
with patch("bridge.cli.TunnelManager") as mock_mgr_cls:
|
|
mock_mgr = MagicMock()
|
|
mock_mgr.is_running.return_value = False
|
|
mock_mgr_cls.return_value = mock_mgr
|
|
|
|
result = runner.invoke(app, ["up", "test-tunnel"], env=env)
|
|
|
|
assert result.exit_code == 0
|
|
mock_mgr.start.assert_called_once()
|
|
|
|
def test_up_already_running_exit_2(self, env, state_dir):
|
|
with patch("bridge.cli.TunnelManager") as mock_mgr_cls:
|
|
mock_mgr = MagicMock()
|
|
mock_mgr.is_running.return_value = True
|
|
mock_mgr_cls.return_value = mock_mgr
|
|
|
|
result = runner.invoke(app, ["up", "test-tunnel"], env=env)
|
|
|
|
assert result.exit_code == 2
|
|
|
|
|
|
class TestDownCommand:
|
|
def test_down_unknown_tunnel_exit_1(self, env):
|
|
result = runner.invoke(app, ["down", "nonexistent"], env=env)
|
|
assert result.exit_code == 1
|
|
|
|
@pytest.mark.capability("bridge_down")
|
|
@pytest.mark.access_mode("cli")
|
|
def test_down_calls_manager_stop(self, env, state_dir):
|
|
with patch("bridge.cli.TunnelManager") as mock_mgr_cls:
|
|
mock_mgr = MagicMock()
|
|
mock_mgr.is_running.return_value = True
|
|
mock_mgr_cls.return_value = mock_mgr
|
|
|
|
result = runner.invoke(app, ["down", "test-tunnel"], env=env)
|
|
|
|
assert result.exit_code == 0
|
|
mock_mgr.stop.assert_called_once()
|
|
|
|
def test_down_not_running_exit_2(self, env, state_dir):
|
|
with patch("bridge.cli.TunnelManager") as mock_mgr_cls:
|
|
mock_mgr = MagicMock()
|
|
mock_mgr.is_running.return_value = False
|
|
mock_mgr_cls.return_value = mock_mgr
|
|
|
|
result = runner.invoke(app, ["down", "test-tunnel"], env=env)
|
|
|
|
assert result.exit_code == 2
|
|
|
|
|
|
class TestLogsCommand:
|
|
def test_logs_unknown_tunnel_exit_1(self, env):
|
|
result = runner.invoke(app, ["logs", "nonexistent"], env=env)
|
|
assert result.exit_code == 1
|
|
|
|
def test_logs_no_log_file_shows_empty(self, env, state_dir):
|
|
result = runner.invoke(app, ["logs", "test-tunnel"], env=env)
|
|
assert result.exit_code == 0
|
|
|
|
@pytest.mark.capability("bridge_logs")
|
|
@pytest.mark.access_mode("cli")
|
|
def test_logs_shows_events(self, env, state_dir):
|
|
import json as _json
|
|
state_dir.mkdir(parents=True, exist_ok=True)
|
|
log_file = state_dir / "test-tunnel.log"
|
|
log_file.write_text(
|
|
_json.dumps({
|
|
"timestamp": "2026-01-01T00:00:00+00:00",
|
|
"tunnel": "test-tunnel",
|
|
"actor": "operator.bernd",
|
|
"actor_class": "human",
|
|
"event": "bridge_started",
|
|
}) + "\n"
|
|
)
|
|
result = runner.invoke(app, ["logs", "test-tunnel"], env=env)
|
|
assert result.exit_code == 0
|
|
assert "bridge_started" in result.output
|
|
|
|
|
|
class TestRestartCommand:
|
|
def test_restart_unknown_tunnel_exit_1(self, env):
|
|
result = runner.invoke(app, ["restart", "nonexistent"], env=env)
|
|
assert result.exit_code == 1
|
|
|
|
@pytest.mark.capability("bridge_restart")
|
|
@pytest.mark.access_mode("cli")
|
|
def test_restart_calls_stop_then_start(self, env):
|
|
with patch("bridge.cli.TunnelManager") as mock_mgr_cls:
|
|
mock_mgr = MagicMock()
|
|
mock_mgr_cls.return_value = mock_mgr
|
|
call_order = []
|
|
mock_mgr.stop.side_effect = lambda: call_order.append("stop")
|
|
mock_mgr.start.side_effect = lambda: call_order.append("start")
|
|
|
|
result = runner.invoke(app, ["restart", "test-tunnel"], env=env)
|
|
|
|
assert result.exit_code == 0
|
|
assert call_order == ["stop", "start"]
|