mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
## Summary
Andrew's review depth varies: Thursday live-event jobs often get only a
quick review. Treating every admin review as perfect sync truth
therefore poisons aligner training and evaluation (full-auto v2). This
adds an explicit, admin-only **"perfectly synced"** mark in the lyrics
review. Ticked jobs join the word-timing reference set; unticked means
unverified. Andrew's definition: a confident spot-check (anchors +
waveforms), not necessarily a full listen.
## Changes
- **Backend:** new `POST /api/review/{job_id}/sync-reference`
(`require_admin`).
- Stores `state_data.sync_reference = {verified, by, at, notes}`.
- `verified: true` needs saved review output: the `file_urls` pointer,
or the direct GCS path for older jobs. Otherwise it returns 409.
Revoking is always allowed.
- **`correction-data`** returns `sync_reference`, so a re-review shows
the existing mark and can revoke it.
- **Frontend:**
- The review modal shows the checkbox plus an optional notes field, to
admins only.
- The modal passes the mark to `onSubmit`. `LyricsAnalyzer` saves it
**after** `submitCorrections` succeeds, so a mark can't sit on unsaved
timing. A failure shows a toast and never blocks the review.
- Once the box is touched, its current state is always sent, so it can
be revoked.
- **i18n:** 4 new strings × 33 locales.
- **Docs:** API.md. Version 0.290.0.
## Testing
- Backend: `test_review_sync_reference.py` (admin store, notes trim,
404, 422, 403 non-admin, 409 without review, direct-path fallback).
Related review suites pass (29).
- Frontend: `ReviewChangesModal.syncReference.test.tsx` (hidden for
non-admin, tick passes the mark, untick sends nothing, saved mark
pre-ticks and revokes, touched-then-unticked sends `false`). The modal
suites pass (29). tsc shows no new errors (21 pre-existing test-file
errors, the same on main).
- **CodeRabbit:** ran locally for 3 cycles and fixed 6 findings. One was
left by design: a pre-ticked mark on an edited re-review is visible in
the modal Andrew confirms, so submitting re-affirms it. The only user is
Andrew.
@coderabbitai ignore
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_012RYLVVVPsvPbXFhRYY5VSJ
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
104 lines
4.1 KiB
Python
104 lines
4.1 KiB
Python
"""POST /api/review/{job_id}/sync-reference: admin-only "perfectly synced" mark that
|
|
puts a job's reviewed word timing in the reference set (full-auto v2)."""
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
from fastapi.testclient import TestClient
|
|
|
|
from backend.api.dependencies import require_admin, require_auth
|
|
from backend.main import app
|
|
from backend.services.auth_service import AuthResult, UserType
|
|
|
|
|
|
def _admin():
|
|
return AuthResult(is_valid=True, user_type=UserType.ADMIN, remaining_uses=999, message="ok",
|
|
is_admin=True, user_email="andrew@nomadkaraoke.com")
|
|
|
|
|
|
@pytest.fixture
|
|
def jm():
|
|
m = MagicMock()
|
|
m.get_job.return_value = MagicMock(
|
|
job_id="j1", file_urls={"lyrics": {"corrections_updated": "jobs/j1/lyrics/corrections_updated.json"}})
|
|
return m
|
|
|
|
|
|
@pytest.fixture
|
|
def client(jm):
|
|
async def fake_admin():
|
|
return _admin()
|
|
|
|
saved = dict(app.dependency_overrides)
|
|
app.dependency_overrides[require_admin] = fake_admin
|
|
try:
|
|
with patch("backend.api.routes.review.JobManager", return_value=jm):
|
|
yield TestClient(app)
|
|
finally:
|
|
app.dependency_overrides.clear(); app.dependency_overrides.update(saved)
|
|
|
|
|
|
def test_admin_marks_job_verified(client, jm):
|
|
r = client.post("/api/review/j1/sync-reference", json={"verified": True, "notes": " spot-checked chorus "})
|
|
assert r.status_code == 200
|
|
jid, key, record = jm.update_state_data.call_args.args
|
|
assert (jid, key) == ("j1", "sync_reference")
|
|
assert record["verified"] is True
|
|
assert record["by"] == "andrew@nomadkaraoke.com"
|
|
assert record["notes"] == "spot-checked chorus"
|
|
assert record["at"]
|
|
|
|
|
|
def test_blank_notes_stored_as_none(client, jm):
|
|
client.post("/api/review/j1/sync-reference", json={"verified": False, "notes": " "})
|
|
assert jm.update_state_data.call_args.args[2]["notes"] is None
|
|
assert jm.update_state_data.call_args.args[2]["verified"] is False
|
|
|
|
|
|
def test_unknown_job_404(client, jm):
|
|
jm.get_job.return_value = None
|
|
r = client.post("/api/review/nope/sync-reference", json={"verified": True})
|
|
assert r.status_code == 404
|
|
jm.update_state_data.assert_not_called()
|
|
|
|
|
|
def test_missing_verified_is_422(client):
|
|
assert client.post("/api/review/j1/sync-reference", json={}).status_code == 422
|
|
|
|
|
|
def test_non_admin_forbidden(jm):
|
|
async def fake_user():
|
|
return AuthResult(is_valid=True, user_type=UserType.UNLIMITED, remaining_uses=999, message="ok",
|
|
is_admin=False, user_email="someone@example.com")
|
|
|
|
saved = dict(app.dependency_overrides)
|
|
app.dependency_overrides.pop(require_admin, None) # conftest stubs it to always-admin
|
|
app.dependency_overrides[require_auth] = fake_user
|
|
try:
|
|
with patch("backend.api.routes.review.JobManager", return_value=jm):
|
|
r = TestClient(app).post("/api/review/j1/sync-reference", json={"verified": True})
|
|
assert r.status_code == 403
|
|
jm.update_state_data.assert_not_called()
|
|
finally:
|
|
app.dependency_overrides.clear(); app.dependency_overrides.update(saved)
|
|
|
|
|
|
def test_verified_requires_saved_review(client, jm):
|
|
jm.get_job.return_value = MagicMock(job_id="j1", file_urls={"lyrics": {}})
|
|
with patch("backend.api.routes.review.StorageService") as st:
|
|
st.return_value.file_exists.return_value = False
|
|
r = client.post("/api/review/j1/sync-reference", json={"verified": True})
|
|
assert r.status_code == 409
|
|
jm.update_state_data.assert_not_called()
|
|
# revoking is always allowed
|
|
assert client.post("/api/review/j1/sync-reference", json={"verified": False}).status_code == 200
|
|
|
|
|
|
def test_verified_accepts_direct_path_file(client, jm):
|
|
jm.get_job.return_value = MagicMock(job_id="j1", file_urls={})
|
|
with patch("backend.api.routes.review.StorageService") as st:
|
|
st.return_value.file_exists.return_value = True
|
|
r = client.post("/api/review/j1/sync-reference", json={"verified": True})
|
|
assert r.status_code == 200
|
|
st.return_value.file_exists.assert_called_with("jobs/j1/lyrics/corrections_updated.json")
|