hermes-agent/tests/gateway/test_sticker_cache.py
kshitijk4poor 66827f8947 chore: prune unused imports and duplicate import redefinitions
Remove unused imports (F401) and duplicate/shadowed import
redefinitions (F811) across the codebase using ruff's safe
autofixes. No behavioral changes -- imports only.

- ~1400 safe autofixes applied across 644 files (net -1072 lines)
- __init__.py re-exports preserved (excluded from F401 removal so
  public re-export surfaces stay intact)
- Re-exports that are imported or monkeypatched by tests but look
  unused in their defining module are kept with explicit # noqa:
  F401 (gateway/run.py load_dotenv; run_agent re-exports from
  agent.message_sanitization, agent.context_compressor,
  agent.retry_utils, agent.prompt_builder, agent.process_bootstrap,
  agent.codex_responses_adapter)
- Unsafe F841 (unused-variable) fixes deliberately skipped -- those
  can change behavior when the RHS has side effects
- ruff lints remain disabled in pyproject.toml (only PLW1514 is
  selected); this is a one-time cleanup, not a config change

Verification:
- python -m compileall: clean
- pytest --collect-only: all 27161 tests collect (zero import errors)
- core entry points import clean (run_agent, model_tools, cli,
  toolsets, hermes_state, batch_runner, gateway)
- static scan: every name any test imports directly from an edited
  module still resolves
2026-05-28 22:26:25 -07:00

124 lines
5 KiB
Python

"""Tests for gateway/sticker_cache.py — sticker description cache."""
from unittest.mock import patch
from gateway.sticker_cache import (
_load_cache,
_save_cache,
get_cached_description,
cache_sticker_description,
build_sticker_injection,
build_animated_sticker_injection,
)
class TestLoadSaveCache:
def test_load_missing_file(self, tmp_path):
with patch("gateway.sticker_cache.CACHE_PATH", tmp_path / "nope.json"):
assert _load_cache() == {}
def test_load_corrupt_file(self, tmp_path):
bad_file = tmp_path / "bad.json"
bad_file.write_text("not json{{{")
with patch("gateway.sticker_cache.CACHE_PATH", bad_file):
assert _load_cache() == {}
def test_save_and_load_roundtrip(self, tmp_path):
cache_file = tmp_path / "cache.json"
data = {"abc123": {"description": "A cat", "emoji": "", "set_name": "", "cached_at": 1.0}}
with patch("gateway.sticker_cache.CACHE_PATH", cache_file):
_save_cache(data)
loaded = _load_cache()
assert loaded == data
def test_save_creates_parent_dirs(self, tmp_path):
cache_file = tmp_path / "sub" / "dir" / "cache.json"
with patch("gateway.sticker_cache.CACHE_PATH", cache_file):
_save_cache({"key": "value"})
assert cache_file.exists()
class TestCacheSticker:
def test_cache_and_retrieve(self, tmp_path):
cache_file = tmp_path / "cache.json"
with patch("gateway.sticker_cache.CACHE_PATH", cache_file):
cache_sticker_description("uid_1", "A happy dog", emoji="🐕", set_name="Dogs")
result = get_cached_description("uid_1")
assert result is not None
assert result["description"] == "A happy dog"
assert result["emoji"] == "🐕"
assert result["set_name"] == "Dogs"
assert "cached_at" in result
def test_missing_sticker_returns_none(self, tmp_path):
cache_file = tmp_path / "cache.json"
with patch("gateway.sticker_cache.CACHE_PATH", cache_file):
result = get_cached_description("nonexistent")
assert result is None
def test_overwrite_existing(self, tmp_path):
cache_file = tmp_path / "cache.json"
with patch("gateway.sticker_cache.CACHE_PATH", cache_file):
cache_sticker_description("uid_1", "Old description")
cache_sticker_description("uid_1", "New description")
result = get_cached_description("uid_1")
assert result["description"] == "New description"
def test_multiple_stickers(self, tmp_path):
cache_file = tmp_path / "cache.json"
with patch("gateway.sticker_cache.CACHE_PATH", cache_file):
cache_sticker_description("uid_1", "Cat")
cache_sticker_description("uid_2", "Dog")
r1 = get_cached_description("uid_1")
r2 = get_cached_description("uid_2")
assert r1["description"] == "Cat"
assert r2["description"] == "Dog"
class TestBuildStickerInjection:
def test_exact_format_no_context(self):
result = build_sticker_injection("A cat waving")
assert result == '[The user sent a sticker~ It shows: "A cat waving" (=^.w.^=)]'
def test_exact_format_emoji_only(self):
result = build_sticker_injection("A cat", emoji="😀")
assert result == '[The user sent a sticker 😀~ It shows: "A cat" (=^.w.^=)]'
def test_exact_format_emoji_and_set_name(self):
result = build_sticker_injection("A cat", emoji="😀", set_name="MyPack")
assert result == '[The user sent a sticker 😀 from "MyPack"~ It shows: "A cat" (=^.w.^=)]'
def test_set_name_without_emoji_ignored(self):
"""set_name alone (no emoji) produces no context — only emoji+set_name triggers 'from' clause."""
result = build_sticker_injection("A cat", set_name="MyPack")
assert result == '[The user sent a sticker~ It shows: "A cat" (=^.w.^=)]'
assert "MyPack" not in result
def test_description_with_quotes(self):
result = build_sticker_injection('A "happy" dog')
assert '"A \\"happy\\" dog"' not in result # no escaping happens
assert 'A "happy" dog' in result
def test_empty_description(self):
result = build_sticker_injection("")
assert result == '[The user sent a sticker~ It shows: "" (=^.w.^=)]'
class TestBuildAnimatedStickerInjection:
def test_exact_format_with_emoji(self):
result = build_animated_sticker_injection(emoji="🎉")
assert result == (
"[The user sent an animated sticker 🎉~ "
"I can't see animated ones yet, but the emoji suggests: 🎉]"
)
def test_exact_format_without_emoji(self):
result = build_animated_sticker_injection()
assert result == "[The user sent an animated sticker~ I can't see animated ones yet]"
def test_empty_emoji_same_as_no_emoji(self):
result = build_animated_sticker_injection(emoji="")
assert result == build_animated_sticker_injection()