mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
## Summary Two invisible upload failures on the same song led to this: - **Job 8729f19a**: large audio upload (signed-URL flow) abandoned mid-upload. The only UI signal was the "Creating..." button text and the card already said "Setting up", so the job sat `pending` forever with the credit spent. - **Job 2d0c1ae0**: 44 MB instrumental upload on the review screen was a multipart POST through Cloud Run (32 MiB cap) and failed silently. Only the CORS preflight was logged. ## Changes **Shared upload layer (frontend)**: all user uploads now go through one path: - `lib/upload.ts`: signed-URL PUT with progress, sequential multi-file upload with aggregate progress, client-side duration check. - `hooks/useUploadTask.ts`: progress state plus a `beforeunload` guard. - `components/upload/UploadProgressModal.tsx`: a modal the user can't close, showing progress, speed, ETA, "file N of M" and a keep-tab-open warning. - Used by guided job creation, tenant single job and instrumental review. Tenant bulk keeps its per-row table and uses the shared guard. **Guided flow** - Always uses the signed-URL path, which also fixes `requires_audio_edit` being silently dropped for uploads of 25MB or more. - Private upload jobs can attach their own instrumental (`existing_instrumental`). This skips separation and instrumental review. The duration is checked before upload, and the backing-vocals option is hidden. **Instrumental review** - New `instrumental-upload-url` and `instrumental-upload-complete` endpoints: direct-to-GCS upload, then a finalize step. - One shared processing helper with the multipart route: duration check, FLAC conversion, `stems.custom_instrumental`. - Returns a signed `audio_url`. The old post-upload preview URL had no backend route. - Inline error plus toast for mismatches and failures. **Backend robustness** - `state_data.awaiting_upload`: the dashboard shows "Waiting for upload" instead of "Setting up". - The hourly stale-review run also cancels and refunds non-tenant jobs still awaiting upload after 2h with nothing uploaded. - `uploads-complete` now checks ownership (previously any signed-in user could finalize any pending job). On an instrumental duration mismatch it cancels and refunds the job instead of stranding it. ## Testing - `make test`: backend 1839 + 4788 + 150 emulator passed; frontend Jest 1391 passed across 117 suites. - New tests: - backend: stale-upload processor; signed instrumental endpoints (15 cases); `requires_audio_edit` persistence; uploads-complete ownership and cancel-on-mismatch - frontend: upload lib, `useUploadTask`, modal (speed/ETA, can't be dismissed), `OwnInstrumentalField`, `TenantJobFlow`, API flows - Manual check against the real frontend (local dev, prod read-only; create, upload and finalize endpoints mocked, uploads sent to a throttled local PUT server, so no prod data was changed): - guided flow with and without an instrumental - instrumental review upload on 2d0c1ae0, including a mismatch caught before upload - Escape and outside clicks don't close the modal; navigating away triggers the `beforeunload` prompt Plan: `docs/archive/2026-09-26-unified-upload-flow-plan.md` 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
571 lines
24 KiB
Python
571 lines
24 KiB
Python
"""
|
|
Tests for audio_transcoding_service.py - Review audio transcoding to OGG Opus.
|
|
|
|
These tests mock StorageService and subprocess to verify:
|
|
- Cache path derivation for various GCS paths
|
|
- Cache hit path (no ffmpeg call)
|
|
- Cache miss with successful transcode
|
|
- Fallback to FLAC on transcoding failure
|
|
- Bulk prepare_review_audio_for_job transcodes all expected files
|
|
"""
|
|
|
|
import pytest
|
|
from unittest.mock import Mock, patch, call, ANY
|
|
|
|
|
|
class TestGetCachePath:
|
|
"""Test _get_cache_path() derivation logic."""
|
|
|
|
def _make_service(self):
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
return AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
def test_input_audio_path(self):
|
|
service = self._make_service()
|
|
result = service._get_cache_path("jobs/abc123/input/song.flac")
|
|
assert result == "jobs/abc123/review-audio/song.ogg"
|
|
|
|
def test_stem_path(self):
|
|
service = self._make_service()
|
|
result = service._get_cache_path("jobs/abc123/stems/instrumental_clean.flac")
|
|
assert result == "jobs/abc123/review-audio/instrumental_clean.ogg"
|
|
|
|
def test_backing_vocals_path(self):
|
|
service = self._make_service()
|
|
result = service._get_cache_path("jobs/abc123/stems/backing_vocals.flac")
|
|
assert result == "jobs/abc123/review-audio/backing_vocals.ogg"
|
|
|
|
def test_nested_job_path(self):
|
|
service = self._make_service()
|
|
result = service._get_cache_path("jobs/4bed6f80/stems/instrumental_with_backing.flac")
|
|
assert result == "jobs/4bed6f80/review-audio/instrumental_with_backing.ogg"
|
|
|
|
def test_wav_extension(self):
|
|
service = self._make_service()
|
|
result = service._get_cache_path("jobs/abc123/input/song.wav")
|
|
assert result == "jobs/abc123/review-audio/song.ogg"
|
|
|
|
def test_no_jobs_prefix_fallback_uses_hash(self):
|
|
service = self._make_service()
|
|
result = service._get_cache_path("other/path/audio.flac")
|
|
# Should use a hash-based fallback, not a literal "_unknown"
|
|
assert result.startswith("jobs/_")
|
|
assert result.endswith("/review-audio/audio.ogg")
|
|
assert len(result.split("/")[1]) == 13 # "_" + 12-char hash
|
|
|
|
def test_no_jobs_prefix_different_paths_different_hashes(self):
|
|
service = self._make_service()
|
|
result1 = service._get_cache_path("other/path/audio.flac")
|
|
result2 = service._get_cache_path("another/path/audio.flac")
|
|
# Different source paths should produce different cache paths
|
|
assert result1 != result2
|
|
|
|
|
|
class TestTranscodeIfNeeded:
|
|
"""Test transcode_if_needed() cache hit/miss logic."""
|
|
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
def test_cache_hit_skips_transcode(self, mock_subprocess):
|
|
"""When cached file exists, return it without transcoding."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = True
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
result = service.transcode_if_needed("jobs/abc/input/song.flac")
|
|
|
|
assert result == "jobs/abc/review-audio/song.ogg"
|
|
mock_storage.file_exists.assert_called_once_with("jobs/abc/review-audio/song.ogg")
|
|
mock_subprocess.run.assert_not_called()
|
|
mock_storage.download_file.assert_not_called()
|
|
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
def test_cache_miss_transcodes_and_uploads(self, mock_subprocess):
|
|
"""When cached file is missing, download + transcode + upload."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = False
|
|
mock_subprocess.run.return_value = Mock(returncode=0, stderr="")
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
result = service.transcode_if_needed("jobs/abc/input/song.flac")
|
|
|
|
assert result == "jobs/abc/review-audio/song.ogg"
|
|
|
|
# Verify download from correct source path
|
|
download_args = mock_storage.download_file.call_args[0]
|
|
assert download_args[0] == "jobs/abc/input/song.flac"
|
|
|
|
# Verify ffmpeg command
|
|
mock_subprocess.run.assert_called_once()
|
|
ffmpeg_call = mock_subprocess.run.call_args
|
|
ffmpeg_cmd = ffmpeg_call[0][0]
|
|
assert "ffmpeg" in ffmpeg_cmd[0]
|
|
assert "libopus" in ffmpeg_cmd
|
|
assert "128k" in ffmpeg_cmd
|
|
assert "-vn" in ffmpeg_cmd
|
|
# Verify timeout is set
|
|
assert ffmpeg_call[1]["timeout"] == 120
|
|
|
|
# Verify upload to correct cache path
|
|
upload_args = mock_storage.upload_file.call_args[0]
|
|
assert upload_args[1] == "jobs/abc/review-audio/song.ogg"
|
|
|
|
|
|
class TestGetReviewAudioUrl:
|
|
"""Test get_review_audio_url() with fallback behavior."""
|
|
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
def test_returns_transcoded_url_on_success(self, mock_subprocess):
|
|
"""On success, return signed URL for transcoded file."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = True
|
|
mock_storage.generate_signed_url.return_value = "https://signed-url/song.ogg"
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
result = service.get_review_audio_url("jobs/abc/input/song.flac")
|
|
|
|
assert result == "https://signed-url/song.ogg"
|
|
mock_storage.generate_signed_url.assert_called_once_with(
|
|
"jobs/abc/review-audio/song.ogg", 120
|
|
)
|
|
|
|
def test_signed_url_timeout_short_circuits_fallbacks(self):
|
|
"""A signing stall must NOT trigger the FLAC/cache fallbacks (they'd stall too).
|
|
|
|
On a cache hit the first sign is the only sign attempted; a SignedUrlTimeout
|
|
propagates immediately so the caller degrades fast.
|
|
"""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
from backend.services.storage_service import SignedUrlTimeout
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = True # cache hit
|
|
mock_storage.generate_signed_url.side_effect = SignedUrlTimeout("stalled")
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
with pytest.raises(SignedUrlTimeout):
|
|
service.get_review_audio_url("jobs/abc/input/song.flac")
|
|
|
|
# Exactly one sign attempt — no fallback re-signing.
|
|
assert mock_storage.generate_signed_url.call_count == 1
|
|
|
|
def test_falls_back_to_flac_on_error(self):
|
|
"""On transcoding error with no cache, fall back to original FLAC signed URL."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = False
|
|
mock_storage.download_file.side_effect = Exception("GCS download failed")
|
|
mock_storage.generate_signed_url.return_value = "https://signed-url/song.flac"
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
result = service.get_review_audio_url("jobs/abc/input/song.flac")
|
|
|
|
assert result == "https://signed-url/song.flac"
|
|
mock_storage.generate_signed_url.assert_called_once_with(
|
|
"jobs/abc/input/song.flac", 120
|
|
)
|
|
# file_exists called twice: once in transcode_if_needed (cache miss),
|
|
# once in fallback (cache still missing → fall through to FLAC)
|
|
assert mock_storage.file_exists.call_count == 2
|
|
|
|
def test_serves_cache_when_source_deleted(self):
|
|
"""When source is deleted but OGG cache exists, serve the cache."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
# First file_exists (in transcode_if_needed): cache miss
|
|
# download_file fails: source deleted
|
|
# Second file_exists (in fallback): cache found
|
|
mock_storage.file_exists.side_effect = [False, True]
|
|
mock_storage.download_file.side_effect = Exception("blob not found")
|
|
mock_storage.generate_signed_url.return_value = "https://signed-url/song.ogg"
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
result = service.get_review_audio_url("jobs/abc/input/song.flac")
|
|
|
|
assert result == "https://signed-url/song.ogg"
|
|
mock_storage.generate_signed_url.assert_called_once_with(
|
|
"jobs/abc/review-audio/song.ogg", 120
|
|
)
|
|
|
|
def test_falls_back_to_flac_when_cache_check_also_fails(self):
|
|
"""When source is deleted and file_exists() also throws, fall back to FLAC."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
# First file_exists (in transcode_if_needed): cache miss
|
|
# download_file fails: source deleted
|
|
# Second file_exists (in fallback): also throws
|
|
mock_storage.file_exists.side_effect = [False, Exception("GCS unavailable")]
|
|
mock_storage.download_file.side_effect = Exception("blob not found")
|
|
mock_storage.generate_signed_url.return_value = "https://signed-url/song.flac"
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
result = service.get_review_audio_url("jobs/abc/input/song.flac")
|
|
|
|
assert result == "https://signed-url/song.flac"
|
|
mock_storage.generate_signed_url.assert_called_once_with(
|
|
"jobs/abc/input/song.flac", 120
|
|
)
|
|
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
def test_custom_expiration_minutes(self, mock_subprocess):
|
|
"""Custom expiration_minutes is passed through to generate_signed_url."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = True
|
|
mock_storage.generate_signed_url.return_value = "https://signed-url/song.ogg"
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
service.get_review_audio_url("jobs/abc/input/song.flac", expiration_minutes=60)
|
|
|
|
mock_storage.generate_signed_url.assert_called_once_with(
|
|
"jobs/abc/review-audio/song.ogg", 60
|
|
)
|
|
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
def test_falls_back_on_ffmpeg_failure(self, mock_subprocess):
|
|
"""On ffmpeg failure, fall back to original FLAC signed URL."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = False
|
|
mock_subprocess.run.return_value = Mock(returncode=1, stderr="codec error")
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
result = service.get_review_audio_url("jobs/abc/input/song.flac")
|
|
|
|
# Should fall back to FLAC URL
|
|
mock_storage.generate_signed_url.assert_called_once_with(
|
|
"jobs/abc/input/song.flac", 120
|
|
)
|
|
|
|
|
|
class TestPrepareReviewAudioForJob:
|
|
"""Test prepare_review_audio_for_job() bulk transcoding."""
|
|
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
def test_transcodes_all_expected_files(self, mock_subprocess):
|
|
"""Should transcode input + all stems and return correct cache paths."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = True # All cache hits
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
mock_job = Mock()
|
|
mock_job.job_id = "abc123"
|
|
mock_job.input_media_gcs_path = "jobs/abc123/input/song.flac"
|
|
mock_job.file_urls = {
|
|
"stems": {
|
|
"instrumental_clean": "jobs/abc123/stems/instrumental_clean.flac",
|
|
"instrumental_with_backing": "jobs/abc123/stems/instrumental_with_backing.flac",
|
|
"backing_vocals": "jobs/abc123/stems/backing_vocals.flac",
|
|
}
|
|
}
|
|
|
|
result = service.prepare_review_audio_for_job(mock_job)
|
|
|
|
assert len(result) == 4
|
|
assert "jobs/abc123/review-audio/song.ogg" in result
|
|
assert "jobs/abc123/review-audio/instrumental_clean.ogg" in result
|
|
assert "jobs/abc123/review-audio/instrumental_with_backing.ogg" in result
|
|
assert "jobs/abc123/review-audio/backing_vocals.ogg" in result
|
|
assert mock_storage.file_exists.call_count == 4
|
|
# No ffmpeg calls since all are cache hits
|
|
mock_subprocess.run.assert_not_called()
|
|
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
def test_continues_on_individual_failure(self, mock_subprocess):
|
|
"""If one file fails to transcode, others should still succeed."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
# First call (input): cache miss + download fails
|
|
# Subsequent calls: cache hits
|
|
mock_storage.file_exists.side_effect = [False, True, True, True]
|
|
mock_storage.download_file.side_effect = Exception("download failed")
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
mock_job = Mock()
|
|
mock_job.job_id = "abc123"
|
|
mock_job.input_media_gcs_path = "jobs/abc123/input/song.flac"
|
|
mock_job.file_urls = {
|
|
"stems": {
|
|
"instrumental_clean": "jobs/abc123/stems/instrumental_clean.flac",
|
|
"instrumental_with_backing": "jobs/abc123/stems/instrumental_with_backing.flac",
|
|
"backing_vocals": "jobs/abc123/stems/backing_vocals.flac",
|
|
}
|
|
}
|
|
|
|
result = service.prepare_review_audio_for_job(mock_job)
|
|
|
|
# 3 out of 4 succeeded (the 3 cache hits)
|
|
assert len(result) == 3
|
|
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
def test_handles_missing_stems(self, mock_subprocess):
|
|
"""Should handle jobs with missing stem paths gracefully."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = True
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
mock_job = Mock()
|
|
mock_job.job_id = "abc123"
|
|
mock_job.input_media_gcs_path = "jobs/abc123/input/song.flac"
|
|
mock_job.file_urls = {"stems": {}} # No stems
|
|
|
|
result = service.prepare_review_audio_for_job(mock_job)
|
|
|
|
# Only the input audio
|
|
assert len(result) == 1
|
|
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
def test_handles_no_input_audio(self, mock_subprocess):
|
|
"""Should handle jobs with no input_media_gcs_path."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = True
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
mock_job = Mock()
|
|
mock_job.job_id = "abc123"
|
|
mock_job.input_media_gcs_path = None
|
|
mock_job.file_urls = {
|
|
"stems": {
|
|
"instrumental_clean": "jobs/abc123/stems/instrumental_clean.flac",
|
|
}
|
|
}
|
|
|
|
result = service.prepare_review_audio_for_job(mock_job)
|
|
|
|
assert len(result) == 1
|
|
|
|
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
def test_handles_no_stems_key(self, mock_subprocess):
|
|
"""Should handle jobs where file_urls has no stems key at all."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = True
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
mock_job = Mock()
|
|
mock_job.job_id = "abc123"
|
|
mock_job.input_media_gcs_path = "jobs/abc123/input/song.flac"
|
|
mock_job.file_urls = {} # No stems key
|
|
|
|
result = service.prepare_review_audio_for_job(mock_job)
|
|
|
|
assert len(result) == 1
|
|
assert result[0] == "jobs/abc123/review-audio/song.ogg"
|
|
|
|
|
|
class TestInit:
|
|
"""Test AudioTranscodingService initialization."""
|
|
|
|
@patch("backend.services.audio_transcoding_service.StorageService")
|
|
def test_creates_default_storage_service(self, mock_storage_class):
|
|
"""Creates a StorageService when none is provided."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
service = AudioTranscodingService()
|
|
|
|
mock_storage_class.assert_called_once()
|
|
assert service.storage is mock_storage_class.return_value
|
|
|
|
def test_uses_provided_storage_service(self):
|
|
"""Uses the provided storage service instead of creating one."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
assert service.storage is mock_storage
|
|
|
|
|
|
class TestGetReviewAudioUrlAsync:
|
|
"""Test async wrapper."""
|
|
|
|
@pytest.mark.asyncio
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
async def test_async_wrapper_delegates(self, mock_subprocess):
|
|
"""Async wrapper should call sync method via to_thread."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = True
|
|
mock_storage.generate_signed_url.return_value = "https://signed/url"
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
result = await service.get_review_audio_url_async("jobs/abc/input/song.flac")
|
|
|
|
assert result == "https://signed/url"
|
|
|
|
|
|
class TestGetReviewAudioBytes:
|
|
"""get_review_audio_bytes() proxies the audio through the API (CORS-safe)."""
|
|
|
|
def test_returns_ogg_bytes_on_success(self):
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = True # cache hit
|
|
mock_storage.download_bytes.return_value = b"OggS-transcoded-bytes"
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
data, content_type = service.get_review_audio_bytes("jobs/abc/stems/vocals_clean.flac")
|
|
|
|
assert data == b"OggS-transcoded-bytes"
|
|
assert content_type == "audio/ogg"
|
|
mock_storage.download_bytes.assert_called_once_with("jobs/abc/review-audio/vocals_clean.ogg")
|
|
|
|
def test_falls_back_to_source_bytes_on_transcode_error(self):
|
|
"""A transient transcode failure serves the original source, not a 500."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = False # cache miss → transcode path
|
|
mock_storage.download_file.side_effect = Exception("ffmpeg input download failed")
|
|
mock_storage.download_bytes.return_value = b"fLaC-source-bytes"
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
data, content_type = service.get_review_audio_bytes("jobs/abc/stems/vocals_clean.flac")
|
|
|
|
assert data == b"fLaC-source-bytes"
|
|
assert content_type == "audio/flac"
|
|
# Fell back to proxying the original source object.
|
|
mock_storage.download_bytes.assert_called_once_with("jobs/abc/stems/vocals_clean.flac")
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_wrapper_delegates(self):
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = True
|
|
mock_storage.download_bytes.return_value = b"OggS"
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
data, content_type = await service.get_review_audio_bytes_async("jobs/abc/stems/vocals_clean.flac")
|
|
|
|
assert data == b"OggS"
|
|
assert content_type == "audio/ogg"
|
|
|
|
|
|
class TestTranscodeReviewStems:
|
|
"""transcode_review_stems(): post-separation pre-transcode of review-playable stems."""
|
|
|
|
def _job(self, stems):
|
|
job = Mock()
|
|
job.job_id = "abc123"
|
|
job.file_urls = {"stems": stems}
|
|
return job
|
|
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
def test_force_retranscodes_even_when_cached(self, mock_subprocess):
|
|
"""Fresh stems supersede any earlier OGG: force skips the cache check."""
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_subprocess.run.return_value = Mock(returncode=0, stderr="")
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = True
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
result = service.transcode_review_stems(self._job({
|
|
"instrumental_clean": "jobs/abc123/stems/instrumental_clean.flac",
|
|
"instrumental_with_backing": "jobs/abc123/stems/instrumental_with_backing.flac",
|
|
"backing_vocals": "jobs/abc123/stems/backing_vocals.flac",
|
|
"lead_vocals": "jobs/abc123/stems/lead_vocals.flac", # not review-playable
|
|
}))
|
|
|
|
assert sorted(result) == [
|
|
"jobs/abc123/review-audio/backing_vocals.ogg",
|
|
"jobs/abc123/review-audio/instrumental_clean.ogg",
|
|
"jobs/abc123/review-audio/instrumental_with_backing.ogg",
|
|
]
|
|
assert mock_subprocess.run.call_count == 3
|
|
mock_storage.file_exists.assert_not_called()
|
|
uploaded = sorted(c.args[1] for c in mock_storage.upload_file.call_args_list)
|
|
assert uploaded == sorted(result) and "lead_vocals" not in " ".join(uploaded)
|
|
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
def test_not_forced_uses_cache(self, mock_subprocess):
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
mock_storage.file_exists.return_value = True
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
result = service.transcode_review_stems(
|
|
self._job({"instrumental_clean": "jobs/abc123/stems/instrumental_clean.flac"}),
|
|
force=False,
|
|
)
|
|
assert result == ["jobs/abc123/review-audio/instrumental_clean.ogg"]
|
|
mock_subprocess.run.assert_not_called()
|
|
|
|
@patch("backend.services.audio_transcoding_service.subprocess")
|
|
def test_one_failure_does_not_block_others(self, mock_subprocess):
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_subprocess.run.return_value = Mock(returncode=0, stderr="")
|
|
mock_storage = Mock()
|
|
|
|
def _download(src, dest):
|
|
if "with_backing" in src:
|
|
raise Exception("download failed")
|
|
mock_storage.download_file.side_effect = _download
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
|
|
result = service.transcode_review_stems(self._job({
|
|
"instrumental_clean": "jobs/abc123/stems/instrumental_clean.flac",
|
|
"instrumental_with_backing": "jobs/abc123/stems/instrumental_with_backing.flac",
|
|
}))
|
|
assert result == ["jobs/abc123/review-audio/instrumental_clean.ogg"]
|
|
|
|
def test_no_stems_is_noop(self):
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
mock_storage = Mock()
|
|
service = AudioTranscodingService(storage_service=mock_storage)
|
|
job = Mock()
|
|
job.job_id = "abc123"
|
|
job.file_urls = {}
|
|
assert service.transcode_review_stems(job) == []
|
|
mock_storage.download_file.assert_not_called()
|
|
|
|
|
|
class TestInvalidateCache:
|
|
"""invalidate_cache() drops the cached OGG for a source overwritten in place."""
|
|
|
|
def test_deletes_cached_ogg(self):
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
storage = Mock()
|
|
AudioTranscodingService(storage_service=storage).invalidate_cache(
|
|
"jobs/abc/stems/custom_instrumental.flac"
|
|
)
|
|
storage.delete_file.assert_called_once_with(
|
|
"jobs/abc/review-audio/custom_instrumental.ogg", ignore_missing=True
|
|
)
|
|
|
|
def test_storage_errors_are_swallowed(self):
|
|
from backend.services.audio_transcoding_service import AudioTranscodingService
|
|
|
|
storage = Mock()
|
|
storage.delete_file.side_effect = RuntimeError("gcs down")
|
|
# Must not raise — a failed invalidation only risks a stale preview
|
|
AudioTranscodingService(storage_service=storage).invalidate_cache("jobs/abc/stems/x.flac")
|