mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-12-01 09:37:12 +09:00
## Summary
- Fix CDG+MP3 package sync issue where lyrics appeared 3 seconds early
- The CDG was using padded LRC timestamps with unpadded instrumental
audio
- CDGGenerator now strips countdown segment and adjusts timestamps when
needed
## The Bug
The MP4 video has a 3-second countdown ("3... 2... 1...") prepended, and
LRC timestamps are shifted forward by 3 seconds to match. However, the
CDG+MP3 package uses the original instrumental audio (no countdown), but
was using the shifted timestamps.
Result: CDG lyrics appeared 3 seconds too early.
## The Fix
1. Added `lrc_has_countdown_padding` flag to `OrchestratorConfig`
2. Flag is read from job's `lyrics_metadata.has_countdown_padding`
3. Flag is passed through `PackagingService` to `CDGGenerator`
4. When True, `CDGGenerator` strips the countdown segment from LRC and
shifts all timestamps back by 3 seconds
## Test Plan
- [x] Added 11 unit tests for countdown removal logic
- [x] Added packaging service integration test
- [x] Added orchestrator countdown pipeline tests
- [x] All 103 emulator tests pass
- [x] Verified manually with playable CDG+MP3 output
## Files Changed
- `karaoke_gen/lyrics_transcriber/output/cdg.py` - Core fix
- `backend/workers/video_worker_orchestrator.py` - Pipeline changes
- `backend/services/packaging_service.py` - Param passing
- `backend/tests/test_cdg_countdown.py` - New test file
- `backend/tests/test_video_worker_orchestrator.py` - Pipeline tests
- `docs/LESSONS-LEARNED.md` - Documented the pattern
- `docs/README.md` - Added to known issues
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---
@coderabbitai ignore
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
436 lines
16 KiB
Python
436 lines
16 KiB
Python
"""
|
|
Tests for PackagingService.
|
|
|
|
Tests cover:
|
|
- Service initialization
|
|
- CDG package creation
|
|
- TXT package creation
|
|
- Dry run mode
|
|
- Error handling
|
|
"""
|
|
|
|
import os
|
|
import zipfile
|
|
import tempfile
|
|
import pytest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from backend.services.packaging_service import (
|
|
PackagingService,
|
|
get_packaging_service,
|
|
)
|
|
|
|
|
|
class TestPackagingServiceInit:
|
|
"""Test service initialization."""
|
|
|
|
def test_init_default_values(self):
|
|
"""Test default initialization."""
|
|
service = PackagingService()
|
|
assert service.cdg_styles is None
|
|
assert service.dry_run is False
|
|
assert service.non_interactive is False
|
|
|
|
def test_init_with_cdg_styles(self):
|
|
"""Test initialization with CDG styles."""
|
|
styles = {"font_size": 24, "bg_color": "black"}
|
|
service = PackagingService(cdg_styles=styles)
|
|
assert service.cdg_styles == styles
|
|
|
|
def test_init_with_dry_run(self):
|
|
"""Test initialization with dry run mode."""
|
|
service = PackagingService(dry_run=True)
|
|
assert service.dry_run is True
|
|
|
|
def test_init_with_non_interactive(self):
|
|
"""Test initialization with non-interactive mode."""
|
|
service = PackagingService(non_interactive=True)
|
|
assert service.non_interactive is True
|
|
|
|
|
|
class TestPackagingServiceCreateZipFromFiles:
|
|
"""Test internal ZIP creation."""
|
|
|
|
def test_create_zip_from_files(self):
|
|
"""Test creating a ZIP file from multiple files."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
# Create test files
|
|
file1 = os.path.join(tmpdir, "test1.txt")
|
|
file2 = os.path.join(tmpdir, "test2.txt")
|
|
zip_path = os.path.join(tmpdir, "output.zip")
|
|
|
|
with open(file1, "w") as f:
|
|
f.write("content1")
|
|
with open(file2, "w") as f:
|
|
f.write("content2")
|
|
|
|
service = PackagingService()
|
|
service._create_zip_from_files(
|
|
zip_path,
|
|
[(file1, "test1.txt"), (file2, "test2.txt")]
|
|
)
|
|
|
|
assert os.path.isfile(zip_path)
|
|
with zipfile.ZipFile(zip_path, "r") as zf:
|
|
names = zf.namelist()
|
|
assert "test1.txt" in names
|
|
assert "test2.txt" in names
|
|
|
|
|
|
class TestPackagingServiceCDGPackage:
|
|
"""Test CDG package creation."""
|
|
|
|
def test_create_cdg_package_missing_lrc_file(self):
|
|
"""Test that missing LRC file raises FileNotFoundError."""
|
|
service = PackagingService()
|
|
|
|
with pytest.raises(FileNotFoundError) as exc_info:
|
|
service.create_cdg_package(
|
|
lrc_file="/nonexistent/file.lrc",
|
|
audio_file="/some/audio.flac",
|
|
output_zip_path="/output/test.zip",
|
|
artist="Test Artist",
|
|
title="Test Title",
|
|
)
|
|
assert "LRC file not found" in str(exc_info.value)
|
|
|
|
def test_create_cdg_package_missing_audio_file(self):
|
|
"""Test that missing audio file raises FileNotFoundError."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
lrc_file = os.path.join(tmpdir, "test.lrc")
|
|
with open(lrc_file, "w") as f:
|
|
f.write("[00:00.00]Test lyrics")
|
|
|
|
service = PackagingService()
|
|
|
|
with pytest.raises(FileNotFoundError) as exc_info:
|
|
service.create_cdg_package(
|
|
lrc_file=lrc_file,
|
|
audio_file="/nonexistent/audio.flac",
|
|
output_zip_path="/output/test.zip",
|
|
artist="Test Artist",
|
|
title="Test Title",
|
|
)
|
|
assert "Audio file not found" in str(exc_info.value)
|
|
|
|
def test_create_cdg_package_no_styles_raises(self):
|
|
"""Test that CDG generation without styles raises ValueError."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
lrc_file = os.path.join(tmpdir, "test.lrc")
|
|
audio_file = os.path.join(tmpdir, "test.flac")
|
|
zip_path = os.path.join(tmpdir, "output.zip")
|
|
|
|
with open(lrc_file, "w") as f:
|
|
f.write("[00:00.00]Test lyrics")
|
|
with open(audio_file, "w") as f:
|
|
f.write("fake audio")
|
|
|
|
# Service without CDG styles
|
|
service = PackagingService(cdg_styles=None)
|
|
|
|
with pytest.raises(ValueError) as exc_info:
|
|
service.create_cdg_package(
|
|
lrc_file=lrc_file,
|
|
audio_file=audio_file,
|
|
output_zip_path=zip_path,
|
|
artist="Test Artist",
|
|
title="Test Title",
|
|
)
|
|
assert "CDG styles configuration is required" in str(exc_info.value)
|
|
|
|
def test_create_cdg_package_dry_run(self):
|
|
"""Test CDG package creation in dry run mode."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
lrc_file = os.path.join(tmpdir, "test.lrc")
|
|
audio_file = os.path.join(tmpdir, "test.flac")
|
|
zip_path = os.path.join(tmpdir, "output.zip")
|
|
|
|
with open(lrc_file, "w") as f:
|
|
f.write("[00:00.00]Test lyrics")
|
|
with open(audio_file, "w") as f:
|
|
f.write("fake audio")
|
|
|
|
service = PackagingService(dry_run=True)
|
|
|
|
result = service.create_cdg_package(
|
|
lrc_file=lrc_file,
|
|
audio_file=audio_file,
|
|
output_zip_path=zip_path,
|
|
artist="Test Artist",
|
|
title="Test Title",
|
|
)
|
|
|
|
# In dry run, ZIP should not be created
|
|
assert not os.path.isfile(zip_path)
|
|
assert result[0] == zip_path
|
|
|
|
def test_create_cdg_package_existing_files(self):
|
|
"""Test CDG package creation with existing MP3 and CDG files."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
lrc_file = os.path.join(tmpdir, "test.lrc")
|
|
audio_file = os.path.join(tmpdir, "test.flac")
|
|
mp3_file = os.path.join(tmpdir, "test.mp3")
|
|
cdg_file = os.path.join(tmpdir, "test.cdg")
|
|
zip_path = os.path.join(tmpdir, "output.zip")
|
|
|
|
# Create all files
|
|
with open(lrc_file, "w") as f:
|
|
f.write("[00:00.00]Test lyrics")
|
|
with open(audio_file, "w") as f:
|
|
f.write("fake audio")
|
|
with open(mp3_file, "w") as f:
|
|
f.write("fake mp3")
|
|
with open(cdg_file, "w") as f:
|
|
f.write("fake cdg")
|
|
|
|
service = PackagingService()
|
|
|
|
result = service.create_cdg_package(
|
|
lrc_file=lrc_file,
|
|
audio_file=audio_file,
|
|
output_zip_path=zip_path,
|
|
artist="Test Artist",
|
|
title="Test Title",
|
|
output_mp3_path=mp3_file,
|
|
output_cdg_path=cdg_file,
|
|
)
|
|
|
|
# ZIP should be created from existing files
|
|
assert os.path.isfile(zip_path)
|
|
assert result[0] == zip_path
|
|
assert result[1] == mp3_file
|
|
assert result[2] == cdg_file
|
|
|
|
@patch("karaoke_gen.lyrics_transcriber.output.cdg.CDGGenerator")
|
|
def test_create_cdg_package_with_generator(self, mock_cdg_generator_class):
|
|
"""Test CDG package creation using CDGGenerator."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
lrc_file = os.path.join(tmpdir, "test.lrc")
|
|
audio_file = os.path.join(tmpdir, "test.flac")
|
|
zip_path = os.path.join(tmpdir, "output.zip")
|
|
generated_zip = os.path.join(tmpdir, "generated.zip")
|
|
|
|
with open(lrc_file, "w") as f:
|
|
f.write("[00:00.00]Test lyrics")
|
|
with open(audio_file, "w") as f:
|
|
f.write("fake audio")
|
|
|
|
# Create the zip file that would be generated
|
|
with zipfile.ZipFile(generated_zip, "w") as zf:
|
|
zf.writestr("test.mp3", "fake mp3")
|
|
zf.writestr("test.cdg", "fake cdg")
|
|
|
|
mock_generator = MagicMock()
|
|
mock_generator.generate_cdg_from_lrc.return_value = (
|
|
"test.cdg", "test.mp3", generated_zip
|
|
)
|
|
mock_cdg_generator_class.return_value = mock_generator
|
|
|
|
styles = {"font_size": 24}
|
|
service = PackagingService(cdg_styles=styles)
|
|
|
|
result = service.create_cdg_package(
|
|
lrc_file=lrc_file,
|
|
audio_file=audio_file,
|
|
output_zip_path=zip_path,
|
|
artist="Test Artist",
|
|
title="Test Title",
|
|
)
|
|
|
|
mock_generator.generate_cdg_from_lrc.assert_called_once_with(
|
|
lrc_file=lrc_file,
|
|
audio_file=audio_file,
|
|
title="Test Title",
|
|
artist="Test Artist",
|
|
cdg_styles=styles,
|
|
lrc_has_countdown_padding=False,
|
|
countdown_padding_seconds=3.0,
|
|
)
|
|
assert os.path.isfile(zip_path)
|
|
|
|
@patch("karaoke_gen.lyrics_transcriber.output.cdg.CDGGenerator")
|
|
def test_create_cdg_package_with_countdown_padding(self, mock_cdg_generator_class):
|
|
"""Test CDG package creation with countdown padding parameters."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
lrc_file = os.path.join(tmpdir, "test.lrc")
|
|
audio_file = os.path.join(tmpdir, "test.flac")
|
|
zip_path = os.path.join(tmpdir, "output.zip")
|
|
generated_zip = os.path.join(tmpdir, "generated.zip")
|
|
|
|
with open(lrc_file, "w") as f:
|
|
f.write("[00:00.00]Test lyrics")
|
|
with open(audio_file, "w") as f:
|
|
f.write("fake audio")
|
|
|
|
# Create the zip file that would be generated
|
|
with zipfile.ZipFile(generated_zip, "w") as zf:
|
|
zf.writestr("test.mp3", "fake mp3")
|
|
zf.writestr("test.cdg", "fake cdg")
|
|
|
|
mock_generator = MagicMock()
|
|
mock_generator.generate_cdg_from_lrc.return_value = (
|
|
"test.cdg", "test.mp3", generated_zip
|
|
)
|
|
mock_cdg_generator_class.return_value = mock_generator
|
|
|
|
styles = {"font_size": 24}
|
|
service = PackagingService(cdg_styles=styles)
|
|
|
|
# Call with countdown padding enabled
|
|
result = service.create_cdg_package(
|
|
lrc_file=lrc_file,
|
|
audio_file=audio_file,
|
|
output_zip_path=zip_path,
|
|
artist="Test Artist",
|
|
title="Test Title",
|
|
lrc_has_countdown_padding=True,
|
|
countdown_padding_seconds=3.5,
|
|
)
|
|
|
|
# Verify countdown params are passed to generator
|
|
mock_generator.generate_cdg_from_lrc.assert_called_once_with(
|
|
lrc_file=lrc_file,
|
|
audio_file=audio_file,
|
|
title="Test Title",
|
|
artist="Test Artist",
|
|
cdg_styles=styles,
|
|
lrc_has_countdown_padding=True,
|
|
countdown_padding_seconds=3.5,
|
|
)
|
|
assert os.path.isfile(zip_path)
|
|
|
|
|
|
class TestPackagingServiceTXTPackage:
|
|
"""Test TXT package creation."""
|
|
|
|
def test_create_txt_package_missing_lrc_file(self):
|
|
"""Test that missing LRC file raises FileNotFoundError."""
|
|
service = PackagingService()
|
|
|
|
with pytest.raises(FileNotFoundError) as exc_info:
|
|
service.create_txt_package(
|
|
lrc_file="/nonexistent/file.lrc",
|
|
mp3_file="/some/audio.mp3",
|
|
output_zip_path="/output/test.zip",
|
|
)
|
|
assert "LRC file not found" in str(exc_info.value)
|
|
|
|
def test_create_txt_package_missing_mp3_file(self):
|
|
"""Test that missing MP3 file raises FileNotFoundError."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
lrc_file = os.path.join(tmpdir, "test.lrc")
|
|
with open(lrc_file, "w") as f:
|
|
f.write("[00:00.00]Test lyrics")
|
|
|
|
service = PackagingService()
|
|
|
|
with pytest.raises(FileNotFoundError) as exc_info:
|
|
service.create_txt_package(
|
|
lrc_file=lrc_file,
|
|
mp3_file="/nonexistent/audio.mp3",
|
|
output_zip_path="/output/test.zip",
|
|
)
|
|
assert "MP3 file not found" in str(exc_info.value)
|
|
|
|
def test_create_txt_package_dry_run(self):
|
|
"""Test TXT package creation in dry run mode."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
lrc_file = os.path.join(tmpdir, "test.lrc")
|
|
mp3_file = os.path.join(tmpdir, "test.mp3")
|
|
zip_path = os.path.join(tmpdir, "output.zip")
|
|
|
|
with open(lrc_file, "w") as f:
|
|
f.write("[00:00.00]Test lyrics")
|
|
with open(mp3_file, "w") as f:
|
|
f.write("fake mp3")
|
|
|
|
service = PackagingService(dry_run=True)
|
|
|
|
result = service.create_txt_package(
|
|
lrc_file=lrc_file,
|
|
mp3_file=mp3_file,
|
|
output_zip_path=zip_path,
|
|
)
|
|
|
|
# In dry run, ZIP should not be created
|
|
assert not os.path.isfile(zip_path)
|
|
assert result[0] == zip_path
|
|
|
|
@patch("lyrics_converter.LyricsConverter")
|
|
def test_create_txt_package_success(self, mock_converter_class):
|
|
"""Test successful TXT package creation."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
lrc_file = os.path.join(tmpdir, "test.lrc")
|
|
mp3_file = os.path.join(tmpdir, "test.mp3")
|
|
zip_path = os.path.join(tmpdir, "output.zip")
|
|
txt_path = os.path.join(tmpdir, "output.txt")
|
|
|
|
with open(lrc_file, "w") as f:
|
|
f.write("[00:00.00]Test lyrics")
|
|
with open(mp3_file, "w") as f:
|
|
f.write("fake mp3")
|
|
|
|
mock_converter = MagicMock()
|
|
mock_converter.convert_file.return_value = "Converted lyrics text"
|
|
mock_converter_class.return_value = mock_converter
|
|
|
|
service = PackagingService()
|
|
|
|
result = service.create_txt_package(
|
|
lrc_file=lrc_file,
|
|
mp3_file=mp3_file,
|
|
output_zip_path=zip_path,
|
|
output_txt_path=txt_path,
|
|
)
|
|
|
|
mock_converter_class.assert_called_once_with(
|
|
output_format="txt",
|
|
filepath=lrc_file
|
|
)
|
|
mock_converter.convert_file.assert_called_once()
|
|
|
|
assert os.path.isfile(zip_path)
|
|
assert os.path.isfile(txt_path)
|
|
assert result[0] == zip_path
|
|
assert result[1] == txt_path
|
|
|
|
# Verify ZIP contents
|
|
with zipfile.ZipFile(zip_path, "r") as zf:
|
|
names = zf.namelist()
|
|
assert "test.mp3" in names
|
|
assert "output.txt" in names
|
|
|
|
|
|
class TestGetPackagingService:
|
|
"""Test factory function."""
|
|
|
|
def test_get_service_creates_instance(self):
|
|
"""Test that factory function creates a new instance."""
|
|
import backend.services.packaging_service as module
|
|
module._packaging_service = None
|
|
|
|
service = get_packaging_service()
|
|
|
|
assert service is not None
|
|
assert isinstance(service, PackagingService)
|
|
|
|
def test_get_service_with_cdg_styles(self):
|
|
"""Test factory function with CDG styles."""
|
|
import backend.services.packaging_service as module
|
|
module._packaging_service = None
|
|
|
|
styles = {"font_size": 24}
|
|
service = get_packaging_service(cdg_styles=styles)
|
|
|
|
assert service.cdg_styles == styles
|
|
|
|
def test_get_service_with_options(self):
|
|
"""Test factory function with additional options."""
|
|
import backend.services.packaging_service as module
|
|
module._packaging_service = None
|
|
|
|
service = get_packaging_service(dry_run=True, non_interactive=True)
|
|
|
|
assert service.dry_run is True
|
|
assert service.non_interactive is True
|