# retoor <retoor@molodetz.nl>

import json

from devplacepy.services.jobs.isslop.events import KIND_DONE, WorkerEvent


def test_event_roundtrip():
    event = WorkerEvent(kind=KIND_DONE, message="Analysis complete", data={"grade": "A"})
    parsed = WorkerEvent.parse(event.to_json())
    assert parsed is not None
    assert parsed.kind == KIND_DONE
    assert parsed.message == "Analysis complete"
    assert parsed.data == {"grade": "A"}


def test_parse_rejects_non_json_lines():
    assert WorkerEvent.parse("") is None
    assert WorkerEvent.parse("plain log output") is None
    assert WorkerEvent.parse("{broken json") is None


def test_parse_rejects_missing_fields():
    assert WorkerEvent.parse(json.dumps({"kind": "log"})) is None
    assert WorkerEvent.parse(json.dumps({"message": "no kind"})) is None


def test_parse_tolerates_non_dict_data():
    parsed = WorkerEvent.parse(json.dumps({"kind": "log", "message": "x", "data": [1, 2]}))
    assert parsed is not None
    assert parsed.data == {}
