Files
hermes-agent/tests/gateway/test_whatsapp_multiplex_secret_scope.py
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.

Parallel directories for one source package, folded into the mirror:
  tests/acp        -> tests/acp_adapter   (its __init__/conftest move with it)
  tests/cli        -> tests/hermes_cli    (prompt_toolkit fixture merged into
                                           hermes_cli/conftest.py)
  tests/run_agent  -> tests/agent         (backoff fixture becomes
                                           agent/conftest.py)
  tests/relay      -> tests/gateway/relay
  tests/state      -> tests/hermes_state

246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.

Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.

Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).

Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
2026-09-13 09:18:02 -07:00

159 lines
6.5 KiB
Python

"""Regression test for #75349 — WhatsApp bridge loses WHATSAPP_* vars under multiplex.
Under ``_profile_runtime_scope`` (the context installed for every secondary-profile
turn in a multiplexed gateway), ``os.getenv("WHATSAPP_MODE")`` bypasses the
profile's secret scope and returns the process-global value (often unset),
causing the bridge to silently fall back to ``"self-chat"`` and reject all
inbound messages.
The fix routes WHATSAPP_* reads through ``get_secret()`` (``agent.secret_scope``)
which honours the active scope.
"""
import pytest
from agent import secret_scope as ss
@pytest.fixture(autouse=True)
def _reset_multiplex(monkeypatch):
"""Ensure multiplex mode is off before and after each test."""
ss.set_multiplex_active(False)
yield
ss.set_multiplex_active(False)
class TestWhatsAppEnvViaSecretScope:
"""WHATSAPP_* reads must go through ``get_secret``, not ``os.getenv``."""
def test_wenv_reads_scope_under_multiplex(self, tmp_path, monkeypatch):
"""When multiplexing is active and a scope is installed, the profile's
.env values are returned — not the global os.environ values."""
from plugins.platforms.whatsapp.adapter import _wenv
# Set a misleading value in os.environ that would be returned by
# os.getenv("WHATSAPP_MODE", "self-chat") if the bug was present.
monkeypatch.setenv("WHATSAPP_MODE", "self-chat")
ss.set_multiplex_active(True)
# Write a profile .env with bot mode
(tmp_path / ".env").write_text("WHATSAPP_MODE=bot\nWHATSAPP_DM_POLICY=allowlist\n")
tok = ss.set_secret_scope(ss.build_profile_secret_scope(tmp_path))
try:
# The fix reads from the scope, not os.environ
mode = _wenv("WHATSAPP_MODE", "self-chat")
assert mode == "bot", f"Expected 'bot', got {mode!r}"
dm_policy = _wenv("WHATSAPP_DM_POLICY", "pairing")
assert dm_policy == "allowlist", f"Expected 'allowlist', got {dm_policy!r}"
finally:
ss.reset_secret_scope(tok)
def test_wenv_fallback_when_scope_absent(self, monkeypatch):
"""When no scope is installed and multiplex is off, _wenv returns default."""
from plugins.platforms.whatsapp.adapter import _wenv
monkeypatch.delenv("WHATSAPP_MODE", raising=False)
result = _wenv("WHATSAPP_MODE", "self-chat")
assert result == "self-chat"
def test_wenv_does_not_leak_cross_profile(self, tmp_path, monkeypatch):
"""Two different profiles under the same process see their own values."""
from plugins.platforms.whatsapp.adapter import _wenv
ss.set_multiplex_active(True)
(tmp_path / "profA").mkdir()
(tmp_path / "profA" / ".env").write_text("WHATSAPP_MODE=bot\n")
(tmp_path / "profB").mkdir()
(tmp_path / "profB" / ".env").write_text("WHATSAPP_MODE=self-chat\n")
tok_a = ss.set_secret_scope(ss.build_profile_secret_scope(tmp_path / "profA"))
try:
assert _wenv("WHATSAPP_MODE", "self-chat") == "bot"
finally:
ss.reset_secret_scope(tok_a)
tok_b = ss.set_secret_scope(ss.build_profile_secret_scope(tmp_path / "profB"))
try:
assert _wenv("WHATSAPP_MODE", "self-chat") == "self-chat"
finally:
ss.reset_secret_scope(tok_b)
class TestWhatsAppCommonUsesSecretScope:
"""The shared WhatsAppBehaviorMixin methods must also use get_secret."""
def test_effective_reply_prefix_uses_scope(self, tmp_path, monkeypatch):
"""_effective_reply_prefix respects the profile's WHATSAPP_MODE from scope."""
from gateway.platforms.whatsapp_common import WhatsAppBehaviorMixin
monkeypatch.delenv("WHATSAPP_MODE", raising=False)
monkeypatch.delenv("WHATSAPP_REPLY_PREFIX", raising=False)
ss.set_multiplex_active(True)
# Set scope with bot mode (should NOT add reply prefix)
(tmp_path / ".env").write_text("WHATSAPP_MODE=bot\n")
tok = ss.set_secret_scope(ss.build_profile_secret_scope(tmp_path))
try:
mixin = object.__new__(WhatsAppBehaviorMixin)
mixin.config = type("C", (), {"extra": {}})()
mixin.name = "test"
mixin._reply_prefix = None
mixin.MAX_MESSAGE_LENGTH = 4096
mixin.DEFAULT_REPLY_PREFIX = "[Reply] "
prefix = mixin._effective_reply_prefix()
# bot mode → no prefix
assert prefix == ""
finally:
ss.reset_secret_scope(tok)
def test_whatsapp_require_mention_uses_scope(self, tmp_path, monkeypatch):
"""_whatsapp_require_mention respects the profile's env from scope."""
from gateway.platforms.whatsapp_common import WhatsAppBehaviorMixin
monkeypatch.delenv("WHATSAPP_REQUIRE_MENTION", raising=False)
ss.set_multiplex_active(True)
(tmp_path / ".env").write_text("WHATSAPP_REQUIRE_MENTION=true\n")
tok = ss.set_secret_scope(ss.build_profile_secret_scope(tmp_path))
try:
mixin = object.__new__(WhatsAppBehaviorMixin)
mixin.config = type("C", (), {"extra": {}})()
mixin.name = "test"
assert mixin._whatsapp_require_mention() is True
finally:
ss.reset_secret_scope(tok)
class TestWhatsAppCloudAdapterUsesSecretScope:
"""The Cloud API adapter must also read WHATSAPP_* through get_secret."""
def test_cloud_dm_policy_reads_scope(self, tmp_path, monkeypatch):
"""WhatsAppCloudAdapter._dm_policy respects profile scope."""
from gateway.config import PlatformConfig
monkeypatch.delenv("WHATSAPP_CLOUD_DM_POLICY", raising=False)
monkeypatch.delenv("WHATSAPP_DM_POLICY", raising=False)
ss.set_multiplex_active(True)
(tmp_path / ".env").write_text("WHATSAPP_DM_POLICY=allowlist\n")
tok = ss.set_secret_scope(ss.build_profile_secret_scope(tmp_path))
try:
from gateway.platforms.whatsapp_cloud import WhatsAppCloudAdapter
cfg = type("C", (), {
"extra": {},
"enabled": True,
})()
# Cloud adapter won't fully init without creds, but we can at least
# verify the dm_policy assignment path doesn't crash under scope.
# We test via the mixin's behavior instead.
from gateway.platforms.whatsapp_common import _get_wsecret
assert _get_wsecret("WHATSAPP_DM_POLICY", default="pairing") == "allowlist"
finally:
ss.reset_secret_scope(tok)