Compare commits

...
Author SHA1 Message Date
Andrew BeveridgeandClaude Opus 4.5 5c71d58a8d Fix progress display to show step 4 when workers are active during download
When the backend status is "downloading" but parallel workers (audio/lyrics)
are actively running, the frontend now correctly displays step 4 progress
instead of appearing stuck at step 3 "Downloading".

This improves UX by showing detailed worker progress (e.g., "Audio 1/2 + Transcribing")
when both workers are active, making the job appear responsive.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 22:26:26 -05:00
Andrew BeveridgeandClaude Opus 4.5 78d7eedeee Add tests for YouTube URL support
Add comprehensive test coverage for the new download_from_url()
functionality:

- FlacFetchAudioFetcher.download_from_url() tests:
  - YouTube URL download success
  - youtu.be short URL support
  - Embed URL support
  - Filename generation from artist/title
  - Filename fallback to video ID
  - Error handling for download failures
  - Output directory creation

- RemoteFlacFetchAudioFetcher.download_from_url() tests:
  - Verifies local flacfetch is used (not remote API)
  - Video ID extraction
  - Error handling

- KaraokePrep URL handling tests:
  - process() with YouTube URL
  - process() with URL but no artist/title
  - process() with youtu.be short URL
  - _is_url() helper method edge cases

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 22:18:17 -05:00
Andrew Beveridge 6fa81c1b69 Updated dependencies 2025-12-27 22:10:59 -05:00
Andrew BeveridgeandClaude Opus 4.5 706e6f7527 Fix YouTube URL support in karaoke-gen CLI
Restore YouTube URL input functionality that was broken during
flacfetch integration. Now users can pass a YouTube URL as the
first argument to karaoke-gen again.

Changes:
- Add download_from_url() abstract method to AudioFetcher base class
- Implement download_from_url() in FlacFetchAudioFetcher using
  flacfetch's download_by_id() method for direct YouTube downloads
- Implement download_from_url() in RemoteFlacFetchAudioFetcher
  using local flacfetch for YouTube (no remote API needed)
- Update KaraokePrep.process() to detect URLs and route them to
  the new download_from_url() method instead of flacfetch search
- Add _is_url() helper method for URL detection
- Add _use_url_download flag to differentiate URL downloads from
  artist+title searches

