clipwright/tests/test_transcript_checkpoint.py
pedro-bento 83ad1feaba
All checks were successful
CI / backend-tests (pull_request) Successful in 30s
CI / frontend-build (pull_request) Successful in 29s
refactor: move backend into the clipwright package
Restructure the 37 root-level Python modules into a proper package:
  clipwright/{core,api,pipeline,layouts,media,integrations,tools}

- Rewrite every local import (module-level and deferred) to absolute
  clipwright.<group>.<mod> form; no legacy root shims or re-exports.
- Add clipwright/core/paths.py and route fonts/assets/cookies through it.
- Entry points: uvicorn clipwright.api.app:app; jobs run as
  python -m clipwright.pipeline.main; probe as -m ...quality_probe.
- child_env() appends REPO_ROOT to PYTHONPATH so the -m workers work.
- transcribe_backends: sys.modules key now clipwright.media.scene_detection.
- Tooling: root pyproject.toml (pytest pythonpath=[.], ruff), shared
  requirements-test.txt, CI installs it, Docker PYTHONPATH=/app.
- Tests: all imports/fakes/source-scans repointed; new tests/test_package_layout.py
  enforces no root modules, docstring-only __init__, and no legacy imports.
- Docs/comments updated for the new layout.

Backend: 744 passed, 19 skipped. Frontend lint+build green.
2026-10-04 20:40:38 +01:00

86 lines
3.4 KiB
Python

"""Transcript checkpoint: a job re-run after a container restart must not
transcribe (the slow, paid stage) a second time, and must never reuse the
transcript of a DIFFERENT video.
app.py re-enqueues an interrupted job with the same command and the same
output directory (resume manifest). main.py leaves the finished transcript
there, tied to the source's name and duration, picks it up on the re-run, and
removes it once the job completes.
"""
import json
import os
import pytest
main = pytest.importorskip("clipwright.pipeline.main") # needs cv2/mediapipe, absent in minimal CI
TRANSCRIPT = {"text": "hola", "language": "es",
"segments": [{"start": 0.0, "end": 1.0, "text": "hola", "words": []}]}
SRC = "/app/output/job1/video.mp4"
DUR = 58.64
def _save(d, transcript=TRANSCRIPT, src=SRC, dur=DUR):
main.save_transcript_checkpoint(str(d), transcript, src, dur)
def _load(d, src=SRC, dur=DUR):
return main.load_transcript_checkpoint(str(d), src, dur)
class TestRoundTrip:
def test_same_source_is_reused(self, tmp_path):
_save(tmp_path)
assert os.path.isfile(tmp_path / main.TRANSCRIPT_CHECKPOINT)
assert _load(tmp_path) == TRANSCRIPT
def test_resumed_job_redownloads_to_the_same_name(self, tmp_path):
# The re-run's file lives at the same path; a slightly different
# duration reading (container decoders differ by a frame) still matches.
_save(tmp_path)
assert _load(tmp_path, dur=DUR + 0.3) == TRANSCRIPT
def test_missing_checkpoint_is_none(self, tmp_path):
assert _load(tmp_path) is None
class TestNeverTheWrongTranscript:
def test_another_file_in_the_same_directory_is_ignored(self, tmp_path):
# THE RISK: CLI run on A dies after transcribing, then B is processed
# in the same directory without -o. B must get its own transcript.
_save(tmp_path, src="/videos/A.mp4")
assert _load(tmp_path, src="/videos/B.mp4") is None
def test_same_name_but_different_length_is_ignored(self, tmp_path):
_save(tmp_path, dur=600.0)
assert _load(tmp_path, dur=58.6) is None
def test_legacy_shape_without_source_is_ignored(self, tmp_path):
(tmp_path / main.TRANSCRIPT_CHECKPOINT).write_text(json.dumps(TRANSCRIPT))
assert _load(tmp_path) is None
class TestRobustness:
def test_empty_transcript_is_ignored(self, tmp_path):
_save(tmp_path, transcript={"segments": []})
assert _load(tmp_path) is None
def test_truncated_file_is_ignored(self, tmp_path):
(tmp_path / main.TRANSCRIPT_CHECKPOINT).write_text('{"source": {"name": "vi')
assert _load(tmp_path) is None
def test_unwritable_directory_does_not_raise(self, tmp_path):
main.save_transcript_checkpoint(str(tmp_path / "missing" / "dir"), TRANSCRIPT, SRC, DUR)
def test_clear_is_idempotent(self, tmp_path):
_save(tmp_path)
main.clear_transcript_checkpoint(str(tmp_path))
main.clear_transcript_checkpoint(str(tmp_path))
assert _load(tmp_path) is None
def test_checkpoint_is_hidden_from_job_listings(self):
# app.py globs *_metadata.json and *.mp4 in the job dir; a dotfile
# named unlike either can never be mistaken for a deliverable.
assert main.TRANSCRIPT_CHECKPOINT.startswith(".")
assert not main.TRANSCRIPT_CHECKPOINT.endswith("_metadata.json")
assert not main.TRANSCRIPT_CHECKPOINT.endswith(".mp4")