aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tasks')
-rw-r--r--tests/tasks/test_scanner.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/tasks/test_scanner.py b/tests/tasks/test_scanner.py
index dbeeac9..af5a64e 100644
--- a/tests/tasks/test_scanner.py
+++ b/tests/tasks/test_scanner.py
@@ -17,6 +17,7 @@ from huey.exceptions import RetryTask
from pytest import MonkeyPatch
from webmentions_ssg import db
+from webmentions_ssg.models.scan_state import ScanState
from webmentions_ssg.models.sources import (
SentWebmention,
SentWebmentionStatus,
@@ -1464,3 +1465,59 @@ def test_scan_sources_ignores_root_index(
assert len(sources) == 1
assert sources[0].path == "example/index.html"
+
+
+def test_scan_records_only_completed_source_scans(
+ app: Flask, scanner_module: ModuleType, tmp_path: Path
+) -> None:
+ configure_scanner(app, tmp_path)
+ with app.app_context():
+ assert db.session.get(ScanState, 1) is None
+
+ # Empty traversals are inconclusive and do not report completion.
+ run_scan(app, scanner_module)
+ with app.app_context():
+ assert db.session.get(ScanState, 1) is None
+
+ write_post(tmp_path, "completed", "Completed scan")
+ before = datetime.now(UTC)
+ run_scan(app, scanner_module)
+ after = datetime.now(UTC)
+ with app.app_context():
+ state = db.session.get(ScanState, 1)
+ assert state is not None
+ recorded = state.last_completed_at.replace(tzinfo=UTC)
+ assert before <= recorded <= after
+
+ # Another inconclusive pass must preserve the completion time.
+ (tmp_path / "completed" / "index.html").unlink()
+ run_scan(app, scanner_module)
+ with app.app_context():
+ state = db.session.get(ScanState, 1)
+ assert state is not None
+ assert state.last_completed_at.replace(tzinfo=UTC) == recorded
+
+
+def test_failed_scan_does_not_update_completion_time(
+ app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch
+) -> None:
+ configure_scanner(app, tmp_path)
+ write_post(tmp_path, "existing", "Scan succeeded")
+ run_scan(app, scanner_module)
+
+ with app.app_context():
+ previous = db.session.get(ScanState, 1)
+ assert previous is not None
+ previous_time = previous.last_completed_at
+
+ def fail(*args: Any, **kwargs: Any) -> None:
+ raise RuntimeError("parse failed")
+
+ monkeypatch.setattr(scanner_module, "reconcile_source_tree", fail)
+ with pytest.raises(RuntimeError, match="parse failed"):
+ run_scan(app, scanner_module)
+
+ with app.app_context():
+ state = db.session.get(ScanState, 1)
+ assert state is not None
+ assert state.last_completed_at == previous_time