clipwright/tests/test_watermark_delivery.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

141 lines
6.7 KiB
Python

"""The watermark is a served COPY, never the file itself.
Until 30-sep-2026 the mark was burned into the canonical reframe, so every
derivative (hook, captions, recut) inherited it and nothing short of running
the whole job again could remove it.
Now every file the pipeline and the editor write is clean, and the served file
on a marked job is a ``wm_`` twin of the final one (``main.mark_delivery``).
"""
import os
import pytest
from clipwright.media import watermarked
class TestNames:
def test_marked_and_clean_are_inverses(self):
assert watermarked.marked_name("a_clip_1.mp4") == "wm_a_clip_1.mp4"
assert watermarked.marked_name("wm_a_clip_1.mp4") == "wm_a_clip_1.mp4"
assert watermarked.clean_name("wm_subtitled_1_hooked_2_a_clip_1.mp4") == \
"subtitled_1_hooked_2_a_clip_1.mp4"
assert watermarked.clean_name("a_clip_1.mp4") == "a_clip_1.mp4"
assert watermarked.is_marked("wm_x.mp4") and not watermarked.is_marked("x.mp4")
def test_clean_deliverables_are_clips_and_their_derivatives(self):
for name in ("t_clip_1.mp4", "subtitled_1_t_clip_1.mp4", "hooked_1_t_clip_2.mp4",
"recut_1_ab_t_clip_1.mp4", "edited_t_clip_1.mp4",
"translated_es_t_clip_1.mp4"):
assert watermarked.is_clean_deliverable(name), name
# The marked copy itself, the editor's scratch files, and sidecars are
# not clean deliverables: they stay servable on a marked job.
for name in ("wm_t_clip_1.mp4", "temp_preview_0.mp4", "temp_scene_ab_001.jpg",
"subs_0_1.ass", "t_clip_1.layout.json"):
assert not watermarked.is_clean_deliverable(name), name
def test_marker_round_trip(self, tmp_path):
d = str(tmp_path)
assert not watermarked.job_is_marked(d)
watermarked.mark_job(d)
assert watermarked.job_is_marked(d)
watermarked.unmark_job(d)
assert not watermarked.job_is_marked(d)
watermarked.unmark_job(d) # idempotent
class TestMarkDelivery:
"""The pipeline's last step on a marked job: a wm_ copy next to the clean final."""
def test_writes_the_copy_and_leaves_the_final_alone(self, tmp_path):
main = pytest.importorskip("clipwright.pipeline.main")
final = tmp_path / "subtitled_1_t_clip_1.mp4"
final.write_bytes(b"clean")
calls = []
def marker(src, out):
calls.append((os.path.basename(src), os.path.basename(out)))
with open(out, "wb") as f:
f.write(b"marked")
return True
served = main.mark_delivery(str(final), marker=marker)
assert os.path.basename(served) == "wm_subtitled_1_t_clip_1.mp4"
assert calls == [("subtitled_1_t_clip_1.mp4", "wm_subtitled_1_t_clip_1.mp4")]
assert final.read_bytes() == b"clean"
def test_reuses_an_existing_copy(self, tmp_path):
main = pytest.importorskip("clipwright.pipeline.main")
(tmp_path / "t_clip_1.mp4").write_bytes(b"clean")
(tmp_path / "wm_t_clip_1.mp4").write_bytes(b"marked")
served = main.mark_delivery(str(tmp_path / "t_clip_1.mp4"),
marker=lambda *_: pytest.fail("re-encoded"))
assert os.path.basename(served) == "wm_t_clip_1.mp4"
def test_fails_open_under_the_marked_name(self, tmp_path):
"""A failed mark still serves the clip, and still under the wm_ name:
the /videos guard refuses the clean deliverables of a marked job, so a
clean name here would be a clip the user cannot play."""
main = pytest.importorskip("clipwright.pipeline.main")
(tmp_path / "t_clip_1.mp4").write_bytes(b"clean")
served = main.mark_delivery(str(tmp_path / "t_clip_1.mp4"),
marker=lambda *_: False)
assert os.path.basename(served) == "wm_t_clip_1.mp4"
assert (tmp_path / "wm_t_clip_1.mp4").read_bytes() == b"clean"
class TestWalkBacks:
"""The mark is the outermost layer: every editor derivation strips it first."""
def test_strip_captions_walks_through_the_mark(self, tmp_path):
app = pytest.importorskip("clipwright.api.app")
d = str(tmp_path)
for name in ("t_clip_1.mp4", "hooked_2_t_clip_1.mp4",
"subtitled_3_hooked_2_t_clip_1.mp4",
"wm_subtitled_3_hooked_2_t_clip_1.mp4"):
(tmp_path / name).write_bytes(b"x")
assert app._strip_burned_captions(d, "wm_subtitled_3_hooked_2_t_clip_1.mp4") == \
"hooked_2_t_clip_1.mp4"
assert app._strip_burned_hook(d, "hooked_2_t_clip_1.mp4") == "t_clip_1.mp4"
# The mark is a copy of a file that is on disk: it walks back the same.
assert app._strip_burned_hook(d, "wm_hooked_2_t_clip_1.mp4") == "t_clip_1.mp4"
def test_strip_keeps_the_mark_when_the_twin_is_gone(self, tmp_path):
app = pytest.importorskip("clipwright.api.app")
(tmp_path / "wm_t_clip_1.mp4").write_bytes(b"x")
assert app._strip_burned_captions(str(tmp_path), "wm_t_clip_1.mp4") == "wm_t_clip_1.mp4"
def test_canonical_clip_file_prefers_the_marked_copy(self, tmp_path):
app = pytest.importorskip("clipwright.api.app")
import time
(tmp_path / "t_clip_1.mp4").write_bytes(b"x")
(tmp_path / "subtitled_5_t_clip_1.mp4").write_bytes(b"x")
time.sleep(0.02)
(tmp_path / "wm_subtitled_5_t_clip_1.mp4").write_bytes(b"x")
os.utime(tmp_path / "wm_subtitled_5_t_clip_1.mp4", None)
assert app._canonical_clip_file(str(tmp_path), "t", 0) == "wm_subtitled_5_t_clip_1.mp4"
class TestMediaGuard:
"""A marked job's clean twins are not fetchable by stripping wm_ off the URL."""
def test_marked_job_refuses_clean_clips_only(self, tmp_path, monkeypatch):
app = pytest.importorskip("clipwright.api.app")
monkeypatch.setattr(app, "OUTPUT_DIR", str(tmp_path))
(tmp_path / "job1").mkdir()
watermarked.mark_job(str(tmp_path / "job1"))
assert app._media_guard("job1/wm_subtitled_1_t_clip_1.mp4")
assert not app._media_guard("job1/subtitled_1_t_clip_1.mp4")
assert not app._media_guard("job1/t_clip_1.mp4")
# Editor scratch files and sidecars stay servable.
assert app._media_guard("job1/temp_preview_0.mp4")
assert app._media_guard("job1/subs_0_1.ass")
# And a dotfile never is, marker included.
assert not app._media_guard("job1/.marked")
def test_unmarked_job_serves_everything_deliverable(self, tmp_path, monkeypatch):
app = pytest.importorskip("clipwright.api.app")
monkeypatch.setattr(app, "OUTPUT_DIR", str(tmp_path))
(tmp_path / "job2").mkdir()
assert app._media_guard("job2/t_clip_1.mp4")
assert not app._media_guard("job2/t_metadata.json")