"""Tests for persistent audit event storage.""" import time from fusionagi.api.audit_store import get_audit_count, get_audit_events, record_audit_event def test_record_and_retrieve(tmp_path, monkeypatch): """Should record and retrieve audit events.""" monkeypatch.setenv("FUSIONAGI_AUDIT_DB", str(tmp_path / "test_audit.db")) # Reset connection import fusionagi.api.audit_store as mod mod._conn = None eid = record_audit_event("test.action", actor="user1", resource_type="session", resource_id="s1") assert eid > 0 events = get_audit_events(limit=10) assert len(events) >= 1 assert events[0]["action"] == "test.action" assert events[0]["actor"] == "user1" def test_filter_by_action(tmp_path, monkeypatch): """Should filter events by action.""" monkeypatch.setenv("FUSIONAGI_AUDIT_DB", str(tmp_path / "test_audit2.db")) import fusionagi.api.audit_store as mod mod._conn = None record_audit_event("session.create") record_audit_event("prompt.submit") record_audit_event("session.create") events = get_audit_events(action="session.create") assert all(e["action"] == "session.create" for e in events) def test_filter_by_since(tmp_path, monkeypatch): """Should filter events by timestamp.""" monkeypatch.setenv("FUSIONAGI_AUDIT_DB", str(tmp_path / "test_audit3.db")) import fusionagi.api.audit_store as mod mod._conn = None record_audit_event("old.event") future = time.time() + 1000 events = get_audit_events(since=future) assert len(events) == 0 def test_count(tmp_path, monkeypatch): """Should return total count.""" monkeypatch.setenv("FUSIONAGI_AUDIT_DB", str(tmp_path / "test_audit4.db")) import fusionagi.api.audit_store as mod mod._conn = None record_audit_event("count.test") record_audit_event("count.test") assert get_audit_count() >= 2