70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Tests for bin/session_tool.py and Hermes session resetting in hermes_llm.py."""
|
|
|
|
import json
|
|
import tempfile
|
|
from pathlib import Path
|
|
from bin.session_tool import get_active_session_id, reset_session, SESSION_FILE_NAME
|
|
from hermes_llm import HermesLLM
|
|
|
|
|
|
def test_session_tool_api():
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
workspace = Path(tmpdir)
|
|
session_file = workspace / SESSION_FILE_NAME
|
|
|
|
# Initially no session
|
|
assert get_active_session_id(workspace) is None
|
|
|
|
# Write mock session file
|
|
session_file.write_text(json.dumps({"session_id": "test_session_123"}) + "\n")
|
|
assert get_active_session_id(workspace) == "test_session_123"
|
|
|
|
# Reset session
|
|
ok, msg = reset_session(workspace)
|
|
assert ok
|
|
assert "reset successfully" in msg.lower() or "no active session" in msg.lower()
|
|
assert get_active_session_id(workspace) is None
|
|
assert not session_file.exists()
|
|
|
|
# Double reset safely handles non-existent file
|
|
ok, msg = reset_session(workspace)
|
|
assert ok
|
|
print("PASS: test_session_tool_api verified")
|
|
|
|
|
|
def test_hermes_llm_session_sync():
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
workspace = Path(tmpdir)
|
|
session_file = workspace / SESSION_FILE_NAME
|
|
|
|
# Write mock session ID
|
|
session_file.write_text(json.dumps({"session_id": "test_session_456"}) + "\n")
|
|
|
|
llm = HermesLLM(cwd=workspace)
|
|
assert llm._session_id == "test_session_456"
|
|
|
|
# External reset via tool
|
|
reset_session(workspace)
|
|
assert get_active_session_id(workspace) is None
|
|
|
|
# Sync disk state in HermesLLM
|
|
llm._sync_disk_session()
|
|
assert llm._session_id is None
|
|
|
|
# Call reset_session directly on instance
|
|
session_file.write_text(json.dumps({"session_id": "test_session_789"}) + "\n")
|
|
llm._sync_disk_session()
|
|
assert llm._session_id == "test_session_789"
|
|
|
|
llm.reset_session()
|
|
assert llm._session_id is None
|
|
assert not session_file.exists()
|
|
print("PASS: test_hermes_llm_session_sync verified")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_session_tool_api()
|
|
test_hermes_llm_session_sync()
|
|
print("\nAll session tool tests passed successfully!")
|