## Summary
Fixes `credits_charged` being wiped from `state_data`, which made
duration confirm, refunds and the web cost dialog disagree. Mobile E2E
against the local stack found it: a 15-minute track showed "1 credit in
total (1 more)", and confirming the true total was rejected with 409.
**Root cause:** `create_job` debits the credit and records
`state_data.credits_charged`. But create-from-search, audio-search
create, made-for-you orders (which also lost `payment_bypassed`) and the
admin source override then replaced the whole `state_data` map, deleting
the field. Readers filled the gap with different defaults:
- reconcile: 1
- confirm-duration: 0
- 48h expiry: 0
**Fix**
- **Writers:** per-key `state_data.<key>` updates instead of whole-map
writes. Retry-from-scratch keeps the billing keys. Reconcile records
`credits_charged` when it pauses a job.
- **Readers:** one helper, `backend/services/job_credits.py`
(`get_credits_charged`, `is_payment_bypassed`). Every reader uses it:
reconcile, confirm-duration, `/select`, the bulk worker,
`_refund_credit_for_job` and the 48h expiry.
- For legacy jobs that lack the field, it returns what `create_job`
actually debited: 0 for bypassed, admin, tenant or no-email jobs,
otherwise 1.
- **`/select` and the bulk worker** never bill payment-bypassed jobs
(made-for-you, tenant, admin-created). This includes legacy ones whose
flag was wiped.
- **API responses:** `GET /api/jobs/{id}` and the job lists return the
resolved `credits_charged` when the stored field is missing. This is
response-only and never persisted, so the web and mobile clients
acknowledge exactly `credits_charged + pending`.
- **Confirm-duration** records the full total, so cancelling after a
confirm refunds it all. The 48h expiry refunds once:
`cancel_job(refund=False)` stops a second refund.
- **Admin source override** clears the old source's duration, selection
and confirm markers and keeps `credits_charged`. The next `/select` then
charges only the difference.
- **Web cost hook:** uses the server's resolved figure.
- Version 0.278.2.
## Testing
- New `test_credits_charged_consistency.py` covers:
- reconcile, then confirm with total 2, then cancel: 2 refunded, for new
and legacy jobs
- 409 on the old total
- refund amounts unchanged for jobs that have the field
- `/select` and the bulk worker never deducting on bypassed jobs
- the override followed by `/select` charging only the difference
- Stale-expiry legacy tests, and an emulator test for the dotted-path
merge (runs in CI).
- Backend unit tests: 6411 passed. Emulator integration tests run in CI;
locally they collided with a running local stack. Frontend jest: 1754
passed.
- An independent review found one Critical issue (`/select` billing
bypassed jobs) and one Important issue (web/server default mismatch).
Both are fixed and re-reviewed clean. Local CodeRabbit raised one minor,
which is fixed.
@coderabbitai ignore
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_011yLH4kWXMqjeKfZUG3ZiWZ
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Karaoke Generator Frontend
Next.js frontend for the Karaoke Generator application.
Quick Start
# Install dependencies
npm install
# Run development server
npm run dev
The app will be available at http://localhost:3000
Development Setup
Connecting to the Cloud Backend
In development, the frontend proxies API requests to the cloud backend (api.nomadkaraoke.com) via Next.js rewrites. This avoids CORS issues and allows local development without running a local backend.
The proxy is configured in next.config.mjs:
- All
/api/*requests are forwarded to the cloud backend - You can override the backend URL with
BACKEND_URLenvironment variable
Authentication
The backend requires authentication. To authenticate:
-
Create a
.env.localfile in the frontend directory:KARAOKE_ACCESS_TOKEN=your-token-here -
The token is automatically loaded by Playwright tests
-
In the browser, the token is stored in
localStorageaskaraoke_access_token
Testing
Unit Tests (Jest)
# Run tests in watch mode
npm test
# Run tests with coverage
npm run test:coverage
# Run tests in CI mode
npm run test:ci
E2E Tests (Playwright)
E2E tests run against the local dev server connected to the real cloud backend.
# Run all E2E tests
npm run test:e2e
# Run with UI (interactive mode)
npm run test:e2e:ui
# Run in headed mode (see the browser)
npm run test:e2e:headed
# Run in debug mode
npm run test:e2e:debug
Authentication for E2E Tests
E2E tests require a valid access token. Set it in .env.local:
KARAOKE_ACCESS_TOKEN=your-token-here
The Playwright config (playwright.config.ts) automatically loads this file.
Test Structure
Tests are located in e2e/:
karaoke-generation.spec.ts- Main test suite covering:- Homepage loading and form display
- Search tab functionality
- Audio search submission
- Audio selection dialog flow
- API health checks
Test Architecture
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────────┐
│ Playwright │────▶│ Next.js Dev │────▶│ Cloud Backend │
│ Browser │ │ Server (:3000) │ │ api.nomadkaraoke │
└─────────────────┘ └──────────────────┘ └─────────────────────┘
│
▼
API Proxy
/api/* → backend
Debugging Test Failures
- Screenshots: Failed tests save screenshots to
test-results/ - Videos: First retry captures video (when retries enabled)
- Traces: View detailed traces with
npx playwright show-trace <trace.zip> - HTML Report: After running tests, view
npx playwright show-report
Common Issues
- 401 Unauthorized: Token missing or invalid - check
.env.local - Socket hang up: Backend operation taking too long - some operations (like audio download) can take minutes
- Element outside viewport: Dialog scrolling issues - tests use JavaScript click to handle this
Project Structure
frontend/
├── app/ # Next.js app router pages
├── components/ # React components
│ ├── ui/ # shadcn/ui components
│ └── audio-search/ # Audio search feature components
├── lib/ # Utilities and API client
│ └── api.ts # Backend API client
├── e2e/ # Playwright E2E tests
├── playwright.config.ts # Playwright configuration
└── next.config.mjs # Next.js configuration
Scripts
| Script | Description |
|---|---|
npm run dev |
Start development server |
npm run build |
Build for production |
npm run start |
Start production server |
npm run lint |
Run ESLint |
npm test |
Run unit tests (watch mode) |
npm run test:ci |
Run unit tests (CI mode with coverage) |
npm run test:e2e |
Run E2E tests |
npm run test:e2e:ui |
Run E2E tests with interactive UI |