pipeline+ingest: one stage descriptor table and a real ingest stage

This commit is contained in:
alex committed 2026-11-24 10:51:54 +00:00
1 parent 799a810b60
commit 265b834858
6 files changed
+1120 -640

No files matched your search

+32 -20
View File
@@ -6,14 +6,14 @@ tests never open a socket and never download anything.
from __future__ import annotations
import re
from pathlib import Path
from typing import Any, ClassVar
import pytest
from yt_dlp.utils import DownloadError
from video_analyse.ingest import IngestResult, _safe_dir_name, fetch
from video_analyse.cache import StageCache
from video_analyse.ingest import IngestResult, fetch, ingest_key_params
URL = "https://example.invalid/watch?v=abc123&list=PL123"
DOWNLOAD_FILE_NAME = "My Clip [abc123].mp4"
@@ -136,8 +136,9 @@ async def test_fetch_downloads_http_url_into_cache(
result = await fetch(URL, cache_dir=cache_dir, quality=720)
slug = f"{_safe_dir_name(URL)}-q720"
expected_dir = (cache_dir / "downloads" / slug).resolve()
cache = StageCache(cache_dir)
key = cache.stage_key("ingest", ingest_key_params(URL, 720))
expected_dir = cache.source_dir(key).resolve()
assert result.downloaded is True
assert result.title == "My Clip"
assert result.info["id"] == "abc123"
@@ -145,6 +146,8 @@ async def test_fetch_downloads_http_url_into_cache(
assert result.path.is_file()
assert result.path.name == DOWNLOAD_FILE_NAME
assert result.path.parent == expected_dir
# The old parallel manifest cache is gone: the StageCache artifact is the cache.
assert not (result.path.parent / "ingest-info.json").exists()
assert len(fake_ydl.calls) == 1
options = fake_ydl.calls[0]
@@ -165,12 +168,27 @@ async def test_fetch_uses_a_stable_directory_per_url(
home_dirs = {Path(options["paths"]["home"]) for options in fake_ydl.calls}
assert len(home_dirs) == 1
assert first.downloaded is True
assert second.downloaded is False # reused from the URL+quality cache directory
assert second.downloaded is False # reused from the StageCache ingest artifact
assert len(fake_ydl.calls) == 1
assert first.path == second.path
assert first.info == second.info
async def test_fetch_force_bypasses_the_cached_result_and_redownloads(
fake_ydl: type[_FakeYoutubeDL], tmp_path: Path
) -> None:
cache_dir = tmp_path / "cache"
first = await fetch(URL, cache_dir=cache_dir)
assert first.downloaded is True
assert len(fake_ydl.calls) == 1
forced = await fetch(URL, cache_dir=cache_dir, force=True)
assert forced.downloaded is True
assert len(fake_ydl.calls) == 2
assert forced.path.is_file()
async def test_fetch_keys_the_download_directory_by_quality(
fake_ydl: type[_FakeYoutubeDL], tmp_path: Path
) -> None:
@@ -199,19 +217,13 @@ async def test_fetch_maps_download_errors_to_actionable_runtime_error(
assert excinfo.value.__cause__ is not None
def test_safe_dir_name_is_stable_filesystem_safe_and_url_specific() -> None:
slug = _safe_dir_name(URL)
assert slug == _safe_dir_name(URL)
assert re.fullmatch(r"[A-Za-z0-9._-]+", slug)
assert "/" not in slug and "?" not in slug and "&" not in slug
assert len(slug) <= 75
assert slug != _safe_dir_name("https://example.invalid/other?v=abc123")
# a URL with nothing usable still yields a directory name
assert re.fullmatch(r"[A-Za-z0-9._-]+", _safe_dir_name("https://example.invalid/"))
async def test_fetch_never_puts_url_credentials_in_the_cache_path(
fake_ydl: type[_FakeYoutubeDL], tmp_path: Path
) -> None:
result = await fetch(
"https://alice:s3cr3t@example.invalid/watch?v=abc",
cache_dir=tmp_path / "cache",
)
def test_safe_dir_name_never_contains_url_credentials() -> None:
slug = _safe_dir_name("https://alice:s3cr3t@example.invalid/private/clip.mp4")
assert "s3cr3t" not in slug
assert "alice" not in slug
assert slug.startswith("example.invalid")
assert "s3cr3t" not in str(result.path)
assert "alice" not in str(result.path)