hermes-agent/tests/cli/test_save_conversation_location.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

101 lines
3.5 KiB
Python

"""Tests for /save — the conversation snapshot slash command.
Regression: the old implementation wrote ``hermes_conversation_<ts>.json``
to the current working directory (CWD). Users who ran /save expected the
file to be discoverable via ``hermes sessions browse``, but CWD-resident
snapshots are not indexed in the state DB and are generally invisible.
The fix writes snapshots under ``~/.hermes/sessions/saved/`` and prints
the absolute path plus the resume hint for the live session.
"""
from __future__ import annotations
import json
import sys
from datetime import datetime
from pathlib import Path
from types import SimpleNamespace
import pytest
@pytest.fixture
def hermes_home(tmp_path, monkeypatch):
home = tmp_path / ".hermes"
home.mkdir()
monkeypatch.setattr(Path, "home", lambda: tmp_path)
monkeypatch.setenv("HERMES_HOME", str(home))
# Clear any cached hermes_home computation
import hermes_constants
if hasattr(hermes_constants, "_hermes_home_cache"):
hermes_constants._hermes_home_cache = None
return home
def _make_stub_cli(history):
"""Build a minimal object exposing just what save_conversation uses."""
return SimpleNamespace(
conversation_history=history,
model="test-model",
session_id="20260101_120000_abc123",
session_start=datetime(2026, 1, 1, 12, 0, 0),
)
def test_save_conversation_writes_under_hermes_home(hermes_home, tmp_path, monkeypatch, capsys):
"""Snapshot must land under ~/.hermes/sessions/saved/, not CWD."""
# Change CWD to a different directory to prove the file does NOT go there.
work = tmp_path / "somewhere-else"
work.mkdir()
monkeypatch.chdir(work)
# Import fresh to pick up the HERMES_HOME fixture
for mod in [m for m in sys.modules if m.startswith("cli") or m == "hermes_constants"]:
sys.modules.pop(mod, None)
import cli # noqa: F401 (module under test)
stub = _make_stub_cli([
{"role": "user", "content": "hi"},
{"role": "assistant", "content": "hello"},
])
# Call the unbound method against our stub.
cli.HermesCLI.save_conversation(stub)
# File must NOT be in CWD
cwd_leak = list(work.glob("hermes_conversation_*.json"))
assert not cwd_leak, f"snapshot leaked to CWD: {cwd_leak}"
# File MUST be under ~/.hermes/sessions/saved/
saved_dir = hermes_home / "sessions" / "saved"
assert saved_dir.is_dir(), "expected saved/ subdirectory to be created"
files = list(saved_dir.glob("hermes_conversation_*.json"))
assert len(files) == 1, files
payload = json.loads(files[0].read_text())
assert payload["model"] == "test-model"
assert payload["session_id"] == "20260101_120000_abc123"
assert payload["messages"] == [
{"role": "user", "content": "hi"},
{"role": "assistant", "content": "hello"},
]
# User-facing message must include the absolute path AND the resume hint.
out = capsys.readouterr().out
assert str(files[0]) in out, out
assert "hermes --resume 20260101_120000_abc123" in out, out
def test_save_conversation_empty_history_does_nothing(hermes_home, capsys):
for mod in [m for m in sys.modules if m.startswith("cli") or m == "hermes_constants"]:
sys.modules.pop(mod, None)
import cli
stub = _make_stub_cli([])
cli.HermesCLI.save_conversation(stub)
saved_dir = hermes_home / "sessions" / "saved"
assert not saved_dir.exists() or not list(saved_dir.iterdir())
out = capsys.readouterr().out
assert "No conversation to save" in out