`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.
128 lines
4.2 KiB
Python
128 lines
4.2 KiB
Python
import ast
|
|
import asyncio
|
|
import threading
|
|
from pathlib import Path
|
|
|
|
from hermes_cli import web_server
|
|
import hermes_cli.web_models as _web_models
|
|
import hermes_cli.web_routers.sessions as _rt_sessions
|
|
import hermes_cli.web_server_sessions as _web_server_sessions
|
|
from hermes_cli import web_server_sessions
|
|
from hermes_cli.web_routers import analytics as web_analytics
|
|
from hermes_cli.web_routers import sessions as web_sessions
|
|
|
|
|
|
TARGET_HANDLERS = {
|
|
"bulk_delete_sessions_endpoint",
|
|
"count_empty_sessions_endpoint",
|
|
"delete_empty_sessions_endpoint",
|
|
"get_session_latest_descendant",
|
|
"get_session_messages",
|
|
"delete_session_endpoint",
|
|
"export_session_endpoint",
|
|
"prune_sessions_endpoint",
|
|
"get_usage_analytics",
|
|
"get_models_analytics",
|
|
}
|
|
|
|
|
|
def _call_name(call: ast.Call) -> str | None:
|
|
if isinstance(call.func, ast.Name):
|
|
return call.func.id
|
|
if isinstance(call.func, ast.Attribute):
|
|
return call.func.attr
|
|
return None
|
|
|
|
|
|
def test_sessiondb_handlers_open_connections_inside_executor_helpers():
|
|
# The session and analytics route handlers were extracted to
|
|
# web_routers/{sessions,analytics}.py; the executor helpers live in
|
|
# web_server_sessions.py (and any left in web_server.py) — scan all
|
|
# four modules' top-level bodies.
|
|
handlers: dict[str, ast.AsyncFunctionDef] = {}
|
|
top_level_helpers: dict[str, ast.FunctionDef] = {}
|
|
for mod in (web_server, web_server_sessions, web_sessions, web_analytics):
|
|
tree = ast.parse(Path(mod.__file__).read_text(encoding="utf-8"))
|
|
for node in tree.body:
|
|
if isinstance(node, ast.AsyncFunctionDef) and node.name in TARGET_HANDLERS:
|
|
handlers[node.name] = node
|
|
elif isinstance(node, ast.FunctionDef):
|
|
top_level_helpers[node.name] = node
|
|
assert handlers.keys() == TARGET_HANDLERS
|
|
|
|
for name, handler in handlers.items():
|
|
helpers = {
|
|
**top_level_helpers,
|
|
**{
|
|
node.name: node
|
|
for node in handler.body
|
|
if isinstance(node, ast.FunctionDef)
|
|
},
|
|
}
|
|
offloaded = {
|
|
arg.id
|
|
for node in ast.walk(handler)
|
|
if isinstance(node, ast.Call)
|
|
and _call_name(node) == "to_thread"
|
|
for arg in node.args[:1]
|
|
if isinstance(arg, ast.Name)
|
|
}
|
|
db_open_owners = {
|
|
helper_name
|
|
for helper_name, helper in helpers.items()
|
|
if helper_name in offloaded
|
|
and any(
|
|
isinstance(node, ast.Call)
|
|
and _call_name(node) == "_open_session_db_for_profile"
|
|
for node in ast.walk(helper)
|
|
)
|
|
}
|
|
assert db_open_owners, f"{name} does not offload SessionDB open + work"
|
|
|
|
|
|
def test_sessiondb_opens_declare_access_mode():
|
|
for mod in (web_server_sessions, web_sessions):
|
|
tree = ast.parse(Path(mod.__file__).read_text(encoding="utf-8"))
|
|
calls = [
|
|
node
|
|
for node in ast.walk(tree)
|
|
if isinstance(node, ast.Call)
|
|
and _call_name(node) == "_open_session_db_for_profile"
|
|
]
|
|
assert calls
|
|
for call in calls:
|
|
assert any(keyword.arg == "read_only" for keyword in call.keywords)
|
|
|
|
|
|
def test_bulk_delete_sessiondb_work_runs_off_event_loop(monkeypatch):
|
|
loop_thread = threading.get_ident()
|
|
db_threads: list[int] = []
|
|
db_modes: list[bool] = []
|
|
|
|
class _DB:
|
|
def delete_sessions(self, ids):
|
|
db_threads.append(threading.get_ident())
|
|
assert ids == ["one", "two"]
|
|
return 2
|
|
|
|
def close(self):
|
|
db_threads.append(threading.get_ident())
|
|
|
|
def _open_db(profile=None, *, read_only):
|
|
assert profile is None
|
|
db_modes.append(read_only)
|
|
return _DB()
|
|
|
|
monkeypatch.setattr(_web_server_sessions, "_open_session_db_for_profile", _open_db)
|
|
|
|
result = asyncio.run(
|
|
_rt_sessions.bulk_delete_sessions_endpoint(
|
|
_web_models.BulkDeleteSessions(ids=["one", "two"])
|
|
)
|
|
)
|
|
|
|
assert result == {"ok": True, "deleted": 2}
|
|
assert db_modes == [False]
|
|
assert db_threads
|
|
assert all(thread_id != loop_thread for thread_id in db_threads)
|