Files
hermes-agent/tests/agent/test_session_reset_fix.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

93 lines
3.2 KiB
Python

"""Tests for session reset completeness (fixes #2635).
/clear and /new must not carry stale state into the next session.
Two fields were added after reset_session_state() was written and were
therefore never cleared:
- ContextCompressor._previous_summary
- AIAgent._user_turn_count
"""
import sys
import types
from pathlib import Path
# Ensure repo root is importable
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))
# Stub out optional heavy dependencies not installed in the test environment
sys.modules.setdefault("fire", types.SimpleNamespace(Fire=lambda *a, **k: None))
sys.modules.setdefault("firecrawl", types.SimpleNamespace(Firecrawl=object))
sys.modules.setdefault("fal_client", types.SimpleNamespace())
from run_agent import AIAgent
from agent.context_compressor import ContextCompressor
def _make_minimal_agent() -> AIAgent:
"""Return an AIAgent constructed with the absolute minimum args.
We pass dummy values that bypass network calls and filesystem access.
The object is never used to make API calls — only its attributes and
reset_session_state() are exercised.
"""
agent = AIAgent.__new__(AIAgent) # skip __init__ entirely
# Seed the exact attributes that reset_session_state() writes
agent.session_total_tokens = 0
agent.session_input_tokens = 0
agent.session_output_tokens = 0
agent.session_prompt_tokens = 0
agent.session_completion_tokens = 0
agent.session_cache_read_tokens = 0
agent.session_cache_write_tokens = 0
agent.session_reasoning_tokens = 0
agent.session_api_calls = 0
agent.session_estimated_cost_usd = 0.0
agent.session_cost_status = "unknown"
agent.session_cost_source = "none"
# The two fields under test
agent._user_turn_count = 0
agent.context_compressor = None # will be set per-test as needed
return agent
class TestResetSessionState:
"""reset_session_state() must clear ALL session-scoped state."""
def test_previous_summary_cleared_on_reset(self):
"""Compression summary from old session must not leak into new session."""
agent = _make_minimal_agent()
compressor = ContextCompressor.__new__(ContextCompressor)
compressor._previous_summary = "Old session summary about unrelated topic"
# Seed counter attributes that reset_session_state touches
compressor.last_prompt_tokens = 100
compressor.last_completion_tokens = 50
compressor.last_total_tokens = 150
compressor.compression_count = 3
compressor._context_probed = True
agent.context_compressor = compressor
agent.reset_session_state()
assert compressor._previous_summary is None, (
"_previous_summary must be None after reset; got: "
f"{compressor._previous_summary!r}"
)
def test_user_turn_count_cleared_on_reset(self):
"""Turn counter must reset to 0 on new session."""
agent = _make_minimal_agent()
agent._user_turn_count = 7 # simulates turns accumulated in previous session
agent.context_compressor = None
agent.reset_session_state()
assert agent._user_turn_count == 0, (
f"_user_turn_count must be 0 after reset; got: {agent._user_turn_count}"
)