roledesk is built on the boilerplate from here on: pipeline 2.0 (queue and scheduler), auth (one password, sessions, passkeys), the deploy kit and its docs, plus the coupling and packaging tests. Downstream copies, unedited. Refreshed from boilerplate/d56949f7d0f6c0bfe5745478e09007bfad67ae87 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
135 lines
5.2 KiB
Python
135 lines
5.2 KiB
Python
"""The notice log: said once, kept until cleared, forgotten when it stops.
|
|
|
|
A notice used to be a line that faded in twelve seconds in whichever tab was
|
|
open, and was raised as often as not while none was. These hold the three
|
|
things the table is for: a repeat is the same row and not news, a cleared row
|
|
makes the next one news again, and what nobody clears goes when it has stopped
|
|
being said rather than when it was first said.
|
|
|
|
python -m pytest pipeline/tests/test_notice_log.py -q
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
import sys
|
|
import tempfile
|
|
from contextlib import closing
|
|
from datetime import UTC, datetime, timedelta
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
|
|
|
from pipeline import NULL, JobQueue, Jobs, Notice, RunContext, job
|
|
from pipeline.web import connect
|
|
|
|
|
|
@job("n_refused", "Meeting the same refusal as last time")
|
|
def n_refused(config, store, *, context: RunContext = NULL) -> dict:
|
|
context.notice(Notice(code="agent.refused", key="Worm hits npm",
|
|
text="No abstract: Worm hits npm", fix="Nothing to do."))
|
|
return {}
|
|
|
|
|
|
@pytest.fixture
|
|
def jobs():
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
yield Jobs(Path(tmp) / "n.db")
|
|
|
|
|
|
def refusal(text: str = "No abstract: Worm hits npm") -> Notice:
|
|
return Notice(code="agent.refused", key="Worm hits npm", text=text,
|
|
where="the tagging lines in the server log")
|
|
|
|
|
|
def test_the_same_notice_again_is_the_same_row_with_a_count(jobs):
|
|
first = jobs.record_notice(refusal())
|
|
again = jobs.record_notice(refusal("No abstract, again: Worm hits npm"))
|
|
other = jobs.record_notice(Notice(code="agent.refused", key="Other", text="x"))
|
|
|
|
assert (first.count, again.count) == (1, 2), "one is news, two is a repeat"
|
|
assert again.id == first.id and other.id != first.id, "keyed by code and key"
|
|
assert again.first_at == first.first_at and again.last_at >= first.last_at
|
|
assert again.text.startswith("No abstract, again"), "the newer wording is kept"
|
|
assert again.where == first.where, "and `where` survives its SQL column name"
|
|
assert [n.key for n in jobs.notices()] == ["Other", "Worm hits npm"]
|
|
|
|
|
|
def test_clearing_one_makes_the_next_occurrence_news_again(jobs):
|
|
said = jobs.record_notice(refusal())
|
|
|
|
assert jobs.clear_notice(said.id)
|
|
assert not jobs.clear_notice(said.id), "gone already is an answer, not an error"
|
|
assert jobs.record_notice(refusal()).count == 1
|
|
|
|
|
|
def test_a_notice_is_forgotten_when_it_stops_being_said_not_when_it_began(jobs):
|
|
"""By `last_at`. A feed that has failed nightly for a month is an old row
|
|
that was true an hour ago, and dropping it would make tonight's failure
|
|
read as the first."""
|
|
chronic = jobs.record_notice(Notice(code="source.failed", key="rss", text="x"))
|
|
over = jobs.record_notice(Notice(code="source.failed", key="old", text="y"))
|
|
month = (datetime.now(UTC) - timedelta(days=30)).isoformat()
|
|
with closing(sqlite3.connect(jobs.path)) as conn, conn:
|
|
conn.execute("UPDATE notices SET first_at=? WHERE id=?", (month, chronic.id))
|
|
conn.execute("UPDATE notices SET first_at=?, last_at=? WHERE id=?",
|
|
(month, month, over.id))
|
|
|
|
assert jobs.purge_notices(14 * 86400) == 1
|
|
assert [n.key for n in jobs.notices()] == ["rss"]
|
|
|
|
|
|
def test_a_job_that_repeats_itself_is_heard_as_a_repeat(jobs):
|
|
"""The whole path. Every notice still reaches the listener — what
|
|
interrupts a person is the interface's business — and the second carries
|
|
the count that says it is nothing new."""
|
|
heard: list = []
|
|
|
|
async def on_notice(notice) -> None:
|
|
heard.append(notice)
|
|
|
|
for _ in range(2):
|
|
jobs.enqueue("n_refused")
|
|
JobQueue(jobs, None, None, on_notice=on_notice).drain(timeout=10)
|
|
|
|
assert [n.count for n in heard] == [1, 2]
|
|
assert len(jobs.notices()) == 1
|
|
|
|
|
|
def test_a_log_that_cannot_be_written_does_not_cost_the_notice(jobs, monkeypatch):
|
|
heard: list = []
|
|
|
|
async def on_notice(notice) -> None:
|
|
heard.append(notice)
|
|
|
|
def broken(notice):
|
|
raise sqlite3.OperationalError("database is locked")
|
|
|
|
monkeypatch.setattr(jobs, "record_notice", broken)
|
|
done = jobs.enqueue("n_refused")
|
|
JobQueue(jobs, None, None, on_notice=on_notice).drain(timeout=10)
|
|
|
|
assert [n.code for n in heard] == ["agent.refused"]
|
|
assert jobs.get(done.id).state == "done", "and the job that said it is unharmed"
|
|
|
|
|
|
def test_the_log_is_read_and_cleared_over_http(jobs):
|
|
app = FastAPI()
|
|
connect(app, jobs, None, None)
|
|
jobs.record_notice(refusal())
|
|
jobs.record_notice(Notice(code="source.failed", key="rss", text="rss failed"))
|
|
|
|
with TestClient(app) as client:
|
|
listed = client.get("/api/notices").json()
|
|
assert [n["code"] for n in listed] == ["source.failed", "agent.refused"]
|
|
assert listed[1]["where"] == "the tagging lines in the server log"
|
|
|
|
assert client.delete(f"/api/notices/{listed[0]['id']}").json() == {"cleared": True}
|
|
assert client.delete(f"/api/notices/{listed[0]['id']}").json() == {"cleared": False}
|
|
assert client.delete("/api/notices").json() == {"cleared": 1}
|
|
assert client.get("/api/notices").json() == []
|