Main made the strict ruff config the default (`ruff check .`, blocking), and
the branch's code carried 278 findings under it (main itself is clean). Fixed:
- PLW1510 (86): `check=False` on each subprocess.run, preserving behaviour,
as main's own sweep (0006a5e229) did.
- ASYNC220 (9, test fixtures only): `noqa` with the reason, main's convention
for a Popen that returns at once and whose handle the test drives.
- ruff's safe autofixes plus the narrow unsafe ones (UP006/UP037/RUF100/RUF013/
PLR0402/RUF059 underscore renames/UP031/UP041/UP017/FURB167/PIE807/RUF007/
RUF015/RUF022/FURB162), and by hand: four %-formats to f-strings, three
unused unpacked names, one fixture loop over `.items()`.
No behaviour change. `ruff check .` passes; scripts/check passes; the 412 test
files mirroring every edited file pass (3 tests/pm files that fail only under
that batch's uv lookup pass solo, before and after this commit).
74 lines
3.6 KiB
Python
74 lines
3.6 KiB
Python
"""Cold gateway config load imports only the platform adapters the profile configures.
|
|
|
|
``platform_registry.plugin_entries()`` materializes every deferred bundled adapter (discord,
|
|
telegram, google_chat SDKs, telegram's i18n catalog: ~0.5 s per cold start). The gateway config /
|
|
env passes must resolve only platforms with activation evidence (config block, env or .env under
|
|
the platform prefix / manifest ``requires_env``, credential pool), and a configured platform —
|
|
env-only included — must still be enabled.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import textwrap
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
REPO = Path(__file__).resolve().parents[2]
|
|
|
|
_PROBE = textwrap.dedent("""
|
|
import json, sys
|
|
from gateway.config import load_gateway_config
|
|
from gateway.platform_registry import platform_registry
|
|
config = load_gateway_config()
|
|
print(json.dumps({
|
|
"enabled": sorted(p.value for p, c in config.platforms.items() if c.enabled),
|
|
"adapters": sorted(m.split("__", 1)[1].split(".")[0] for m in sys.modules
|
|
if m.startswith("hermes_plugins.platforms__") and m.endswith(".adapter")),
|
|
"registered": sorted(platform_registry.registered_names()),
|
|
}))
|
|
""")
|
|
|
|
|
|
def _load(tmp_path, *, config_yaml="", env=None, dotenv=""):
|
|
home = tmp_path / ".hermes"
|
|
home.mkdir()
|
|
(home / "config.yaml").write_text(config_yaml, encoding="utf-8")
|
|
(home / ".env").write_text(dotenv, encoding="utf-8")
|
|
# A clean child: the suite process has already imported adapters, and the developer's shell
|
|
# may carry platform env vars that are genuine activation evidence.
|
|
child_env = {k: v for k, v in os.environ.items()
|
|
if k in {"PATH", "LANG", "TZ", "HERMES_RUNTIME_DIR", "TMPDIR", "SYSTEMROOT"}}
|
|
child_env.update(HOME=str(tmp_path), HERMES_HOME=str(home), PYTHONPATH=str(REPO), **(env or {}))
|
|
out = subprocess.run([sys.executable, "-c", _PROBE], cwd=str(REPO), env=child_env,
|
|
capture_output=True, text=True, timeout=120, check=False)
|
|
assert out.returncode == 0, out.stderr[-2000:]
|
|
return json.loads(out.stdout.strip().splitlines()[-1])
|
|
|
|
|
|
def test_unconfigured_profile_imports_no_platform_adapter(tmp_path):
|
|
result = _load(tmp_path)
|
|
assert result["enabled"] == []
|
|
assert result["adapters"] == [], f"unconfigured adapters imported at config load: {result['adapters']}"
|
|
# Still registered (deferred): a later lookup by name imports it on demand.
|
|
assert {"discord", "telegram", "google_chat", "slack"} <= set(result["registered"])
|
|
|
|
|
|
@pytest.mark.parametrize("case", ["env", "dotenv", "manifest_env", "yaml_sibling"])
|
|
def test_configured_platform_still_enabled_and_only_it_is_imported(tmp_path, case):
|
|
kwargs, want, module = {
|
|
"env": ({"env": {"TELEGRAM_BOT_TOKEN": "123456:fake-token-abcdef"}}, "telegram", "telegram"),
|
|
"dotenv": ({"dotenv": "TELEGRAM_BOT_TOKEN=123456:fake-token-abcdef\n"}, "telegram", "telegram"),
|
|
# sms is enabled by TWILIO_* (its manifest requires_env), not an SMS_ prefix.
|
|
"manifest_env": ({"env": {"TWILIO_ACCOUNT_SID": "ACfake", "TWILIO_AUTH_TOKEN": "x",
|
|
"TWILIO_PHONE_NUMBER": "+15550000000"}}, "sms", "sms"),
|
|
# wecom_callback is registered by the wecom plugin.
|
|
"yaml_sibling": ({"config_yaml": "platforms:\n wecom_callback:\n enabled: true\n extra:\n"
|
|
" corp_id: c\n corp_secret: s\n"}, "wecom_callback", "wecom"),
|
|
}[case]
|
|
result = _load(tmp_path, **kwargs)
|
|
assert want in result["enabled"]
|
|
assert result["adapters"] == [module], result
|