| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- """Tests for the daily action audit log."""
- import json
- from stt_daemon.assistant.audit import AuditLog
- def test_record_writes_daily_file(tmp_path):
- log = AuditLog(enabled=True, directory=str(tmp_path))
- log.record(mode="command", text="what time is it", tool="get_datetime", ok=True, result="12:00")
- path = log.path_for_today()
- assert path.exists()
- record = json.loads(path.read_text().splitlines()[0])
- assert record["mode"] == "command"
- assert record["tool"] == "get_datetime"
- assert record["ok"] is True
- assert "ts" in record
- def test_disabled_writes_nothing(tmp_path):
- log = AuditLog(enabled=False, directory=str(tmp_path))
- log.record(mode="command")
- assert list(tmp_path.iterdir()) == []
- def test_appends_one_line_per_record(tmp_path):
- log = AuditLog(enabled=True, directory=str(tmp_path))
- log.record(n=1)
- log.record(n=2)
- lines = log.path_for_today().read_text().splitlines()
- assert len(lines) == 2
- assert json.loads(lines[1])["n"] == 2
- def test_best_effort_never_raises(tmp_path):
- blocker = tmp_path / "blocker"
- blocker.write_text("not a directory")
- log = AuditLog(enabled=True, directory=str(blocker / "sub"))
- log.record(x=1) # must not raise
|