openclaw-session-amplifier / tests /test_session_snapshot.py
Ordo
Initial public release
63c75d5
from reviewer import session_snapshot as snap
def test_build_session_snapshot(monkeypatch):
summary = {
"session_id": "s1",
"agent_id": "ops",
"last_event_at": "2026-05-12T12:00:00+00:00",
"event_count": 4,
"tool_result_count": 2,
"error_count": 1,
"noisy_tool_results": 0,
"last_entry_idx": 4,
"hints": ["1 error(s)"],
}
activity = [
{"session_id": "s1", "agent_id": "ops", "entry_idx": 1, "role": "user", "preview": "deploy to Cloud Run?", "clean_text": "deploy to Cloud Run?", "is_error": 0},
{"session_id": "s1", "agent_id": "ops", "entry_idx": 2, "role": "toolResult", "tool_name": "exec", "preview": "permission denied", "clean_text": "permission denied", "is_error": 1},
]
monkeypatch.setattr(snap, "get_session_activity", lambda session_id, limit: activity)
result = snap.build_session_snapshot(summary, activity_limit=20)
assert result["schema"] == "openclaw.session.v1"
assert result["id"] == "s1"
assert result["owner"]["agent_id"] == "ops"
assert result["state"]["state"] == "warning"
assert "errors_present" in result["state"]["reasons"]
assert result["health"]["tool_ratio"] == 0.5
assert "approval_or_permission" in result["risk"]["flags"]
assert "external_infra" in result["risk"]["flags"]
assert result["outputs"]["recent_events"][1]["event_type"] == "tool_error"
def test_build_recent_session_snapshots(monkeypatch):
monkeypatch.setattr(snap, "get_recent_sessions", lambda limit: [{"session_id": "s1", "agent_id": "ops"}])
monkeypatch.setattr(snap, "get_session_activity", lambda session_id, limit: [])
result = snap.build_recent_session_snapshots(limit=1, activity_limit=1)
assert result["schema"] == "openclaw.session.v1.collection"
assert result["count"] == 1
assert result["snapshots"][0]["id"] == "s1"