Files
karaoke-gen/backend/tests/test_youtube_queue_processor.py
Andrew Beveridge ☄️andClaude Opus 5.5 d4fcab521c Translated lyrics follow-ups + review Waveforms 'separating' spinner (v0.261.0) (#1103)
## Summary
Follow-ups from Andrew's test of translated lyrics (job 948b6760), plus
a review-page UX gap found while investigating that job.

- **Translation row is now secondary:** it renders at 70% of the unsung
colour's opacity, with outline and shadow faded to match. A theme's
`karaoke.translation_color` still wins.
(`subtitles.py:TRANSLATION_OPACITY`)
- **YouTube label:** published videos with translations get "(With
Translation into X)" in the title, before "(Karaoke)". Only the
artist/title get truncated, never the label. The description gets an
up-front notice. This covers the live upload and the quota queue, and
applies only when `state_data.lyrics_translation.status ==
"translated"`. Turning translation off and re-rendering clears the
outcome.
- **Review preview modal:** an always-visible note explains the preview
is a quick black-background sync check, not the final look. Jobs with a
translation language also get a note that translations are added after
review.
- **Waveforms toggle:** shows a spinner and tooltip while audio
separation is still producing the vocal stem (peaks endpoint returns
202). Previously the rows just stayed empty.

## Testing
- Jest: lyrics-review suite (245+) incl. new
TranscriptionView/Preview/loader tests
- pytest: youtube description/queue/orchestrator, lyrics_translation,
translated-lyrics unit tests
- Linux libass render tests (Debian + prod static ffmpeg) pass
- Independent review agent: 2 findings fixed (Tooltip data-state
clobbering toggle state; stale translation outcome)

No local CodeRabbit run (CLI has no seat on this org), so leaving this
open to the CodeRabbit bot.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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

## Summary by CodeRabbit

* **New Features**
* YouTube titles and descriptions now identify the language of
translated lyrics. Titles are kept within the platform’s length limit.
  * Translated lyrics appear with softer opacity by default.
* The lyric-review screen explains how the quick preview differs from
the final video, including that translated lyrics are added after
review.
* A spinner and notice indicate when vocal waveforms are still being
prepared.

* **Bug Fixes**
* Translation status now reflects when translation is turned off,
preventing stale translated-language labels.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 16:39:56 -04:00

324 lines
16 KiB
Python

