mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-28 15:01:34 +08:00
* refactor: add shared helper modules for code deduplication New modules: - gateway/platforms/helpers.py: MessageDeduplicator, TextBatchAggregator, strip_markdown, ThreadParticipationTracker, redact_phone - hermes_cli/cli_output.py: print_info/success/warning/error, prompt helpers - tools/path_security.py: validate_within_dir, has_traversal_component - utils.py additions: safe_json_loads, read_json_file, read_jsonl, append_jsonl, env_str/lower/int/bool helpers - hermes_constants.py additions: get_config_path, get_skills_dir, get_logs_dir, get_env_path * refactor: migrate gateway adapters to shared helpers - MessageDeduplicator: discord, slack, dingtalk, wecom, weixin, mattermost - strip_markdown: bluebubbles, feishu, sms - redact_phone: sms, signal - ThreadParticipationTracker: discord, matrix - _acquire/_release_platform_lock: telegram, discord, slack, whatsapp, signal, weixin Net -316 lines across 19 files. * refactor: migrate CLI modules to shared helpers - tools_config.py: use cli_output print/prompt + curses_radiolist (-117 lines) - setup.py: use cli_output print helpers + curses_radiolist (-101 lines) - mcp_config.py: use cli_output prompt (-15 lines) - memory_setup.py: use curses_radiolist (-86 lines) Net -263 lines across 5 files. * refactor: migrate to shared utility helpers - safe_json_loads: agent/display.py (4 sites) - get_config_path: skill_utils.py, hermes_logging.py, hermes_time.py - get_skills_dir: skill_utils.py, prompt_builder.py - Token estimation dedup: skills_tool.py imports from model_metadata - Path security: skills_tool, cronjob_tools, skill_manager_tool, credential_files - Non-atomic YAML writes: doctor.py, config.py now use atomic_yaml_write - Platform dict: new platforms.py, skills_config + tools_config derive from it - Anthropic key: new get_anthropic_key() in auth.py, used by doctor/status/config/main * test: update tests for shared helper migrations - test_dingtalk: use _dedup.is_duplicate() instead of _is_duplicate() - test_mattermost: use _dedup instead of _seen_posts/_prune_seen - test_signal: import redact_phone from helpers instead of signal - test_discord_connect: _platform_lock_identity instead of _token_lock_identity - test_telegram_conflict: updated lock error message format - test_skill_manager_tool: 'escapes' instead of 'boundary' in error msgs
85 lines
3.3 KiB
Python
85 lines
3.3 KiB
Python
"""Tests for Discord thread participation persistence.
|
|
|
|
Verifies that _threads (ThreadParticipationTracker) survives adapter restarts by
|
|
being persisted to ~/.hermes/discord_threads.json.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
|
|
class TestDiscordThreadPersistence:
|
|
"""Thread IDs are saved to disk and reloaded on init."""
|
|
|
|
def _make_adapter(self, tmp_path):
|
|
"""Build a minimal DiscordAdapter with HERMES_HOME pointed at tmp_path."""
|
|
from gateway.config import PlatformConfig
|
|
from gateway.platforms.discord import DiscordAdapter
|
|
|
|
config = PlatformConfig(enabled=True, token="test-token")
|
|
with patch.dict(os.environ, {"HERMES_HOME": str(tmp_path)}):
|
|
return DiscordAdapter(config=config)
|
|
|
|
def test_starts_empty_when_no_state_file(self, tmp_path):
|
|
adapter = self._make_adapter(tmp_path)
|
|
assert "$nonexistent" not in adapter._threads
|
|
|
|
def test_track_thread_persists_to_disk(self, tmp_path):
|
|
adapter = self._make_adapter(tmp_path)
|
|
with patch.dict(os.environ, {"HERMES_HOME": str(tmp_path)}):
|
|
adapter._threads.mark("111")
|
|
adapter._threads.mark("222")
|
|
|
|
state_file = tmp_path / "discord_threads.json"
|
|
assert state_file.exists()
|
|
saved = json.loads(state_file.read_text())
|
|
assert set(saved) == {"111", "222"}
|
|
|
|
def test_threads_survive_restart(self, tmp_path):
|
|
"""Threads tracked by one adapter instance are visible to the next."""
|
|
adapter1 = self._make_adapter(tmp_path)
|
|
with patch.dict(os.environ, {"HERMES_HOME": str(tmp_path)}):
|
|
adapter1._threads.mark("aaa")
|
|
adapter1._threads.mark("bbb")
|
|
|
|
adapter2 = self._make_adapter(tmp_path)
|
|
assert "aaa" in adapter2._threads
|
|
assert "bbb" in adapter2._threads
|
|
|
|
def test_duplicate_track_does_not_double_save(self, tmp_path):
|
|
adapter = self._make_adapter(tmp_path)
|
|
with patch.dict(os.environ, {"HERMES_HOME": str(tmp_path)}):
|
|
adapter._threads.mark("111")
|
|
adapter._threads.mark("111") # no-op
|
|
|
|
saved = json.loads((tmp_path / "discord_threads.json").read_text())
|
|
assert saved.count("111") == 1
|
|
|
|
def test_caps_at_max_tracked_threads(self, tmp_path):
|
|
adapter = self._make_adapter(tmp_path)
|
|
adapter._threads._max_tracked = 5
|
|
with patch.dict(os.environ, {"HERMES_HOME": str(tmp_path)}):
|
|
for i in range(10):
|
|
adapter._threads.mark(str(i))
|
|
|
|
saved = json.loads((tmp_path / "discord_threads.json").read_text())
|
|
assert len(saved) == 5
|
|
|
|
def test_corrupted_state_file_falls_back_to_empty(self, tmp_path):
|
|
state_file = tmp_path / "discord_threads.json"
|
|
state_file.write_text("not valid json{{{")
|
|
adapter = self._make_adapter(tmp_path)
|
|
assert "$nonexistent" not in adapter._threads
|
|
|
|
def test_missing_hermes_home_does_not_crash(self, tmp_path):
|
|
"""Load/save tolerate missing directories."""
|
|
fake_home = tmp_path / "nonexistent" / "deep"
|
|
with patch.dict(os.environ, {"HERMES_HOME": str(fake_home)}):
|
|
from gateway.platforms.helpers import ThreadParticipationTracker
|
|
# ThreadParticipationTracker should return empty set, not crash
|
|
tracker = ThreadParticipationTracker("discord")
|
|
assert "$test" not in tracker
|