Three-level end-to-end suite, all deterministic (no retries, no sleeps, no unmocked network): - tests/e2e/api (209 tests): every route in clipwright/api/app.py + free_tools — health/config, uploads, process submit matrix + worker/webhook/ auto-retry/resume, status/source/download-all, edit/effects, transcript/EDL/ rerender, scenes/reframe, subtitle, hook, Postiz, thumbnails, free tools, and the security/path guards. - tests/e2e/pipeline (136 tests): every stage with real ffmpeg and stubbed Gemini/yt-dlp/ASR/ML — download, transcribe, scene detect, selection, layout, reframe/render, subtitles, hooks, watermark, thumbnails, plus the real CLI full pipeline via stub_env. - dashboard/e2e (19 Playwright specs): route-interception state machine covering every dashboard flow; one real-backend spec left test.fixme. - Foundation: tests/e2e/conftest.py (state isolation, no-network guard, ffmpeg media factory, disk-seeded jobs), FakePopen worker harness, child-process stub_env, architecture-consistent fixtures. - CI: unit + backend-e2e + dashboard-e2e jobs in the python+node container; ffmpeg + Playwright installed in-job; no setup-node/cache (act runner). Verified: unit 790 passed; API E2E 209 passed; pipeline E2E 136 passed; Playwright 19 passed / 1 skipped.
242 lines
10 KiB
Python
242 lines
10 KiB
Python
"""L3 E2E: the anonymous free tools (``clipwright.api.free_tools``).
|
|
|
|
The network seams are stubbed (``_fetch_with_slot`` for the transcript, the
|
|
``_generate`` Gemini call for metadata), so the real route logic runs: URL
|
|
validation, caption/error mapping, the 24 h cache, the in-process rate limiter
|
|
and the metadata mode/shape handling.
|
|
"""
|
|
|
|
import asyncio
|
|
from collections import OrderedDict
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
pytestmark = pytest.mark.e2e
|
|
|
|
VIDEO_A = "arj7oStGLkU"
|
|
VIDEO_B = "dQw4w9WgXcQ"
|
|
|
|
|
|
@pytest.fixture
|
|
def ft(monkeypatch):
|
|
"""A clean free_tools module: fresh cache and a fresh limiter per test."""
|
|
from clipwright.api import free_tools
|
|
|
|
monkeypatch.setattr(free_tools, "_TRANSCRIPT_CACHE", OrderedDict())
|
|
monkeypatch.setattr(free_tools, "_limiter", free_tools.RateLimiter(free_tools.LIMITS))
|
|
return free_tools
|
|
|
|
|
|
@pytest.fixture
|
|
def transcript_stub(monkeypatch, ft):
|
|
"""Stub ``_fetch_with_slot`` (what the route hands to ``to_thread``)."""
|
|
state = SimpleNamespace(
|
|
calls=[],
|
|
error=None,
|
|
result={"video_id": "x", "title": "A video", "channel": "Chan",
|
|
"duration": 12, "languages": [], "language": "en", "kind": "manual",
|
|
"segments": [{"start": 0.0, "dur": 1.0, "text": "hello"}]},
|
|
)
|
|
|
|
def fake(video_id, lang):
|
|
state.calls.append((video_id, lang))
|
|
if state.error:
|
|
raise state.error
|
|
return state.result
|
|
|
|
monkeypatch.setattr(ft, "_fetch_with_slot", fake)
|
|
return state
|
|
|
|
|
|
@pytest.fixture
|
|
def generate_stub(monkeypatch, ft):
|
|
"""Stub the Gemini ``_generate`` seam used by the metadata route."""
|
|
state = SimpleNamespace(calls=[], result={"titles": [{"title": "A", "angle": "x"}]},
|
|
error=None)
|
|
|
|
def fake(prompt):
|
|
state.calls.append(prompt)
|
|
if state.error:
|
|
raise state.error
|
|
return state.result
|
|
|
|
monkeypatch.setattr(ft, "_generate", fake)
|
|
return state
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# GET /api/tools/youtube-transcript
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
async def test_transcript_success_then_served_from_cache(client, ft, transcript_stub):
|
|
r1 = await client.get("/api/tools/youtube-transcript",
|
|
params={"url": f"https://youtu.be/{VIDEO_A}"})
|
|
assert r1.status_code == 200, r1.text
|
|
assert r1.json()["cached"] is False
|
|
assert r1.json()["segments"] == [{"start": 0.0, "dur": 1.0, "text": "hello"}]
|
|
|
|
# A bare id resolves to the same video id -> cache hit, no second fetch.
|
|
r2 = await client.get("/api/tools/youtube-transcript", params={"url": VIDEO_A})
|
|
assert r2.status_code == 200
|
|
assert r2.json()["cached"] is True
|
|
assert len(transcript_stub.calls) == 1
|
|
assert transcript_stub.calls[0][0] == VIDEO_A
|
|
|
|
|
|
async def test_transcript_bad_url_is_400_without_work(client, ft, transcript_stub):
|
|
r = await client.get("/api/tools/youtube-transcript",
|
|
params={"url": "https://youtube.com/@ted"})
|
|
assert r.status_code == 400
|
|
assert r.json()["detail"]["code"] == "bad_url"
|
|
assert transcript_stub.calls == []
|
|
|
|
|
|
async def test_transcript_no_captions_is_404_and_cached(client, ft, transcript_stub):
|
|
transcript_stub.error = ft.TranscriptError("no_captions", "none", 404)
|
|
r1 = await client.get("/api/tools/youtube-transcript", params={"url": VIDEO_A})
|
|
assert r1.status_code == 404
|
|
assert r1.json()["detail"]["code"] == "no_captions"
|
|
|
|
# The verdict about the video is cached: no second fetch, same 404.
|
|
r2 = await client.get("/api/tools/youtube-transcript", params={"url": VIDEO_A})
|
|
assert r2.status_code == 404
|
|
assert r2.json()["detail"]["code"] == "no_captions"
|
|
assert len(transcript_stub.calls) == 1
|
|
|
|
|
|
async def test_transcript_unavailable_is_422(client, ft, transcript_stub):
|
|
transcript_stub.error = ft.TranscriptError("unavailable", "private", 422)
|
|
r = await client.get("/api/tools/youtube-transcript", params={"url": VIDEO_A})
|
|
assert r.status_code == 422
|
|
assert r.json()["detail"]["code"] == "unavailable"
|
|
|
|
|
|
async def test_transcript_blocked_is_503(client, ft, transcript_stub):
|
|
transcript_stub.error = ft.TranscriptError("youtube_blocked", "blocked", 503)
|
|
r = await client.get("/api/tools/youtube-transcript", params={"url": VIDEO_A})
|
|
assert r.status_code == 503
|
|
assert r.json()["detail"]["code"] == "youtube_blocked"
|
|
|
|
|
|
async def test_transcript_busy_is_503(client, ft, transcript_stub):
|
|
transcript_stub.error = ft.TranscriptError("busy", "busy", 503)
|
|
r = await client.get("/api/tools/youtube-transcript", params={"url": VIDEO_A})
|
|
assert r.status_code == 503
|
|
assert r.json()["detail"]["code"] == "busy"
|
|
|
|
|
|
async def test_transcript_timeout_is_504(client, ft, transcript_stub):
|
|
transcript_stub.error = asyncio.TimeoutError()
|
|
r = await client.get("/api/tools/youtube-transcript", params={"url": VIDEO_A})
|
|
assert r.status_code == 504
|
|
assert r.json()["detail"]["code"] == "timeout"
|
|
|
|
|
|
async def test_transcript_per_ip_rate_limit_is_429(client, ft, transcript_stub, monkeypatch):
|
|
monkeypatch.setattr(
|
|
ft, "_limiter",
|
|
ft.RateLimiter({"transcript": {"per_ip": [(600, 1)], "global_day": 1000}}))
|
|
r1 = await client.get("/api/tools/youtube-transcript", params={"url": VIDEO_A})
|
|
assert r1.status_code == 200
|
|
r2 = await client.get("/api/tools/youtube-transcript", params={"url": VIDEO_B})
|
|
assert r2.status_code == 429
|
|
assert r2.json()["detail"]["code"] == "rate_limited"
|
|
|
|
|
|
async def test_transcript_global_rate_limit_is_429_busy(client, ft, transcript_stub,
|
|
monkeypatch):
|
|
monkeypatch.setattr(
|
|
ft, "_limiter",
|
|
ft.RateLimiter({"transcript": {"per_ip": [(600, 100)], "global_day": 1}}))
|
|
r1 = await client.get("/api/tools/youtube-transcript", params={"url": VIDEO_A})
|
|
assert r1.status_code == 200
|
|
r2 = await client.get("/api/tools/youtube-transcript", params={"url": VIDEO_B})
|
|
assert r2.status_code == 429
|
|
assert r2.json()["detail"]["code"] == "busy"
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# POST /api/tools/youtube-metadata
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
async def test_metadata_bad_mode_is_400(client, ft, monkeypatch):
|
|
monkeypatch.setenv("GEMINI_API_KEY", "k")
|
|
r = await client.post("/api/tools/youtube-metadata",
|
|
json={"mode": "bogus", "topic": "a" * 40})
|
|
assert r.status_code == 400
|
|
assert r.json()["detail"]["code"] == "bad_mode"
|
|
|
|
|
|
async def test_metadata_too_short_topic_is_400(client, ft, monkeypatch):
|
|
monkeypatch.setenv("GEMINI_API_KEY", "k")
|
|
r = await client.post("/api/tools/youtube-metadata",
|
|
json={"mode": "titles", "topic": "short"})
|
|
assert r.status_code == 400
|
|
assert r.json()["detail"]["code"] == "too_short"
|
|
|
|
|
|
async def test_metadata_not_configured_is_503(client, ft, monkeypatch):
|
|
monkeypatch.delenv("GEMINI_API_KEY", raising=False)
|
|
r = await client.post("/api/tools/youtube-metadata",
|
|
json={"mode": "titles", "topic": "A video about sourdough bread"})
|
|
assert r.status_code == 503
|
|
assert r.json()["detail"]["code"] == "not_configured"
|
|
|
|
|
|
async def test_metadata_titles_mode(client, ft, generate_stub, monkeypatch):
|
|
monkeypatch.setenv("GEMINI_API_KEY", "k")
|
|
generate_stub.result = {"titles": [{"title": "How to bake", "angle": "how-to"}]}
|
|
r = await client.post("/api/tools/youtube-metadata",
|
|
json={"mode": "titles", "topic": "A video about sourdough bread",
|
|
"keyword": "sourdough"})
|
|
assert r.status_code == 200, r.text
|
|
assert r.json()["mode"] == "titles"
|
|
assert r.json()["titles"] == [{"title": "How to bake", "angle": "how-to"}]
|
|
# The keyword rides into the prompt.
|
|
assert "sourdough" in generate_stub.calls[0]
|
|
|
|
|
|
async def test_metadata_description_mode(client, ft, generate_stub, monkeypatch):
|
|
monkeypatch.setenv("GEMINI_API_KEY", "k")
|
|
generate_stub.result = {"description": "A long enough description."}
|
|
r = await client.post("/api/tools/youtube-metadata",
|
|
json={"mode": "description",
|
|
"topic": "A video about sourdough bread"})
|
|
assert r.status_code == 200
|
|
assert r.json()["description"] == "A long enough description."
|
|
|
|
|
|
async def test_metadata_tags_mode(client, ft, generate_stub, monkeypatch):
|
|
monkeypatch.setenv("GEMINI_API_KEY", "k")
|
|
generate_stub.result = {"tags": ["sourdough", "baking"]}
|
|
r = await client.post("/api/tools/youtube-metadata",
|
|
json={"mode": "tags", "topic": "A video about sourdough bread"})
|
|
assert r.status_code == 200
|
|
assert r.json()["tags"] == ["sourdough", "baking"]
|
|
assert r.json()["characters"] == len("sourdough,baking")
|
|
|
|
|
|
async def test_metadata_generation_failure_is_502_after_retry(client, ft, generate_stub,
|
|
monkeypatch):
|
|
monkeypatch.setenv("GEMINI_API_KEY", "k")
|
|
generate_stub.error = RuntimeError("model exploded")
|
|
r = await client.post("/api/tools/youtube-metadata",
|
|
json={"mode": "titles", "topic": "A video about sourdough bread"})
|
|
assert r.status_code == 502
|
|
assert r.json()["detail"]["code"] == "generation_failed"
|
|
assert len(generate_stub.calls) == 2 # one retry
|
|
|
|
|
|
async def test_metadata_rate_limit_is_429(client, ft, generate_stub, monkeypatch):
|
|
monkeypatch.setenv("GEMINI_API_KEY", "k")
|
|
monkeypatch.setattr(
|
|
ft, "_limiter",
|
|
ft.RateLimiter({"metadata": {"per_ip": [(600, 1)], "global_day": 1000}}))
|
|
generate_stub.result = {"titles": [{"title": "A", "angle": "x"}]}
|
|
body = {"mode": "titles", "topic": "A video about sourdough bread"}
|
|
r1 = await client.post("/api/tools/youtube-metadata", json=body)
|
|
assert r1.status_code == 200
|
|
r2 = await client.post("/api/tools/youtube-metadata", json=body)
|
|
assert r2.status_code == 429
|
|
assert r2.json()["detail"]["code"] == "rate_limited"
|