Files
karaoke-gen/backend/tests/api/test_upload_preflight.py
T
Andrew Beveridge ☄️andClaude Opus 4.8 7cf42c8983 feat(pricing): duration-based credit pricing + long-duration input handling (#799)
@coderabbitai ignore

## Summary

Implements **duration-based credit pricing** for karaoke jobs, replacing
the flat 1-credit/job model. Credits now scale with input audio length
(`ceil(min/10)`, hard-block >60 min), with cost confirmed before heavy
processing and reconciled against the actual measured audio. Closes out
the planned fix from the 2026-06-04 long-duration mashup incident.

- Design:
`docs/archive/2026-06-04-long-duration-input-handling-design.md`
- Plan: `docs/archive/2026-06-04-long-duration-input-handling-plan.md`

## Pricing model

`credits = max(1, ceil(minutes / 10))` — 1 credit ≤10 min, 2 ≤20 min … 6
≤60 min. Inputs over 60 min are blocked. Single source of truth in
`backend/services/pricing.py`, mirrored in `frontend/lib/pricing.ts`.

## How it works

- **`state_data.credits_charged`** is the authoritative running total.
`create_job` charges the base (URL = full N via `acknowledged_credits`;
search/upload = 1); top-ups deduct only the **delta**
(`count_as_job_creation=False`).
- **Reconciliation engine** (`duration_reconciliation.py`) runs at the
single convergence point before separation+transcription in all three
processing paths (download / post-edit / `start_job_processing`),
exactly once each. It measures the audio actually sent to AudioShake
(post-edit if trimmed):
  - actual < estimate → auto-refund the difference
- actual > estimate → pause at new **`AWAITING_DURATION_CONFIRM`**
state, user re-confirms (inline Buy Credits)
  - actual > 60 min → refund everything + cancel
- **`POST /api/jobs/{id}/confirm-duration`** (+ `POST
/api/jobs/estimate`) reuse the review-stage pause/resume.
- **Notifications**: in-browser + push, 15-min idle email, 24h reminder,
**48h auto-cancel + full refund** — via the existing idle-reminder /
stale-job machinery.
- **Admin / made-for-you jobs bypass charging entirely**
(`payment_bypassed` → reconcile short-circuits).

## Endpoints

| Method | Path | Purpose |
|---|---|---|
| `POST` | `/api/jobs/estimate` | URL duration probe →
`{duration_seconds, credits, blocked, source}` |
| `POST` | `/api/jobs/{id}/confirm-duration` | confirm cost / resume
(402 insufficient, 409 mismatch) |
| `POST` | `/api/jobs` | URL create gains optional `duration_seconds`,
`acknowledged_credits` |
| `POST` | `/api/audio-search/{id}/select` | gains optional
`acknowledged_credits` |

## Frontend

`DurationCostConfirm` modal (Radix Dialog) surfaced via a "Confirm Cost"
action on `awaiting_duration_confirm` jobs (`useDurationConfirm` hook),
per-result cost chips on audio search, `pricing` i18n namespace in all
33 locales.

## Testing

- Backend: **3358 passing** (incl. boundary tables, money-math
conservation, reconcile up/down/over-limit, confirm 402/409, idempotency
guards, stale-expiry refund, an end-to-end integration test). Frontend:
**960 passing**. i18n completeness clean.
- Reviewed locally: per-task spec + quality review, a holistic
cross-cutting review, `/test-review`, and two CodeRabbit fix cycles.

## Notable bugs caught & fixed during review

- **Multi-credit under-refund**: `_refund_credit_for_job` refunded a
flat 1 — now refunds `credits_charged` (user-cancel of a long job).
- **Admin-bypass trap**: admin jobs would pause at the confirm gate and
be charged real credits — now short-circuited.
- Over-limit double-refund (suppress `cancel_job`'s built-in 1-credit
refund); over-limit email used the wrong "48h" message;
double-confirm/double-select idempotency guards.
- Hit the summary-projection gotcha (added fields to both projection
lists + regression test).

## Known residuals / follow-ups (non-blocking)

- **URL pre-flight cost-confirm is inert** until flacfetch's
`/check-youtube` returns a `duration` key — currently falls back to a
1-credit hold + post-download reconcile (functionally correct). Separate
flacfetch change.
- Duration-confirm email **bodies** are English (URLs are
locale-prefixed; backend i18n covers en/es/de) — `# TODO(i18n)` left.
- Concurrent confirm/select **double-charge TOCTOU** is an accepted
low-probability residual (atomic claim would need a Firestore
transaction); the status-transition guard handles the realistic
double-click case.

## Deploy note

Reuses the existing Cloud Tasks idle-reminder queue and the hourly
stale-review Cloud Scheduler (status-list / action-type expansions only)
— no new infra expected, but worth confirming `pulumi up` is a no-op on
deploy.

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

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-07 18:39:06 -04:00

210 lines
8.4 KiB
Python

"""
Tests for duration reconciliation wired into start_job_processing (Task 12).
Covers:
- start_job_processing pauses when reconcile_and_maybe_pause returns True
→ workers are NOT triggered
- start_job_processing proceeds when reconcile_and_maybe_pause returns False
→ both workers ARE triggered
- reconcile is called AFTER input_media_gcs_path is set (upload's exact file)
- reconcile is bypassed for existing_instrumental jobs (they only trigger lyrics worker)
— reconcile is still called but the no-instrumental-present path is covered
"""
from __future__ import annotations
import asyncio
from datetime import datetime, UTC
from unittest.mock import AsyncMock, MagicMock, Mock, patch, call
import pytest
import sys
# Prevent real Firestore/GCS imports from being resolved at module load.
sys.modules.setdefault("google.cloud.firestore", MagicMock())
sys.modules.setdefault("google.cloud.storage", MagicMock())
from backend.models.job import Job, JobStatus
from backend.exceptions import InvalidStateTransitionError
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
NOW = datetime.now(UTC)
def _pending_job(
job_id="upload-job-1",
gcs_path="uploads/upload-job-1/audio/song.flac",
existing_instrumental=None,
):
return Job(
job_id=job_id,
status=JobStatus.PENDING,
created_at=NOW,
updated_at=NOW,
input_media_gcs_path=gcs_path,
existing_instrumental_gcs_path=existing_instrumental,
state_data={"credits_charged": 1},
)
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
@pytest.fixture()
def mock_firestore():
with patch("backend.services.job_manager.FirestoreService") as cls:
svc = Mock()
# get_job returns a PENDING job with input_media_gcs_path set
svc.get_job.return_value = _pending_job()
cls.return_value = svc
yield svc
@pytest.fixture()
def job_manager(mock_firestore):
from backend.services.job_manager import JobManager
return JobManager()
@pytest.fixture()
def mock_worker_service():
ws = Mock()
ws.trigger_audio_worker = AsyncMock(return_value=True)
ws.trigger_lyrics_worker = AsyncMock(return_value=True)
return ws
# ---------------------------------------------------------------------------
# Tests: reconcile wired into start_job_processing
# ---------------------------------------------------------------------------
class TestStartJobProcessingReconcile:
"""Duration reconciliation is invoked from start_job_processing."""
@pytest.mark.asyncio
async def test_reconcile_called_before_workers(self, job_manager, mock_firestore, mock_worker_service):
"""reconcile_and_maybe_pause is called before triggering workers."""
mock_reconcile = AsyncMock(return_value=False)
with (
patch("backend.services.worker_service.get_worker_service", return_value=mock_worker_service),
patch("backend.services.job_manager.reconcile_and_maybe_pause", mock_reconcile, create=True),
):
await job_manager.start_job_processing("upload-job-1")
mock_reconcile.assert_awaited_once_with("upload-job-1")
@pytest.mark.asyncio
async def test_workers_not_triggered_when_reconcile_pauses(
self, job_manager, mock_firestore, mock_worker_service
):
"""When reconcile_and_maybe_pause returns True, no workers must be started."""
mock_reconcile = AsyncMock(return_value=True)
with (
patch("backend.services.worker_service.get_worker_service", return_value=mock_worker_service),
patch("backend.services.job_manager.reconcile_and_maybe_pause", mock_reconcile, create=True),
):
await job_manager.start_job_processing("upload-job-1")
mock_worker_service.trigger_audio_worker.assert_not_called()
mock_worker_service.trigger_lyrics_worker.assert_not_called()
@pytest.mark.asyncio
async def test_both_workers_triggered_when_reconcile_proceeds(
self, job_manager, mock_firestore, mock_worker_service
):
"""When reconcile_and_maybe_pause returns False, both workers ARE triggered."""
mock_reconcile = AsyncMock(return_value=False)
with (
patch("backend.services.worker_service.get_worker_service", return_value=mock_worker_service),
patch("backend.services.job_manager.reconcile_and_maybe_pause", mock_reconcile, create=True),
):
await job_manager.start_job_processing("upload-job-1")
mock_worker_service.trigger_audio_worker.assert_awaited_once_with("upload-job-1")
mock_worker_service.trigger_lyrics_worker.assert_awaited_once_with("upload-job-1")
@pytest.mark.asyncio
async def test_reconcile_is_called_after_transition_to_downloading(
self, job_manager, mock_firestore, mock_worker_service
):
"""Reconcile must run AFTER the job has transitioned to DOWNLOADING.
input_media_gcs_path is already set by the upload handler before
start_job_processing is called; the job is transitioned to DOWNLOADING
first, then reconcile probes it. This test confirms the ordering by
checking that transition_to_state is called before reconcile.
"""
call_log: list[str] = []
orig_transition = job_manager.transition_to_state
def tracking_transition(*a, **kw):
call_log.append("transition")
return orig_transition(*a, **kw)
async def tracking_reconcile(job_id):
call_log.append("reconcile")
return False
with (
patch("backend.services.worker_service.get_worker_service", return_value=mock_worker_service),
patch("backend.services.job_manager.reconcile_and_maybe_pause", side_effect=tracking_reconcile, create=True),
patch.object(job_manager, "transition_to_state", side_effect=tracking_transition),
):
await job_manager.start_job_processing("upload-job-1")
assert call_log.index("transition") < call_log.index("reconcile"), (
"transition_to_state must precede reconcile_and_maybe_pause; "
f"got order: {call_log}"
)
@pytest.mark.asyncio
async def test_existing_instrumental_job_reconcile_then_lyrics_only(
self, job_manager, mock_firestore, mock_worker_service
):
"""Jobs with existing_instrumental_gcs_path: reconcile is still called,
then only lyrics worker is triggered (audio separation is skipped)."""
mock_firestore.get_job.return_value = _pending_job(
existing_instrumental="uploads/upload-job-1/audio/existing_instrumental.mp3"
)
mock_reconcile = AsyncMock(return_value=False)
with (
patch("backend.services.worker_service.get_worker_service", return_value=mock_worker_service),
patch("backend.services.job_manager.reconcile_and_maybe_pause", mock_reconcile, create=True),
):
await job_manager.start_job_processing("upload-job-1")
# Reconcile must still be called
mock_reconcile.assert_awaited_once_with("upload-job-1")
# Audio worker must NOT be called (separation skipped)
mock_worker_service.trigger_audio_worker.assert_not_called()
# Lyrics worker IS called
mock_worker_service.trigger_lyrics_worker.assert_awaited_once_with("upload-job-1")
@pytest.mark.asyncio
async def test_existing_instrumental_paused_by_reconcile(
self, job_manager, mock_firestore, mock_worker_service
):
"""When reconcile returns True for existing-instrumental job,
even the lyrics worker is NOT triggered."""
mock_firestore.get_job.return_value = _pending_job(
existing_instrumental="uploads/upload-job-1/audio/existing_instrumental.mp3"
)
mock_reconcile = AsyncMock(return_value=True)
with (
patch("backend.services.worker_service.get_worker_service", return_value=mock_worker_service),
patch("backend.services.job_manager.reconcile_and_maybe_pause", mock_reconcile, create=True),
):
await job_manager.start_job_processing("upload-job-1")
mock_worker_service.trigger_audio_worker.assert_not_called()
mock_worker_service.trigger_lyrics_worker.assert_not_called()