Fixes #67

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 22:08:19 -05:00
Andrew Beveridge ☄️ 8bbb1351f8 Merge pull request #73 from nomadkaraoke/feature/ci-improvements
ci: Add path filtering, caching, and frontend tests to GHA workflow
2025-12-27 21:50:35 -05:00
Andrew BeveridgeandClaude Opus 4.5 18e97dc0a0 ci: Use poetry.lock exclusively for deps filter
Remove pyproject.toml from the deps filter since poetry.lock is
already used as the source of truth for dependency changes elsewhere
(e.g., base image rebuild triggers). Version bumps alone shouldn't
trigger full test suites.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 21:48:50 -05:00
Andrew BeveridgeandClaude Opus 4.5 642c80164c ci: Add path filtering, caching, and frontend tests to GHA workflow
- Add `changes` job using dorny/paths-filter@v3 to detect what changed
- Add frontend-unit-tests job (Jest with coverage)
- Make all test jobs conditional based on path changes:
  - Frontend tests: run only if frontend/** changed
  - Backend tests: run if backend/** or package or deps changed
  - Package tests: run if karaoke_gen/** or tests/** or deps changed
- Add Poetry dependency caching using actions/cache@v4:
  - Cache .venv directory keyed on poetry.lock hash
  - Shared cache between package, backend, and lint jobs
- Add npm caching using actions/setup-node's built-in cache feature
- Add Yarn caching for lyrics frontend in deploy-frontend job
- Add spaCy model caching
- Jobs always run on main branch to ensure full validation before deploy
- Add summary table to changes job showing what areas changed

This significantly reduces CI time for focused PRs (e.g., frontend-only
changes skip all backend tests) while ensuring comprehensive testing
before merging to main.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 21:40:56 -05:00
Andrew Beveridge ☄️andClaude Opus 4.5 46eca212fe Fix: Prepare theme styles when creating job via audio search (#72)
* Fix: Prepare theme styles when creating job via audio search

When creating a job via the audio search endpoint with a theme_id,
the theme styles were not being prepared. This resulted in jobs that
had theme_id set but no style_params_gcs_path or style_assets, causing
preview videos to have black backgrounds instead of themed backgrounds.

The fix adds a call to _prepare_theme_for_job() in audio_search.py
when theme_id is provided and no custom style files are being uploaded.
This matches the behavior of file_upload.py.

Also adds regression tests to catch this issue:
- test_audio_search_with_theme_calls_prepare_job_style
- test_audio_search_code_handles_theme_preparation

Fixes issue observed in production job ffb0b8fa.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Remove unused variable from test (CodeRabbit feedback)

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 21:38:05 -05:00
12 changed files with 1057 additions and 55 deletions

No files matched your search

+171 -10
View File
@@ -16,6 +16,85 @@ concurrency:
cancel-in-progress: true
jobs:
# ============================================
# PATH FILTERING - Determine what changed
# ============================================
changes:
name: "Detect Changes"
runs-on: ubuntu-latest
outputs:
frontend: ${{ steps.filter.outputs.frontend }}
backend: ${{ steps.filter.outputs.backend }}
package: ${{ steps.filter.outputs.package }}
infra: ${{ steps.filter.outputs.infra }}
deps: ${{ steps.filter.outputs.deps }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
frontend:
- 'frontend/**'
backend:
- 'backend/**'
package:
- 'karaoke_gen/**'
- 'lyrics_transcriber_temp/**'
- 'tests/**'
infra:
- 'infrastructure/**'
deps:
- 'poetry.lock'
- name: Summary
run: |
echo "### Change Detection Results" >> $GITHUB_STEP_SUMMARY
echo "| Area | Changed |" >> $GITHUB_STEP_SUMMARY
echo "|------|---------|" >> $GITHUB_STEP_SUMMARY
echo "| Frontend | ${{ steps.filter.outputs.frontend }} |" >> $GITHUB_STEP_SUMMARY
echo "| Backend | ${{ steps.filter.outputs.backend }} |" >> $GITHUB_STEP_SUMMARY
echo "| Package | ${{ steps.filter.outputs.package }} |" >> $GITHUB_STEP_SUMMARY
echo "| Infrastructure | ${{ steps.filter.outputs.infra }} |" >> $GITHUB_STEP_SUMMARY
echo "| Dependencies | ${{ steps.filter.outputs.deps }} |" >> $GITHUB_STEP_SUMMARY
# ============================================
# FRONTEND TESTS
# ============================================
frontend-unit-tests:
name: "Frontend - Unit Tests"
runs-on: ubuntu-latest
needs: changes
if: ${{ needs.changes.outputs.frontend == 'true' || github.ref == 'refs/heads/main' }}
steps:
- uses: actions/checkout@v4
- name: Setup Node.js with caching
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: frontend/package-lock.json
- name: Install dependencies
working-directory: frontend
run: npm ci --legacy-peer-deps
- name: Run unit tests with coverage
working-directory: frontend
run: npm run test:ci
- name: Upload coverage report
if: always()
uses: actions/upload-artifact@v4
with:
name: frontend-coverage
path: frontend/coverage/
retention-days: 7
# ============================================
# PACKAGE TESTS (karaoke-gen Python CLI)
# ============================================
@@ -23,6 +102,8 @@ jobs:
package-unit-tests:
name: "Package - Unit Tests (70% coverage)"
runs-on: ubuntu-latest
needs: changes
if: ${{ needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true' || github.ref == 'refs/heads/main' }}
steps:
- uses: actions/checkout@v4
with:
@@ -39,6 +120,14 @@ jobs:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-package-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-package-${{ runner.os }}-py3.13-
- name: Install dependencies
run: poetry install --with dev
@@ -47,6 +136,12 @@ jobs:
sudo apt-get update
sudo apt-get install -y ffmpeg
- name: Cache spaCy model
uses: actions/cache@v4
with:
path: ~/.cache/spacy
key: spacy-en_core_web_sm-${{ runner.os }}
- name: Install spaCy language models
run: poetry run python -m spacy download en_core_web_sm
@@ -65,6 +160,8 @@ jobs:
package-integration-tests:
name: "Package - Integration Tests"
runs-on: ubuntu-latest
needs: changes
if: ${{ needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true' || github.ref == 'refs/heads/main' }}
steps:
- uses: actions/checkout@v4
with:
@@ -81,6 +178,14 @@ jobs:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-package-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-package-${{ runner.os }}-py3.13-
- name: Install dependencies
run: poetry install --with dev
@@ -89,6 +194,12 @@ jobs:
sudo apt-get update
sudo apt-get install -y ffmpeg
- name: Cache spaCy model
uses: actions/cache@v4
with:
path: ~/.cache/spacy
key: spacy-en_core_web_sm-${{ runner.os }}
- name: Install spaCy language models
run: poetry run python -m spacy download en_core_web_sm
@@ -98,7 +209,8 @@ jobs:
package-build:
name: "Package - Build & Install"
runs-on: ubuntu-latest
needs: [package-unit-tests, package-integration-tests]
needs: [changes, package-unit-tests, package-integration-tests]
if: ${{ always() && (needs.package-unit-tests.result == 'success' || needs.package-unit-tests.result == 'skipped') && (needs.package-integration-tests.result == 'success' || needs.package-integration-tests.result == 'skipped') && (needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true' || github.ref == 'refs/heads/main') }}
steps:
- uses: actions/checkout@v4
with:
@@ -115,6 +227,14 @@ jobs:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-package-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-package-${{ runner.os }}-py3.13-
- name: Build package
run: poetry build
@@ -148,6 +268,8 @@ jobs:
backend-unit-tests:
name: "Backend - Unit Tests"
runs-on: ubuntu-latest
needs: changes
if: ${{ needs.changes.outputs.backend == 'true' || needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true' || github.ref == 'refs/heads/main' }}
steps:
- name: Checkout code
uses: actions/checkout@v4
@@ -165,6 +287,14 @@ jobs:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-backend-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-backend-${{ runner.os }}-py3.13-
- name: Install dependencies
run: poetry install
@@ -183,6 +313,8 @@ jobs:
backend-emulator-tests:
name: "Backend - Emulator Integration Tests"
runs-on: ubuntu-latest
needs: changes
if: ${{ needs.changes.outputs.backend == 'true' || needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true' || github.ref == 'refs/heads/main' }}
steps:
- name: Free disk space
run: |
@@ -207,6 +339,14 @@ jobs:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-backend-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-backend-${{ runner.os }}-py3.13-
- name: Set up Java 21 (required by Firestore emulator)
uses: actions/setup-java@v4
with:
@@ -243,6 +383,8 @@ jobs:
backend-lint:
name: "Backend - Code Quality"
runs-on: ubuntu-latest
needs: changes
if: ${{ needs.changes.outputs.backend == 'true' || needs.changes.outputs.package == 'true' || github.ref == 'refs/heads/main' }}
steps:
- name: Checkout code
uses: actions/checkout@v4
@@ -260,6 +402,14 @@ jobs:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-lint-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-lint-${{ runner.os }}-py3.13-
- name: Install dependencies
run: poetry install
@@ -362,7 +512,8 @@ jobs:
deploy-frontend:
name: "Deploy - Frontend (GitHub Pages)"
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
needs: [frontend-unit-tests]
if: ${{ always() && github.ref == 'refs/heads/main' && github.event_name == 'push' && (needs.frontend-unit-tests.result == 'success' || needs.frontend-unit-tests.result == 'skipped') }}
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
@@ -370,10 +521,12 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
- name: Setup Node.js with caching
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: frontend/package-lock.json
- name: Enable Yarn (for lyrics frontend)
run: corepack enable
@@ -390,6 +543,14 @@ jobs:
run: npm run build
# ===== Build Lyrics Review Frontend (Vite) =====
- name: Cache Yarn dependencies (lyrics frontend)
uses: actions/cache@v4
with:
path: lyrics_transcriber_temp/lyrics_transcriber/frontend/node_modules
key: yarn-lyrics-${{ runner.os }}-${{ hashFiles('lyrics_transcriber_temp/lyrics_transcriber/frontend/yarn.lock') }}
restore-keys: |
yarn-lyrics-${{ runner.os }}-
- name: Install lyrics frontend dependencies
working-directory: lyrics_transcriber_temp/lyrics_transcriber/frontend
run: yarn install --frozen-lockfile
@@ -428,7 +589,7 @@ jobs:
name: "Deploy - Backend (Cloud Run)"
runs-on: ubuntu-latest
needs: [backend-unit-tests, backend-emulator-tests, backend-lint]
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
if: ${{ always() && github.ref == 'refs/heads/main' && github.event_name == 'push' && (needs.backend-unit-tests.result == 'success' || needs.backend-unit-tests.result == 'skipped') && (needs.backend-emulator-tests.result == 'success' || needs.backend-emulator-tests.result == 'skipped') && (needs.backend-lint.result == 'success' || needs.backend-lint.result == 'skipped') }}
permissions:
contents: read
id-token: write
@@ -453,7 +614,7 @@ jobs:
VERSION=$(poetry version --short)
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "📋 Current version: $VERSION"
if git ls-remote --tags origin | grep -q "refs/tags/v$VERSION$"; then
echo "needs_deploy=false" >> $GITHUB_OUTPUT
echo "⏭️ Tag v$VERSION already exists - skipping backend deployment"
@@ -604,22 +765,22 @@ jobs:
echo "Service URL: $SERVICE_URL"
echo "Expected version: $EXPECTED_VERSION"
# Wait for new revision to be ready
echo "Waiting for new revision to be ready..."
sleep 15
# Basic health check
curl -f "$SERVICE_URL/api/health" || exit 1
echo "✅ Service is healthy"
# Version verification with retries (Cloud Run may take time to route to new revision)
MAX_RETRIES=6
RETRY_DELAY=10
for i in $(seq 1 $MAX_RETRIES); do
echo "Checking deployed version (attempt $i/$MAX_RETRIES)..."
DEPLOYED_VERSION=$(curl -sf "$SERVICE_URL/api/health/detailed" | jq -r '.version // empty')
if [ "$DEPLOYED_VERSION" = "$EXPECTED_VERSION" ]; then
echo "✅ Version verified: $DEPLOYED_VERSION"
break
@@ -643,7 +804,7 @@ jobs:
echo "Commit: ${{ github.sha }}"
echo "Branch: ${{ github.ref_name }}"
echo "Run: ${{ github.run_number }}"
- name: Skipped deployment summary
if: steps.version_check.outputs.needs_deploy == 'false'
run: |
+22 -1
View File
@@ -537,7 +537,28 @@ async def search_audio(
'audio_search_title': body.title,
'auto_download': body.auto_download,
})
# If theme is set and no custom style files are being uploaded, prepare theme style now
# This copies the theme's style_params.json to the job folder so LyricsTranscriber
# can access the style configuration for preview videos
if body.theme_id and not body.style_files:
from backend.api.routes.file_upload import _prepare_theme_for_job
try:
style_params_path, theme_style_assets, youtube_desc = _prepare_theme_for_job(
job_id, body.theme_id, body.color_overrides
)
theme_update = {
'style_params_gcs_path': style_params_path,
'style_assets': theme_style_assets,
}
if youtube_desc and not effective_youtube_description:
theme_update['youtube_description_template'] = youtube_desc
job_manager.update_job(job_id, theme_update)
logger.info(f"Applied theme '{body.theme_id}' to job {job_id}")
except Exception as e:
logger.warning(f"Failed to prepare theme '{body.theme_id}' for job {job_id}: {e}")
# Continue without theme - job can still be processed with defaults
# Handle style file uploads if provided
style_upload_urls: List[StyleUploadUrl] = []
style_assets = {}
@@ -330,7 +330,7 @@ class TestReviewEndpoints:
class TestRenderVideoWorker:
"""Test render video worker endpoint."""
def test_render_video_worker_endpoint_exists(self, client, auth_headers):
"""Test that render-video worker endpoint exists."""
# Create a job
@@ -340,9 +340,9 @@ class TestRenderVideoWorker:
json={"url": "https://youtube.com/watch?v=render-test"}
)
job_id = create_resp.json()["job_id"]
time.sleep(0.2)
# Test render-video worker endpoint exists
response = client.post(
"/api/internal/workers/render-video",
+88
View File
@@ -805,6 +805,7 @@ class TestAudioSearchThemeSupport:
1. Accepted in AudioSearchRequest model
2. Passed through to JobCreate
3. Result in correct CDG/TXT defaults when theme is set
4. Theme style is PREPARED (copied to job folder) when theme_id is set
"""
def test_audio_search_request_accepts_theme_id(self):
@@ -958,6 +959,93 @@ class TestAudioSearchThemeSupport:
assert job.enable_txt is True
class TestAudioSearchThemePreparation:
"""Test that audio search endpoint properly prepares theme styles.
CRITICAL: This test class was added to catch the bug where audio_search.py
accepted theme_id but never called prepare_job_style(), resulting in jobs
with theme_id set but no style_params_gcs_path or style_assets.
The symptom: Preview videos show black background instead of themed background.
Job ID ffb0b8fa in production demonstrated this issue.
"""
def test_audio_search_with_theme_calls_prepare_job_style(self):
"""Test that creating a job via audio search with theme_id prepares the style.
CRITICAL: This test verifies that when a job is created via the audio-search
endpoint with a theme_id (and no custom style files), the code calls
prepare_job_style() to set:
1. style_params_gcs_path (pointing to the copied style_params.json)
2. style_assets (populated with asset mappings)
Without this, LyricsTranscriber won't have access to the theme's styles
and preview videos will have black backgrounds instead of themed ones.
"""
# Import the endpoint module to access internal functions
from backend.api.routes import audio_search as audio_search_module
# Check if there's an import of prepare_job_style from theme_service
source_code_path = audio_search_module.__file__
with open(source_code_path, 'r') as f:
source_code = f.read()
# Check for either:
# 1. Import of _prepare_theme_for_job from file_upload
# 2. Import of prepare_job_style from theme_service
# 3. Inline call to theme_service.prepare_job_style
has_theme_prep_import = (
'_prepare_theme_for_job' in source_code or
'prepare_job_style' in source_code
)
assert has_theme_prep_import, (
"audio_search.py does not import or call prepare_job_style() or _prepare_theme_for_job(). "
"When theme_id is provided without custom style files, the endpoint MUST call "
"prepare_job_style() to copy the theme's style_params.json to the job folder. "
"Without this, LyricsTranscriber won't have style configuration and preview "
"videos will have black backgrounds instead of themed ones."
)
def test_audio_search_code_handles_theme_preparation(self):
"""Verify audio_search.py has theme preparation logic similar to file_upload.py.
This test compares audio_search.py with file_upload.py to ensure they have
similar theme preparation patterns. file_upload.py correctly prepares themes,
audio_search.py should do the same.
"""
from backend.api.routes import audio_search as audio_search_module
from backend.api.routes import file_upload as file_upload_module
# Read source code of both modules
with open(audio_search_module.__file__, 'r') as f:
audio_search_code = f.read()
with open(file_upload_module.__file__, 'r') as f:
file_upload_code = f.read()
# file_upload.py has this pattern for theme preparation:
# if body.theme_id and not has_style_params_upload:
# style_params_path, style_assets, youtube_desc = _prepare_theme_for_job(...)
assert '_prepare_theme_for_job' in file_upload_code, \
"file_upload.py should have _prepare_theme_for_job function"
# audio_search.py should have similar pattern
# Look for either the function or a similar conditional check
# STRICT CHECK: Look for actual theme preparation logic being CALLED, not just mentioned
has_theme_preparation_call = (
# Check for import or call of theme preparation function
'_prepare_theme_for_job(' in audio_search_code or
'prepare_job_style(' in audio_search_code
)
assert has_theme_preparation_call, (
"audio_search.py does not CALL theme preparation logic. "
"It accepts theme_id but never prepares the theme style like file_upload.py does. "
"Add a call to _prepare_theme_for_job() when theme_id is set and no style files are uploaded."
)
class TestFlacfetchIntegration:
"""
Integration tests that verify flacfetch imports work correctly.
+75 -1
View File
@@ -57,12 +57,86 @@ describe('getJobStep', () => {
expect(result.label).toBe('Downloading audio');
});
it('returns step 3 for downloading status', () => {
it('returns step 3 for downloading status without worker progress', () => {
const job = createJob('downloading');
const result = getJobStep(job);
expect(result.step).toBe(3);
expect(result.label).toBe('Downloading');
});
it('returns step 3 for downloading with empty state_data', () => {
const job = createJob('downloading', {});
const result = getJobStep(job);
expect(result.step).toBe(3);
expect(result.label).toBe('Downloading');
});
});
describe('Step 3→4 Transition: Downloading with active workers', () => {
// When backend status is "downloading" but workers are actively running,
// the frontend should show step 4 (Processing) to avoid appearing stuck
it('returns step 4 when downloading with active audio worker', () => {
const job = createJob('downloading', {
audio_progress: { stage: 'separating_stage1', progress: 10 },
});
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.color).toBe('text-purple-400');
});
it('returns step 4 when downloading with active lyrics worker', () => {
const job = createJob('downloading', {
lyrics_progress: { stage: 'transcribing', progress: 10 },
});
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.color).toBe('text-purple-400');
});
it('returns step 4 with combined label when both workers active', () => {
const job = createJob('downloading', {
audio_progress: { stage: 'separating_stage1', progress: 10 },
lyrics_progress: { stage: 'transcribing', progress: 10 },
});
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.label).toMatch(/Audio.*\+.*Transcribing|Transcribing.*\+.*Audio/);
});
it('shows only active worker when one is complete', () => {
const job = createJob('downloading', {
audio_progress: { stage: 'audio_complete' },
lyrics_progress: { stage: 'transcribing' },
audio_complete: true,
});
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.label).toBe('Transcribing');
});
it('shows processing complete when both workers done', () => {
const job = createJob('downloading', {
audio_progress: { stage: 'audio_complete' },
lyrics_progress: { stage: 'lyrics_complete' },
audio_complete: true,
lyrics_complete: true,
});
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.label).toBe('Processing complete');
});
it('returns step 3 when state_data has no stage info', () => {
// state_data exists but no actual progress stage set
const job = createJob('downloading', {
audio_progress: {},
lyrics_progress: {},
});
const result = getJobStep(job);
expect(result.step).toBe(3);
expect(result.label).toBe('Downloading');
});
});
describe('Step 4: Parallel Processing', () => {
+29
View File
@@ -87,6 +87,20 @@ const STATUS_CONFIG: Record<
const TOTAL_STEPS = 10;
/**
* Check if parallel workers (audio/lyrics) are actively running.
* This is indicated by state_data containing audio_progress or lyrics_progress.
*/
function isParallelProcessingActive(job: Job): boolean {
if (!job.state_data) return false;
const audioProgress = job.state_data.audio_progress as { stage?: string } | undefined;
const lyricsProgress = job.state_data.lyrics_progress as { stage?: string } | undefined;
// Workers are active if we have progress data with a stage
return !!(audioProgress?.stage || lyricsProgress?.stage);
}
/**
* Get the step information for a job based on its status.
*
@@ -108,6 +122,21 @@ export function getJobStep(job: Job): JobStep {
};
}
// Special case: "downloading" status but parallel workers are actually running.
// The backend sets status to "downloading" when audio download completes and workers start,
// but doesn't update to step 4 statuses until screens_worker runs.
// Show step 4 with detailed progress to avoid appearing "stuck" at downloading.
if (status === "downloading" && isParallelProcessingActive(job)) {
const enhancedLabel = getParallelProcessingLabel(job, "Processing");
return {
step: 4,
total: TOTAL_STEPS,
label: enhancedLabel,
isBlocking: false,
color: "text-purple-400", // Same as step 4 processing color
};
}
// During parallel processing (step 4), check state_data for more detail
if (config.step === 4 && job.state_data) {
const enhancedLabel = getParallelProcessingLabel(job, config.label);
+218
View File
@@ -264,6 +264,36 @@ class AudioFetcher(ABC):
"""
pass
@abstractmethod
def download_from_url(
self,
url: str,
output_dir: str,
output_filename: Optional[str] = None,
artist: Optional[str] = None,
title: Optional[str] = None,
) -> AudioFetchResult:
"""
Download audio directly from a URL (e.g., YouTube URL).
This bypasses the search step and downloads directly from the provided URL.
Useful when the user provides a specific YouTube URL rather than artist/title.
Args:
url: The URL to download from (e.g., YouTube video URL)
output_dir: Directory to save the downloaded file
output_filename: Optional filename (without extension)
artist: Optional artist name for metadata
title: Optional title for metadata
Returns:
AudioFetchResult with the downloaded file path
Raises:
DownloadError: If download fails
"""
pass
class FlacFetchAudioFetcher(AudioFetcher):
"""
@@ -645,6 +675,94 @@ class FlacFetchAudioFetcher(AudioFetcher):
except Exception as e:
raise DownloadError(f"Failed to download {artist} - {title}: {e}") from e
def download_from_url(
self,
url: str,
output_dir: str,
output_filename: Optional[str] = None,
artist: Optional[str] = None,
title: Optional[str] = None,
) -> AudioFetchResult:
"""
Download audio directly from a URL (e.g., YouTube URL).
Uses flacfetch's download_by_id() method which supports direct YouTube downloads.
Args:
url: The URL to download from (e.g., YouTube video URL)
output_dir: Directory to save the downloaded file
output_filename: Optional filename (without extension)
artist: Optional artist name for metadata
title: Optional title for metadata
Returns:
AudioFetchResult with the downloaded file path
Raises:
DownloadError: If download fails
"""
import re
manager = self._get_manager()
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
# Detect source type from URL
source_name = "YouTube" # Default to YouTube for now
source_id = None
# Extract YouTube video ID from URL
youtube_patterns = [
r'(?:youtube\.com/watch\?v=|youtu\.be/)([a-zA-Z0-9_-]{11})',
r'youtube\.com/embed/([a-zA-Z0-9_-]{11})',
r'youtube\.com/v/([a-zA-Z0-9_-]{11})',
]
for pattern in youtube_patterns:
match = re.search(pattern, url)
if match:
source_id = match.group(1)
break
if not source_id:
# For other URLs, use the full URL as the source_id
source_id = url
# Generate filename if not provided
if output_filename is None:
if artist and title:
output_filename = f"{artist} - {title}"
else:
output_filename = source_id
self.logger.info(f"Downloading from URL: {url}")
try:
filepath = manager.download_by_id(
source_name=source_name,
source_id=source_id,
output_path=output_dir,
output_filename=output_filename,
download_url=url, # Pass full URL for direct download
)
if not filepath:
raise DownloadError(f"Download returned no file path for URL: {url}")
self.logger.info(f"Downloaded to: {filepath}")
return AudioFetchResult(
filepath=filepath,
artist=artist or "",
title=title or "",
provider=source_name,
duration=None, # Could extract from yt-dlp info if needed
quality=None,
)
except Exception as e:
raise DownloadError(f"Failed to download from URL {url}: {e}") from e
def _interruptible_search(self, manager, query) -> list:
"""
Run search in a way that can be interrupted by Ctrl+C.
@@ -1548,6 +1666,106 @@ class RemoteFlacFetchAudioFetcher(AudioFetcher):
print("\nCancelled")
raise UserCancelledError("Selection cancelled by user (Ctrl+C)")
def download_from_url(
self,
url: str,
output_dir: str,
output_filename: Optional[str] = None,
artist: Optional[str] = None,
title: Optional[str] = None,
) -> AudioFetchResult:
"""
Download audio directly from a URL (e.g., YouTube URL).
For YouTube URLs, this uses local flacfetch since YouTube downloads
don't require the remote flacfetch infrastructure (no torrents).
Args:
url: The URL to download from (e.g., YouTube video URL)
output_dir: Directory to save the downloaded file
output_filename: Optional filename (without extension)
artist: Optional artist name for metadata
title: Optional title for metadata
Returns:
AudioFetchResult with the downloaded file path
Raises:
DownloadError: If download fails
"""
import re
self.logger.info(f"[RemoteFlacFetcher] Downloading from URL: {url}")
self.logger.info(f"[RemoteFlacFetcher] Using local flacfetch for YouTube download (no remote API needed)")
try:
# Use local flacfetch for YouTube downloads - no need for remote API
# This avoids needing yt-dlp directly in karaoke-gen
from flacfetch.core.manager import FetchManager
from flacfetch.providers.youtube import YoutubeProvider
from flacfetch.downloaders.youtube import YoutubeDownloader
# Create a minimal local manager for YouTube downloads
manager = FetchManager()
manager.add_provider(YoutubeProvider())
manager.register_downloader("YouTube", YoutubeDownloader())
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
# Extract video ID from URL
source_id = None
youtube_patterns = [
r'(?:youtube\.com/watch\?v=|youtu\.be/)([a-zA-Z0-9_-]{11})',
r'youtube\.com/embed/([a-zA-Z0-9_-]{11})',
r'youtube\.com/v/([a-zA-Z0-9_-]{11})',
]
for pattern in youtube_patterns:
match = re.search(pattern, url)
if match:
source_id = match.group(1)
break
if not source_id:
source_id = url
# Generate filename if not provided
if output_filename is None:
if artist and title:
output_filename = f"{artist} - {title}"
else:
output_filename = source_id
# Use flacfetch's download_by_id for direct URL download
filepath = manager.download_by_id(
source_name="YouTube",
source_id=source_id,
output_path=output_dir,
output_filename=output_filename,
download_url=url,
)
if not filepath:
raise DownloadError(f"Download returned no file path for URL: {url}")
self.logger.info(f"[RemoteFlacFetcher] Downloaded to: {filepath}")
return AudioFetchResult(
filepath=filepath,
artist=artist or "",
title=title or "",
provider="YouTube",
duration=None,
quality=None,
)
except ImportError as e:
raise DownloadError(
f"flacfetch is required for URL downloads but import failed: {e}"
)
except Exception as e:
raise DownloadError(f"Failed to download from URL {url}: {e}") from e
# Alias for shorter name
RemoteFlacFetcher = RemoteFlacFetchAudioFetcher
+78 -25
View File
@@ -488,41 +488,56 @@ class KaraokePrep:
self.logger.info(f"Found existing media files matching extractor '{self.extractor}', skipping download/conversion.")
elif getattr(self, '_use_audio_fetcher', False):
# Use flacfetch to search and download audio
self.logger.info(f"Using flacfetch to search and download: {self.artist} - {self.title}")
try:
# Search and download audio using the AudioFetcher
fetch_result = self.audio_fetcher.search_and_download(
artist=self.artist,
title=self.title,
output_dir=track_output_dir,
output_filename=f"{artist_title} (flacfetch)",
auto_select=self.auto_download,
)
# Update extractor to reflect the actual provider used
self.extractor = f"flacfetch-{fetch_result.provider}"
# Check if this is a URL download or search+download
if getattr(self, '_use_url_download', False):
# Direct URL download (e.g., YouTube URL)
self.logger.info(f"Using flacfetch to download from URL: {self.url}")
fetch_result = self.audio_fetcher.download_from_url(
url=self.url,
output_dir=track_output_dir,
output_filename=f"{artist_title} (youtube)" if artist_title != "Unknown - Unknown" else None,
artist=self.artist,
title=self.title,
)
# Update extractor to reflect the source
self.extractor = f"youtube"
else:
# Use flacfetch to search and download audio
self.logger.info(f"Using flacfetch to search and download: {self.artist} - {self.title}")
fetch_result = self.audio_fetcher.search_and_download(
artist=self.artist,
title=self.title,
output_dir=track_output_dir,
output_filename=f"{artist_title} (flacfetch)",
auto_select=self.auto_download,
)
# Update extractor to reflect the actual provider used
self.extractor = f"flacfetch-{fetch_result.provider}"
# Set up the output paths
output_filename_no_extension = os.path.join(track_output_dir, f"{artist_title} ({self.extractor})")
# Copy/move the downloaded file to the expected location
processed_track["input_media"] = self.file_handler.download_audio_from_fetcher_result(
fetch_result.filepath, output_filename_no_extension
)
self.logger.info(f"Audio downloaded from {fetch_result.provider}: {processed_track['input_media']}")
# Convert to WAV for audio processing
self.logger.info("Converting downloaded audio to WAV for processing...")
processed_track["input_audio_wav"] = self.file_handler.convert_to_wav(
processed_track["input_media"], output_filename_no_extension
)
# No still image for audio-only downloads
processed_track["input_still_image"] = None
except UserCancelledError:
# User cancelled - propagate up to CLI for graceful exit
raise
@@ -1103,17 +1118,56 @@ class KaraokePrep:
return tracks
def _is_url(self, string: str) -> bool:
"""Check if a string is a URL."""
return string is not None and (string.startswith("http://") or string.startswith("https://"))
async def process(self):
if self.input_media is not None and os.path.isdir(self.input_media):
self.logger.info(f"Input media {self.input_media} is a local folder, processing each file individually...")
return await self.process_folder()
elif self.input_media is not None and os.path.isfile(self.input_media):
self.logger.info(f"Input media {self.input_media} is a local file, audio download will be skipped")
return [await self.prep_single_track()]
elif self.input_media is not None and self._is_url(self.input_media):
# URL provided - download directly via flacfetch
self.logger.info(f"Input media {self.input_media} is a URL, downloading via flacfetch...")
# Extract video ID for metadata if it's a YouTube URL
video_id = None
youtube_patterns = [
r'(?:youtube\.com/watch\?v=|youtu\.be/)([a-zA-Z0-9_-]{11})',
r'youtube\.com/embed/([a-zA-Z0-9_-]{11})',
r'youtube\.com/v/([a-zA-Z0-9_-]{11})',
]
for pattern in youtube_patterns:
match = re.search(pattern, self.input_media)
if match:
video_id = match.group(1)
break
# Set up the extracted_info for metadata consistency
self.extracted_info = {
"title": f"{self.artist} - {self.title}" if self.artist and self.title else video_id or "Unknown",
"artist": self.artist or "",
"track_title": self.title or "",
"extractor_key": "youtube",
"id": video_id or self.input_media,
"url": self.input_media,
"source": "youtube",
}
self.extractor = "youtube"
self.url = self.input_media
# Mark that we need to use audio fetcher for URL download
self._use_audio_fetcher = True
self._use_url_download = True # New flag for URL-based download
return [await self.prep_single_track()]
elif self.artist and self.title:
# No input file provided - use flacfetch to search and download audio
self.logger.info(f"No input file provided, using flacfetch to search for: {self.artist} - {self.title}")
# Set up the extracted_info for metadata consistency
self.extracted_info = {
"title": f"{self.artist} - {self.title}",
@@ -1126,13 +1180,12 @@ class KaraokePrep:
}
self.extractor = "flacfetch"
self.url = None # URL will be determined by flacfetch
# Mark that we need to use audio fetcher for download
self._use_audio_fetcher = True
return [await self.prep_single_track()]
else:
raise ValueError(
"Either a local file path or both artist and title must be provided. "
"URL-based input has been replaced with flacfetch audio fetching."
"Either a local file path, a URL, or both artist and title must be provided."
)
Generated
+12 -12
View File
@@ -1772,14 +1772,14 @@ readthedocs = ["sphinx-rtd-theme"]
[[package]]
name = "fastapi"
version = "0.127.0"
version = "0.128.0"
description = "FastAPI framework, high performance, easy to learn, fast to code, ready for production"
optional = false
python-versions = ">=3.9"
groups = ["main"]
files = [
{file = "fastapi-0.127.0-py3-none-any.whl", hash = "sha256:725aa2bb904e2eff8031557cf4b9b77459bfedd63cae8427634744fd199f6a49"},
{file = "fastapi-0.127.0.tar.gz", hash = "sha256:5a9246e03dcd1fdb19f1396db30894867c1d630f5107dc167dcbc5ed1ea7d259"},
{file = "fastapi-0.128.0-py3-none-any.whl", hash = "sha256:aebd93f9716ee3b4f4fcfe13ffb7cf308d99c9f3ab5622d8877441072561582d"},
{file = "fastapi-0.128.0.tar.gz", hash = "sha256:1cc179e1cef10a6be60ffe429f79b829dce99d8de32d7acb7e6c8dfdf7f2645a"},
]
[package.dependencies]
@@ -1873,14 +1873,14 @@ platformdirs = ">=4.3.6"
[[package]]
name = "flacfetch"
version = "0.11.0"
version = "0.15.0"
description = "Search and download high-quality audio from multiple sources"
optional = false
python-versions = ">=3.8"
groups = ["main"]
files = [
{file = "flacfetch-0.11.0-py3-none-any.whl", hash = "sha256:56657cba85cf6e390b4213d3f44d9fd05603138735d01dd10ebc1f695b5f77f3"},
{file = "flacfetch-0.11.0.tar.gz", hash = "sha256:56bbc3a66f1241c831490a88c51fcbfd5d5026108daac9d41f5a23c66c1f79b3"},
{file = "flacfetch-0.15.0-py3-none-any.whl", hash = "sha256:fa2eb155dd92113d37952fe0df5587ce472c85d5c924574342955c0968e802a1"},
{file = "flacfetch-0.15.0.tar.gz", hash = "sha256:ff7ce633d89fae69c7c221a8244afdf56598bfbe2b1ae71e6584df424b9ca995"},
]
[package.dependencies]
@@ -1889,11 +1889,11 @@ transmission-rpc = ">=7.0.0"
yt-dlp = ">=2023.0.0"
[package.extras]
all = ["fastapi (>=0.115.0)", "google-cloud-secret-manager (>=2.16.0)", "google-cloud-storage (>=2.13.0)", "httpx (>=0.28.0)", "mypy (>=1.0.0)", "pydantic (>=2.0.0)", "pytest (>=7.0.0)", "pytest-asyncio (>=0.23.0)", "pytest-cov (>=4.0.0)", "pytest-mock (>=3.10.0)", "ruff (>=0.1.0)", "spotipy (>=2.25.1)", "types-requests (>=2.28.0)", "uvicorn[standard] (>=0.24.0)", "yt-dlp-ejs (>=0.3.0)"]
all = ["fastapi (>=0.115.0)", "google-cloud-secret-manager (>=2.16.0)", "google-cloud-storage (>=2.13.0)", "httpx (>=0.28.0)", "mypy (>=1.0.0)", "playwright (>=1.40.0)", "pydantic (>=2.0.0)", "pytest (>=7.0.0)", "pytest-asyncio (>=0.23.0)", "pytest-cov (>=4.0.0)", "pytest-mock (>=3.10.0)", "ruff (>=0.1.0)", "spotipy (>=2.25.1)", "types-requests (>=2.28.0)", "uvicorn[standard] (>=0.24.0)", "yt-dlp-ejs (>=0.3.0)"]
api = ["fastapi (>=0.115.0)", "google-cloud-secret-manager (>=2.16.0)", "google-cloud-storage (>=2.13.0)", "httpx (>=0.28.0)", "pydantic (>=2.0.0)", "uvicorn[standard] (>=0.24.0)"]
dev = ["mypy (>=1.0.0)", "pytest (>=7.0.0)", "pytest-asyncio (>=0.23.0)", "pytest-cov (>=4.0.0)", "pytest-mock (>=3.10.0)", "ruff (>=0.1.0)", "types-requests (>=2.28.0)"]
ejs = ["yt-dlp-ejs (>=0.3.0)"]
remote = ["httpx (>=0.28.0)"]
remote = ["httpx (>=0.28.0)", "playwright (>=1.40.0)"]
spotify = ["spotipy (>=2.25.1)"]
[[package]]
@@ -5570,14 +5570,14 @@ win-unicode-console = {version = "*", markers = "platform_system == \"Windows\"
[[package]]
name = "pdfminer-six"
version = "20251107"
version = "20251227"
description = "PDF parser and analyzer"
optional = false
python-versions = ">=3.9"
python-versions = ">=3.10"
groups = ["main"]
files = [
{file = "pdfminer_six-20251107-py3-none-any.whl", hash = "sha256:c09df33e4cbe6b26b2a79248a4ffcccafaa5c5d39c9fff0e6e81567f165b5401"},
{file = "pdfminer_six-20251107.tar.gz", hash = "sha256:5fb0c553799c591777f22c0c72b77fc2522d7d10c70654e25f4c5f1fd996e008"},
{file = "pdfminer_six-20251227-py3-none-any.whl", hash = "sha256:7a41f668a74fdde9e3a6c9265e400992a8312224fb6dbc13ee8861111e330d36"},
{file = "pdfminer_six-20251227.tar.gz", hash = "sha256:1d98781cf884d7274c694e742a94ffe6326ee11ee7ea79e73e1e75c4c74d91eb"},
]
[package.dependencies]
+1 -1
View File
@@ -1,6 +1,6 @@
[tool.poetry]
name = "karaoke-gen"
version = "0.76.14"
version = "0.76.17"
description = "Generate karaoke videos with synchronized lyrics. Handles the entire process from downloading audio and lyrics to creating the final video with title screens."
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
license = "MIT"
+110 -2
View File
@@ -566,7 +566,115 @@ class TestAsync:
basic_karaoke_gen.input_media = None # Not a file
basic_karaoke_gen.artist = "Test Artist"
basic_karaoke_gen.title = None # Missing title
# Test that ValueError is raised
with pytest.raises(ValueError, match="Either a local file path or both artist and title must be provided"):
with pytest.raises(ValueError, match="Either a local file path.*or both artist and title must be provided"):
await basic_karaoke_gen.process()
@pytest.mark.asyncio
async def test_process_with_youtube_url(self, basic_karaoke_gen):
"""Test processing with a YouTube URL as input."""
# Setup - YouTube URL input
basic_karaoke_gen.input_media = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
basic_karaoke_gen.artist = "Rick Astley"
basic_karaoke_gen.title = "Never Gonna Give You Up"
# Mock dependencies
with patch.object(basic_karaoke_gen, 'prep_single_track', new_callable=AsyncMock) as mock_prep_single_track:
mock_prep_single_track.return_value = {"track": "result"}
result = await basic_karaoke_gen.process()
# Verify prep_single_track was called
mock_prep_single_track.assert_called_once()
# Verify URL mode was enabled
assert basic_karaoke_gen._use_audio_fetcher is True
assert basic_karaoke_gen._use_url_download is True
assert basic_karaoke_gen.extractor == "youtube"
assert basic_karaoke_gen.url == "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
# Verify the result
assert len(result) == 1
assert result[0] == {"track": "result"}
@pytest.mark.asyncio
async def test_process_with_youtube_url_without_artist_title(self, basic_karaoke_gen):
"""Test processing with a YouTube URL but no artist/title."""
# Setup - YouTube URL input without artist/title
basic_karaoke_gen.input_media = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
basic_karaoke_gen.artist = None
basic_karaoke_gen.title = None
# Mock dependencies
with patch.object(basic_karaoke_gen, 'prep_single_track', new_callable=AsyncMock) as mock_prep_single_track:
mock_prep_single_track.return_value = {"track": "result"}
result = await basic_karaoke_gen.process()
# Verify URL mode was enabled
assert basic_karaoke_gen._use_audio_fetcher is True
assert basic_karaoke_gen._use_url_download is True
# Verify extracted info has video ID as fallback
assert basic_karaoke_gen.extracted_info["id"] == "dQw4w9WgXcQ"
@pytest.mark.asyncio
async def test_process_with_youtu_be_short_url(self, basic_karaoke_gen):
"""Test processing with a youtu.be short URL."""
# Setup - Short URL input
basic_karaoke_gen.input_media = "https://youtu.be/dQw4w9WgXcQ"
basic_karaoke_gen.artist = "Rick Astley"
basic_karaoke_gen.title = "Never Gonna Give You Up"
# Mock dependencies
with patch.object(basic_karaoke_gen, 'prep_single_track', new_callable=AsyncMock) as mock_prep_single_track:
mock_prep_single_track.return_value = {"track": "result"}
result = await basic_karaoke_gen.process()
# Verify URL mode was enabled
assert basic_karaoke_gen._use_audio_fetcher is True
assert basic_karaoke_gen._use_url_download is True
assert basic_karaoke_gen.url == "https://youtu.be/dQw4w9WgXcQ"
class TestKaraokePrepIsUrlHelper:
"""Tests for KaraokePrep._is_url() helper method."""
@pytest.fixture
def karaoke_prep(self, tmp_path):
"""Create a KaraokePrep instance for testing."""
from karaoke_gen.karaoke_gen import KaraokePrep
return KaraokePrep(
artist="Test",
title="Test",
output_dir=str(tmp_path),
)
def test_is_url_http(self, karaoke_prep):
"""Test _is_url detects http URLs."""
assert karaoke_prep._is_url("http://example.com") is True
def test_is_url_https(self, karaoke_prep):
"""Test _is_url detects https URLs."""
assert karaoke_prep._is_url("https://www.youtube.com/watch?v=abc") is True
def test_is_url_youtube(self, karaoke_prep):
"""Test _is_url detects YouTube URLs."""
assert karaoke_prep._is_url("https://www.youtube.com/watch?v=dQw4w9WgXcQ") is True
assert karaoke_prep._is_url("https://youtu.be/dQw4w9WgXcQ") is True
def test_is_url_local_path(self, karaoke_prep):
"""Test _is_url returns False for local paths."""
assert karaoke_prep._is_url("/path/to/file.mp3") is False
assert karaoke_prep._is_url("./relative/path.wav") is False
assert karaoke_prep._is_url("file.flac") is False
def test_is_url_none(self, karaoke_prep):
"""Test _is_url returns False for None."""
assert karaoke_prep._is_url(None) is False
def test_is_url_empty_string(self, karaoke_prep):
"""Test _is_url returns False for empty string."""
assert karaoke_prep._is_url("") is False
+250
View File
@@ -2381,3 +2381,253 @@ class TestCreateAudioFetcherRemoteSelection:
assert isinstance(fetcher, RemoteFlacFetchAudioFetcher)
assert fetcher.api_url == "http://arg:9090"
assert fetcher.api_key == "arg_key"
class TestFlacFetchAudioFetcherDownloadFromUrl:
"""Tests for FlacFetchAudioFetcher.download_from_url() method."""
@pytest.fixture
def mock_logger(self):
"""Create a mock logger."""
return MagicMock(spec=logging.Logger)
@pytest.fixture
def fetcher(self, mock_logger):
"""Create a FlacFetchAudioFetcher instance with mocked dependencies."""
return FlacFetchAudioFetcher(logger=mock_logger)
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_youtube_success(self, mock_get_manager, fetcher, tmp_path):
"""Test successful download from YouTube URL."""
mock_manager = MagicMock()
downloaded_path = str(tmp_path / "song.webm")
mock_manager.download_by_id.return_value = downloaded_path
mock_get_manager.return_value = mock_manager
result = fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(tmp_path),
output_filename="Rick Astley - Never Gonna Give You Up",
artist="Rick Astley",
title="Never Gonna Give You Up",
)
assert result.filepath == downloaded_path
assert result.artist == "Rick Astley"
assert result.title == "Never Gonna Give You Up"
assert result.provider == "YouTube"
mock_manager.download_by_id.assert_called_once_with(
source_name="YouTube",
source_id="dQw4w9WgXcQ",
output_path=str(tmp_path),
output_filename="Rick Astley - Never Gonna Give You Up",
download_url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
)
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_youtu_be_short_url(self, mock_get_manager, fetcher, tmp_path):
"""Test download from youtu.be short URL."""
mock_manager = MagicMock()
downloaded_path = str(tmp_path / "song.webm")
mock_manager.download_by_id.return_value = downloaded_path
mock_get_manager.return_value = mock_manager
result = fetcher.download_from_url(
url="https://youtu.be/dQw4w9WgXcQ",
output_dir=str(tmp_path),
)
assert result.filepath == downloaded_path
assert result.provider == "YouTube"
# Should extract video ID from short URL
mock_manager.download_by_id.assert_called_once()
call_args = mock_manager.download_by_id.call_args
assert call_args.kwargs["source_id"] == "dQw4w9WgXcQ"
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_embed_url(self, mock_get_manager, fetcher, tmp_path):
"""Test download from YouTube embed URL."""
mock_manager = MagicMock()
downloaded_path = str(tmp_path / "song.webm")
mock_manager.download_by_id.return_value = downloaded_path
mock_get_manager.return_value = mock_manager
result = fetcher.download_from_url(
url="https://www.youtube.com/embed/dQw4w9WgXcQ",
output_dir=str(tmp_path),
)
call_args = mock_manager.download_by_id.call_args
assert call_args.kwargs["source_id"] == "dQw4w9WgXcQ"
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_generates_filename_from_artist_title(self, mock_get_manager, fetcher, tmp_path):
"""Test that filename is generated from artist and title if not provided."""
mock_manager = MagicMock()
downloaded_path = str(tmp_path / "song.webm")
mock_manager.download_by_id.return_value = downloaded_path
mock_get_manager.return_value = mock_manager
result = fetcher.download_from_url(
url="https://www.youtube.com/watch?v=test123test",
output_dir=str(tmp_path),
artist="Test Artist",
title="Test Song",
)
call_args = mock_manager.download_by_id.call_args
assert call_args.kwargs["output_filename"] == "Test Artist - Test Song"
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_generates_filename_from_video_id(self, mock_get_manager, fetcher, tmp_path):
"""Test that filename falls back to video ID if no artist/title."""
mock_manager = MagicMock()
downloaded_path = str(tmp_path / "song.webm")
mock_manager.download_by_id.return_value = downloaded_path
mock_get_manager.return_value = mock_manager
result = fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(tmp_path),
)
call_args = mock_manager.download_by_id.call_args
assert call_args.kwargs["output_filename"] == "dQw4w9WgXcQ"
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_raises_download_error_on_failure(self, mock_get_manager, fetcher, tmp_path):
"""Test that DownloadError is raised when download fails."""
mock_manager = MagicMock()
mock_manager.download_by_id.side_effect = Exception("Download failed")
mock_get_manager.return_value = mock_manager
with pytest.raises(DownloadError) as exc_info:
fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(tmp_path),
)
assert "Failed to download from URL" in str(exc_info.value)
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_raises_on_empty_filepath(self, mock_get_manager, fetcher, tmp_path):
"""Test that DownloadError is raised when download returns no filepath."""
mock_manager = MagicMock()
mock_manager.download_by_id.return_value = None
mock_get_manager.return_value = mock_manager
with pytest.raises(DownloadError) as exc_info:
fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(tmp_path),
)
assert "Download returned no file path" in str(exc_info.value)
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_creates_output_dir(self, mock_get_manager, fetcher, tmp_path):
"""Test that output directory is created if it doesn't exist."""
mock_manager = MagicMock()
mock_manager.download_by_id.return_value = str(tmp_path / "song.webm")
mock_get_manager.return_value = mock_manager
new_dir = tmp_path / "new_subdir"
assert not new_dir.exists()
fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(new_dir),
)
assert new_dir.exists()
class TestRemoteFlacFetchAudioFetcherDownloadFromUrl:
"""Tests for RemoteFlacFetchAudioFetcher.download_from_url() method."""
@pytest.fixture
def mock_logger(self):
"""Create a mock logger."""
return MagicMock(spec=logging.Logger)
@pytest.fixture
def fetcher(self, mock_logger):
"""Create a RemoteFlacFetchAudioFetcher instance."""
return RemoteFlacFetchAudioFetcher(
api_url="http://localhost:8080",
api_key="test_key",
logger=mock_logger,
)
@patch('flacfetch.core.manager.FetchManager')
@patch('flacfetch.providers.youtube.YoutubeProvider')
@patch('flacfetch.downloaders.youtube.YoutubeDownloader')
def test_download_from_url_uses_local_flacfetch(
self, mock_yt_downloader, mock_yt_provider, mock_fetch_manager, fetcher, tmp_path
):
"""Test that download_from_url uses local flacfetch (not remote API)."""
mock_manager_instance = MagicMock()
downloaded_path = str(tmp_path / "song.webm")
mock_manager_instance.download_by_id.return_value = downloaded_path
mock_fetch_manager.return_value = mock_manager_instance
result = fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(tmp_path),
artist="Rick Astley",
title="Never Gonna Give You Up",
)
# Verify local flacfetch was used
mock_fetch_manager.assert_called_once()
mock_manager_instance.add_provider.assert_called_once()
mock_manager_instance.register_downloader.assert_called_once_with("YouTube", mock_yt_downloader.return_value)
mock_manager_instance.download_by_id.assert_called_once()
assert result.filepath == downloaded_path
assert result.artist == "Rick Astley"
assert result.title == "Never Gonna Give You Up"
assert result.provider == "YouTube"
@patch('flacfetch.core.manager.FetchManager')
@patch('flacfetch.providers.youtube.YoutubeProvider')
@patch('flacfetch.downloaders.youtube.YoutubeDownloader')
def test_download_from_url_extracts_video_id(
self, mock_yt_downloader, mock_yt_provider, mock_fetch_manager, fetcher, tmp_path
):
"""Test that video ID is extracted from URL correctly."""
mock_manager_instance = MagicMock()
mock_manager_instance.download_by_id.return_value = str(tmp_path / "song.webm")
mock_fetch_manager.return_value = mock_manager_instance
fetcher.download_from_url(
url="https://www.youtube.com/watch?v=abc123xyz99",
output_dir=str(tmp_path),
)
call_args = mock_manager_instance.download_by_id.call_args
assert call_args.kwargs["source_id"] == "abc123xyz99"
@patch('flacfetch.core.manager.FetchManager')
@patch('flacfetch.providers.youtube.YoutubeProvider')
@patch('flacfetch.downloaders.youtube.YoutubeDownloader')
def test_download_from_url_raises_on_failure(
self, mock_yt_downloader, mock_yt_provider, mock_fetch_manager, fetcher, tmp_path
):
"""Test that DownloadError is raised on failure."""
mock_manager_instance = MagicMock()
mock_manager_instance.download_by_id.side_effect = Exception("Network error")
mock_fetch_manager.return_value = mock_manager_instance
with pytest.raises(DownloadError) as exc_info:
fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(tmp_path),
)
assert "Failed to download from URL" in str(exc_info.value)
def test_download_from_url_method_exists(self, fetcher):
"""Test that download_from_url method exists on RemoteFlacFetchAudioFetcher."""
assert hasattr(fetcher, 'download_from_url')
assert callable(fetcher.download_from_url)