Files
karaoke-gen/backend/tests/api/test_confirm_duration.py
T
Andrew Beveridge ☄️andClaude Opus 5.5 c277bc8ee3 feat: local E2E stack (fake workers + test API) + email-code sign-in (v0.275.0) (#1165)
## Summary
- **Local stack:** `scripts/local-stack.sh up --frontend` runs the
**real** backend on the Firestore + GCS emulators with `FAKE_WORKERS=1`.
In-process fake workers move jobs through their real states in seconds,
using tiny real media, so web Playwright and mobile Maestro full-journey
tests can run offline against the same endpoints.
- **Email-code sign-in** (nomad-mobile spec §4.1): `POST
/api/users/auth/send-code` + `verify-code`. The code logic is shared
with kjbox via `LoginCodeStore`, and sends are throttled per email and
per IP.
- **Bug fix found by the new suite:** `confirm-duration` moved jobs to
`separating_stage1`, from which they can never reach review. It now
resumes at `downloading`. No prod job had taken this path yet.

## Changes
- `backend/services/fake_workers/`: dispatch seam in
`WorkerService.trigger_*`, plus fake search, download, checkout and
outbox, scenarios, and per-user config. The real stem upload, transcode,
backing-vocals analysis and preview render are reused. Every check is
strict (`is True`), so a MagicMock settings object never enables the
fakes, and startup **refuses FAKE_WORKERS in production**.
- `/api/test/*` router (mounted only in that mode): `status`,
`scenario`, `fake-config`, `emails`, `reset`, plus the fake Stripe
checkout page. Paying it runs the real webhook handler.
- `frontend/e2e/local-stack/` (9 specs, ~50s):
  - sign-up → make → review → instrumental → download
  - buy and cancel credits
  - duration confirm, failed, complete and review states
  - code sign-in and lockout
- New required CI job `local-stack-e2e`.
- Docs: TESTING.md § Local stack, DEVELOPMENT.md, API.md, README,
LESSONS-LEARNED.
- `MAX_SIGNUPS_PER_IP` can now be set by env. Only the local stack
raises it; production keeps the default of 2.

## Testing
- [x] Backend unit: 6175 passed
- [x] Emulator: 152 passed
- [x] Frontend Jest: 1706 passed
- [x] Local-stack Playwright: 9/9 passed (CI mode)

## Review
- [x] CodeRabbit CLI review completed locally. 1 minor finding declined:
it suggested swallowing infrastructure errors in send-code as fake
"sent" responses, which would hide real outages from users.

@coderabbitai ignore

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

https://claude.ai/code/session_01AGuxtgoscF6RJGtpjHPg3W

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 16:00:24 -04:00

499 lines
19 KiB
Python

"""
Tests for POST /api/jobs/{job_id}/confirm-duration endpoint.
Covers:
- reconcile path: deduct pending_additional_credits, trigger workers
- preflight path: deduct pending_additional_credits (delta, not full cost), trigger workers
- insufficient credits → 402
- acknowledged_credits mismatch → 409
- wrong status → 409
- not found / not owner → 404
"""
from __future__ import annotations
import asyncio
from datetime import datetime, UTC
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from backend.models.job import Job, JobStatus
from backend.services.auth_service import UserType, AuthResult
NOW = datetime.now(UTC)
@pytest.fixture(autouse=True)
def _ensure_auth_overrides():
"""Ensure auth dependency overrides are set for every test in this file.
Mirrors the pattern used by test_audio_edit_routes.py — necessary because
the conftest autouse fixture can be cleared by intervening tests when running
the whole suite with -k filters.
"""
from backend.api.dependencies import require_auth, require_admin
from backend.main import app
async def mock_require_auth():
return AuthResult(
is_valid=True,
user_type=UserType.ADMIN,
remaining_uses=999,
message="Test admin token",
is_admin=True,
user_email="test@example.com",
)
app.dependency_overrides[require_auth] = mock_require_auth
app.dependency_overrides[require_admin] = mock_require_auth
yield
app.dependency_overrides.pop(require_auth, None)
app.dependency_overrides.pop(require_admin, None)
def _job(**kwargs) -> Job:
defaults = dict(
job_id="job-dur-1",
created_at=NOW,
updated_at=NOW,
user_email="owner@example.com",
)
defaults.update(kwargs)
return Job(**defaults)
def _dur_confirm_job_reconcile(
job_id="job-dur-1",
credits_charged=2,
pending_additional_credits=1,
user_email="owner@example.com",
):
"""Job in AWAITING_DURATION_CONFIRM with reason=reconcile."""
return _job(
job_id=job_id,
status=JobStatus.AWAITING_DURATION_CONFIRM,
user_email=user_email,
state_data={
"duration_confirm_reason": "reconcile",
"credits_charged": credits_charged,
"pending_additional_credits": pending_additional_credits,
},
)
def _dur_confirm_job_preflight(
job_id="job-dur-2",
credits_charged=1,
pending_additional_credits=2,
user_email="owner@example.com",
):
"""Job in AWAITING_DURATION_CONFIRM with reason=preflight.
Delta model: create_job already charged 1 credit; the upload's duration
estimate (e.g. ~25 min / 3-credit song) means 2 MORE credits are owed.
pending_additional_credits=2, credits_charged=1, expected_total=3.
"""
return _job(
job_id=job_id,
status=JobStatus.AWAITING_DURATION_CONFIRM,
user_email=user_email,
state_data={
"duration_confirm_reason": "preflight",
"credits_charged": credits_charged,
"pending_additional_credits": pending_additional_credits,
},
)
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
@pytest.fixture
def mock_job_manager():
jm = MagicMock()
jm.get_job.return_value = _dur_confirm_job_reconcile()
jm.update_job.return_value = None
jm.update_state_data.return_value = None
jm.transition_to_state.return_value = True
return jm
@pytest.fixture
def mock_user_service():
us = MagicMock()
# Default: successful deduction
us.deduct_credits.return_value = (True, 10, "ok")
return us
@pytest.fixture
def mock_worker_service():
ws = MagicMock()
ws.trigger_audio_worker = AsyncMock(return_value=True)
ws.trigger_lyrics_worker = AsyncMock(return_value=True)
return ws
@pytest.fixture
def patched_app(mock_job_manager, mock_user_service, mock_worker_service):
"""Patch module-level singletons and yield the TestClient."""
from fastapi.testclient import TestClient
from backend.main import app
# get_user_service and get_worker_service are called at request-time inside the
# endpoint (not at module load), so we patch them where they are looked up:
# get_user_service at the jobs module scope, get_worker_service at the module level.
with (
patch("backend.api.routes.jobs.job_manager", mock_job_manager),
patch(
"backend.api.routes.jobs.get_user_service",
return_value=mock_user_service,
),
patch(
"backend.api.routes.jobs.get_worker_service",
return_value=mock_worker_service,
),
):
yield TestClient(app), mock_job_manager, mock_user_service, mock_worker_service
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _post(client, job_id, acknowledged_credits):
return client.post(
f"/api/jobs/{job_id}/confirm-duration",
json={"acknowledged_credits": acknowledged_credits},
)
# ---------------------------------------------------------------------------
# Test classes
# ---------------------------------------------------------------------------
class TestConfirmDurationReconcile:
"""Reconcile path: credits_charged=2, pending_additional_credits=1, owed=1, expected_total=3."""
def test_returns_200_and_deducts_one_credit(self, patched_app):
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_reconcile(
credits_charged=2, pending_additional_credits=1
)
resp = _post(client, "job-dur-1", acknowledged_credits=3)
assert resp.status_code == 200, resp.text
# Verify deduct was called with count_as_job_creation=False
us.deduct_credits.assert_called_once()
call_kwargs = us.deduct_credits.call_args
args, kwargs = call_kwargs
# Signature: deduct_credits(email, job_id, amount, reason, count_as_job_creation)
assert kwargs.get("amount", args[2] if len(args) > 2 else None) == 1
assert kwargs.get("reason", args[3] if len(args) > 3 else None) == "duration_confirm"
count_flag = kwargs.get(
"count_as_job_creation", args[4] if len(args) > 4 else True
)
assert count_flag is False, "reconcile top-up must NOT increment total_jobs_created"
def test_state_data_updated_after_confirm(self, patched_app):
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_reconcile(
credits_charged=2, pending_additional_credits=1
)
resp = _post(client, "job-dur-1", acknowledged_credits=3)
assert resp.status_code == 200, resp.text
# update_job must have been called to persist state changes
jm.update_job.assert_called()
def test_returns_job_in_response(self, patched_app):
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_reconcile()
resp = _post(client, "job-dur-1", acknowledged_credits=3)
assert resp.status_code == 200, resp.text
body = resp.json()
assert "job_id" in body
def test_insufficient_credits_returns_402(self, patched_app):
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_reconcile(
credits_charged=2, pending_additional_credits=1
)
us.deduct_credits.return_value = (False, 0, "Insufficient credits")
resp = _post(client, "job-dur-1", acknowledged_credits=3)
assert resp.status_code == 402, resp.text
def test_acknowledged_mismatch_returns_409(self, patched_app):
"""Client sends acknowledged_credits=99, expected is 3 → 409."""
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_reconcile(
credits_charged=2, pending_additional_credits=1
)
resp = _post(client, "job-dur-1", acknowledged_credits=99)
assert resp.status_code == 409, resp.text
# No credits should be deducted
us.deduct_credits.assert_not_called()
def test_workers_triggered_on_success(self, patched_app):
"""Verify that create_task was called (worker trigger fired)."""
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_reconcile()
with patch("backend.api.routes.jobs.asyncio.create_task") as mock_ct:
mock_ct.return_value = MagicMock()
resp = _post(client, "job-dur-1", acknowledged_credits=3)
assert resp.status_code == 200, resp.text
mock_ct.assert_called_once()
def test_workers_not_triggered_on_credit_failure(self, patched_app):
"""If deduction fails, workers must NOT be triggered."""
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_reconcile()
us.deduct_credits.return_value = (False, 0, "Insufficient credits")
with patch("backend.api.routes.jobs.asyncio.create_task") as mock_ct:
resp = _post(client, "job-dur-1", acknowledged_credits=3)
assert resp.status_code == 402
mock_ct.assert_not_called()
class TestConfirmDurationPreflight:
"""Preflight path (delta model): credits_charged=1, pending_additional_credits=2, owed=2, expected_total=3.
create_job charged 1 credit at upload time; the pause-creating site computed
pending_additional_credits=2 as the delta for a ~25-min (3-credit) song.
confirm_duration deducts ONLY the delta (2), bringing credits_charged to 3.
"""
def test_preflight_returns_200(self, patched_app):
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_preflight(
credits_charged=1, pending_additional_credits=2
)
resp = _post(client, "job-dur-2", acknowledged_credits=3)
assert resp.status_code == 200, resp.text
def test_preflight_deducts_delta_only(self, patched_app):
"""Delta model: only deduct pending_additional_credits (2), not the full cost (3)."""
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_preflight(
credits_charged=1, pending_additional_credits=2
)
resp = _post(client, "job-dur-2", acknowledged_credits=3)
assert resp.status_code == 200, resp.text
us.deduct_credits.assert_called_once()
call_args, call_kwargs = us.deduct_credits.call_args
amount = call_kwargs.get("amount", call_args[2] if len(call_args) > 2 else None)
assert amount == 2, (
"preflight must deduct the delta (pending_additional_credits=2), "
"not the full cost — create_job already charged 1 credit"
)
def test_preflight_count_as_job_creation_false(self, patched_app):
"""Preflight uses count_as_job_creation=False (job was already counted at creation)."""
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_preflight(
credits_charged=1, pending_additional_credits=2
)
resp = _post(client, "job-dur-2", acknowledged_credits=3)
assert resp.status_code == 200, resp.text
call_args, call_kwargs = us.deduct_credits.call_args
count_flag = call_kwargs.get(
"count_as_job_creation",
call_args[4] if len(call_args) > 4 else True,
)
assert count_flag is False, (
"preflight: job already counted at create_job() credit deduction; "
"do not double-count total_jobs_created"
)
def test_preflight_credits_charged_becomes_total(self, patched_app):
"""After confirm, credits_charged should be updated to credits_charged + owed = 3."""
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_preflight(
credits_charged=1, pending_additional_credits=2
)
resp = _post(client, "job-dur-2", acknowledged_credits=3)
assert resp.status_code == 200, resp.text
jm.update_job.assert_called()
update_call_args = jm.update_job.call_args
update_dict = update_call_args[0][1] if update_call_args[0] else update_call_args[1]
assert update_dict.get("state_data.credits_charged") == 3, (
"credits_charged must be updated to 1 (prior) + 2 (owed) = 3"
)
def test_preflight_insufficient_credits_returns_402(self, patched_app):
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_preflight(
credits_charged=1, pending_additional_credits=2
)
us.deduct_credits.return_value = (False, 1, "Insufficient credits")
resp = _post(client, "job-dur-2", acknowledged_credits=3)
assert resp.status_code == 402, resp.text
def test_preflight_mismatch_returns_409(self, patched_app):
"""acknowledged_credits=5 but expected 3 (1+2) → 409."""
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_preflight(
credits_charged=1, pending_additional_credits=2
)
resp = _post(client, "job-dur-2", acknowledged_credits=5)
assert resp.status_code == 409, resp.text
def test_preflight_workers_triggered(self, patched_app):
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_preflight(
credits_charged=1, pending_additional_credits=2
)
with patch("backend.api.routes.jobs.asyncio.create_task") as mock_ct:
mock_ct.return_value = MagicMock()
resp = _post(client, "job-dur-2", acknowledged_credits=3)
assert resp.status_code == 200, resp.text
mock_ct.assert_called_once()
class TestConfirmDurationIdempotency:
"""Duplicate-confirm and owed=0 paths."""
def test_duplicate_confirm_is_rejected(self, patched_app):
"""A second confirm POST must 409 because the job is no longer AWAITING_DURATION_CONFIRM.
After a successful confirm the endpoint calls transition_to_state →
DOWNLOADING. The mock default returns True from transition_to_state;
what matters is that the endpoint *issued* the transition so that a
subsequent request (now seeing a different status) would be rejected.
We verify this by asserting that transition_to_state was called with
new_status=DOWNLOADING on the first confirm — the status guard at
step 2 would reject a second request once the real Firestore document
reflects the new status.
"""
client, jm, us, ws = patched_app
jm.get_job.return_value = _dur_confirm_job_reconcile(
credits_charged=2, pending_additional_credits=1
)
resp = _post(client, "job-dur-1", acknowledged_credits=3)
assert resp.status_code == 200, resp.text
# Verify the endpoint transitioned the job out of AWAITING_DURATION_CONFIRM
# so a duplicate confirm would hit the status guard (409).
jm.transition_to_state.assert_called_once()
call_kwargs = jm.transition_to_state.call_args[1]
assert call_kwargs.get("new_status") == JobStatus.DOWNLOADING, (
"confirm_duration must transition to DOWNLOADING before triggering "
"workers so a duplicate POST is rejected by the status guard"
)
def test_owed_zero_path(self, patched_app):
"""Job with pending_additional_credits=0: 200, deduct_credits NOT called,
duration_confirmed set, workers triggered, status transitioned out."""
client, jm, us, ws = patched_app
jm.get_job.return_value = _job(
job_id="job-zero",
status=JobStatus.AWAITING_DURATION_CONFIRM,
user_email="owner@example.com",
state_data={
"duration_confirm_reason": "reconcile",
"credits_charged": 3,
"pending_additional_credits": 0,
},
)
with patch("backend.api.routes.jobs.asyncio.create_task") as mock_ct:
mock_ct.return_value = MagicMock()
resp = _post(client, "job-zero", acknowledged_credits=3)
assert resp.status_code == 200, resp.text
# No credits should be deducted when owed == 0
us.deduct_credits.assert_not_called()
# State must still be persisted and workers triggered
jm.update_job.assert_called()
mock_ct.assert_called_once()
# Job must still be transitioned out of the pause state
jm.transition_to_state.assert_called_once()
call_kwargs = jm.transition_to_state.call_args[1]
assert call_kwargs.get("new_status") == JobStatus.DOWNLOADING
class TestConfirmDurationErrorCases:
"""Status guard, not-found, and not-owner cases."""
def test_wrong_status_returns_409(self, patched_app):
"""Job not in AWAITING_DURATION_CONFIRM → 409."""
client, jm, us, ws = patched_app
jm.get_job.return_value = _job(
job_id="done-job",
status=JobStatus.COMPLETE,
user_email="owner@example.com",
state_data={},
)
resp = _post(client, "done-job", acknowledged_credits=1)
assert resp.status_code == 409, resp.text
def test_job_not_found_returns_404(self, patched_app):
client, jm, us, ws = patched_app
jm.get_job.return_value = None
resp = _post(client, "nonexistent", acknowledged_credits=1)
assert resp.status_code == 404, resp.text
def test_not_owner_returns_404(self, patched_app):
"""Job owned by different user (non-admin) should 404 to avoid leaking job existence."""
client, jm, us, ws = patched_app
# The conftest mock_auth_dependency sets user_email="test@example.com" and is_admin=True.
# To test the non-owner path we need to override auth to return a non-admin user.
from backend.api.dependencies import require_auth
from backend.main import app
async def non_owner_auth():
return AuthResult(
is_valid=True,
user_type=UserType.STRIPE,
remaining_uses=5,
message="regular user",
is_admin=False,
user_email="other@example.com", # different from owner@example.com
)
jm.get_job.return_value = _dur_confirm_job_reconcile(user_email="owner@example.com")
app.dependency_overrides[require_auth] = non_owner_auth
try:
resp = _post(client, "job-dur-1", acknowledged_credits=3)
finally:
app.dependency_overrides.pop(require_auth, None)
assert resp.status_code == 404, resp.text