mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-12-03 08:39:34 +09:00
## Summary
Fixes the race condition behind job `8a9c74ff` where a single user Retry
click caused the `audio-download-job` Cloud Run Job to run 3 times,
producing a visible "Audio download failed: Invalid state transition for
job ...: downloading -> JobStatus.DOWNLOADING" error AND
double-triggering downstream L4 GPU separation + lyrics transcription
jobs.
Root cause: the user clicked Retry inside the ~75s window between Cloud
Run's failed task and its automatic task-attempt retry. Two parallel
executions raced on job state.
## Changes
**Backend**
- `audio_download_worker.py`:
- `DOWNLOAD_ALREADY_DONE_STATUSES` idempotency guard — return success
early when status is already past `DOWNLOADING_AUDIO`, instead of
re-downloading and crashing on a redundant state transition.
- `_mark_retry_pending_if_attempts_remain` writes
`state_data.cloud_run_retry_pending = { expires_at }` when
`CLOUD_RUN_TASK_ATTEMPT < CLOUD_RUN_MAX_RETRIES` (2). Final attempt
explicitly clears any stale marker.
- `_clear_retry_pending_and_error` clears the marker and `error_message`
on successful download so the dashboard stops showing a resolved
transient failure.
- `jobs.py` `/retry` endpoint returns **409** when
`cloud_run_retry_pending` is active, preventing duplicate parallel
executions.
- `job_manager.py` error-message rendering fix: f-strings now use
`.value` consistently so we don't get the misleading `downloading ->
JobStatus.DOWNLOADING` artifact.
- `cloud_run_retry_pending` added to `SUMMARY_FIELD_PATHS` and the
dashboard prune allowlist.
**Frontend**
- `isAutoRetryPending(job)` helper in `lib/job-status.ts`.
- `JobActions`: when auto-retry is pending, hide the manual Retry button
and show "Retrying automatically…" with a spinner. Falls back to Retry
once the window expires.
- `JobCard`: don't render the `error_message` banner on completed jobs
(covers legacy data where a transient failure left a stale error
string).
- New i18n keys auto-translated to all 33 locales.
**Docs**
- `LESSONS-LEARNED.md`: Cloud Run Job retry race pattern + idempotency
defenses.
## Testing
- [x] Backend unit tests pass: 3183 passed, 36 skipped (10 new tests for
this work)
- [x] Frontend Jest tests pass: 110 passed in affected suites (8 new
tests)
- [x] TDD throughout — failing test before each implementation change
Tests added cover:
- Worker idempotency across 8 post-download statuses (`DOWNLOADING`,
`AWAITING_AUDIO_EDIT`, `TRANSCRIBING`, ... `COMPLETE`)
- Retry-pending marker: written on non-final attempt, cleared on final
attempt, cleared on success
- `_is_auto_retry_pending` helper edge cases (missing / past / future /
malformed)
- Frontend: indicator shows during window, falls back to Retry after
expiry, JobCard hides stale error on complete
## Review
- [x] Local code review completed (CodeRabbit CLI auth issue →
`feature-dev:code-reviewer` agent fallback)
- [x] Should-fix finding addressed: stale marker after final attempt
blocked manual retry for ~3 min (commit `d87cd86`)
- [ ] Optional finding deferred: frontend doesn't auto-re-render at
exact expiry time (dashboard polls, self-heals)
## Infrastructure
No Pulumi changes. `audio-download-job` `max_retries=2` retained —
automatic recovery from transient Spotify timeouts still works, and the
new idempotency guard makes retries safe.
@coderabbitai ignore
---
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
229 lines
8.5 KiB
TypeScript
229 lines
8.5 KiB
TypeScript
/**
|
|
* Tests for JobCard component
|
|
*
|
|
* Note: Job cards are no longer collapsible - details are always visible
|
|
*/
|
|
|
|
import { render, screen } from '@testing-library/react'
|
|
import { JobCard } from '../job/JobCard'
|
|
import { Job } from '@/lib/api'
|
|
|
|
// Mock the child components
|
|
jest.mock('../job/JobActions', () => ({
|
|
JobActions: () => <div data-testid="job-actions">Job Actions</div>
|
|
}))
|
|
|
|
jest.mock('../job/AdminJobActions', () => ({
|
|
AdminJobActions: () => <div data-testid="admin-job-actions">Admin Job Actions</div>
|
|
}))
|
|
|
|
jest.mock('../job/JobLogs', () => ({
|
|
JobLogs: () => <div data-testid="job-logs">Job Logs</div>
|
|
}))
|
|
|
|
jest.mock('../job/OutputLinks', () => ({
|
|
OutputLinks: () => <div data-testid="output-links">Output Links</div>
|
|
}))
|
|
|
|
jest.mock('../audio-search/AudioSearchDialog', () => ({
|
|
AudioSearchDialog: () => <div data-testid="audio-search-dialog">Audio Search Dialog</div>
|
|
}))
|
|
|
|
describe('JobCard', () => {
|
|
const mockOnRefresh = jest.fn()
|
|
|
|
const mockJob: Job = {
|
|
job_id: '123',
|
|
status: 'processing',
|
|
progress: 50,
|
|
created_at: '2025-01-01T00:00:00Z',
|
|
updated_at: '2025-01-01T00:00:00Z',
|
|
artist: 'Test Artist',
|
|
title: 'Test Song',
|
|
}
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
})
|
|
|
|
it('renders job information', () => {
|
|
render(<JobCard job={mockJob} onRefresh={mockOnRefresh} />)
|
|
|
|
expect(screen.getByText('Test Artist - Test Song')).toBeInTheDocument()
|
|
})
|
|
|
|
it('shows job ID (details always visible)', () => {
|
|
render(<JobCard job={mockJob} onRefresh={mockOnRefresh} />)
|
|
|
|
// Details are always visible now (no need to click to expand)
|
|
// ID is shown in format: "ID: 123 • date • status"
|
|
expect(screen.getByText(/ID:/)).toBeInTheDocument()
|
|
expect(screen.getByText(/123/)).toBeInTheDocument()
|
|
})
|
|
|
|
it('shows job actions (details always visible)', () => {
|
|
render(<JobCard job={mockJob} onRefresh={mockOnRefresh} />)
|
|
|
|
// Actions should be immediately visible
|
|
expect(screen.getByTestId('job-actions')).toBeInTheDocument()
|
|
})
|
|
|
|
// Note: Progress bars have been removed from JobCard in favor of a simpler design
|
|
|
|
it('shows error message for failed jobs', () => {
|
|
const failedJob = { ...mockJob, status: 'failed', error_message: 'Something went wrong' }
|
|
render(<JobCard job={failedJob} onRefresh={mockOnRefresh} />)
|
|
|
|
expect(screen.getByText('Something went wrong')).toBeInTheDocument()
|
|
})
|
|
|
|
it('hides stale error message on completed jobs', () => {
|
|
// Regression: a transient failure (e.g. Cloud Run auto-retried download)
|
|
// leaves error_message populated even after the job completes. We must
|
|
// not alarm users about a resolved issue.
|
|
const completedWithStaleError = {
|
|
...mockJob,
|
|
status: 'complete',
|
|
error_message: 'Audio download failed: transient timeout',
|
|
}
|
|
render(<JobCard job={completedWithStaleError} onRefresh={mockOnRefresh} />)
|
|
|
|
expect(screen.queryByText(/Audio download failed/)).not.toBeInTheDocument()
|
|
})
|
|
|
|
// Note: Orange border for interactive jobs removed - uses default slate border now
|
|
|
|
it('applies green border for complete jobs', () => {
|
|
const completeJob = { ...mockJob, status: 'complete' }
|
|
const { container } = render(<JobCard job={completeJob} onRefresh={mockOnRefresh} />)
|
|
|
|
const card = container.firstChild
|
|
expect(card).toHaveClass('border-green-500/30')
|
|
})
|
|
|
|
it('applies red border for failed jobs', () => {
|
|
const failedJob = { ...mockJob, status: 'failed' }
|
|
const { container } = render(<JobCard job={failedJob} onRefresh={mockOnRefresh} />)
|
|
|
|
const card = container.firstChild
|
|
expect(card).toHaveClass('border-red-500/30')
|
|
})
|
|
|
|
describe('regular jobs (non_interactive: false)', () => {
|
|
it('shows lyrics review link for awaiting_review status', () => {
|
|
const reviewJob = { ...mockJob, status: 'awaiting_review', review_token: 'test-token-123' }
|
|
render(<JobCard job={reviewJob} onRefresh={mockOnRefresh} />)
|
|
|
|
const reviewLink = screen.getByText('Review Lyrics')
|
|
expect(reviewLink).toBeInTheDocument()
|
|
const href = reviewLink.closest('a')?.getAttribute('href') || ''
|
|
// Uses hash-based routing for static hosting
|
|
// Uses <a> tag (not Next.js Link) to ensure hashchange event fires
|
|
expect(href).toBe('/en/app/jobs#/123/review')
|
|
})
|
|
|
|
it('shows audio search button for awaiting_audio_selection status', () => {
|
|
const audioSelectionJob = { ...mockJob, status: 'awaiting_audio_selection' }
|
|
render(<JobCard job={audioSelectionJob} onRefresh={mockOnRefresh} />)
|
|
|
|
expect(screen.getByRole('button', { name: 'Select Audio' })).toBeInTheDocument()
|
|
})
|
|
|
|
it('shows instrumental selection link for awaiting_instrumental_selection status', () => {
|
|
const instrumentalJob = {
|
|
...mockJob,
|
|
status: 'awaiting_instrumental_selection',
|
|
instrumental_token: 'test-instrumental-token'
|
|
}
|
|
render(<JobCard job={instrumentalJob} onRefresh={mockOnRefresh} />)
|
|
|
|
const instrumentalLink = screen.getByText('Select Instrumental')
|
|
expect(instrumentalLink).toBeInTheDocument()
|
|
const href = instrumentalLink.closest('a')?.getAttribute('href') || ''
|
|
// Uses hash-based routing for static hosting
|
|
// Uses <a> tag (not Next.js Link) to ensure hashchange event fires
|
|
expect(href).toBe('/en/app/jobs#/123/instrumental')
|
|
})
|
|
|
|
it('does not show Auto badge for regular jobs', () => {
|
|
render(<JobCard job={mockJob} onRefresh={mockOnRefresh} />)
|
|
|
|
expect(screen.queryByText('Auto')).not.toBeInTheDocument()
|
|
})
|
|
})
|
|
|
|
describe('non-interactive jobs (non_interactive: true)', () => {
|
|
it('shows Auto badge for non-interactive jobs', () => {
|
|
const nonInteractiveJob = { ...mockJob, non_interactive: true }
|
|
render(<JobCard job={nonInteractiveJob} onRefresh={mockOnRefresh} />)
|
|
|
|
expect(screen.getByText('Auto')).toBeInTheDocument()
|
|
})
|
|
|
|
it('shows auto-accept message for awaiting_review status', () => {
|
|
const reviewJob = { ...mockJob, status: 'awaiting_review', non_interactive: true }
|
|
render(<JobCard job={reviewJob} onRefresh={mockOnRefresh} />)
|
|
|
|
expect(screen.getByText(/Will auto-accept/)).toBeInTheDocument()
|
|
expect(screen.queryByText('Review Lyrics')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('shows auto-select message for awaiting_instrumental_selection status', () => {
|
|
const instrumentalJob = { ...mockJob, status: 'awaiting_instrumental_selection', non_interactive: true }
|
|
render(<JobCard job={instrumentalJob} onRefresh={mockOnRefresh} />)
|
|
|
|
expect(screen.getByText(/Will auto-select/)).toBeInTheDocument()
|
|
expect(screen.queryByText('Select Instrumental')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('shows auto-select first message for awaiting_audio_selection status', () => {
|
|
const audioJob = { ...mockJob, status: 'awaiting_audio_selection', non_interactive: true }
|
|
render(<JobCard job={audioJob} onRefresh={mockOnRefresh} />)
|
|
|
|
expect(screen.getByText(/Will auto-select first/)).toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: 'Select Audio' })).not.toBeInTheDocument()
|
|
})
|
|
})
|
|
|
|
it('shows output links for complete jobs', () => {
|
|
const completeJob = { ...mockJob, status: 'complete' }
|
|
render(<JobCard job={completeJob} onRefresh={mockOnRefresh} />)
|
|
|
|
expect(screen.getByTestId('output-links')).toBeInTheDocument()
|
|
})
|
|
|
|
it('handles unknown artist gracefully', () => {
|
|
const noArtistJob = { ...mockJob, artist: undefined }
|
|
render(<JobCard job={noArtistJob} onRefresh={mockOnRefresh} />)
|
|
|
|
expect(screen.getByText('Unknown - Test Song')).toBeInTheDocument()
|
|
})
|
|
|
|
it('handles unknown title gracefully', () => {
|
|
const noTitleJob = { ...mockJob, title: undefined }
|
|
render(<JobCard job={noTitleJob} onRefresh={mockOnRefresh} />)
|
|
|
|
expect(screen.getByText('Test Artist - Unknown')).toBeInTheDocument()
|
|
})
|
|
|
|
describe('admin controls', () => {
|
|
it('shows admin job actions when showAdminControls is true', () => {
|
|
render(<JobCard job={mockJob} onRefresh={mockOnRefresh} showAdminControls={true} />)
|
|
|
|
expect(screen.getByTestId('admin-job-actions')).toBeInTheDocument()
|
|
})
|
|
|
|
it('does not show admin job actions when showAdminControls is false', () => {
|
|
render(<JobCard job={mockJob} onRefresh={mockOnRefresh} showAdminControls={false} />)
|
|
|
|
expect(screen.queryByTestId('admin-job-actions')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('does not show admin job actions by default', () => {
|
|
render(<JobCard job={mockJob} onRefresh={mockOnRefresh} />)
|
|
|
|
expect(screen.queryByTestId('admin-job-actions')).not.toBeInTheDocument()
|
|
})
|
|
})
|
|
})
|