mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-30 17:21:26 +09:00
@coderabbitai ignore ## Summary - Route `render_video_worker` (OutputGenerator/ffmpeg lyrics rendering) to the GCE encoding worker instead of running on Cloud Run - Fixes OOM crashes after Cloud Run memory was right-sized to 2Gi - Adds style asset disk cache on GCE worker to avoid repeated downloads of the same default theme - Preserves local fallback when GCE is disabled ## Changes ### GCE Worker (`backend/services/gce_encoding/main.py`) - New `/render-video` endpoint with `RenderVideoRequest` model - `run_render_video()` — downloads corrections/audio/styles from GCS, runs OutputGenerator, uploads outputs - `download_with_cache()` — SHA-256 keyed disk cache for style assets (fonts, backgrounds) - `metadata` field added to `JobStatus` model for countdown padding info ### EncodingService (`backend/services/encoding_service.py`) - `submit_render_video_job()` — POST to `/render-video` with retry logic - `render_video_on_gce()` — submit + wait convenience method ### render_video_worker (`backend/workers/render_video_worker.py`) - Conditional GCE routing when `encoding_service.is_enabled` - Cloud Run does zero downloads in GCE path — constructs GCS paths from Firestore metadata - Existing local path preserved unchanged as fallback ## Test plan - [ ] 18 new unit tests covering cache, endpoint, service methods, and routing logic (all pass) - [ ] Full test suite: 1164 passed, 0 failures - [ ] CodeRabbit review: 1 bug caught and fixed (missing metadata in cached response) - [ ] E2E happy path CI validates full pipeline after merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
81 lines
5.0 KiB
Python
81 lines
5.0 KiB
Python
"""Tests for EncodingService render_video methods."""
|
|
import asyncio
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from backend.services.encoding_service import EncodingService
|
|
|
|
|
|
@pytest.fixture
|
|
def encoding_service():
|
|
"""Create an EncodingService with mocked credentials."""
|
|
with patch.object(EncodingService, '_load_credentials') as mock_load:
|
|
service = EncodingService()
|
|
service._url = "http://test-worker:8080"
|
|
service._api_key = "test-key"
|
|
service._initialized = True
|
|
yield service
|
|
|
|
|
|
class TestSubmitRenderVideoJob:
|
|
def test_posts_to_render_video_endpoint(self, encoding_service):
|
|
render_config = {
|
|
"job_id": "test-123",
|
|
"original_corrections_gcs_path": "gs://bucket/corrections.json",
|
|
"audio_gcs_path": "gs://bucket/audio.flac",
|
|
"output_gcs_prefix": "gs://bucket/jobs/test-123/",
|
|
"artist": "Test",
|
|
"title": "Song",
|
|
}
|
|
mock_response = {"status": 200, "json": {"status": "accepted", "job_id": "test-123"}, "text": None}
|
|
with patch.object(encoding_service, '_request_with_retry', new_callable=AsyncMock, return_value=mock_response):
|
|
result = asyncio.get_event_loop().run_until_complete(
|
|
encoding_service.submit_render_video_job("test-123", render_config)
|
|
)
|
|
assert result["status"] == "accepted"
|
|
|
|
def test_raises_on_401(self, encoding_service):
|
|
render_config = {"job_id": "test-123", "original_corrections_gcs_path": "gs://bucket/corrections.json", "audio_gcs_path": "gs://bucket/audio.flac", "output_gcs_prefix": "gs://bucket/jobs/test-123/", "artist": "Test", "title": "Song"}
|
|
mock_response = {"status": 401, "json": None, "text": "Unauthorized"}
|
|
with patch.object(encoding_service, '_request_with_retry', new_callable=AsyncMock, return_value=mock_response):
|
|
with pytest.raises(RuntimeError, match="Invalid API key"):
|
|
asyncio.get_event_loop().run_until_complete(
|
|
encoding_service.submit_render_video_job("test-123", render_config)
|
|
)
|
|
|
|
def test_handles_cached_result(self, encoding_service):
|
|
render_config = {"job_id": "test-123", "original_corrections_gcs_path": "gs://bucket/corrections.json", "audio_gcs_path": "gs://bucket/audio.flac", "output_gcs_prefix": "gs://bucket/jobs/test-123/", "artist": "Test", "title": "Song"}
|
|
submit_response = {"status": 409, "json": None, "text": "Conflict"}
|
|
status_response = {"status": "complete", "output_files": ["jobs/test-123/videos/with_vocals.mkv"], "metadata": {"countdown_padding_added": False}}
|
|
with patch.object(encoding_service, '_request_with_retry', new_callable=AsyncMock, return_value=submit_response):
|
|
with patch.object(encoding_service, 'get_job_status', new_callable=AsyncMock, return_value=status_response):
|
|
result = asyncio.get_event_loop().run_until_complete(
|
|
encoding_service.submit_render_video_job("test-123", render_config)
|
|
)
|
|
assert result["status"] == "cached"
|
|
|
|
|
|
class TestRenderVideoOnGce:
|
|
def test_submits_and_waits(self, encoding_service):
|
|
render_config = {"job_id": "test-123", "original_corrections_gcs_path": "gs://bucket/corrections.json", "audio_gcs_path": "gs://bucket/audio.flac", "output_gcs_prefix": "gs://bucket/jobs/test-123/", "artist": "Test", "title": "Song"}
|
|
submit_result = {"status": "accepted", "job_id": "test-123"}
|
|
wait_result = {"status": "complete", "output_files": ["jobs/test-123/videos/with_vocals.mkv"], "metadata": {"countdown_padding_added": False}}
|
|
with patch.object(encoding_service, 'submit_render_video_job', new_callable=AsyncMock, return_value=submit_result):
|
|
with patch.object(encoding_service, 'wait_for_completion', new_callable=AsyncMock, return_value=wait_result):
|
|
result = asyncio.get_event_loop().run_until_complete(
|
|
encoding_service.render_video_on_gce("test-123", render_config)
|
|
)
|
|
assert result["status"] == "complete"
|
|
|
|
def test_returns_immediately_on_cached(self, encoding_service):
|
|
render_config = {"job_id": "test-123", "original_corrections_gcs_path": "gs://bucket/corrections.json", "audio_gcs_path": "gs://bucket/audio.flac", "output_gcs_prefix": "gs://bucket/jobs/test-123/", "artist": "Test", "title": "Song"}
|
|
submit_result = {"status": "cached", "job_id": "test-123", "output_files": ["jobs/test-123/videos/with_vocals.mkv"], "metadata": {"countdown_padding_added": False}}
|
|
with patch.object(encoding_service, 'submit_render_video_job', new_callable=AsyncMock, return_value=submit_result):
|
|
with patch.object(encoding_service, 'wait_for_completion', new_callable=AsyncMock) as mock_wait:
|
|
result = asyncio.get_event_loop().run_until_complete(
|
|
encoding_service.render_video_on_gce("test-123", render_config)
|
|
)
|
|
mock_wait.assert_not_called()
|
|
assert result["status"] == "complete"
|