Files
hermes-agent/tests/bots/test_bot_dm_payload_cache.py
alt-glitch 2115d74421 refactor(bots): Bot Mode and Bot Desktop move from tools/ to bots/
tools/ holds model tools. These 19 files register none: they resolve the
Bot Mode roster, deliver message_agent's DMs and run their by-path runner,
hold the cross-connection relay and its receipts, and run the Bot Desktop
screen. They move to a new root package, bots/ (flat plus bots/desktop/,
with the launcher and wallpaper beside the runtime that reads them).
tools/message_agent_tool.py stays; only its imports change.

Function bodies are unchanged and every import stays where it was. Tests
whose subject is a moved file go to tests/bots/, whose conftest imports
the tests/tools/ autouse fixtures they ran under. Loggers follow their
modules; bots joins the tools log component, so `hermes logs --component
tools` shows the same lines, and every hand-written root-package list.

Old names keep working. Each moved top-level module has a MOVES key.
Processes started on v0.21.6 to v0.23.5 that outlive an update import
these modules inside functions and start tools/bot_mode_dm.py by path as
the DM runner, so those names get generated old-path files. Pinned catalog
plugins import tools.bot_desktop and tools.bot_live_delivery; the plugin
guide lists both.
2026-12-01 17:49:22 +05:30

69 lines
2.4 KiB
Python

"""DM payload files must be reaped by gateway housekeeping, not only in-band.
`message_agent` writes the message body to a file and hands the path to a
*background* delivery, so it cannot be removed at the call site. The runner
owns per-delivery cleanup, and `_write_dm_file` sweeps opportunistically —
but a gateway that never sends another DM would still keep orphans forever.
`cleanup_bot_dm_cache` follows the same contract as the other
``cleanup_*_cache`` helpers (returns the number of files removed) so the
gateway housekeeping loop in ``gateway/run.py`` prunes this cache on the
same hourly cadence as the media caches.
"""
import os
import time
from pathlib import Path
import pytest
from bots import dm_delivery as bot_mode_dm
@pytest.fixture()
def temp_root(tmp_path, monkeypatch):
monkeypatch.setattr(bot_mode_dm.tempfile, "gettempdir", lambda: str(tmp_path))
return tmp_path
def _age(path: Path, seconds: float) -> None:
past = time.time() - seconds
os.utime(path, (past, past))
class TestCleanupContract:
def test_expired_payloads_are_removed_and_counted(self, temp_root):
old = Path(bot_mode_dm._write_dm_file("stale"))
fresh = Path(bot_mode_dm._write_dm_file("recent"))
_age(old, bot_mode_dm._DM_STALE_SECONDS + 1)
removed = bot_mode_dm.cleanup_bot_dm_cache()
assert removed == 1
assert not old.exists()
assert fresh.exists(), "a payload still in flight was reaped"
def test_a_missing_dm_dir_is_not_an_error(self, temp_root):
assert bot_mode_dm.cleanup_bot_dm_cache() == 0
def test_legacy_and_relay_prefixed_orphans_are_swept(self, temp_root):
legacy = temp_root / "hermes-dm-legacy.txt"
relay = temp_root / "hermes-relay-dm-orphan.txt"
unrelated = temp_root / "other.txt"
for f in (legacy, relay, unrelated):
f.write_text("secret", encoding="utf-8")
_age(f, bot_mode_dm._DM_STALE_SECONDS + 1)
removed = bot_mode_dm.cleanup_bot_dm_cache()
assert removed == 2
assert not legacy.exists()
assert not relay.exists()
assert unrelated.exists()
def test_shorter_max_age_hours_is_honored(self, temp_root):
recent = Path(bot_mode_dm._write_dm_file("an hour old"))
_age(recent, 2 * 3600)
assert bot_mode_dm.cleanup_bot_dm_cache(max_age_hours=1) == 1
assert not recent.exists()