fix(search): every related and live-search request takes a slot, and a failed one gives it back
A related list's cursor cost nothing, and a cursor is a free string: a replayed one read and enriched without end. Every request now takes a slot of the user's daily cap — taken under a lock before the work (`quota.claim_action`), so requests sent together cannot all pass the last free one, and given back when the request fails outright (`quota.refund_action`). The live search's scrape source takes its slots the same way. A limit of 0 turns the feature off: a distinct 409 `related_disabled` / `search_disabled`, and `/api/me` says `related_enabled` / `search_enabled` so the UI can leave it out. A videos.list that fails no longer reads as an empty page: `ingest_candidates` raises, and the search and the related list answer the refusal (the shared budget spent says "quota") instead of "no results". The two routes' page loops are one (`_gather`). `clear` deletes only what the caller found: a related list's rows carry no find, and any user could name them.
This commit is contained in:
@@ -13,10 +13,13 @@ a placeholder never becomes the video's permanent date.
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import pytest
|
||||
|
||||
from app import sysconfig
|
||||
from app.models import User
|
||||
from app.catalog.models import Video
|
||||
from app.catalog.search import ingest_candidates
|
||||
from app.youtube.client import YouTubeError
|
||||
|
||||
|
||||
class _FakeYT:
|
||||
@@ -97,6 +100,25 @@ def test_scrape_ingest_enrich_miss_keeps_nonnull_placeholder(db):
|
||||
assert c.enriched_at is None
|
||||
|
||||
|
||||
class _FailingYT(_FakeYT):
|
||||
def get_videos(self, ids):
|
||||
raise YouTubeError("videos -> 403", reason="quotaExceeded", path="videos")
|
||||
|
||||
|
||||
def test_a_videos_list_failure_raises_and_keeps_the_stubs(db):
|
||||
# Swallowed, it answered as a page with nothing on it — "no results" for a search whose lookup
|
||||
# never ran. The stubs stay: the FK targets, backfilled later.
|
||||
user = User(email="searcher3@example.com")
|
||||
db.add(user)
|
||||
db.commit()
|
||||
|
||||
with pytest.raises(YouTubeError):
|
||||
ingest_candidates(db, _FailingYT({}), user, [_candidate("vid_ddddddd", "UC_chan_d")])
|
||||
|
||||
db.rollback() # what the caller does: the stubs were committed before the lookup
|
||||
assert db.get(Video, "vid_ddddddd") is not None
|
||||
|
||||
|
||||
def test_search_youtube_route_returns_422_when_ingest_fails(db, monkeypatch, override_dep):
|
||||
# Route-level guard (B-core-1): an ingest failure with nothing yet collected must surface as a 422
|
||||
# + message on this JSON endpoint (the FE hides 5xx behind a generic "connection lost" + auto-retry,
|
||||
|
||||
Reference in New Issue
Block a user