File size: 1,905 Bytes
63c75d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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"