from app.core.audit_log import read_audit_events, write_audit_event


def test_audit_log_roundtrip(tmp_path, monkeypatch):
    audit_file = tmp_path / "admin-audit.jsonl"
    monkeypatch.setattr("app.core.audit_log.settings.admin_audit_log_path", str(audit_file))
    write_audit_event("admin.user.patch", "u1", "admin@example.com", {"target": "u2"})
    events = read_audit_events()
    assert len(events) == 1
    assert events[0]["action"] == "admin.user.patch"
    assert events[0]["details"]["target"] == "u2"


def test_read_audit_events_ignores_invalid_json(tmp_path, monkeypatch):
    audit_file = tmp_path / "admin-audit.jsonl"
    audit_file.write_text('{"action":"ok"}\nnot-json\n', encoding="utf-8")
    monkeypatch.setattr("app.core.audit_log.settings.admin_audit_log_path", str(audit_file))
    events = read_audit_events()
    assert len(events) == 1
