mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-28 01:49:00 +09:00
## Bulk Mode — submit up to 100 karaoke jobs at once
Adds a **Bulk Mode** tab to the job-submission flow so users can make
karaoke versions of a whole album (or a typed list) in one go.
### Two ways in
- **By album** — MusicBrainz artist → album → tracklist.
Live/remix/bonus tracks and songs that already have a community version
on KaraokeNerds start **unticked** (re-tick anything). Auto-picks the
canonical edition, with an edition switcher.
- **By text** — up to 100 artist/title rows, each with a
community-version availability badge.
### Auto + review-queue model
Batch settings (auto-pick lossless / public-private / skip audio edit /
skip customization) drive auto-completion. The server runs each audio
search and **auto-selects only a confident lossless ("tier 1") match**;
everything else is parked in the existing `AWAITING_AUDIO_SELECTION`
state and surfaced as a review queue (reusing the existing
`AudioSearchDialog`). A popular full-FLAC album with all toggles on =
zero further prompts; the tab can be closed (searches run as a Cloud Run
Job).
### Credits
Gated upfront: requires `credits >= number_of_songs` (1/song minimum)
before any job is created; admins bypass. Duration deltas for long
tracks are charged per-song during processing as today.
### Backend
- `POST /api/bulk/submit`, `GET /api/bulk/{batch_id}`, `GET
/api/bulk/album/{artists,albums,tracklist}`, `POST
/api/bulk/availability`
- `bulk_search_worker` Cloud Run Job (`infrastructure/`:
`bulk-search-job`) — search → tier-1 auto-select (with duration-delta
charge) or park; per-job crash isolation; idempotent on retry
(charge_pending guard prevents double-charge)
- New `musicbrainz_service` (release lookup + extras detection); reuses
existing `karaokenerds_service` scraper (batch helper) for availability
— **no karaoke-decide change** (it has the `is_community` flag matching
"community-created"; see design doc)
- `pick_auto_selection` ports the frontend tier-1 confidence rule to the
backend (parity test)
### Frontend
- `components/job/bulk/*` (album/text modes, batch settings, submit bar,
batch progress + review queue); api client + types; 33-locale i18n
### Testing
- 40 new backend tests (foundations, MusicBrainz, lookup routes, submit
credit gate, search worker auto/park + credit math, progress) + frontend
Jest for the default-selection and credit-gate helpers
- Safe production E2E smoke
(`frontend/e2e/production/bulk-mode.spec.ts`) — read-only lookups + gate
rejection, does not spawn real jobs
- Full backend suite green (3588 passed); frontend typechecks clean (no
new errors)
### Deploy notes
- **Requires `pulumi up`** to create the `bulk-search-job` Cloud Run Job
before submit works in prod.
- Design + plan: `docs/archive/2026-06-18-bulk-mode-design.md`,
`-plan.md`
Docs: `docs/API.md` § Bulk Mode, `docs/LESSONS-LEARNED.md`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
@coderabbitai ignore
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
151 lines
5.3 KiB
Python
151 lines
5.3 KiB
Python
"""Tests for POST /api/bulk/submit — upfront credit gate + batch job creation."""
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from backend.main import app
|
|
from backend.api.dependencies import require_auth, AuthResult, UserType
|
|
from backend.exceptions import InsufficientCreditsError
|
|
|
|
|
|
def _auth(is_admin=False, email="t@example.com"):
|
|
def _dep():
|
|
return AuthResult(
|
|
is_valid=True,
|
|
user_type=UserType.ADMIN if is_admin else UserType.STRIPE,
|
|
remaining_uses=999,
|
|
message="ok",
|
|
user_email=email,
|
|
is_admin=is_admin,
|
|
)
|
|
return _dep
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _patch_deps(monkeypatch):
|
|
"""Patch the heavy collaborators used by bulk_submit."""
|
|
import backend.api.routes.bulk as bulk
|
|
|
|
# Theme + settings + cdg/txt
|
|
theme = MagicMock()
|
|
theme.get_default_theme_id.return_value = "nomad"
|
|
monkeypatch.setattr("backend.services.theme_service.get_theme_service", lambda: theme)
|
|
monkeypatch.setattr("backend.services.job_defaults_service.resolve_cdg_txt_defaults", lambda *a, **k: (False, False))
|
|
settings = MagicMock(
|
|
default_brand_prefix="NOMAD", default_enable_youtube_upload=False,
|
|
default_youtube_description="", default_discord_webhook_url=None,
|
|
default_dropbox_path=None, default_gdrive_folder_id=None,
|
|
)
|
|
monkeypatch.setattr("backend.config.get_settings", lambda: settings)
|
|
|
|
# Worker trigger
|
|
worker = MagicMock()
|
|
async def _trigger(batch_id):
|
|
return True
|
|
worker.trigger_bulk_search_worker = _trigger
|
|
monkeypatch.setattr("backend.services.worker_service.get_worker_service", lambda: worker)
|
|
|
|
yield
|
|
|
|
|
|
def _patch_user_service(monkeypatch, credits):
|
|
us = MagicMock()
|
|
us.check_credits.return_value = credits
|
|
monkeypatch.setattr("backend.services.user_service.get_user_service", lambda: us)
|
|
return us
|
|
|
|
|
|
def _patch_job_manager(monkeypatch, fail_after=None):
|
|
"""JobManager whose create_job returns sequential ids, optionally failing after N."""
|
|
created = {"count": 0}
|
|
jm = MagicMock()
|
|
|
|
def _create_job(job_create, is_admin=False):
|
|
if fail_after is not None and created["count"] >= fail_after:
|
|
raise InsufficientCreditsError(message="out", credits_available=0, credits_required=1)
|
|
created["count"] += 1
|
|
job = MagicMock()
|
|
job.job_id = f"job{created['count']}"
|
|
return job
|
|
|
|
jm.create_job.side_effect = _create_job
|
|
jm.update_job.return_value = None
|
|
monkeypatch.setattr("backend.services.job_manager.JobManager", lambda: jm)
|
|
return jm
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
c = TestClient(app)
|
|
yield c
|
|
app.dependency_overrides.clear()
|
|
|
|
|
|
def test_happy_path_creates_jobs_and_dispatches(client, monkeypatch):
|
|
app.dependency_overrides[require_auth] = _auth()
|
|
_patch_user_service(monkeypatch, credits=10)
|
|
jm = _patch_job_manager(monkeypatch)
|
|
|
|
resp = client.post("/api/bulk/submit", json={
|
|
"songs": [{"artist": "Daft Punk", "title": "One More Time"},
|
|
{"artist": "Daft Punk", "title": "Aerodynamic"}],
|
|
"settings": {"auto_select_if_lossless": True, "is_private": False},
|
|
})
|
|
assert resp.status_code == 200, resp.text
|
|
data = resp.json()
|
|
assert data["total"] == 2
|
|
assert len(data["job_ids"]) == 2
|
|
assert jm.create_job.call_count == 2
|
|
# batch_id + bulk settings stamped via update_job
|
|
assert jm.update_job.call_count == 2
|
|
_, kwargs_call = jm.update_job.call_args
|
|
stamped = jm.update_job.call_args[0][1]
|
|
assert "state_data.batch_id" in stamped
|
|
assert stamped["state_data.bulk_pending_search"] is True
|
|
|
|
|
|
def test_insufficient_credits_creates_nothing(client, monkeypatch):
|
|
app.dependency_overrides[require_auth] = _auth()
|
|
_patch_user_service(monkeypatch, credits=1)
|
|
jm = _patch_job_manager(monkeypatch)
|
|
|
|
resp = client.post("/api/bulk/submit", json={
|
|
"songs": [{"artist": "A", "title": "B"}, {"artist": "C", "title": "D"}],
|
|
})
|
|
assert resp.status_code == 402
|
|
assert jm.create_job.call_count == 0
|
|
|
|
|
|
def test_admin_bypasses_credit_gate(client, monkeypatch):
|
|
app.dependency_overrides[require_auth] = _auth(is_admin=True)
|
|
us = _patch_user_service(monkeypatch, credits=0)
|
|
jm = _patch_job_manager(monkeypatch)
|
|
|
|
resp = client.post("/api/bulk/submit", json={"songs": [{"artist": "A", "title": "B"}]})
|
|
assert resp.status_code == 200
|
|
us.check_credits.assert_not_called()
|
|
assert jm.create_job.call_count == 1
|
|
|
|
|
|
def test_rejects_over_100(client, monkeypatch):
|
|
app.dependency_overrides[require_auth] = _auth()
|
|
_patch_user_service(monkeypatch, credits=1000)
|
|
_patch_job_manager(monkeypatch)
|
|
songs = [{"artist": f"A{i}", "title": f"T{i}"} for i in range(101)]
|
|
resp = client.post("/api/bulk/submit", json={"songs": songs})
|
|
assert resp.status_code == 422
|
|
|
|
|
|
def test_partial_failure_returns_created_jobs(client, monkeypatch):
|
|
app.dependency_overrides[require_auth] = _auth()
|
|
_patch_user_service(monkeypatch, credits=3) # gate passes for 3
|
|
jm = _patch_job_manager(monkeypatch, fail_after=2) # but only 2 succeed
|
|
|
|
resp = client.post("/api/bulk/submit", json={
|
|
"songs": [{"artist": "A", "title": "1"}, {"artist": "A", "title": "2"}, {"artist": "A", "title": "3"}],
|
|
})
|
|
assert resp.status_code == 200
|
|
assert resp.json()["total"] == 2
|