"""nanobot 渠道的配置读取规则:config.json 是正规来源,env 只是兜底。 用户按「其它渠道怎么配就怎么配」接入时,写的是 ~/.nanobot/config.json 里的 channels.agenthub。曾经有一段时间 displayName / labels 这些字段只认 AGENTHUB_* 环境变量,于是页面只能教用户绕开配置文件 —— 现在这条规则被测试钉住: · 普通字段 config.json 优先,env 兜底(docker 里没配置文件时才用 env) · 凭证 env 优先,否则 compose 注入的凭证会输给 config.json 里的陈旧副本 需要一个能 import nanobot 的解释体(见 test_interop_nanobot 的说明),否则跳过。 """ from __future__ import annotations import json import subprocess from pathlib import Path import pytest from tests.test_interop_nanobot import nanobot_python PROBE = Path(__file__).resolve().parent / "channel_config_probe.py" FULL_CONFIG = { "enabled": True, "server": "http://127.0.0.1:8100", "agentId": "agt_from_config", "agentToken": "ag_from_config", "name": "alpha", "displayName": "阿尔法", "description": "测试岗", "deploymentKind": "docker", "deploymentRef": "agenthub-gw-alpha", "labels": {"team": "core"}, "maxConcurrency": 5, } #: 老配置(改名之前)必须继续能读:一个版本窗口内兼容,见规范 §4.2 LEGACY_CONFIG = { "enabled": True, "hubUrl": "http://127.0.0.1:8100", "agentToken": "ag_from_config", "name": "alpha", } def resolve(cases: list[dict]) -> list[dict]: python = nanobot_python() if not python: pytest.skip("no interpreter that can import nanobot") done = subprocess.run( [python, str(PROBE), json.dumps(cases, ensure_ascii=False)], capture_output=True, text=True, check=False, ) assert done.returncode == 0, done.stderr[-2000:] return json.loads(done.stdout.strip().splitlines()[-1]) def test_config_json_is_the_normal_place_for_every_field() -> None: """只写 config.json(容器里没有 AGENTHUB_*)时,每个字段都要生效。""" got = resolve([{"config": FULL_CONFIG}])[0] assert "error" not in got, got assert got["hub_url"] == "http://127.0.0.1:8100" assert got["name"] == "alpha" and got["display_name"] == "阿尔法" assert got["description"] == "测试岗" assert got["agent_id"] == "agt_from_config" and got["agent_token"] == "ag_from_config" assert got["deployment_kind"] == "docker" and got["deployment_ref"] == "agenthub-gw-alpha" assert got["max_concurrency"] == 5 assert got["labels"] == {"runtime": "nanobot", "team": "core"} # driver / connection_mode 由渠道本身决定,不给配置留口子 assert got["driver"] == "nanobot" and got["connection_mode"] == "embedded" def test_legacy_field_names_still_resolve() -> None: """改名不能砸掉已经在跑的实例:hubUrl 等旧名继续读。""" got = resolve([{"config": LEGACY_CONFIG}])[0] assert "error" not in got, got assert got["hub_url"] == "http://127.0.0.1:8100" assert got["name"] == "alpha" and got["agent_token"] == "ag_from_config" def test_credential_from_env_beats_a_stale_copy_in_config() -> None: """compose 注入的凭证要赢过 config.json 里的陈旧副本;其余字段仍是 config 优先。""" got = resolve([{"config": FULL_CONFIG, "env": {"AGENTHUB_AGENT_TOKEN": "ag_from_env"}}])[0] assert got["agent_token"] == "ag_from_env" assert got["agent_id"] == "agt_from_config", "没注入 agent_id 时沿用配置里的" assert got["name"] == "alpha", "凭证优先级不外溢到普通字段" def test_env_still_works_when_there_is_no_config_file() -> None: """本地/裸进程可以完全不碰 config.json。""" got = resolve([{"config": {"server": "http://h:1"}, "env": { "AGENTHUB_AGENT_TOKEN": "ag_env", "AGENTHUB_NAME": "beta", "AGENTHUB_DISPLAY_NAME": "贝塔", "AGENTHUB_DEPLOYMENT_KIND": "cloud", "AGENTHUB_LABELS": "team=core,env=lab", }}])[0] assert got["name"] == "beta" and got["display_name"] == "贝塔" assert got["agent_token"] == "ag_env" assert got["deployment_kind"] == "cloud" assert got["labels"] == {"runtime": "nanobot", "team": "core", "env": "lab"} def test_channel_refuses_to_start_without_a_server() -> None: got = resolve([{"config": {"enabled": True}}])[0] assert "ValueError" in got.get("error", ""), got