test_audit.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """Tests for the daily action audit log."""
  2. import json
  3. from stt_daemon.assistant.audit import AuditLog
  4. def test_record_writes_daily_file(tmp_path):
  5. log = AuditLog(enabled=True, directory=str(tmp_path))
  6. log.record(mode="command", text="what time is it", tool="get_datetime", ok=True, result="12:00")
  7. path = log.path_for_today()
  8. assert path.exists()
  9. record = json.loads(path.read_text().splitlines()[0])
  10. assert record["mode"] == "command"
  11. assert record["tool"] == "get_datetime"
  12. assert record["ok"] is True
  13. assert "ts" in record
  14. def test_disabled_writes_nothing(tmp_path):
  15. log = AuditLog(enabled=False, directory=str(tmp_path))
  16. log.record(mode="command")
  17. assert list(tmp_path.iterdir()) == []
  18. def test_appends_one_line_per_record(tmp_path):
  19. log = AuditLog(enabled=True, directory=str(tmp_path))
  20. log.record(n=1)
  21. log.record(n=2)
  22. lines = log.path_for_today().read_text().splitlines()
  23. assert len(lines) == 2
  24. assert json.loads(lines[1])["n"] == 2
  25. def test_best_effort_never_raises(tmp_path):
  26. blocker = tmp_path / "blocker"
  27. blocker.write_text("not a directory")
  28. log = AuditLog(enabled=True, directory=str(blocker / "sub"))
  29. log.record(x=1) # must not raise