mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
feat(local-stack): fake review fixtures and knobs for mobile native review (v0.279.5) (#1202)
## Summary
Local-stack (FAKE_WORKERS=1) support for nomad-mobile Phase 2, which is
the native lyrics and instrumental review. Maestro can now drive every
review state against the real review endpoints. Nothing changes in prod:
all hooks are gated on `fake_workers_enabled()`.
## Changes
- **New per-user `FakeConfig` knobs:**
- `review_fixture` (`default` / `corrections` / `duet` / `rtl` /
`large`). The fixtures are built from the real `CorrectionResult` types:
AI spans with `undo_info`, an untimed word, an evidenced vocal gap with
synced reference lines, duet singers, Hebrew/Arabic lines, and 300
lines.
- `backing_vocals` (`silent` / `audible`)
- `stems_delay_seconds` (stems land after review opens)
- `review_mode` and `auto_approval` (writes
`processing_metadata.auto_approval`, so the confident and skipped-step
states can be reached)
- `review_complete_fail_once`
- **Fake media gets an amplitude envelope**, so waveforms aren't flat.
There is also an audible backing stem variant.
- **`/api/review/{id}/audio` dev-proxy branch** now uses
`_ranged_response` (Range → 206), so seeking works on iOS. The prod
signed-URL path is untouched.
- **`GET /api/test/jobs/{id}/review-result`** returns the submitted
review (selection, `is_duet`, corrected lines) for Maestro assertions.
- `docs/TESTING.md` § Local stack documents the knobs.
## Testing
- `make test`: backend 2184 + 6532 + 163 passed, frontend 1760 passed.
- 29 new tests in `backend/tests/fake_workers/test_review_fixtures.py`.
- Smoke-tested on the local stack: every fixture loads; `with_backing`
analysis; Range 206; 503 then 200 on complete; `review-result`;
`auto_approval` states; late stems.
Reviewed locally (CodeRabbit CLI, 1 finding fixed).
@coderabbitai ignore
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01G81fc5sBB8GwG5Xhz2UMgc
---------
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e050cb8b3d
commit
83e097fa3b
11 files changed
+792
-33
No files matched your search
@@ -1069,6 +1069,9 @@ async def _stream_audio(job_id: str, request: Optional[Request] = None, stem: Li
|
||||
# 302-redirecting to a signed URL. Prod (flag unset) is unchanged.
|
||||
audio_bytes, content_type = await transcoding.get_review_audio_bytes_async(audio_gcs_path)
|
||||
logger.info(f"Job {job_id}: [dev-proxy] streaming {len(audio_bytes)}B input audio")
|
||||
if request is not None:
|
||||
# Range support so native players (iOS AVPlayer) can seek the song source.
|
||||
return _ranged_response(request, audio_bytes, content_type)
|
||||
return Response(content=audio_bytes, media_type=content_type)
|
||||
|
||||
signed_url = await transcoding.get_review_audio_url_async(audio_gcs_path, expiration_minutes=120)
|
||||
@@ -1289,6 +1292,11 @@ async def complete_review(
|
||||
After saving, triggers the render video worker which will use the
|
||||
pre-selected instrumental (no separate instrumental selection step).
|
||||
"""
|
||||
# Local stack only (FAKE_WORKERS=1): a one-shot 503 for the finish-failure flows.
|
||||
from backend.services.fake_workers import fake_workers_enabled
|
||||
if fake_workers_enabled():
|
||||
await asyncio.to_thread(_fake_complete_failure_hook, job_id)
|
||||
|
||||
# Validation + GCS/Firestore writes are all sync (the corrections upload alone
|
||||
# took 21s on 2026-10-08, freezing every request on the instance) — run them
|
||||
# in a worker thread; only the render dispatch below runs on the loop.
|
||||
@@ -1319,6 +1327,15 @@ async def complete_review(
|
||||
}
|
||||
|
||||
|
||||
def _fake_complete_failure_hook(job_id: str) -> None:
|
||||
"""Raise a 503 once when the job's user set ``review_complete_fail_once`` (fake workers only)."""
|
||||
from backend.services.fake_workers.config import consume_review_complete_failure
|
||||
|
||||
job = JobManager().get_job(job_id)
|
||||
if job and consume_review_complete_failure(getattr(job, "user_email", None)):
|
||||
raise HTTPException(status_code=503, detail="Fake review-complete failure (review_complete_fail_once)")
|
||||
|
||||
|
||||
def _save_review_completion(job_id: str, updated_data: Dict[str, Any]) -> str:
|
||||
"""Validate a review submission, persist it and move the job to REVIEW_COMPLETE.
|
||||
|
||||
|
||||
@@ -98,6 +98,42 @@ def scenario(body: ScenarioRequest) -> Dict[str, Any]:
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/jobs/{job_id}/review-result")
|
||||
def review_result(job_id: str) -> Dict[str, Any]:
|
||||
"""What the review submitted: the saved corrected lines plus the instrumental / duet choice.
|
||||
|
||||
Lets Maestro assert on a review without going through the review auth.
|
||||
``lines`` is empty until a review was completed (corrections_updated.json exists).
|
||||
"""
|
||||
from backend.services.job_manager import JobManager
|
||||
from backend.services.storage_service import StorageService
|
||||
|
||||
job = JobManager().get_job(job_id)
|
||||
if not job:
|
||||
raise HTTPException(status_code=404, detail="unknown job")
|
||||
state = job.state_data or {}
|
||||
path = ((job.file_urls or {}).get("lyrics") or {}).get("corrections_updated") \
|
||||
or f"jobs/{job_id}/lyrics/corrections_updated.json"
|
||||
storage = StorageService()
|
||||
lines = []
|
||||
if storage.file_exists(path):
|
||||
for seg in storage.download_json(path).get("corrected_segments") or []:
|
||||
lines.append({
|
||||
"text": seg.get("text"),
|
||||
"singer": seg.get("singer"),
|
||||
"words": [
|
||||
{"text": w.get("text"), "start_time": w.get("start_time"), "end_time": w.get("end_time")}
|
||||
for w in seg.get("words") or []
|
||||
],
|
||||
})
|
||||
return {
|
||||
"status": getattr(job.status, "value", job.status),
|
||||
"instrumental_selection": state.get("instrumental_selection"),
|
||||
"is_duet": bool(state.get("is_duet", False)),
|
||||
"lines": lines,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/emails")
|
||||
def emails(to: Optional[str] = None, limit: int = 20) -> Dict[str, Any]:
|
||||
return {"emails": list_emails(to, limit=limit)}
|
||||
|
||||
@@ -20,6 +20,16 @@ WORKER_KINDS = ("audio_download", "audio", "lyrics", "screens", "render_video",
|
||||
SEARCH_CONFIDENCES = ("perfect", "recommended", "unsure")
|
||||
# Values of ``judge_kind`` (the fake match judge / free-text resolve verdict kind).
|
||||
JUDGE_KINDS = ("none", "cosmetic", "content", "ambiguous")
|
||||
LARGE_FIXTURE_MIN_SECONDS = 305
|
||||
|
||||
# Values of ``review_fixture`` (the shape of the fake lyrics the review UI opens on).
|
||||
REVIEW_FIXTURES = ("default", "corrections", "duet", "rtl", "large")
|
||||
# Values of ``backing_vocals`` (silent stem vs one with an audible 10-20 s window).
|
||||
BACKING_VOCALS = ("silent", "audible")
|
||||
# Values of ``review_mode`` (what the seeded job asks for) and ``auto_approval``
|
||||
# (which stored auto-approval verdict the fake screens worker writes).
|
||||
REVIEW_MODES = ("always_review", "auto")
|
||||
AUTO_APPROVALS = ("none", "lyrics_confident", "backing_confident", "both_unsure")
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -52,6 +62,24 @@ class FakeConfig:
|
||||
judge_kind: str = "none"
|
||||
judge_canonical_artist: str = ""
|
||||
judge_canonical_title: str = ""
|
||||
# Review-UI fixtures (per-user, for the mobile review Maestro flows). Defaults keep
|
||||
# today's behaviour. See docs/TESTING.md § Local stack.
|
||||
# Shape of the fake lyrics: default | corrections | duet | rtl | large.
|
||||
review_fixture: str = "default"
|
||||
# silent: the backing stem is silence (analysis recommends clean). audible: a tone in
|
||||
# the 10-20 s window, so the analysis recommends with_backing / review.
|
||||
backing_vocals: str = "silent"
|
||||
# The fake audio worker uploads its stems this many seconds after it starts, so the
|
||||
# job reaches awaiting_review first (instrumental-urls audio_complete is false until then).
|
||||
stems_delay_seconds: float = 0
|
||||
# What the job scenario asks for: always_review (every screen) or auto (a confident
|
||||
# stored verdict lets the UI skip a screen).
|
||||
review_mode: str = "always_review"
|
||||
# Stored processing_metadata.auto_approval verdict written when the job parks at
|
||||
# awaiting_review: none | lyrics_confident | backing_confident | both_unsure.
|
||||
auto_approval: str = "none"
|
||||
# True: the next POST /api/review/{id}/complete answers 503 once, then this resets to False.
|
||||
review_complete_fail_once: bool = False
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Optional[Dict[str, Any]]) -> "FakeConfig":
|
||||
@@ -76,6 +104,25 @@ class FakeConfig:
|
||||
raise ValueError(f"judge_kind must be one of {JUDGE_KINDS}")
|
||||
if not isinstance(self.judge_canonical_artist, str) or not isinstance(self.judge_canonical_title, str):
|
||||
raise ValueError("judge_canonical_artist and judge_canonical_title must be strings")
|
||||
if self.review_fixture not in REVIEW_FIXTURES:
|
||||
raise ValueError(f"review_fixture must be one of {REVIEW_FIXTURES}")
|
||||
if self.review_fixture == "large" and self.audio_duration_seconds < LARGE_FIXTURE_MIN_SECONDS:
|
||||
# 300 lines at >= 1 s each: shorter audio leaves most lyrics past the end of the song.
|
||||
raise ValueError(f"review_fixture 'large' needs audio_duration_seconds >= {LARGE_FIXTURE_MIN_SECONDS}")
|
||||
if self.backing_vocals not in BACKING_VOCALS:
|
||||
raise ValueError(f"backing_vocals must be one of {BACKING_VOCALS}")
|
||||
if (
|
||||
isinstance(self.stems_delay_seconds, bool)
|
||||
or not isinstance(self.stems_delay_seconds, (int, float))
|
||||
or not 0 <= self.stems_delay_seconds <= 600
|
||||
):
|
||||
raise ValueError("stems_delay_seconds must be a number between 0 and 600")
|
||||
if self.review_mode not in REVIEW_MODES:
|
||||
raise ValueError(f"review_mode must be one of {REVIEW_MODES}")
|
||||
if self.auto_approval not in AUTO_APPROVALS:
|
||||
raise ValueError(f"auto_approval must be one of {AUTO_APPROVALS}")
|
||||
if not isinstance(self.review_complete_fail_once, bool):
|
||||
raise ValueError("review_complete_fail_once must be a boolean")
|
||||
|
||||
|
||||
USER_CONFIG_COLLECTION = "local_stack_user_config"
|
||||
@@ -136,3 +183,15 @@ def clear_fake_config(db=None) -> None:
|
||||
_doc(db).delete()
|
||||
for snap in db.collection(USER_CONFIG_COLLECTION).stream():
|
||||
snap.reference.delete()
|
||||
|
||||
|
||||
def consume_review_complete_failure(email: Optional[str] = None, db=None) -> bool:
|
||||
"""True once when ``review_complete_fail_once`` is set for ``email``, then clear it.
|
||||
|
||||
Backs the one-shot 503 on ``POST /api/review/{id}/complete`` (finish-step failure/retry flows).
|
||||
"""
|
||||
db = db or _default_db()
|
||||
if not get_fake_config(db, email=email).review_complete_fail_once:
|
||||
return False
|
||||
set_fake_config({"review_complete_fail_once": False}, db=db, email=email)
|
||||
return True
|
||||
@@ -6,7 +6,7 @@ anything ``from_dict`` can no longer read).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List
|
||||
from typing import Any, Dict, List, Optional, Sequence
|
||||
|
||||
from karaoke_gen.lyrics_transcriber.types import (
|
||||
AnchorSequence,
|
||||
@@ -29,22 +29,65 @@ FAKE_LINES = (
|
||||
REFERENCE_SOURCE = "lrclib"
|
||||
LEAD_IN_SECONDS = 2.0
|
||||
|
||||
# Right-to-left fixture: Hebrew lines, one of them also carrying Arabic (mixed-script bidi).
|
||||
RTL_LINES = (
|
||||
"שיר בדיקה קטן",
|
||||
"כל מילה מגיעה בזמן",
|
||||
"שרים יחד עם המערכת המקומית",
|
||||
"שום דבר כאן לא תומלל أغنية للاختبار",
|
||||
"העובדים מעמידים פנים והעבודות ממשיכות",
|
||||
"עד שהסרטון מוכן",
|
||||
)
|
||||
|
||||
def _segments(prefix: str, duration: float) -> List[LyricsSegment]:
|
||||
"""Spread FAKE_LINES evenly over the song, one word slot per word."""
|
||||
usable = max(duration - LEAD_IN_SECONDS - 1.0, len(FAKE_LINES))
|
||||
line_len = usable / len(FAKE_LINES)
|
||||
# Duet fixture: per-line singer ids cycle 1, 2, 0 (0 = both).
|
||||
DUET_SINGERS = (1, 2, 0)
|
||||
|
||||
LARGE_LINE_COUNT = 300
|
||||
_LARGE_VOCAB = (
|
||||
"sing", "along", "with", "the", "local", "stack", "every", "word", "arrives",
|
||||
"right", "on", "time", "fake", "song", "for", "testing", "workers", "pretend",
|
||||
)
|
||||
|
||||
# The corrections fixture holds one word for longer than the UI's "long word" threshold.
|
||||
LONG_WORD_SECONDS = 2.2
|
||||
# A line the transcription missed, only present in the synced reference (the vocal gap).
|
||||
MISSED_REFERENCE_LINE = "Thanks for singing along tonight"
|
||||
|
||||
|
||||
def _large_lines() -> List[str]:
|
||||
lines = []
|
||||
for i in range(LARGE_LINE_COUNT):
|
||||
count = 6 + (i % 3) # 6-8 words, 7 on average -> 2,100 words
|
||||
words = [_LARGE_VOCAB[(i * 5 + j) % len(_LARGE_VOCAB)] for j in range(count)]
|
||||
lines.append(" ".join(words).capitalize())
|
||||
return lines
|
||||
|
||||
|
||||
def _lines_for(fixture: str) -> Sequence[str]:
|
||||
if fixture == "rtl":
|
||||
return RTL_LINES
|
||||
if fixture == "large":
|
||||
return _large_lines()
|
||||
return FAKE_LINES
|
||||
|
||||
|
||||
def _segments(prefix: str, duration: float, lines: Sequence[str] = FAKE_LINES,
|
||||
singers: Optional[Sequence[int]] = None) -> List[LyricsSegment]:
|
||||
"""Spread ``lines`` evenly over the song, one word slot per word."""
|
||||
usable = max(duration - LEAD_IN_SECONDS - 1.0, len(lines))
|
||||
line_len = usable / len(lines)
|
||||
segments = []
|
||||
for i, line in enumerate(FAKE_LINES):
|
||||
for i, line in enumerate(lines):
|
||||
words_text = line.split()
|
||||
start = LEAD_IN_SECONDS + i * line_len
|
||||
slot = (line_len * 0.9) / len(words_text)
|
||||
gap = min(0.05, slot * 0.1)
|
||||
words = [
|
||||
Word(
|
||||
id=f"{prefix}w{i}_{j}",
|
||||
text=text,
|
||||
start_time=round(start + j * slot, 3),
|
||||
end_time=round(start + (j + 1) * slot - 0.05, 3),
|
||||
end_time=round(start + (j + 1) * slot - gap, 3),
|
||||
confidence=0.99,
|
||||
)
|
||||
for j, text in enumerate(words_text)
|
||||
@@ -56,15 +99,25 @@ def _segments(prefix: str, duration: float) -> List[LyricsSegment]:
|
||||
words=words,
|
||||
start_time=words[0].start_time,
|
||||
end_time=words[-1].end_time,
|
||||
singer=singers[i % len(singers)] if singers else None,
|
||||
)
|
||||
)
|
||||
return segments
|
||||
|
||||
|
||||
def build_corrections(artist: str, title: str, duration: float) -> Dict[str, Any]:
|
||||
"""Return a ``CorrectionResult.to_dict()`` whose transcription matches its reference."""
|
||||
transcribed = _segments("t", duration)
|
||||
reference = _segments("r", duration)
|
||||
def build_corrections(artist: str, title: str, duration: float, fixture: str = "default") -> Dict[str, Any]:
|
||||
"""Return a ``CorrectionResult.to_dict()`` whose transcription matches its reference.
|
||||
|
||||
``fixture`` picks the lyrics shape (see ``FakeConfig.review_fixture``). Everything but
|
||||
``corrections`` is built straight from the real dataclasses; ``corrections`` then edits
|
||||
the dict into the post-AI-apply state gen's server-side pre-apply produces (extra word
|
||||
fields the dataclasses don't model), and carries the matching ``vocal_gaps`` block under
|
||||
a ``vocal_gaps`` key for the lyrics worker to store in ``state_data``.
|
||||
"""
|
||||
lines = _lines_for(fixture)
|
||||
singers = DUET_SINGERS if fixture == "duet" else None
|
||||
transcribed = _segments("t", duration, lines, singers)
|
||||
reference = _segments("r", duration, lines, singers)
|
||||
reference_lyrics = {
|
||||
REFERENCE_SOURCE: LyricsData(
|
||||
segments=reference,
|
||||
@@ -117,7 +170,148 @@ def build_corrections(artist: str, title: str, duration: float) -> Dict[str, Any
|
||||
word_id_map={},
|
||||
segment_id_map={},
|
||||
)
|
||||
return result.to_dict()
|
||||
doc = result.to_dict()
|
||||
if fixture == "duet":
|
||||
doc["is_duet"] = True
|
||||
elif fixture == "corrections":
|
||||
_apply_corrections_fixture(doc, duration)
|
||||
return doc
|
||||
|
||||
|
||||
def _word_dict(word_id: str, text: str, start: Optional[float], end: Optional[float], **extra: Any) -> Dict[str, Any]:
|
||||
return {"id": word_id, "text": text, "start_time": start, "end_time": end, "confidence": 1, **extra}
|
||||
|
||||
|
||||
def _apply_corrections_fixture(doc: Dict[str, Any], duration: float) -> None:
|
||||
"""Turn ``doc`` into the post-AI-apply state: two applied suggestions with undo info,
|
||||
an untimed word, a long word and an evidenced vocal gap with synced reference lines."""
|
||||
import copy
|
||||
from datetime import datetime, timezone
|
||||
|
||||
original = doc["original_segments"]
|
||||
corrected = copy.deepcopy(original)
|
||||
suggestions: List[Dict[str, Any]] = []
|
||||
undo_info: Dict[str, Any] = {}
|
||||
|
||||
# Segment 1: replace "arrives" (heard as "arrive") -> a single AI-corrected word, real timing.
|
||||
seg = corrected[1]
|
||||
idx = next(i for i, w in enumerate(seg["words"]) if w["text"] == "arrives")
|
||||
old = copy.deepcopy(seg["words"][idx])
|
||||
original[1]["words"][idx]["text"] = "arrive"
|
||||
original[1]["text"] = original[1]["text"].replace("arrives", "arrive")
|
||||
sid = "fake-sug-replace"
|
||||
new_word = _word_dict(
|
||||
"fake-ai-arrives", "arrives", old["start_time"], old["end_time"],
|
||||
created_during_correction=True, ai_corrected=True, timing_estimated=False,
|
||||
correction_span_id=sid, original_text="arrive",
|
||||
)
|
||||
removed = {**old, "text": "arrive"}
|
||||
seg["words"][idx] = new_word
|
||||
suggestions.append(_suggestion(sid, "replace", [old["id"]], [seg["id"]], "arrive", "arrives",
|
||||
"Matches the synced reference", "mishearing"))
|
||||
undo_info[sid] = {
|
||||
"op": "replace", "new_word_ids": [new_word["id"]], "removed_words": [removed],
|
||||
"segment_id": seg["id"], "prev_word_id": seg["words"][idx - 1]["id"] if idx else None,
|
||||
"removed_segment": None,
|
||||
}
|
||||
|
||||
# Segment 2: one heard word "localstack" -> two words sharing its slot (estimated timing).
|
||||
seg = corrected[2]
|
||||
idx = next(i for i, w in enumerate(seg["words"]) if w["text"] == "local")
|
||||
old = copy.deepcopy(seg["words"][idx])
|
||||
stack = seg["words"][idx + 1]
|
||||
span_end = stack["end_time"]
|
||||
original[2]["words"][idx]["text"] = "localstack"
|
||||
original[2]["words"][idx]["end_time"] = span_end
|
||||
del original[2]["words"][idx + 1]
|
||||
original[2]["text"] = original[2]["text"].replace("local stack", "localstack")
|
||||
removed_word = {**old, "text": "localstack", "end_time": span_end}
|
||||
sid2 = "fake-sug-split"
|
||||
mid = round((old["start_time"] + span_end) / 2, 3)
|
||||
new_words = [
|
||||
_word_dict("fake-ai-local", "local", old["start_time"], mid, created_during_correction=True,
|
||||
ai_corrected=True, timing_estimated=True, correction_span_id=sid2,
|
||||
original_text="localstack"),
|
||||
_word_dict("fake-ai-stack", "stack", mid, span_end, created_during_correction=True,
|
||||
ai_corrected=True, timing_estimated=True, correction_span_id=sid2),
|
||||
]
|
||||
seg["words"][idx:idx + 2] = new_words
|
||||
suggestions.append(_suggestion(sid2, "replace", [old["id"]], [seg["id"]], "localstack", "local stack",
|
||||
"Two words run together", "mishearing"))
|
||||
undo_info[sid2] = {
|
||||
"op": "replace", "new_word_ids": [w["id"] for w in new_words], "removed_words": [removed_word],
|
||||
"segment_id": seg["id"], "prev_word_id": seg["words"][idx - 1]["id"] if idx else None,
|
||||
"removed_segment": None,
|
||||
}
|
||||
|
||||
# Segment 3: one word with no timing (the render would crash on it if submitted as-is).
|
||||
untimed = next(w for w in corrected[3]["words"] if w["text"] == "ever")
|
||||
untimed["start_time"] = None
|
||||
untimed["end_time"] = None
|
||||
|
||||
# Last segment: first word held for longer than 2 s, the rest squeezed into what remains.
|
||||
seg = corrected[-1]
|
||||
first, rest = seg["words"][0], seg["words"][1:]
|
||||
held_end = round(first["start_time"] + LONG_WORD_SECONDS, 3)
|
||||
span = seg["words"][-1]["end_time"] - held_end
|
||||
step = span / len(rest)
|
||||
first["end_time"] = held_end
|
||||
for k, w in enumerate(rest):
|
||||
w["start_time"] = round(held_end + k * step, 3)
|
||||
w["end_time"] = round(held_end + (k + 1) * step - 0.03, 3)
|
||||
|
||||
for segment in corrected:
|
||||
timed = [w for w in segment["words"] if w["start_time"] is not None]
|
||||
segment["text"] = " ".join(w["text"] for w in segment["words"])
|
||||
segment["start_time"] = timed[0]["start_time"]
|
||||
segment["end_time"] = timed[-1]["end_time"]
|
||||
|
||||
doc["corrected_segments"] = corrected
|
||||
doc["resized_segments"] = copy.deepcopy(corrected)
|
||||
doc["corrections_made"] = len(suggestions)
|
||||
doc["metadata"]["auto_approval"] = {
|
||||
"pre_applied": True,
|
||||
"applied_suggestion_ids": [s["id"] for s in suggestions],
|
||||
"rejected_suggestion_ids": [],
|
||||
"suggestions": suggestions,
|
||||
"undo_info": undo_info,
|
||||
"applied_at": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
|
||||
# One evidenced vocal gap after the last line, where only the synced reference has a line.
|
||||
last_end = corrected[-1]["end_time"]
|
||||
gap_start = round(last_end + 0.2, 3)
|
||||
gap_end = round(max(min(last_end + 1.8, duration - 0.1), gap_start + 0.5), 3)
|
||||
ref = doc["reference_lyrics"][REFERENCE_SOURCE]
|
||||
ref["segments"].append({
|
||||
"id": "rs-missed", "text": MISSED_REFERENCE_LINE,
|
||||
"words": [_word_dict(f"rw-missed-{j}", t, gap_start, gap_end)
|
||||
for j, t in enumerate(MISSED_REFERENCE_LINE.split())],
|
||||
"start_time": gap_start, "end_time": gap_end,
|
||||
})
|
||||
doc["vocal_gaps"] = {
|
||||
"version": "fake",
|
||||
"gaps": [{
|
||||
"start": gap_start, "end": gap_end, "duration": round(gap_end - gap_start, 3),
|
||||
"active_fraction": 0.85, "longest_run_s": round(gap_end - gap_start - 0.1, 3),
|
||||
"reference_lines": {REFERENCE_SOURCE: [MISSED_REFERENCE_LINE]},
|
||||
"synced_reference_lines": {REFERENCE_SOURCE: [MISSED_REFERENCE_LINE]},
|
||||
"suspect": True, "evidenced": True,
|
||||
}],
|
||||
"suspect_count": 1,
|
||||
"evidenced_count": 1,
|
||||
"max_suspect_run_s": round(gap_end - gap_start - 0.1, 3),
|
||||
}
|
||||
|
||||
|
||||
def _suggestion(sid: str, op: str, word_ids: List[str], segment_ids: List[str], original_text: str,
|
||||
new_text: str, reason: str, category: str) -> Dict[str, Any]:
|
||||
return {
|
||||
"id": sid, "op": op, "word_ids": word_ids, "segment_ids": segment_ids,
|
||||
"original_text": original_text, "new_text": new_text, "reason": reason,
|
||||
"category": category, "confidence": 0.95, "models": ["fake-model"],
|
||||
"consensus": 1, "total_models": 1, "conflict_group": None,
|
||||
}
|
||||
|
||||
|
||||
def build_lrc(corrections: Dict[str, Any]) -> str:
|
||||
|
||||
@@ -15,7 +15,7 @@ import tempfile
|
||||
import threading
|
||||
import zipfile
|
||||
from dataclasses import dataclass
|
||||
from typing import Dict
|
||||
from typing import Dict, Tuple
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -23,7 +23,7 @@ SAMPLE_RATE = 22050
|
||||
VIDEO_SIZE = "320x180"
|
||||
|
||||
_lock = threading.Lock()
|
||||
_cache: Dict[int, "MediaSet"] = {}
|
||||
_cache: Dict[Tuple[int, str], "MediaSet"] = {}
|
||||
_root = tempfile.mkdtemp(prefix="fake_worker_media_")
|
||||
|
||||
|
||||
@@ -52,10 +52,29 @@ def _ffmpeg(*args: str) -> None:
|
||||
subprocess.run(cmd, check=True, capture_output=True, timeout=300)
|
||||
|
||||
|
||||
def _tone(path: str, freq: int, duration: int, volume: float = 0.3) -> None:
|
||||
# Amplitude shaping so review waveforms aren't solid blocks: a sung-like burst pattern
|
||||
# (volume swells to full and dips to 30% every second) on the vocal stem, and a slow
|
||||
# tremolo on the instrumental / full mix.
|
||||
VOCAL_ENVELOPE = "volume='0.3+0.7*abs(sin(t*PI/2))':eval=frame"
|
||||
MIX_TREMOLO = "tremolo=f=2:d=0.6"
|
||||
# Window (seconds) in which the "audible" backing stem sings.
|
||||
BACKING_WINDOW = (10, 20)
|
||||
|
||||
|
||||
def _tone(path: str, freq: int, duration: int, volume: float = 0.3, shape: str = "") -> None:
|
||||
chain = f"volume={volume}" + (f",{shape}" if shape else "")
|
||||
_ffmpeg(
|
||||
"-f", "lavfi", "-i", f"sine=frequency={freq}:sample_rate={SAMPLE_RATE}:duration={duration}",
|
||||
"-af", f"volume={volume}", "-ac", "1", path,
|
||||
"-af", chain, "-ac", "1", path,
|
||||
)
|
||||
|
||||
|
||||
def _gated_tone(path: str, freq: int, duration: int, window: Tuple[float, float], volume: float = 0.3) -> None:
|
||||
"""A tone that only sounds inside ``window`` (silent elsewhere)."""
|
||||
start, end = window
|
||||
_ffmpeg(
|
||||
"-f", "lavfi", "-i", f"sine=frequency={freq}:sample_rate={SAMPLE_RATE}:duration={duration}",
|
||||
"-af", f"volume='if(between(t,{start},{end}),{volume},0)':eval=frame", "-ac", "1", path,
|
||||
)
|
||||
|
||||
|
||||
@@ -95,16 +114,21 @@ def _zip(path: str, members: Dict[str, bytes]) -> None:
|
||||
zf.writestr(name, data)
|
||||
|
||||
|
||||
def _build(duration: int) -> MediaSet:
|
||||
d = os.path.join(_root, str(duration))
|
||||
def _build(duration: int, backing_vocals: str = "silent") -> MediaSet:
|
||||
d = os.path.join(_root, f"{duration}-{backing_vocals}")
|
||||
os.makedirs(d, exist_ok=True)
|
||||
p = lambda name: os.path.join(d, name) # noqa: E731
|
||||
|
||||
# "Instrumental" = low tone, "lead vocals" = high tone, backing vocals = silence,
|
||||
# so the real backing-vocals analysis recommends the clean instrumental.
|
||||
_tone(p("instrumental_clean.flac"), 220, duration)
|
||||
_tone(p("lead_vocals.flac"), 660, duration, volume=0.2)
|
||||
_silence(p("backing_vocals.flac"), duration)
|
||||
# "Instrumental" = low tone, "lead vocals" = high tone. Backing vocals = silence
|
||||
# (the real backing-vocals analysis recommends the clean instrumental) or, for
|
||||
# backing_vocals="audible", a tone in BACKING_WINDOW (it recommends keeping them).
|
||||
_tone(p("instrumental_clean.flac"), 220, duration, shape=MIX_TREMOLO)
|
||||
_tone(p("lead_vocals.flac"), 660, duration, volume=0.2, shape=VOCAL_ENVELOPE)
|
||||
if backing_vocals == "audible":
|
||||
window = (min(BACKING_WINDOW[0], duration / 3), min(BACKING_WINDOW[1], duration * 2 / 3))
|
||||
_gated_tone(p("backing_vocals.flac"), 440, duration, window)
|
||||
else:
|
||||
_silence(p("backing_vocals.flac"), duration)
|
||||
_mix(p("vocals_clean.flac"), p("lead_vocals.flac"), p("backing_vocals.flac"))
|
||||
_mix(p("instrumental_with_backing.flac"), p("instrumental_clean.flac"), p("backing_vocals.flac"))
|
||||
_mix(p("input.flac"), p("instrumental_clean.flac"), p("vocals_clean.flac"))
|
||||
@@ -139,12 +163,13 @@ def _build(duration: int) -> MediaSet:
|
||||
)
|
||||
|
||||
|
||||
def get_media(duration: int) -> MediaSet:
|
||||
def get_media(duration: int, backing_vocals: str = "silent") -> MediaSet:
|
||||
"""Return (building once) the media set for a song of ``duration`` seconds."""
|
||||
key = (duration, backing_vocals)
|
||||
with _lock:
|
||||
media = _cache.get(duration)
|
||||
media = _cache.get(key)
|
||||
if media is None:
|
||||
logger.info(f"fake_workers: generating {duration}s media set in {_root}")
|
||||
media = _build(duration)
|
||||
_cache[duration] = media
|
||||
logger.info(f"fake_workers: generating {duration}s ({backing_vocals} backing) media set in {_root}")
|
||||
media = _build(duration, backing_vocals)
|
||||
_cache[key] = media
|
||||
return media
|
||||
@@ -65,7 +65,32 @@ async def _begin(kind: str, job_id: str) -> FakeConfig:
|
||||
|
||||
|
||||
def _media_for(config: FakeConfig) -> MediaSet:
|
||||
return get_media(int(config.audio_duration_seconds))
|
||||
return get_media(int(config.audio_duration_seconds), config.backing_vocals)
|
||||
|
||||
|
||||
# Stored processing_metadata.auto_approval verdicts per FakeConfig.auto_approval value:
|
||||
# (lyrics verdict, backing verdict, backing non_subjective). The shapes
|
||||
# auto_approval_summary() reads, so the review UI sees confident / unsure halves.
|
||||
_AUTO_APPROVAL_VERDICTS = {
|
||||
"lyrics_confident": ("auto", "review", False),
|
||||
"backing_confident": ("review", "clean", True),
|
||||
"both_unsure": ("review", "review", False),
|
||||
}
|
||||
|
||||
|
||||
def auto_approval_payload(kind: str):
|
||||
"""The ``processing_metadata.auto_approval`` verdict for ``kind``, or None for "none"."""
|
||||
verdicts = _AUTO_APPROVAL_VERDICTS.get(kind)
|
||||
if not verdicts:
|
||||
return None
|
||||
lyrics, backing, non_subjective = verdicts
|
||||
return {
|
||||
"outcome": "review",
|
||||
"source": "fake_workers",
|
||||
"lyrics": {"verdict": lyrics},
|
||||
"backing": {"verdict": backing, "non_subjective": non_subjective},
|
||||
"backing_analysis_available": True,
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- audio download
|
||||
@@ -117,6 +142,11 @@ async def run_audio(job_id: str) -> bool:
|
||||
"combined_instrumentals": {"fake_backing_model.ckpt": media.instrumental_with_backing},
|
||||
}
|
||||
job_manager.update_state_data(job_id, "model_names", FAKE_MODEL_NAMES)
|
||||
if config.stems_delay_seconds:
|
||||
# Late stems: this worker is its own thread/loop, so lyrics -> screens -> awaiting_review
|
||||
# proceed meanwhile and the review opens with audio_complete still false.
|
||||
logger.info(f"[job:{job_id}] fake audio: holding stems for {config.stems_delay_seconds}s")
|
||||
await asyncio.sleep(config.stems_delay_seconds)
|
||||
await upload_separation_results(job_id, separation_result, storage, job_manager)
|
||||
await _transcode_review_stems(job_id, job_manager, storage, job_log)
|
||||
job_manager.update_processing_metadata(job_id, "separation", {
|
||||
@@ -149,7 +179,9 @@ async def run_lyrics(job_id: str) -> bool:
|
||||
return False
|
||||
|
||||
corrections = build_corrections(job.artist or "Fake Artist", job.title or "Fake Title",
|
||||
float(config.audio_duration_seconds))
|
||||
float(config.audio_duration_seconds), config.review_fixture)
|
||||
# Not part of corrections.json: gen keeps the vocal-gap analysis in state_data.
|
||||
vocal_gaps = corrections.pop("vocal_gaps", None)
|
||||
with tempfile.TemporaryDirectory(prefix=f"fake_lyrics_{job_id}_") as tmp:
|
||||
corrections_path = os.path.join(tmp, "corrections.json")
|
||||
with open(corrections_path, "w") as f:
|
||||
@@ -162,6 +194,8 @@ async def run_lyrics(job_id: str) -> bool:
|
||||
url = await asyncio.to_thread(storage.upload_file, lrc_path, f"jobs/{job_id}/lyrics/karaoke.lrc")
|
||||
job_manager.update_file_url(job_id, "lyrics", "lrc", url)
|
||||
|
||||
if vocal_gaps:
|
||||
job_manager.update_state_data(job_id, "vocal_gaps", vocal_gaps)
|
||||
job_manager.update_state_data(job_id, "lyrics_metadata", {
|
||||
"segment_count": len(corrections["corrected_segments"]),
|
||||
"has_corrections": True,
|
||||
@@ -228,6 +262,9 @@ async def run_screens(job_id: str) -> bool:
|
||||
|
||||
await get_worker_service().trigger_render_video_worker(job_id)
|
||||
else:
|
||||
verdict = auto_approval_payload(config.auto_approval)
|
||||
if verdict:
|
||||
job_manager.update_processing_metadata(job_id, "auto_approval", verdict)
|
||||
job_manager.transition_to_state(
|
||||
job_id=job_id, new_status=JobStatus.AWAITING_REVIEW, progress=55,
|
||||
message="Ready for review. Please review lyrics and select your instrumental.",
|
||||
|
||||
@@ -17,7 +17,7 @@ import httpx
|
||||
|
||||
from backend.models.job import JobStatus
|
||||
from backend.services.fake_workers.checkout import public_api_url
|
||||
from backend.services.fake_workers.config import set_fake_config
|
||||
from backend.services.fake_workers.config import get_fake_config, set_fake_config
|
||||
|
||||
# target -> (per-user fake config overrides, status the job must reach)
|
||||
JOB_TARGETS: Dict[str, tuple] = {
|
||||
@@ -76,6 +76,9 @@ def create_job(user: Dict[str, Any], target: str, artist: str = DEFAULT_ARTIST,
|
||||
raise ScenarioError(f"Unknown job target {target!r}; one of {sorted(JOB_TARGETS)}")
|
||||
overrides, wanted = JOB_TARGETS[target]
|
||||
set_fake_config(overrides, email=user["email"])
|
||||
# The awaiting_review job honours the user's review_mode knob (default always_review),
|
||||
# so a flow can seed "auto" to reach the confident / skipped-step states.
|
||||
review_mode = get_fake_config(email=user["email"]).review_mode if target == "awaiting_review" else "auto"
|
||||
|
||||
with _api(user["session_token"]) as api:
|
||||
search = api.post("/api/audio-search/search-standalone", json={"artist": artist, "title": title})
|
||||
@@ -86,7 +89,7 @@ def create_job(user: Dict[str, Any], target: str, artist: str = DEFAULT_ARTIST,
|
||||
"selection_index": 0,
|
||||
"artist": artist,
|
||||
"title": title,
|
||||
"review_mode": "always_review" if target == "awaiting_review" else "auto",
|
||||
"review_mode": review_mode,
|
||||
})
|
||||
if created.status_code != 200:
|
||||
raise ScenarioError(f"create-from-search failed: {created.status_code} {created.text}")
|
||||
|
||||
Whitespace-only changes.
@@ -0,0 +1,370 @@
|
||||
"""Fixtures and knobs the fake workers offer the mobile review flows (FakeConfig.review_fixture etc.)."""
|
||||
import asyncio
|
||||
import re
|
||||
import shutil
|
||||
import threading
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from backend.models.job import JobStatus
|
||||
from backend.services.fake_workers import config as fw_config
|
||||
from backend.services.fake_workers.config import FakeConfig, get_fake_config, set_fake_config
|
||||
from backend.services.fake_workers.lyrics import build_corrections
|
||||
from backend.tests.test_fake_workers import FakeDB
|
||||
|
||||
HEBREW = re.compile("[-]")
|
||||
ARABIC = re.compile("[-ۿ]")
|
||||
|
||||
|
||||
def _words(doc):
|
||||
return [w for s in doc["corrected_segments"] for w in s["words"]]
|
||||
|
||||
|
||||
class TestFixtures:
|
||||
def test_fixture_corrections_shape(self):
|
||||
from karaoke_gen.lyrics_transcriber.types import CorrectionResult
|
||||
|
||||
doc = build_corrections("A", "T", 30, "corrections")
|
||||
CorrectionResult.from_dict(doc) # still the real schema
|
||||
|
||||
words = _words(doc)
|
||||
replaced = [w for w in words if w.get("ai_corrected") and w.get("original_text")]
|
||||
assert replaced
|
||||
assert any(w.get("timing_estimated") for w in words)
|
||||
assert [w for w in words if w["start_time"] is None] and \
|
||||
len([w for w in words if w["start_time"] is None]) == 1
|
||||
assert any(w["start_time"] is not None and w["end_time"] - w["start_time"] > 2 for w in words)
|
||||
|
||||
gap = doc["vocal_gaps"]["gaps"][0]
|
||||
assert gap["evidenced"] is True
|
||||
assert gap["synced_reference_lines"]["lrclib"]
|
||||
|
||||
undo = doc["metadata"]["auto_approval"]["undo_info"]
|
||||
assert undo["fake-sug-replace"]["removed_words"][0]["text"] == "arrive"
|
||||
assert undo["fake-sug-replace"]["new_word_ids"] == ["fake-ai-arrives"]
|
||||
# every undo target exists in the served segments, and the heard text is in original_segments
|
||||
ids = {w["id"] for w in words}
|
||||
assert all(set(u["new_word_ids"]) <= ids for u in undo.values())
|
||||
assert "arrive" in [w["text"] for s in doc["original_segments"] for w in s["words"]]
|
||||
|
||||
def test_default_fixture_is_unchanged(self):
|
||||
doc = build_corrections("A", "T", 30)
|
||||
assert "vocal_gaps" not in doc and "is_duet" not in doc
|
||||
assert all(s.get("singer") is None for s in doc["corrected_segments"])
|
||||
|
||||
def test_fixture_duet(self):
|
||||
doc = build_corrections("A", "T", 30, "duet")
|
||||
assert doc["is_duet"] is True
|
||||
assert {s["singer"] for s in doc["corrected_segments"]} == {0, 1, 2}
|
||||
|
||||
def test_fixture_rtl(self):
|
||||
doc = build_corrections("A", "T", 30, "rtl")
|
||||
texts = [s["text"] for s in doc["corrected_segments"]]
|
||||
assert all(HEBREW.search(t) for t in texts)
|
||||
assert sum(1 for t in texts if ARABIC.search(t)) == 1
|
||||
|
||||
def test_fixture_large(self):
|
||||
from karaoke_gen.lyrics_transcriber.types import CorrectionResult
|
||||
|
||||
doc = build_corrections("A", "T", 30, "large")
|
||||
assert len(doc["corrected_segments"]) == 300
|
||||
words = _words(doc)
|
||||
assert len(words) >= 2000
|
||||
starts = [w["start_time"] for w in words]
|
||||
assert starts == sorted(starts) and len(set(starts)) == len(starts)
|
||||
assert all(w["start_time"] < w["end_time"] for w in words)
|
||||
CorrectionResult.from_dict(doc)
|
||||
|
||||
|
||||
class TestKnobs:
|
||||
def test_defaults_keep_todays_behaviour(self):
|
||||
c = FakeConfig()
|
||||
assert (c.review_fixture, c.backing_vocals, c.stems_delay_seconds) == ("default", "silent", 0)
|
||||
assert (c.review_mode, c.auto_approval, c.review_complete_fail_once) == ("always_review", "none", False)
|
||||
|
||||
@pytest.mark.parametrize("bad", [
|
||||
{"review_fixture": "nope"}, {"backing_vocals": "loud"}, {"stems_delay_seconds": -1},
|
||||
{"stems_delay_seconds": 601}, {"stems_delay_seconds": "2"}, {"review_mode": "manual"},
|
||||
{"auto_approval": "yes"}, {"review_complete_fail_once": "true"},
|
||||
{"review_fixture": "large", "audio_duration_seconds": 30},
|
||||
])
|
||||
def test_validate_rejects(self, bad):
|
||||
with pytest.raises(ValueError):
|
||||
set_fake_config(bad, db=FakeDB(), email="u@example.com")
|
||||
|
||||
def test_large_fixture_accepts_long_audio(self):
|
||||
set_fake_config({"review_fixture": "large", "audio_duration_seconds": 305}, db=FakeDB(), email="u@example.com")
|
||||
|
||||
def test_consume_review_complete_failure_is_one_shot_per_user(self):
|
||||
db = FakeDB()
|
||||
set_fake_config({"review_complete_fail_once": True}, db=db, email="a@example.com")
|
||||
assert fw_config.consume_review_complete_failure("b@example.com", db=db) is False
|
||||
assert fw_config.consume_review_complete_failure("a@example.com", db=db) is True
|
||||
assert fw_config.consume_review_complete_failure("a@example.com", db=db) is False
|
||||
|
||||
|
||||
@pytest.mark.skipif(shutil.which("ffmpeg") is None, reason="ffmpeg not installed")
|
||||
class TestBackingVocals:
|
||||
def test_backing_audible_recommends_backing(self, tmp_path):
|
||||
from backend.services.fake_workers.media import get_media
|
||||
from backend.workers.screens_worker import _analyze_backing_vocals
|
||||
|
||||
media = get_media(30, "audible")
|
||||
assert get_media(30, "silent").backing_vocals != media.backing_vocals
|
||||
|
||||
def analyse(stem):
|
||||
storage = MagicMock()
|
||||
storage.download_file.side_effect = lambda _src, dst: shutil.copy(stem, dst)
|
||||
jm = MagicMock()
|
||||
jm.get_job.return_value = SimpleNamespace(file_urls={"stems": {"backing_vocals": "stems/b.flac"}})
|
||||
with patch("backend.services.audio_analysis_service.StorageService", return_value=storage):
|
||||
asyncio.run(_analyze_backing_vocals("job1", jm, storage, MagicMock()))
|
||||
calls = [c for c in jm.update_state_data.call_args_list if c[0][1] == "backing_vocals_analysis"]
|
||||
return calls[-1][0][2]
|
||||
|
||||
audible = analyse(media.backing_vocals)
|
||||
assert audible["has_audible_content"] is True
|
||||
assert audible["recommended_selection"] in ("with_backing", "review_needed")
|
||||
assert analyse(get_media(30, "silent").backing_vocals)["has_audible_content"] is False
|
||||
|
||||
|
||||
class TestPipelineKnobs:
|
||||
def test_auto_approval_payloads_drive_the_review_summary(self):
|
||||
from backend.services.auto_approval.instrumental import auto_approval_summary
|
||||
from backend.services.fake_workers.pipeline import auto_approval_payload
|
||||
|
||||
settings = SimpleNamespace(auto_approval_backing_keep_enabled=False, auto_approval_timing_gate_enabled=True)
|
||||
|
||||
def summary(kind, review_mode="auto"):
|
||||
job = SimpleNamespace(
|
||||
processing_metadata={"auto_approval": auto_approval_payload(kind)}, review_mode=review_mode,
|
||||
backing_preference="auto", existing_instrumental_gcs_path=None, state_data={},
|
||||
file_urls={"stems": {"instrumental_clean": "x"}},
|
||||
)
|
||||
return auto_approval_summary(job, settings)
|
||||
|
||||
assert auto_approval_payload("none") is None
|
||||
assert summary("lyrics_confident")["lyrics"]["confident"] is True
|
||||
assert summary("lyrics_confident")["backing"]["confident"] is False
|
||||
assert summary("backing_confident")["backing"]["resolved_selection"] == "clean"
|
||||
assert summary("backing_confident")["lyrics"]["confident"] is False
|
||||
both = summary("both_unsure")
|
||||
assert both["lyrics"]["confident"] is False and both["backing"]["confident"] is False
|
||||
assert summary("lyrics_confident", "always_review")["lyrics"]["confident"] is False
|
||||
|
||||
def test_auto_approval_knob_writes_summary(self):
|
||||
from backend.services.fake_workers import pipeline
|
||||
|
||||
jm = MagicMock()
|
||||
job = SimpleNamespace(user_email="u@example.com", status=JobStatus.LYRICS_COMPLETE, review_mode="auto")
|
||||
jm.get_job.return_value = job
|
||||
cfg = FakeConfig(step_delay_seconds=0, auto_approval="lyrics_confident", review_mode="auto")
|
||||
with patch.object(pipeline, "JobManager", return_value=jm), \
|
||||
patch.object(pipeline, "StorageService"), \
|
||||
patch.object(pipeline, "get_fake_config", return_value=cfg), \
|
||||
patch.object(pipeline, "get_media", return_value=MagicMock()), \
|
||||
patch("builtins.open", MagicMock()), \
|
||||
patch("backend.workers.screens_worker._upload_screens", AsyncMock()), \
|
||||
patch("backend.workers.screens_worker._transcode_review_audio", AsyncMock()):
|
||||
assert asyncio.run(pipeline.run_screens("job1")) is True
|
||||
writes = [c[0] for c in jm.update_processing_metadata.call_args_list]
|
||||
assert writes[0][1] == "auto_approval"
|
||||
assert writes[0][2]["lyrics"]["verdict"] == "auto"
|
||||
# parked for a human, not auto-completed
|
||||
assert jm.transition_to_state.call_args_list[-1][1]["new_status"] == JobStatus.AWAITING_REVIEW
|
||||
|
||||
def test_stems_delay(self):
|
||||
from backend.services.fake_workers import pipeline
|
||||
|
||||
order = []
|
||||
jm = MagicMock()
|
||||
jm.get_job.return_value = SimpleNamespace(user_email="u@example.com")
|
||||
jm.mark_audio_complete.side_effect = lambda *_: order.append("audio_complete")
|
||||
jm.advance_to_screens_if_ready = AsyncMock()
|
||||
jm.resume_video_if_waiting_for_stems = AsyncMock()
|
||||
|
||||
async def upload(*_a, **_k):
|
||||
order.append("upload")
|
||||
|
||||
async def sleep(seconds):
|
||||
order.append(("sleep", seconds))
|
||||
|
||||
cfg = FakeConfig(step_delay_seconds=0, stems_delay_seconds=2)
|
||||
with patch.object(pipeline, "JobManager", return_value=jm), \
|
||||
patch.object(pipeline, "StorageService"), \
|
||||
patch.object(pipeline, "get_fake_config", return_value=cfg), \
|
||||
patch.object(pipeline, "get_media", return_value=MagicMock()), \
|
||||
patch.object(pipeline.asyncio, "sleep", sleep), \
|
||||
patch("backend.workers.audio_worker.upload_separation_results", upload), \
|
||||
patch("backend.workers.audio_worker._transcode_review_stems", AsyncMock()), \
|
||||
patch("backend.workers.screens_worker._analyze_backing_vocals", AsyncMock()):
|
||||
assert asyncio.run(pipeline.run_audio("job1")) is True
|
||||
assert order == [("sleep", 2), "upload", "audio_complete"]
|
||||
|
||||
def test_audio_worker_does_not_block_other_fake_workers(self):
|
||||
"""Each dispatched fake runs on its own thread, so one worker's delay never holds up the others."""
|
||||
from backend.services import fake_workers
|
||||
|
||||
seen = []
|
||||
started = threading.Event()
|
||||
|
||||
async def fake_run(kind, job_id):
|
||||
if kind == "audio":
|
||||
started.set()
|
||||
await asyncio.sleep(0.3)
|
||||
seen.append(kind)
|
||||
return True
|
||||
|
||||
with patch("backend.services.fake_workers.pipeline.run", fake_run):
|
||||
fake_workers.dispatch("audio", "j")
|
||||
assert started.wait(2)
|
||||
fake_workers.dispatch("lyrics", "j")
|
||||
for t in list(fake_workers._threads):
|
||||
t.join(timeout=5)
|
||||
assert seen == ["lyrics", "audio"]
|
||||
|
||||
|
||||
class TestLyricsWorkerStoresGaps:
|
||||
def test_vocal_gaps_go_to_state_data_not_corrections_json(self):
|
||||
from backend.services.fake_workers import pipeline
|
||||
|
||||
jm, uploaded = MagicMock(), {}
|
||||
jm.get_job.return_value = SimpleNamespace(user_email="u@example.com", artist="A", title="T")
|
||||
jm.advance_to_screens_if_ready = AsyncMock()
|
||||
storage = MagicMock()
|
||||
|
||||
def upload(path, dest):
|
||||
import json
|
||||
if dest.endswith("corrections.json"):
|
||||
uploaded.update(json.load(open(path)))
|
||||
return dest
|
||||
|
||||
storage.upload_file.side_effect = upload
|
||||
cfg = FakeConfig(step_delay_seconds=0, review_fixture="corrections")
|
||||
with patch.object(pipeline, "JobManager", return_value=jm), \
|
||||
patch.object(pipeline, "StorageService", return_value=storage), \
|
||||
patch.object(pipeline, "get_fake_config", return_value=cfg):
|
||||
assert asyncio.run(pipeline.run_lyrics("job1")) is True
|
||||
assert "vocal_gaps" not in uploaded
|
||||
keys = {c[0][1] for c in jm.update_state_data.call_args_list}
|
||||
assert "vocal_gaps" in keys
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- routes
|
||||
|
||||
@pytest.fixture
|
||||
def review_client():
|
||||
from backend.api.dependencies import require_review_auth
|
||||
from backend.main import app
|
||||
|
||||
async def allow():
|
||||
return ("job1", "full")
|
||||
|
||||
app.dependency_overrides[require_review_auth] = allow
|
||||
yield TestClient(app)
|
||||
app.dependency_overrides.pop(require_review_auth, None)
|
||||
|
||||
|
||||
class TestReviewRoutes:
|
||||
def test_complete_fail_once(self, review_client):
|
||||
db = FakeDB()
|
||||
set_fake_config({"review_complete_fail_once": True}, db=db, email="u@example.com")
|
||||
jm = MagicMock()
|
||||
jm.get_job.return_value = SimpleNamespace(user_email="u@example.com")
|
||||
ws = MagicMock()
|
||||
ws.trigger_render_video_worker = AsyncMock(return_value=True)
|
||||
with patch("backend.api.routes.review.JobManager", return_value=jm), \
|
||||
patch("backend.services.fake_workers.fake_workers_enabled", return_value=True), \
|
||||
patch("backend.services.fake_workers.config._default_db", return_value=db), \
|
||||
patch("backend.api.routes.review._save_review_completion", return_value="clean"), \
|
||||
patch("backend.services.worker_service.get_worker_service", return_value=ws):
|
||||
body = {"instrumental_selection": "clean"}
|
||||
assert review_client.post("/api/review/job1/complete", json=body).status_code == 503
|
||||
second = review_client.post("/api/review/job1/complete", json=body)
|
||||
assert second.status_code == 200
|
||||
assert second.json()["instrumental_selection"] == "clean"
|
||||
|
||||
def test_complete_hook_inert_without_fake_workers(self, review_client):
|
||||
db = FakeDB()
|
||||
set_fake_config({"review_complete_fail_once": True}, db=db, email="u@example.com")
|
||||
ws = MagicMock()
|
||||
ws.trigger_render_video_worker = AsyncMock(return_value=True)
|
||||
with patch("backend.services.fake_workers.config._default_db", return_value=db), \
|
||||
patch("backend.api.routes.review._save_review_completion", return_value="clean"), \
|
||||
patch("backend.services.worker_service.get_worker_service", return_value=ws):
|
||||
resp = review_client.post("/api/review/job1/complete", json={"instrumental_selection": "clean"})
|
||||
assert resp.status_code == 200
|
||||
assert get_fake_config(db, email="u@example.com").review_complete_fail_once is True
|
||||
|
||||
def test_audio_range(self, review_client):
|
||||
data = bytes(range(256)) * 4
|
||||
jm = MagicMock()
|
||||
jm.get_job.return_value = SimpleNamespace(input_media_gcs_path="jobs/job1/input.flac")
|
||||
transcoding = MagicMock()
|
||||
transcoding.get_review_audio_bytes_async = AsyncMock(return_value=(data, "audio/ogg"))
|
||||
with patch("backend.api.routes.review.JobManager", return_value=jm), \
|
||||
patch("backend.api.routes.review._dev_audio_proxy_enabled", return_value=True), \
|
||||
patch("backend.services.audio_transcoding_service.AudioTranscodingService",
|
||||
return_value=transcoding):
|
||||
ranged = review_client.get("/api/review/job1/audio", headers={"Range": "bytes=0-99"})
|
||||
full = review_client.get("/api/review/job1/audio")
|
||||
assert ranged.status_code == 206
|
||||
assert ranged.content == data[:100]
|
||||
assert ranged.headers["content-range"] == f"bytes 0-99/{len(data)}"
|
||||
assert full.status_code == 200 and full.headers["accept-ranges"] == "bytes"
|
||||
|
||||
|
||||
class TestReviewResultEndpoint:
|
||||
@pytest.fixture
|
||||
def client(self):
|
||||
from backend.api.routes import test_support
|
||||
|
||||
app = FastAPI()
|
||||
app.include_router(test_support.router, prefix="/api")
|
||||
return TestClient(app)
|
||||
|
||||
def _job(self, **state):
|
||||
return SimpleNamespace(status=JobStatus.REVIEW_COMPLETE, state_data=state, file_urls={})
|
||||
|
||||
def test_returns_submitted_review(self, client):
|
||||
saved = {"corrected_segments": [
|
||||
{"text": "Hi there", "singer": 2, "words": [
|
||||
{"id": "a", "text": "Hi", "start_time": 1.0, "end_time": 1.5},
|
||||
{"id": "b", "text": "there", "start_time": 1.6, "end_time": 2.0},
|
||||
]},
|
||||
]}
|
||||
jm, storage = MagicMock(), MagicMock()
|
||||
jm.get_job.return_value = self._job(instrumental_selection="with_backing", is_duet=True)
|
||||
storage.file_exists.return_value = True
|
||||
storage.download_json.return_value = saved
|
||||
with patch("backend.services.job_manager.JobManager", return_value=jm), \
|
||||
patch("backend.services.storage_service.StorageService", return_value=storage):
|
||||
resp = client.get("/api/test/jobs/job1/review-result")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json() == {
|
||||
"status": "review_complete", "instrumental_selection": "with_backing", "is_duet": True,
|
||||
"lines": [{"text": "Hi there", "singer": 2, "words": [
|
||||
{"text": "Hi", "start_time": 1.0, "end_time": 1.5},
|
||||
{"text": "there", "start_time": 1.6, "end_time": 2.0},
|
||||
]}],
|
||||
}
|
||||
storage.file_exists.assert_called_with("jobs/job1/lyrics/corrections_updated.json")
|
||||
|
||||
def test_before_a_review_lines_are_empty(self, client):
|
||||
jm, storage = MagicMock(), MagicMock()
|
||||
jm.get_job.return_value = self._job()
|
||||
storage.file_exists.return_value = False
|
||||
with patch("backend.services.job_manager.JobManager", return_value=jm), \
|
||||
patch("backend.services.storage_service.StorageService", return_value=storage):
|
||||
body = client.get("/api/test/jobs/job1/review-result").json()
|
||||
assert body["lines"] == [] and body["is_duet"] is False and body["instrumental_selection"] is None
|
||||
|
||||
def test_unknown_job_404(self, client):
|
||||
jm = MagicMock()
|
||||
jm.get_job.return_value = None
|
||||
with patch("backend.services.job_manager.JobManager", return_value=jm):
|
||||
assert client.get("/api/test/jobs/nope/review-result").status_code == 404
|
||||
@@ -373,6 +373,24 @@ shapes, and `frontend/lib/__tests__/fake-search-tiers.test.ts` feeds them to the
|
||||
A backend test fails if that fixture is stale; regenerate it with
|
||||
`poetry run python -m backend.services.fake_workers.search --write-fixture`.
|
||||
|
||||
**Review-flow knobs (per user, for the mobile review Maestro flows):** set with
|
||||
`PUT /api/test/fake-config?email=<user>` *before* `POST /api/test/scenario` with `job_awaiting_review`
|
||||
for that email. Defaults keep the plain 6-line, silent-backing behaviour.
|
||||
|
||||
| Knob | Values | Effect |
|
||||
|---|---|---|
|
||||
| `review_fixture` | `default` / `corrections` / `duet` / `rtl` / `large` | Shape of the fake lyrics. `corrections`: two AI-applied corrections (words with `ai_corrected` / `original_text`, one `timing_estimated` split) with matching `metadata.auto_approval.undo_info`, one untimed word, one word held > 2 s and one evidenced vocal gap with synced reference lines (needs `audio_duration_seconds` >= 30). `duet`: `is_duet` true, per-line singers 1 / 2 / 0. `rtl`: Hebrew lines, one also with Arabic. `large`: 300 lines, 2,100 words (~1.3 MB JSON; needs `audio_duration_seconds` >= 305, or the lyrics run past the end of the audio). |
|
||||
| `backing_vocals` | `silent` / `audible` | `audible`: a 440 Hz tone in the 10-20 s window, so the real analysis recommends `with_backing`. |
|
||||
| `stems_delay_seconds` | 0-600 | The fake audio worker uploads stems this long after it starts, so the job reaches `awaiting_review` with `instrumental-urls` `audio_complete: false` until then. |
|
||||
| `review_mode` | `always_review` / `auto` | `review_mode` of the seeded `job_awaiting_review` job. `auto` lets a stored confident verdict skip a screen. |
|
||||
| `auto_approval` | `none` / `lyrics_confident` / `backing_confident` / `both_unsure` | Writes `processing_metadata.auto_approval` when the job parks at review (only has an effect with `review_mode: auto`; `correction-data` `auto_approval` then reports `lyrics.confident` / `backing.confident`). |
|
||||
| `review_complete_fail_once` | bool | The next `POST /api/review/{id}/complete` for this user's job answers 503, then the knob resets to false. Inert unless `FAKE_WORKERS=1`. |
|
||||
|
||||
`GET /api/test/jobs/{job_id}/review-result` (no auth, test API only) returns
|
||||
`{status, instrumental_selection, is_duet, lines: [{text, singer, words: [{text, start_time, end_time}]}]}`
|
||||
from the saved `corrections_updated.json` (`lines` is empty until a review completed), so Maestro can
|
||||
assert on what the app submitted.
|
||||
|
||||
**Mobile (Maestro / dev builds):** run the stack, then point the app at
|
||||
`http://10.0.2.2:8010` (Android emulator) or `http://127.0.0.1:8010` (iOS
|
||||
simulator). Set `LOCAL_STACK_API_URL` to that address before `up`, so fake-checkout
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "karaoke-gen"
|
||||
version = "0.279.4"
|
||||
version = "0.279.5"
|
||||
description = "Generate karaoke videos with synchronized lyrics. Handles the entire process from downloading audio and lyrics to creating the final video with title screens."
|
||||
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
|
||||
license = "MIT"
|
||||
|
||||
Reference in new issue
Block a user