Files
romm/backend/tests/handler/test_play_session_handler.py
Sam Dornan d05f5cc5e1 feat(ra): sync a player's achievements after they play an RA game (#5425)
* feat(ra): sync a player's achievements after they play an RA game

RetroAchievements progress only synced nightly (and only when the
scheduled task is enabled) or by hand from Settings, so achievements
earned in a session didn't show in RomM until much later.

When a play session is recorded for a ROM matched to RetroAchievements,
queue an incremental progress sync for that user on the low-priority
queue. Every play path (web player, streaming, device sync) records its
sessions through ingest_play_sessions, so all of them trigger it. A fixed
job id keeps one sync per user queued at a time.

The per-user sync body moves out of the scheduled task so both share it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NH5s4iit2hMN1bb49UMpcT

* fix(ra): queue the after-play sync under a valid id that a failure can't hold

The job id used a colon, which RQ rejects, so every enqueue raised and was
logged instead of queued. Use an underscore.

With unique=True, RQ refuses any job whose id still exists, and a failed
job is kept for failure_ttl (a year by default). One failed sync would
have blocked that user's after-play syncs. The job now logs its own
errors instead of failing, and failure_ttl is capped for a worker that
dies mid-run.

Add tests against the real queue: one job per user, and the id is free
again after a sync succeeds or fails. Also drop the docstring's Raises
block and narrow await_args before reading it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NH5s4iit2hMN1bb49UMpcT

* fix(ra): give the after-play sync the RetroAchievements task's timeout

It used RQ's 180s default, which a first sync of a large account can
exceed. Use the scheduled sync's configured timeout (TASK_TIMEOUT).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NH5s4iit2hMN1bb49UMpcT

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 17:10:25 -05:00

65 lines
2.1 KiB
Python

import uuid
from collections.abc import Iterator
import pytest
from rq import Queue, SimpleWorker
from handler import play_session_handler
from handler.play_session_handler import _queue_ra_progress_sync
from handler.redis_handler import redis_client
from tasks.registry import SYNC_RETROACHIEVEMENTS_PROGRESS_SPEC
JOB_PATH = "tasks.scheduled.sync_retroachievements_progress"
@pytest.fixture
def queue(mocker) -> Iterator[Queue]:
queue = Queue(name=f"test-ra-sync-{uuid.uuid4().hex}", connection=redis_client)
mocker.patch.object(play_session_handler, "low_prio_queue", queue)
yield queue
for job in queue.jobs:
job.delete()
@pytest.fixture
def user_ids() -> tuple[int, int]:
"""Ids no other test queues a sync for, as the job ids live in a shared Redis."""
first = uuid.uuid4().int % 10**9
return first, first + 1
def run_queued_jobs(queue: Queue) -> None:
SimpleWorker([queue], connection=redis_client).work(burst=True)
class TestQueueRAProgressSync:
def test_one_sync_per_user_is_queued_at_a_time(
self, queue: Queue, user_ids: tuple[int, int]
):
first, second = user_ids
_queue_ra_progress_sync(first)
_queue_ra_progress_sync(first)
_queue_ra_progress_sync(second)
assert sorted(job.kwargs["user_id"] for job in queue.jobs) == [first, second]
assert {job.timeout for job in queue.jobs} == {
SYNC_RETROACHIEVEMENTS_PROGRESS_SPEC.timeout
}
@pytest.mark.parametrize("fails", [False, True], ids=["synced", "sync_failed"])
def test_a_finished_sync_lets_the_next_play_queue_another(
self, queue: Queue, mocker, fails: bool, user_ids: tuple[int, int]
):
user_id, _ = user_ids
sync = mocker.patch(
f"{JOB_PATH}._sync_user_progress_by_id",
side_effect=RuntimeError("RetroAchievements is down") if fails else None,
)
_queue_ra_progress_sync(user_id)
run_queued_jobs(queue)
_queue_ra_progress_sync(user_id)
sync.assert_called_once_with(user_id)
assert [job.kwargs["user_id"] for job in queue.jobs] == [user_id]