|
| 1 | +"""Runtime validation at the JSONL → SessionDict boundary.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
| 10 | + |
| 11 | +from models.errors import SessionValidationError # noqa: E402 |
| 12 | +from utils.jsonl_parser import parse_session # noqa: E402 |
| 13 | +from utils.validation import validate_session_dict # noqa: E402 |
| 14 | + |
| 15 | +FIXTURES = os.path.join(os.path.dirname(__file__), "fixtures") |
| 16 | + |
| 17 | + |
| 18 | +def _valid_payload(**overrides: Any) -> dict[str, Any]: |
| 19 | + base: dict[str, Any] = { |
| 20 | + "session_id": "abc123", |
| 21 | + "title": "Test Session", |
| 22 | + "messages": [{"role": "user", "text": "hello"}], |
| 23 | + "metadata": {"session_id": "abc123"}, |
| 24 | + } |
| 25 | + base.update(overrides) |
| 26 | + return base |
| 27 | + |
| 28 | + |
| 29 | +class TestValidateSessionDict: |
| 30 | + def test_missing_session_id(self): |
| 31 | + payload = _valid_payload() |
| 32 | + del payload["session_id"] |
| 33 | + with pytest.raises(SessionValidationError) as exc_info: |
| 34 | + validate_session_dict(payload) |
| 35 | + assert exc_info.value.path == "session_id" |
| 36 | + |
| 37 | + def test_wrong_type_session_id(self): |
| 38 | + with pytest.raises(SessionValidationError) as exc_info: |
| 39 | + validate_session_dict(_valid_payload(session_id=123)) |
| 40 | + assert exc_info.value.path == "session_id" |
| 41 | + |
| 42 | + def test_null_session_id(self): |
| 43 | + with pytest.raises(SessionValidationError) as exc_info: |
| 44 | + validate_session_dict(_valid_payload(session_id=None)) |
| 45 | + assert exc_info.value.path == "session_id" |
| 46 | + assert exc_info.value.detail == "must not be null" |
| 47 | + |
| 48 | + def test_null_role_in_message(self): |
| 49 | + with pytest.raises(SessionValidationError) as exc_info: |
| 50 | + validate_session_dict( |
| 51 | + _valid_payload(messages=[{"role": None, "text": "x"}]) |
| 52 | + ) |
| 53 | + assert exc_info.value.path == "messages[0].role" |
| 54 | + |
| 55 | + def test_missing_role_in_message(self): |
| 56 | + with pytest.raises(SessionValidationError) as exc_info: |
| 57 | + validate_session_dict( |
| 58 | + _valid_payload(messages=[{"text": "no role key"}]) |
| 59 | + ) |
| 60 | + assert exc_info.value.path == "messages[0].role" |
| 61 | + |
| 62 | + def test_metadata_not_dict(self): |
| 63 | + with pytest.raises(SessionValidationError) as exc_info: |
| 64 | + validate_session_dict(_valid_payload(metadata="not-a-dict")) |
| 65 | + assert exc_info.value.path == "metadata" |
| 66 | + |
| 67 | + def test_message_not_dict(self): |
| 68 | + with pytest.raises(SessionValidationError) as exc_info: |
| 69 | + validate_session_dict(_valid_payload(messages=["not-a-dict"])) |
| 70 | + assert exc_info.value.path == "messages[0]" |
| 71 | + |
| 72 | + def test_valid_payload_returns_session_dict(self): |
| 73 | + result = validate_session_dict(_valid_payload()) |
| 74 | + assert result["session_id"] == "abc123" |
| 75 | + assert result["messages"][0]["role"] == "user" |
| 76 | + |
| 77 | + |
| 78 | +class TestParseSessionValidationRegression: |
| 79 | + def test_session_minimal_fixture_unchanged(self): |
| 80 | + path = os.path.join(FIXTURES, "session_minimal.jsonl") |
| 81 | + session = parse_session(path) |
| 82 | + assert session["session_id"] == "session_minimal" |
| 83 | + assert session["title"] == "Hello from integration fixture" |
| 84 | + assert len(session["messages"]) == 2 |
| 85 | + assert session["messages"][0]["role"] == "user" |
| 86 | + assert session["messages"][1]["role"] == "assistant" |
0 commit comments