|
| 1 | +"""Tests for the file integrity monitor.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from automation_file import IntegrityMonitor, write_manifest |
| 11 | +from automation_file.exceptions import FileAutomationException |
| 12 | +from automation_file.notify import NotificationManager |
| 13 | +from automation_file.notify.sinks import NotificationSink |
| 14 | + |
| 15 | + |
| 16 | +class _Recorder(NotificationSink): |
| 17 | + name = "recorder" |
| 18 | + |
| 19 | + def __init__(self) -> None: |
| 20 | + self.messages: list[tuple[str, str, str]] = [] |
| 21 | + |
| 22 | + def send(self, subject: str, body: str, level: str = "info") -> None: |
| 23 | + self.messages.append((subject, body, level)) |
| 24 | + |
| 25 | + |
| 26 | +def _build_tree(tmp_path: Path) -> tuple[Path, Path]: |
| 27 | + root = tmp_path / "tree" |
| 28 | + root.mkdir() |
| 29 | + (root / "a.txt").write_text("alpha", encoding="utf-8") |
| 30 | + (root / "b.txt").write_text("bravo", encoding="utf-8") |
| 31 | + manifest_path = tmp_path / "manifest.json" |
| 32 | + write_manifest(root, manifest_path) |
| 33 | + return root, manifest_path |
| 34 | + |
| 35 | + |
| 36 | +def test_check_once_clean_tree(tmp_path: Path) -> None: |
| 37 | + root, manifest_path = _build_tree(tmp_path) |
| 38 | + monitor = IntegrityMonitor(root, manifest_path) |
| 39 | + summary = monitor.check_once() |
| 40 | + assert summary["ok"] is True |
| 41 | + assert monitor.last_summary is summary |
| 42 | + |
| 43 | + |
| 44 | +def test_check_once_detects_modified_file_and_notifies(tmp_path: Path) -> None: |
| 45 | + root, manifest_path = _build_tree(tmp_path) |
| 46 | + (root / "a.txt").write_text("tampered", encoding="utf-8") |
| 47 | + manager = NotificationManager(dedup_seconds=0.0) |
| 48 | + recorder = _Recorder() |
| 49 | + manager.register(recorder) |
| 50 | + monitor = IntegrityMonitor(root, manifest_path, manager=manager) |
| 51 | + summary = monitor.check_once() |
| 52 | + assert summary["ok"] is False |
| 53 | + assert "a.txt" in summary["modified"] |
| 54 | + assert recorder.messages, "expected a drift notification" |
| 55 | + subject, body, level = recorder.messages[0] |
| 56 | + assert level == "error" |
| 57 | + assert "integrity drift" in subject |
| 58 | + assert "a.txt" in body |
| 59 | + |
| 60 | + |
| 61 | +def test_check_once_detects_missing_file(tmp_path: Path) -> None: |
| 62 | + root, manifest_path = _build_tree(tmp_path) |
| 63 | + (root / "b.txt").unlink() |
| 64 | + manager = NotificationManager(dedup_seconds=0.0) |
| 65 | + recorder = _Recorder() |
| 66 | + manager.register(recorder) |
| 67 | + monitor = IntegrityMonitor(root, manifest_path, manager=manager) |
| 68 | + summary = monitor.check_once() |
| 69 | + assert "b.txt" in summary["missing"] |
| 70 | + assert recorder.messages |
| 71 | + |
| 72 | + |
| 73 | +def test_extras_ignored_by_default(tmp_path: Path) -> None: |
| 74 | + root, manifest_path = _build_tree(tmp_path) |
| 75 | + (root / "new.txt").write_text("novel", encoding="utf-8") |
| 76 | + manager = NotificationManager(dedup_seconds=0.0) |
| 77 | + recorder = _Recorder() |
| 78 | + manager.register(recorder) |
| 79 | + monitor = IntegrityMonitor(root, manifest_path, manager=manager) |
| 80 | + summary = monitor.check_once() |
| 81 | + assert "new.txt" in summary["extra"] |
| 82 | + assert summary["ok"] is True |
| 83 | + assert recorder.messages == [] |
| 84 | + |
| 85 | + |
| 86 | +def test_alert_on_extra_flag(tmp_path: Path) -> None: |
| 87 | + root, manifest_path = _build_tree(tmp_path) |
| 88 | + (root / "new.txt").write_text("novel", encoding="utf-8") |
| 89 | + manager = NotificationManager(dedup_seconds=0.0) |
| 90 | + recorder = _Recorder() |
| 91 | + manager.register(recorder) |
| 92 | + monitor = IntegrityMonitor(root, manifest_path, manager=manager, alert_on_extra=True) |
| 93 | + monitor.check_once() |
| 94 | + assert recorder.messages |
| 95 | + |
| 96 | + |
| 97 | +def test_on_drift_callback_invoked(tmp_path: Path) -> None: |
| 98 | + root, manifest_path = _build_tree(tmp_path) |
| 99 | + (root / "a.txt").write_text("tampered", encoding="utf-8") |
| 100 | + seen: list[dict[str, Any]] = [] |
| 101 | + monitor = IntegrityMonitor( |
| 102 | + root, |
| 103 | + manifest_path, |
| 104 | + manager=NotificationManager(dedup_seconds=0.0), |
| 105 | + on_drift=seen.append, |
| 106 | + ) |
| 107 | + monitor.check_once() |
| 108 | + assert len(seen) == 1 |
| 109 | + assert "a.txt" in seen[0]["modified"] |
| 110 | + |
| 111 | + |
| 112 | +def test_missing_manifest_treated_as_drift(tmp_path: Path) -> None: |
| 113 | + root = tmp_path / "tree" |
| 114 | + root.mkdir() |
| 115 | + (root / "a.txt").write_text("alpha", encoding="utf-8") |
| 116 | + manager = NotificationManager(dedup_seconds=0.0) |
| 117 | + recorder = _Recorder() |
| 118 | + manager.register(recorder) |
| 119 | + monitor = IntegrityMonitor(root, tmp_path / "missing.json", manager=manager) |
| 120 | + summary = monitor.check_once() |
| 121 | + assert summary["ok"] is False |
| 122 | + assert "error" in summary |
| 123 | + assert recorder.messages |
| 124 | + |
| 125 | + |
| 126 | +def test_positive_interval_required(tmp_path: Path) -> None: |
| 127 | + root, manifest_path = _build_tree(tmp_path) |
| 128 | + with pytest.raises(FileAutomationException): |
| 129 | + IntegrityMonitor(root, manifest_path, interval=0) |
| 130 | + |
| 131 | + |
| 132 | +def test_start_and_stop_thread(tmp_path: Path) -> None: |
| 133 | + root, manifest_path = _build_tree(tmp_path) |
| 134 | + monitor = IntegrityMonitor(root, manifest_path, interval=0.05) |
| 135 | + monitor.start() |
| 136 | + try: |
| 137 | + # Second start is a no-op. |
| 138 | + monitor.start() |
| 139 | + finally: |
| 140 | + monitor.stop(timeout=1.0) |
0 commit comments