Files
shelfmark/tests/download/test_finished_at.py
T
splitsec2 e575ff0fcb feat(activity): clearer cards, a Rejected view and Reopen for failed deliveries (#1618)
A batch of activity list changes from running it as an admin on my own
instance. They are separate commits that build on each other, so they
can be reviewed in order.

The cards read better. Every card gets a format pill (violet for audio,
orange for ebooks), the requester is named once, and finished cards say
how long a download took and how long ago it ended ("took 5m · 35m
ago"). A download that picked up work already under way shows a
Reattached pill.

Admins can tell what is automatic and what needs a person. A declined
request shows who declined it and when, anything with an `[auto]` note
reads "Declined automatically", and "Needs Review" becomes "No Match
Yet" with a line saying the search repeats on every sync.

There is a Rejected view next to History for admins, with Reopen on each
card and a way to hide the dead ones.

The part to read most carefully is the backend in `requests_service.py`
and `user_db.py`. An admin can now reopen a fulfilled request whose
download failed or was cancelled, and the reopen a failed download
triggers remembers which release failed, so the next search doesn't pick
it again.

I ran it on a scratch instance (when pulling them together to move from
my fork to upstream) as an admin with seeded requests and looked at the
pills, the durations, the wording and the Rejected view in a browser.
Reopening a fulfilled request I only exercised in the tests. The
frontend and Python suites, type check, ruff and basedpyright pass.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Admins can review rejected requests in a dedicated view, hide or
restore cards, and reopen requests.
* Fulfilled requests with failed or cancelled deliveries can be
reopened; the failed release is skipped in the next automatic search.
* Activity cards now show format badges, download timing, and when a
download resumes existing work.
* **Improvements**
* Activity views clarify automatic retries and actions, and show
reviewer names for admins.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-09 12:02:21 -06:00

55 lines
1.8 KiB
Python

"""A live queue entry says when it ended, so a card has its timing before history catches up."""
from __future__ import annotations
from shelfmark.core.models import DownloadTask, QueueStatus
from shelfmark.core.queue import BookQueue
from shelfmark.download.orchestrator import _task_to_dict
def _finished_task(status: QueueStatus) -> tuple[BookQueue, DownloadTask]:
"""A task driven to the given terminal status through the queue."""
queue = BookQueue()
task = DownloadTask(task_id="t", source="direct_download", title="Book")
assert queue.add(task) is True
assert queue.get_next() is not None
queue.update_status("t", QueueStatus.DOWNLOADING)
queue.update_status("t", status)
return queue, task
def test_a_task_is_stamped_when_it_first_ends() -> None:
_queue, task = _finished_task(QueueStatus.COMPLETE)
assert task.finished_at is not None
assert task.finished_at >= task.added_time
def test_the_stamp_is_set_before_the_terminal_hook_runs() -> None:
queue = BookQueue()
task = DownloadTask(task_id="t", source="direct_download", title="Book")
seen: list[float | None] = []
queue.set_terminal_status_hook(lambda _id, _status, hooked: seen.append(hooked.finished_at))
assert queue.add(task) is True
assert queue.get_next() is not None
queue.update_status("t", QueueStatus.ERROR)
assert seen and seen[0] is not None
def test_a_requeued_task_is_running_again_so_it_has_no_end() -> None:
queue, task = _finished_task(QueueStatus.ERROR)
assert queue.enqueue_existing("t") is True
assert task.finished_at is None
def test_the_stamp_reaches_the_status_payload() -> None:
_queue, task = _finished_task(QueueStatus.CANCELLED)
assert (
_task_to_dict(task, current_status=QueueStatus.CANCELLED)["finished_at"] == task.finished_at
)