112 lines
5.2 KiB
Python
112 lines
5.2 KiB
Python
"""The operator CLI is the only way in on a machine that cannot reach the hub.
|
||
|
||
Every subcommand talks to the services directly, so it runs against the same
|
||
database as the API and never needs an HTTP server.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
|
||
from agenthub.cli import main
|
||
from agenthub.config import reset_settings_cache
|
||
from tests.conftest import mock_agent
|
||
|
||
|
||
def cli(capsys, *argv) -> object:
|
||
code = main(list(argv))
|
||
output = capsys.readouterr().out
|
||
assert code == 0, output
|
||
return json.loads(output)
|
||
|
||
|
||
def test_init_then_doctor_describe_the_local_install(hub, capsys) -> None:
|
||
init = cli(capsys, "init")
|
||
assert init["api_key"].startswith("ah_") and init["database"].startswith("sqlite+aiosqlite")
|
||
doctor = cli(capsys, "doctor")
|
||
assert {"nanobot", "openai_compatible", "openclaw", "connector", "mock"} <= set(doctor["drivers"])
|
||
assert doctor["credentials"] is True
|
||
|
||
|
||
def test_cli_sees_the_agents_the_api_registered(hub, capsys) -> None:
|
||
mock_agent(hub, "mocky")
|
||
listing = cli(capsys, "agents")
|
||
assert [row["name"] for row in listing] == ["mocky"]
|
||
status = cli(capsys, "status")
|
||
assert status["agents"]["total"] == 1 and status["hub"] == "http://127.0.0.1:8100"
|
||
|
||
|
||
def test_agent_add_prints_a_paste_ready_snippet(hub, capsys) -> None:
|
||
"""Manual registration must end with something the operator can paste."""
|
||
added = cli(capsys, "agent", "add", "--name", "alpha", "--driver", "nanobot", "--connection", "embedded", "--show-token")
|
||
assert added["agent_token"].startswith("ag_")
|
||
snippet = added["credential"]
|
||
assert "channels.agenthub" in snippet["where"]
|
||
assert "agent_id" not in snippet["config"], "配置只有地址 + 凭证两格"
|
||
assert snippet["config"]["agent_token"] == added["agent_token"]
|
||
assert snippet["config"]["server"].startswith("http")
|
||
|
||
listing = cli(capsys, "agents")
|
||
assert listing[0]["credential"].startswith("ag_") # preview, not the plaintext
|
||
assert added["agent_token"] not in listing[0]["credential"]
|
||
|
||
|
||
def test_agent_add_for_a_bridged_cli_hands_out_the_launch_command(hub, capsys) -> None:
|
||
"""桥接形态没有配置文件可贴:操作员要贴的是这条命令,而且它已经带上真凭证。"""
|
||
added = cli(capsys, "agent", "add", "--name", "codex-1", "--driver", "connector",
|
||
"--connection", "connector", "--show-token")
|
||
command = added["credential"]["command"]
|
||
assert "deploy/cli-bridge/bridge.py" in command
|
||
assert f"--token {added['agent_token']}" in command, "预注册的命令不该再让人去填 token"
|
||
assert "--name codex-1" in command and "enr_" not in command
|
||
|
||
|
||
def test_agent_rotate_reissues_and_the_old_credential_stops_working(hub, capsys) -> None:
|
||
added = cli(capsys, "agent", "add", "--name", "beta", "--driver", "deepseek_harness", "--connection", "embedded", "--show-token")
|
||
assert "cordis.patch.yml" in added["credential"]["where"]
|
||
rotated = cli(capsys, "agent", "rotate", "beta")
|
||
assert rotated["agent_id"] == added["agent_id"]
|
||
assert rotated["agent_token"] != added["agent_token"]
|
||
|
||
old = hub.post(
|
||
"/api/v1/gateway/heartbeat",
|
||
json={},
|
||
headers={"Authorization": f"Bearer {added['agent_token']}"},
|
||
)
|
||
assert old.status_code in (401, 403)
|
||
|
||
|
||
def test_config_command_prints_the_access_config_for_each_agent_type(hub, capsys) -> None:
|
||
"""hub 不再生成"跑一个桥接进程"的产物,只给"粘到哪一行"的配置片段。"""
|
||
nanobot = cli(capsys, "config", "--name", "desk", "--kind", "nanobot")
|
||
assert nanobot["hub_url"] == "http://127.0.0.1:8100"
|
||
blob = "".join(item["content"] for item in nanobot["files"])
|
||
assert "channels" in blob and "agenthub" in blob and "server" in blob
|
||
assert nanobot["files"][0]["filename"] == "nanobot-config.json"
|
||
|
||
dsh = cli(capsys, "config", "--name", "desk", "--kind", "dsh")
|
||
assert "cordis.patch.yml" == dsh["files"][0]["filename"]
|
||
assert "@nex/dsh-agenthub" in dsh["files"][0]["content"]
|
||
|
||
bridge = cli(capsys, "config", "--name", "desk", "--kind", "bridge")
|
||
assert bridge["files"][0]["filename"] == "bridge.sh"
|
||
assert "deploy/cli-bridge/bridge.py" in bridge["files"][0]["content"]
|
||
|
||
claw = cli(capsys, "config", "--name", "desk", "--kind", "claw")
|
||
assert claw["files"][0]["filename"] == "openclaw-agenthub.json"
|
||
assert '"server"' in claw["files"][0]["content"]
|
||
assert "<agent_token>" in claw["files"][0]["content"]
|
||
|
||
|
||
def test_config_for_a_registered_agent_carries_its_real_credential(hub, capsys) -> None:
|
||
"""配置只剩两格:hub 地址 + 这个 Agent 自己的凭证;agent_id 由 hub 连上后回送。"""
|
||
added = cli(capsys, "agent", "add", "--name", "cfg-agent", "--connection", "embedded", "--show-token")
|
||
plan = cli(capsys, "config", "--name", "cfg-agent", "--kind", "nanobot")
|
||
content = plan["files"][0]["content"]
|
||
assert '"server": "http://127.0.0.1:8100"' in content
|
||
assert "agentId" not in content and added["agent_id"] not in content, "凭证就是身份,不用再贴 id"
|
||
assert "enr_" not in content and "hub_" not in content
|
||
# 凭证签发之后可以反复回看,所以已建档的 Agent 在这里就给出真值,不再是占位符
|
||
assert added["agent_token"] in content
|
||
assert "<agent_token>" not in content
|