"""
Unit tests for YouTube queue processor.
Tests the deferred upload processing logic including quota checks,
queue claiming, video downloading, and error handling.
"""
import pytest
from unittest.mock import Mock, MagicMock, AsyncMock, patch
# Mock Google Cloud before imports
import sys
sys.modules.setdefault('google.cloud.firestore', MagicMock())
sys.modules.setdefault('google.cloud.storage', MagicMock())
@pytest.mark.asyncio
class TestProcessYouTubeUploadQueue:
"""Test the main queue processing function."""
@pytest.fixture
def mock_quota_service(self):
service = Mock()
service.check_quota_available.return_value = (True, 9000, "9000 units remaining")
service.record_upload = Mock()
return service
@pytest.fixture
def mock_queue_service(self):
service = Mock()
service.get_queued_uploads.return_value = []
service.mark_processing.return_value = True
service.mark_completed = Mock()
service.mark_failed = Mock()
return service
async def test_skips_when_no_quota(self, mock_quota_service, mock_queue_service):
"""Should skip processing when no quota available."""
mock_quota_service.check_quota_available.return_value = (False, 0, "Quota exhausted")
with patch('backend.workers.youtube_queue_processor.get_youtube_quota_service', return_value=mock_quota_service), \
patch('backend.workers.youtube_queue_processor.get_youtube_upload_queue_service', return_value=mock_queue_service), \
patch('backend.workers.youtube_queue_processor.get_settings'):
from backend.workers.youtube_queue_processor import process_youtube_upload_queue
result = await process_youtube_upload_queue()
assert result["status"] == "skipped"
assert result["reason"] == "no_quota"
assert result["processed"] == 0
async def test_returns_empty_when_no_queued(self, mock_quota_service, mock_queue_service):
"""Should return empty status when no uploads queued."""
mock_queue_service.get_queued_uploads.return_value = []
with patch('backend.workers.youtube_queue_processor.get_youtube_quota_service', return_value=mock_quota_service), \
patch('backend.workers.youtube_queue_processor.get_youtube_upload_queue_service', return_value=mock_queue_service), \
patch('backend.workers.youtube_queue_processor.get_settings'):
from backend.workers.youtube_queue_processor import process_youtube_upload_queue
result = await process_youtube_upload_queue()
assert result["status"] == "empty"
assert result["processed"] == 0
assert result["remaining"] == 0
async def test_processes_queued_upload(self, mock_quota_service, mock_queue_service):
"""Should process a single queued upload successfully."""
mock_queue_service.get_queued_uploads.side_effect = [
[{"job_id": "job-123", "user_email": "user@test.com", "artist": "Artist", "title": "Title", "brand_code": "NOMAD-100"}],
[], # After processing, no more queued
]
with patch('backend.workers.youtube_queue_processor.get_youtube_quota_service', return_value=mock_quota_service), \
patch('backend.workers.youtube_queue_processor.get_youtube_upload_queue_service', return_value=mock_queue_service), \
patch('backend.workers.youtube_queue_processor.get_settings'), \
patch('backend.workers.youtube_queue_processor._process_single_upload', new_callable=AsyncMock, return_value="https://youtube.com/watch?v=abc") as mock_upload, \
patch('backend.workers.youtube_queue_processor._update_job_youtube_url'), \
patch('backend.workers.youtube_queue_processor._send_youtube_upload_notification', new_callable=AsyncMock):
from backend.workers.youtube_queue_processor import process_youtube_upload_queue
result = await process_youtube_upload_queue()
assert result["status"] == "processed"
assert result["processed"] == 1
assert result["failed"] == 0
mock_queue_service.mark_completed.assert_called_once_with("job-123", "https://youtube.com/watch?v=abc")
async def test_handles_upload_failure(self, mock_quota_service, mock_queue_service):
"""Should handle upload failure and mark as failed."""
mock_queue_service.get_queued_uploads.side_effect = [
[{"job_id": "job-fail", "user_email": "u@t.com", "artist": "A", "title": "T", "brand_code": None}],
[],
]
with patch('backend.workers.youtube_queue_processor.get_youtube_quota_service', return_value=mock_quota_service), \
patch('backend.workers.youtube_queue_processor.get_youtube_upload_queue_service', return_value=mock_queue_service), \
patch('backend.workers.youtube_queue_processor.get_settings'), \
patch('backend.workers.youtube_queue_processor._process_single_upload', new_callable=AsyncMock, side_effect=Exception("Upload failed")):
from backend.workers.youtube_queue_processor import process_youtube_upload_queue
result = await process_youtube_upload_queue()
assert result["failed"] == 1
assert result["processed"] == 0
mock_queue_service.mark_failed.assert_called_once()
async def test_stops_on_quota_exceeded(self, mock_quota_service, mock_queue_service):
"""Should stop processing when quota exceeded error occurs."""
mock_queue_service.get_queued_uploads.side_effect = [
[
{"job_id": "job-1", "user_email": "u@t.com", "artist": "A", "title": "T", "brand_code": None},
{"job_id": "job-2", "user_email": "u@t.com", "artist": "A", "title": "T", "brand_code": None},
],
[],
]
with patch('backend.workers.youtube_queue_processor.get_youtube_quota_service', return_value=mock_quota_service), \
patch('backend.workers.youtube_queue_processor.get_youtube_upload_queue_service', return_value=mock_queue_service), \
patch('backend.workers.youtube_queue_processor.get_settings'), \
patch('backend.workers.youtube_queue_processor._process_single_upload', new_callable=AsyncMock, side_effect=Exception("quotaExceeded: daily limit reached")):
from backend.workers.youtube_queue_processor import process_youtube_upload_queue
result = await process_youtube_upload_queue()
# Should stop after first quota exceeded error, not process job-2
assert result["failed"] == 1
assert mock_queue_service.mark_failed.call_count == 1
async def test_skips_unclaimed_entry(self, mock_quota_service, mock_queue_service):
"""Should skip entries that can't be claimed (already processing)."""
mock_queue_service.get_queued_uploads.side_effect = [
[{"job_id": "job-claimed", "user_email": "u@t.com", "artist": "A", "title": "T", "brand_code": None}],
[],
]
mock_queue_service.mark_processing.return_value = False
with patch('backend.workers.youtube_queue_processor.get_youtube_quota_service', return_value=mock_quota_service), \
patch('backend.workers.youtube_queue_processor.get_youtube_upload_queue_service', return_value=mock_queue_service), \
patch('backend.workers.youtube_queue_processor.get_settings'):
from backend.workers.youtube_queue_processor import process_youtube_upload_queue
result = await process_youtube_upload_queue()
assert result["processed"] == 0
assert result["failed"] == 0
async def test_rechecks_quota_before_each_upload(self, mock_quota_service, mock_queue_service):
"""Should re-check quota before each upload, not just at the start."""
mock_queue_service.get_queued_uploads.side_effect = [
[
{"job_id": "job-1", "user_email": "u@t.com", "artist": "A", "title": "T", "brand_code": None},
{"job_id": "job-2", "user_email": "u@t.com", "artist": "A", "title": "T", "brand_code": None},
],
[],
]
# Allow first check, deny second
mock_quota_service.check_quota_available.side_effect = [
(True, 9000, "ok"), # Initial check
(True, 500, "ok"), # Before job-1
(False, 0, "exhausted"), # Before job-2
]
with patch('backend.workers.youtube_queue_processor.get_youtube_quota_service', return_value=mock_quota_service), \
patch('backend.workers.youtube_queue_processor.get_youtube_upload_queue_service', return_value=mock_queue_service), \
patch('backend.workers.youtube_queue_processor.get_settings'), \
patch('backend.workers.youtube_queue_processor._process_single_upload', new_callable=AsyncMock, return_value="https://youtube.com/watch?v=x"), \
patch('backend.workers.youtube_queue_processor._update_job_youtube_url'), \
patch('backend.workers.youtube_queue_processor._send_youtube_upload_notification', new_callable=AsyncMock):
from backend.workers.youtube_queue_processor import process_youtube_upload_queue
result = await process_youtube_upload_queue()
# Only job-1 should have been processed
assert result["processed"] == 1
class TestDownloadVideoFromGCS:
"""Test video file download logic."""
def test_prioritizes_mkv(self):
"""Should prefer MKV over MP4."""
from backend.workers.youtube_queue_processor import _download_video_from_gcs
mock_job = Mock()
mock_job.file_urls = {
"finals": {
"lossless_4k_mkv": "jobs/job-123/finals/lossless_4k_mkv.mkv",
"lossless_4k_mp4": "jobs/job-123/finals/lossless_4k_mp4.mp4",
}
}
mock_storage = Mock()
mock_storage.download_file = Mock()
with patch('os.path.isfile', return_value=True), \
patch('os.path.getsize', return_value=1024):
result = _download_video_from_gcs("job-123", mock_job, mock_storage, "/tmp/test")
assert result is not None
assert result.endswith(".mkv")
mock_storage.download_file.assert_called_once_with("jobs/job-123/finals/lossless_4k_mkv.mkv", "/tmp/test/video.mkv")
def test_falls_back_to_mp4(self):
"""Should fall back to MP4 when MKV not available."""
from backend.workers.youtube_queue_processor import _download_video_from_gcs
mock_job = Mock()
mock_job.file_urls = {
"finals": {
"lossless_4k_mp4": "jobs/job-123/finals/lossless_4k_mp4.mp4",
}
}
mock_storage = Mock()
mock_storage.download_file = Mock()
with patch('os.path.isfile', return_value=True), \
patch('os.path.getsize', return_value=1024):
result = _download_video_from_gcs("job-123", mock_job, mock_storage, "/tmp/test")
assert result is not None
assert result.endswith(".mp4")
def test_returns_none_when_no_files(self):
"""Should return None when no video files available."""
from backend.workers.youtube_queue_processor import _download_video_from_gcs
mock_job = Mock()
mock_job.file_urls = {}
mock_storage = Mock()
result = _download_video_from_gcs("job-123", mock_job, mock_storage, "/tmp/test")
assert result is None
def test_returns_none_on_download_failure(self):
"""Should return None when download fails."""
from backend.workers.youtube_queue_processor import _download_video_from_gcs
mock_job = Mock()
mock_job.file_urls = {"finals": {"lossless_4k_mkv": "jobs/job-123/finals/lossless_4k_mkv.mkv"}}
mock_storage = Mock()
mock_storage.download_file.side_effect = Exception("Download failed")
result = _download_video_from_gcs("job-123", mock_job, mock_storage, "/tmp/test")
assert result is None
class TestUpdateJobYouTubeUrl:
"""Test updating job state_data with YouTube URL."""
def test_updates_state_data(self):
"""Should set youtube_url and clear queued flag via atomic dot-path writes."""
from backend.workers.youtube_queue_processor import _update_job_youtube_url
mock_job = Mock()
mock_job.state_data = {"brand_code": "NOMAD-100"}
mock_job_manager = Mock()
mock_job_manager.get_job.return_value = mock_job
with patch('backend.workers.youtube_queue_processor.JobManager', return_value=mock_job_manager):
_update_job_youtube_url("job-123", "https://youtube.com/watch?v=abc")
update_call = mock_job_manager.update_job.call_args[0]
assert update_call[0] == "job-123"
payload = update_call[1]
assert payload["state_data.youtube_url"] == "https://youtube.com/watch?v=abc"
assert payload["state_data.youtube_upload_queued"] is False
# Atomic field writes only — must NOT re-persist the whole state_data map
# (which would clobber a concurrently-changed sibling like brand_code).
assert "state_data" not in payload
def test_missing_job_raises(self):
"""A missing job must surface as a failure, so the processor flags the
queue entry needs_attention instead of silently losing the URL."""
from backend.workers.youtube_queue_processor import _update_job_youtube_url
mock_job_manager = Mock()
mock_job_manager.get_job.return_value = None
with patch('backend.workers.youtube_queue_processor.JobManager', return_value=mock_job_manager):
with pytest.raises(RuntimeError, match="not found"):
_update_job_youtube_url("nonexistent", "https://youtube.com/watch?v=abc")
mock_job_manager.update_job.assert_not_called()
def test_update_failure_propagates(self):
from backend.workers.youtube_queue_processor import _update_job_youtube_url
mock_job_manager = Mock()
mock_job_manager.get_job.return_value = Mock(state_data={})
mock_job_manager.update_job.side_effect = RuntimeError("firestore down")
with patch('backend.workers.youtube_queue_processor.JobManager', return_value=mock_job_manager):
with pytest.raises(RuntimeError, match="firestore down"):
_update_job_youtube_url("job-123", "https://youtube.com/watch?v=abc")
class TestProcessSingleUploadTranslationLabel:
"""Queued uploads label translated videos exactly like the live upload path."""
def test_translated_job_title_and_description_labelled(self, tmp_path):
import asyncio
from types import SimpleNamespace
from backend.workers.youtube_queue_processor import _process_single_upload
job = SimpleNamespace(
job_id="job-tr", artist="Manel", title="Benvolgut",
state_data={"lyrics_translation": {"language": "en", "status": "translated"}},
)
youtube = MagicMock()
youtube.upload_video.return_value = ("vid", "https://youtube.com/watch?v=vid")
settings = SimpleNamespace(default_youtube_description="Body")
with patch('backend.workers.youtube_queue_processor.JobManager') as jm, \
patch('backend.workers.youtube_queue_processor.StorageService'), \
patch('backend.workers.youtube_queue_processor._download_video_from_gcs', return_value=str(tmp_path / "v.mkv")), \
patch('backend.workers.youtube_queue_processor._download_thumbnail_from_gcs', return_value=None), \
patch('backend.workers.youtube_queue_processor._create_youtube_service', return_value=youtube):
jm.return_value.get_job.return_value = job
url = asyncio.run(_process_single_upload("job-tr", {}, MagicMock(), settings))
assert url == "https://youtube.com/watch?v=vid"
kwargs = youtube.upload_video.call_args.kwargs
assert kwargs["title"] == "Manel - Benvolgut (With Translation into English) (Karaoke)"
assert kwargs["description"].startswith("This karaoke video includes translated lyrics (English)")