Files
thejayman77 452e5a3fe4 Hardening pass: scheduler backoff, FK cascade, a11y, test safety net
Pre-traffic cleanup from an audit:

* Scheduler: poll_due_sources now keys on the last *attempt* (success or
  failure), not the last success, and scales the wait by the consecutive-
  failure streak (capped at a day). A failing feed (e.g. Phys.org's HTTP 429s)
  used to be retried every cycle because it had no successful run; it now backs
  off and recovers on its own. Extracted due_source_rows() + tests.

* FK hygiene: deleting a daily_brief is supposed to cascade to its items, but
  SQLite enforces foreign keys per-connection — connect() already sets the
  pragma, so the cascade is correct going forward; added a regression test.
  (Orphaned items + Phys.org settings were cleaned directly on the live DB.)

* a11y: modal/drawer dialogs are now focusable (tabindex), close on Escape
  (window) and on backdrop click via a target check (dropping the inner
  stopPropagation handlers). Build is warning-free.

* tests: conftest points any un-mocked LLM client at a closed port with a 1s
  timeout, so an accidental real call fails fast instead of hanging the suite.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-06 19:18:18 +00:00

63 lines
2.2 KiB
Python

from goodnews.db import connect, init_db
from goodnews.feeds import due_source_rows
def _src(c, sid, *, interval=120, failures=0, active=1):
c.execute(
"INSERT INTO sources (id, name, feed_url, poll_interval_minutes, consecutive_failures, active) "
"VALUES (?, ?, ?, ?, ?, ?)",
(sid, f"S{sid}", f"http://s/{sid}", interval, failures, active),
)
def _attempt(c, sid, *, minutes_ago, status="ok"):
c.execute(
"INSERT INTO ingest_runs (source_id, finished_at, status) "
"VALUES (?, datetime('now', ?), ?)",
(sid, f"-{minutes_ago} minutes", status),
)
def _due_ids(c):
return {r["id"] for r in due_source_rows(c)}
def test_never_attempted_is_due(tmp_path):
c = connect(str(tmp_path / "t.db")); init_db(c)
_src(c, 1)
assert _due_ids(c) == {1}
def test_recent_attempt_not_due_old_attempt_due(tmp_path):
c = connect(str(tmp_path / "t.db")); init_db(c)
_src(c, 1, interval=120); _attempt(c, 1, minutes_ago=5) # polled recently
_src(c, 2, interval=120); _attempt(c, 2, minutes_ago=200) # overdue
assert _due_ids(c) == {2}
def test_failing_source_backs_off(tmp_path):
# The regression: a perpetually-FAILING source (no 'ok' run) used to be due
# every cycle. With backoff it rests — effective wait = 60*(1+10)=660 min.
c = connect(str(tmp_path / "t.db")); init_db(c)
_src(c, 1, interval=60, failures=10)
_attempt(c, 1, minutes_ago=200, status="failed") # 200 < 660 → not due yet
assert _due_ids(c) == set()
# ...but once past the backed-off window it becomes due again.
c.execute("DELETE FROM ingest_runs WHERE source_id = 1")
_attempt(c, 1, minutes_ago=700, status="failed") # 700 > 660 → due
assert _due_ids(c) == {1}
def test_backoff_capped_at_a_day(tmp_path):
# A long failure streak can't push the wait past MAX_BACKOFF_MINUTES (1440).
c = connect(str(tmp_path / "t.db")); init_db(c)
_src(c, 1, interval=120, failures=999) # 120*1000 would be huge
_attempt(c, 1, minutes_ago=1500, status="failed") # >1440 → due despite streak
assert _due_ids(c) == {1}
def test_inactive_never_due(tmp_path):
c = connect(str(tmp_path / "t.db")); init_db(c)
_src(c, 1, active=0)
assert _due_ids(c) == set()