clipwright/tests/test_provenance.py
pedro-bento 18f6c0fda9
All checks were successful
CI / unit (pull_request) Successful in 47s
CI / backend-e2e (pull_request) Successful in 1m21s
CI / dashboard-e2e (pull_request) Successful in 2m28s
feat(pipeline): integrate profiles, layout, handoff, and provenance
2026-10-05 16:38:42 +01:00

157 lines
6.5 KiB
Python

"""Retention provenance (PLAN #10): one versioned record per clip.
Instrumentation only. Every clip carries its active profile, predicted score,
start-boundary path (#2a hook-first vs the plain word snap), #6 bridge target
and publish target, so the future retention/analytics loop is a data pull rather
than a re-architecture. The loop itself is deliberately not built here.
"""
import asyncio
import json
import httpx
import pytest
main = pytest.importorskip("clipwright.pipeline.main")
from clipwright.pipeline import bridge, profiles # noqa: E402
def _clip():
return {
"start": 12.3, "end": 42.3,
"predicted_score": 85,
"video_title_for_youtube_short": "I broke the world",
"viral_hook_text": "I broke the world in one block",
}
class TestBuildProvenance:
def test_schema_version_and_keys_without_profile(self):
prov = main.build_provenance(_clip(), None)
assert prov["schema_version"] == main.PROVENANCE_SCHEMA_VERSION == 1
assert set(prov) == {"schema_version", "profile", "predicted_score",
"hook", "bridge", "publish"}
assert prov["profile"] is None
assert prov["predicted_score"] == 85
assert prov["publish"]["postiz_integration_id"] is None
assert set(prov["hook"]) == {"applied", "source", "start", "trim",
"proposed"}
assert set(prov["bridge"]) == {"present", "related_video_url"}
def test_profile_and_publish_target_are_recorded(self):
profile = profiles.Profile(
slug="cseea", default_postiz_integration_id="postiz-42")
prov = main.build_provenance(_clip(), profile)
assert prov["profile"] == "cseea"
assert prov["publish"]["postiz_integration_id"] == "postiz-42"
def test_hook_first_boundary_is_reused(self):
clip = _clip()
clip["hook_first"] = {"start": 10.0, "trim": 2.3, "proposed": 12.3}
prov = main.build_provenance(clip, None)
assert prov["hook"] == {"applied": True, "source": "hook_first",
"start": 10.0, "trim": 2.3, "proposed": 12.3}
def test_plain_snap_boundary_is_marked_when_no_hook_moved_it(self):
prov = main.build_provenance(_clip(), None)
assert prov["hook"]["applied"] is False
assert prov["hook"]["source"] == "snap"
assert prov["hook"]["start"] is None
assert prov["hook"]["trim"] is None
assert prov["hook"]["proposed"] is None
def test_bridge_presence_and_related_target(self):
clip = _clip()
assert main.build_provenance(clip, None)["bridge"]["present"] is False
target = "https://www.youtube.com/watch?v=LONGFORM123"
clip.update(bridge.build_bridge(None, clip=clip,
related_video_url=target))
prov = main.build_provenance(clip, None)
assert prov["bridge"] == {"present": True,
"related_video_url": target}
def test_metadata_merge_is_additive_and_round_trips(self):
"""The exact main.py sequence: bridge fields then provenance, JSON-safe."""
profile = profiles.load_profile("cseea")
clip = _clip()
before = set(clip)
clip.update(bridge.build_bridge(profile, clip=clip,
related_video_url="https://youtu.be/x"))
clip["provenance"] = main.build_provenance(clip, profile)
# Additive: the original keys survive untouched.
assert before <= set(clip)
data = {"shorts": [clip]}
reloaded = json.loads(json.dumps(data))
prov = reloaded["shorts"][0]["provenance"]
assert prov["schema_version"] == 1
assert prov["profile"] == "cseea"
assert set(prov) == {"schema_version", "profile", "predicted_score",
"hook", "bridge", "publish"}
app_module = pytest.importorskip("clipwright.api.app")
JOB_ID = "provenance-endpoint-test-job"
def _request(method, path):
async def _do():
transport = httpx.ASGITransport(app=app_module.app)
async with httpx.AsyncClient(transport=transport,
base_url="http://testserver") as client:
return await client.request(method, path)
return asyncio.run(_do())
@pytest.fixture()
def job(tmp_path, monkeypatch):
out_root = tmp_path / "output"
job_dir = out_root / JOB_ID
job_dir.mkdir(parents=True)
monkeypatch.setattr(app_module, "OUTPUT_DIR", str(out_root))
clip = {"start": 0.0, "end": 30.0,
"provenance": main.build_provenance(
{"start": 0.0, "end": 30.0, "predicted_score": 77}, None)}
meta = {"shorts": [clip],
"transcript": {"language": "none", "segments": []}}
(job_dir / "mytitle_metadata.json").write_text(json.dumps(meta))
app_module.jobs[JOB_ID] = {"status": "completed", "logs": [],
"result": {"clips": [dict(clip)]},
"user_id": None}
try:
yield job_dir
finally:
app_module.jobs.pop(JOB_ID, None)
class TestProvenanceEndpoint:
def test_returns_the_versioned_record(self, job):
resp = _request("GET", f"/api/clip/{JOB_ID}/0/provenance")
assert resp.status_code == 200
data = resp.json()
assert data["schema_version"] == 1
assert data["provenance"]["predicted_score"] == 77
assert data["provenance"]["hook"]["source"] == "snap"
def test_old_clip_without_a_record_is_404(self, tmp_path, monkeypatch):
out_root = tmp_path / "output2"
job_dir = out_root / "old-job"
job_dir.mkdir(parents=True)
monkeypatch.setattr(app_module, "OUTPUT_DIR", str(out_root))
meta = {"shorts": [{"start": 0.0, "end": 30.0}],
"transcript": {"language": "none", "segments": []}}
(job_dir / "mytitle_metadata.json").write_text(json.dumps(meta))
app_module.jobs["old-job"] = {"status": "completed", "logs": [],
"result": {"clips": [{"start": 0.0}]},
"user_id": None}
try:
resp = _request("GET", "/api/clip/old-job/0/provenance")
assert resp.status_code == 404
assert "No provenance" in resp.json()["detail"]
finally:
app_module.jobs.pop("old-job", None)
def test_unknown_clip_is_404(self, job):
resp = _request("GET", f"/api/clip/{JOB_ID}/99/provenance")
assert resp.status_code == 404