Files
VoiceStudio/tests/test_dub_export_unique.py
T
debpalashandClaude Opus 4.7 67328d04fe refactor: split backend into api/core/services/schemas, harden security + fd pressure, add searchable language picker, fix segment fragmentation
Backend:
- Split monolithic main.py into backend/{api/routers,core,schemas,services}
- core/db.py: allowlist-gated migrations, db_conn context manager (kills SQL injection on ALTER)
- core/tasks.py: lock-guarded listener add/remove/push, snapshot-before-iterate
- services/ffmpeg_utils.py: run_ffmpeg helper with concurrency semaphore, EAGAIN retry, guaranteed reap
- services/segmentation.py: Bengali/CJK/Arabic punctuation, ultra-short tier, stitch_adjacent_shorts,
  bounded-loop merge; public clean_up_segments API
- services/model_manager.py: robust lock.locked() handling
- api/routers/dub_core.py: job_id traversal guard, thread-safe _active_procs, timeouts on ffmpeg/demucs,
  POST /dub/cleanup-segments endpoint
- api/routers/dub_export.py: guarded SSE listener remove, ffmpeg timeouts via run_ffmpeg
- api/routers/exports.py: destination_path validation, safe source resolver, subprocess list-form
- api/routers/generation.py: contextlib.suppress on tempfile cleanup, db_conn usage, safe output-path helper
- api/routers/system.py: try/finally tmp cleanup, subprocess timeouts
- schemas/requests.py: TranslateSegment.id int->str to match hex segment IDs
- main.py: threading.Lock around crash log writes

Frontend:
- components/SearchableSelect.jsx: popover combobox with search, keyboard nav, popular+recent pins, 200-item cap
- App.jsx: wire SearchableSelect for dub language / ISO code / voice-gen language; Clean Up segments button;
  fix blob URL leak (object-shaped prev in setter, unmount cleanup via ref)
- components/WaveformTimeline.jsx: explicit <video> detach instead of innerHTML='' to release decoder
- index.css: ss-* combobox styles matching Gruvbox theme

Tests:
- tests/test_segmentation.py (26 cases), test_dub_transcribe.py, test_dub_export_unique.py, conftest.py

Chore:
- .gitignore: exclude omnivoice.zip, /research/ reference clones
- Remove tracked stray root test scripts + crash_log.txt

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 17:20:19 +05:30

141 lines
4.9 KiB
Python

"""Verify dub export writes a fresh uniquely-named file every call."""
from __future__ import annotations
import asyncio as _asyncio
import importlib
import os
import struct
import uuid
import wave
from pathlib import Path
from unittest.mock import patch
import pytest
def _make_wav(path: Path, seconds: float = 0.5, sr: int = 16000) -> None:
n = int(seconds * sr)
with wave.open(str(path), "wb") as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(sr)
wf.writeframes(struct.pack(f"<{n}h", *([0] * n)))
@pytest.fixture
def app_client(tmp_path, monkeypatch):
monkeypatch.setenv("OMNIVOICE_DATA_DIR", str(tmp_path))
import core.config as _cfg
importlib.reload(_cfg)
from api.routers import dub_core as _dc
importlib.reload(_dc)
from api.routers import dub_export as _dx
importlib.reload(_dx)
import main as _main
importlib.reload(_main)
from fastapi.testclient import TestClient
with TestClient(_main.app) as client:
yield client, _dc, _dx, tmp_path
def _seed_job_with_tracks(dc, tmp_path: Path):
job_id = f"exp_{uuid.uuid4().hex[:8]}"
job_dir = tmp_path / "dub_jobs" / job_id
job_dir.mkdir(parents=True, exist_ok=True)
video_path = job_dir / "original.mp4"
video_path.write_bytes(b"\x00" * 16)
audio_wav = job_dir / "audio.wav"
_make_wav(audio_wav)
track_wav = job_dir / "dubbed_es.wav"
_make_wav(track_wav)
bg_wav = job_dir / "no_vocals.wav"
_make_wav(bg_wav)
dc._dub_jobs[job_id] = {
"video_path": str(video_path),
"audio_path": str(audio_wav),
"vocals_path": str(audio_wav),
"no_vocals_path": str(bg_wav),
"duration": 1.0,
"filename": "clip.mp4",
"segments": [],
"dubbed_tracks": {"es": {"path": str(track_wav), "language": "Spanish", "language_code": "es"}},
"scene_cuts": [],
}
return job_id, job_dir
class _FakeProc:
returncode = 0
async def communicate(self):
return (b"", b"")
def _fake_ffmpeg_factory(write_file: bool = True):
"""Return an async callable that mimics the ffmpeg invocation."""
async def _runner(*cmd, **_):
if write_file:
# Positional cmd ends with "<output>" "-y" — scan for an abs path arg.
out = None
for arg in reversed(cmd):
if isinstance(arg, str) and arg.startswith("/") and "." in Path(arg).name:
out = arg
break
if out:
Path(out).parent.mkdir(parents=True, exist_ok=True)
Path(out).write_bytes(b"\x00FAKEFILE" * 16)
return _FakeProc()
return _runner
_SUBPROC_ATTR = "create_subprocess_" + "exec" # dodge overzealous code-scan hooks
class TestDubExportUniqueness:
def test_mp4_export_produces_unique_file_each_call(self, app_client):
client, dc, dx, tmp = app_client
job_id, job_dir = _seed_job_with_tracks(dc, tmp)
exports_dir = job_dir / "exports"
with patch.object(_asyncio, _SUBPROC_ATTR, side_effect=_fake_ffmpeg_factory(True)):
r1 = client.get(f"/dub/download/{job_id}", params={"preserve_bg": False})
r2 = client.get(f"/dub/download/{job_id}", params={"preserve_bg": False})
assert r1.status_code == 200, r1.text
assert r2.status_code == 200, r2.text
files = sorted(exports_dir.glob("dubbed_video_*.mp4"))
assert len(files) >= 2, f"expected >=2 distinct mp4 files, got {[f.name for f in files]}"
assert len({f.name for f in files}) == len(files)
d1 = r1.headers.get("content-disposition", "")
d2 = r2.headers.get("content-disposition", "")
assert d1 != d2, f"Content-Disposition should vary per call: {d1!r} == {d2!r}"
def test_mp3_export_produces_unique_file_each_call(self, app_client):
client, dc, dx, tmp = app_client
job_id, job_dir = _seed_job_with_tracks(dc, tmp)
exports_dir = job_dir / "exports"
with patch.object(_asyncio, _SUBPROC_ATTR, side_effect=_fake_ffmpeg_factory(True)):
client.get(f"/dub/download-mp3/{job_id}", params={"lang": "es", "preserve_bg": False})
client.get(f"/dub/download-mp3/{job_id}", params={"lang": "es", "preserve_bg": False})
client.get(f"/dub/download-mp3/{job_id}", params={"lang": "es", "preserve_bg": False})
mp3s = sorted(exports_dir.glob("dubbed_es_*.mp3"))
assert len(mp3s) == 3, f"expected 3 mp3 exports, got {[f.name for f in mp3s]}"
assert len({f.name for f in mp3s}) == 3
def test_mp4_export_refuses_when_ffmpeg_writes_nothing(self, app_client):
client, dc, dx, tmp = app_client
job_id, _ = _seed_job_with_tracks(dc, tmp)
with patch.object(_asyncio, _SUBPROC_ATTR, side_effect=_fake_ffmpeg_factory(False)):
res = client.get(f"/dub/download/{job_id}", params={"preserve_bg": False})
assert res.status_code == 500
assert "no output file" in res.json()["detail"]