132 lines
5.3 KiB
Python
132 lines
5.3 KiB
Python
"""Bridge copy + opt-in first-comment endpoints (PLAN #6).
|
|
|
|
Confirms the generated copy/checklist is exposed from clip metadata, that the
|
|
comment endpoint is guarded when the YouTube Data API is unconfigured (clear
|
|
error, no post), and that posting only happens when the caller opts in.
|
|
"""
|
|
import asyncio
|
|
import json
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
app_module = pytest.importorskip("clipwright.api.app")
|
|
from clipwright.integrations import youtube_data # noqa: E402
|
|
|
|
JOB_ID = "bridge-endpoint-test-job"
|
|
TARGET = "https://www.youtube.com/watch?v=LONGFORM123"
|
|
|
|
CHECKLIST = [
|
|
"Open YouTube Studio -> Content -> this Short -> Edit.",
|
|
f"Details -> Related video: set it to {TARGET} (no API).",
|
|
"Post the generated first comment.",
|
|
"Pin the first comment (no API).",
|
|
]
|
|
|
|
|
|
def _request(method, path, json_body=None):
|
|
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, json=json_body)
|
|
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,
|
|
"video_title_for_youtube_short": "I broke the world",
|
|
"video_url": f"/videos/{JOB_ID}/mytitle_clip_1.mp4",
|
|
# The fields main.py merges into clip metadata before writing.
|
|
"related_video_url": TARGET,
|
|
"description_bridge": f"▶ Full video: {TARGET}",
|
|
"first_comment": f"▶ Full video: {TARGET}",
|
|
"checklist": CHECKLIST,
|
|
"manual_pin_required": True,
|
|
}
|
|
meta = {"shorts": [clip], "transcript": {"language": "none", "segments": []},
|
|
"source_video": "src.mp4", "output_format": "auto"}
|
|
(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 {"dir": job_dir, "meta_path": job_dir / "mytitle_metadata.json"}
|
|
finally:
|
|
app_module.jobs.pop(JOB_ID, None)
|
|
|
|
|
|
@pytest.fixture()
|
|
def unconfigured(monkeypatch):
|
|
for name in (youtube_data.CLIENT_ID_ENV, youtube_data.CLIENT_SECRET_ENV,
|
|
youtube_data.REFRESH_TOKEN_ENV):
|
|
monkeypatch.delenv(name, raising=False)
|
|
|
|
|
|
class TestBridgeCopyEndpoint:
|
|
def test_returns_generated_copy_and_checklist(self, job, unconfigured):
|
|
resp = _request("GET", f"/api/clip/{JOB_ID}/0/bridge")
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["related_video_url"] == TARGET
|
|
assert TARGET in data["description_bridge"]
|
|
assert TARGET in data["first_comment"]
|
|
assert data["checklist"] == CHECKLIST
|
|
assert data["manual_pin_required"] is True
|
|
assert data["comment_api_configured"] is False
|
|
|
|
def test_unknown_clip_is_404(self, job, unconfigured):
|
|
resp = _request("GET", f"/api/clip/{JOB_ID}/99/bridge")
|
|
assert resp.status_code == 404
|
|
|
|
|
|
class TestBridgeCommentEndpoint:
|
|
def test_unconfigured_is_a_clear_400_and_posts_nothing(self, job, unconfigured,
|
|
monkeypatch):
|
|
calls = []
|
|
monkeypatch.setattr(app_module.youtube_data, "post_first_comment",
|
|
lambda *a, **k: calls.append((a, k)))
|
|
resp = _request("POST", f"/api/clip/{JOB_ID}/0/bridge/comment",
|
|
{"video_id": "short-1"})
|
|
assert resp.status_code == 400
|
|
assert "not configured" in resp.json()["detail"]
|
|
assert calls == []
|
|
|
|
def test_configured_posts_the_first_comment(self, job, monkeypatch):
|
|
monkeypatch.setenv(youtube_data.CLIENT_ID_ENV, "cid")
|
|
monkeypatch.setenv(youtube_data.CLIENT_SECRET_ENV, "secret")
|
|
monkeypatch.setenv(youtube_data.REFRESH_TOKEN_ENV, "refresh")
|
|
calls = []
|
|
|
|
def fake_post(video_id, text):
|
|
calls.append((video_id, text))
|
|
return {"id": "comment-1"}
|
|
|
|
monkeypatch.setattr(app_module.youtube_data, "post_first_comment", fake_post)
|
|
resp = _request("POST", f"/api/clip/{JOB_ID}/0/bridge/comment",
|
|
{"video_id": "short-1"})
|
|
assert resp.status_code == 200
|
|
assert resp.json()["posted"] is True
|
|
assert resp.json()["comment_id"] == "comment-1"
|
|
assert resp.json()["manual_pin_required"] is True
|
|
# The generated first_comment was used; posting is explicit, not auto.
|
|
assert calls == [("short-1", f"▶ Full video: {TARGET}")]
|
|
|
|
def test_caller_can_override_the_comment_text(self, job, monkeypatch):
|
|
monkeypatch.setenv(youtube_data.CLIENT_ID_ENV, "cid")
|
|
monkeypatch.setenv(youtube_data.CLIENT_SECRET_ENV, "secret")
|
|
monkeypatch.setenv(youtube_data.REFRESH_TOKEN_ENV, "refresh")
|
|
monkeypatch.setattr(app_module.youtube_data, "post_first_comment",
|
|
lambda video_id, text: {"id": "c"})
|
|
resp = _request("POST", f"/api/clip/{JOB_ID}/0/bridge/comment",
|
|
{"video_id": "short-1", "text": "custom"})
|
|
assert resp.status_code == 200
|