Files
upbeatBytes/tests/test_briefs_fk.py
T
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

26 lines
1.1 KiB
Python

from goodnews.db import connect, init_db
def test_deleting_a_brief_cascades_to_items(tmp_path):
"""The orphaned-items bug: a brief was deleted while its items survived,
because FK enforcement is per-connection in SQLite. connect() turns it on,
so ON DELETE CASCADE must now clear the items and leave no FK violations.
"""
c = connect(str(tmp_path / "t.db")); init_db(c)
assert c.execute("PRAGMA foreign_keys").fetchone()[0] == 1 # enforced
c.execute("INSERT INTO sources (id, name, feed_url) VALUES (1, 'S', 'http://s/f')")
c.execute(
"INSERT INTO articles (id, source_id, canonical_url, title, url_hash, published_at) "
"VALUES (1, 1, 'http://s/a', 'A', 'h1', '2026-01-01T00:00:00')"
)
c.execute("INSERT INTO daily_briefs (id, brief_date, title) VALUES (9, '2026-01-01', 'B')")
c.execute("INSERT INTO daily_brief_items (brief_id, article_id, rank) VALUES (9, 1, 1)")
c.commit()
c.execute("DELETE FROM daily_briefs WHERE id = 9")
c.commit()
assert c.execute("SELECT COUNT(*) FROM daily_brief_items").fetchone()[0] == 0
assert c.execute("PRAGMA foreign_key_check").fetchall() == []