Skip to content

World's first open-source, agentic video production system. 12 production pipelines, 100+ tools, 700+ agent skill and production-knowledge files. Turn your AI coding assistant into a full video production studio.

test(stock-sources): pin the search transport-error contract

Roger Brogan450dfcf644cf
diff --git a/tests/tools/test_stock_source_adapters.py b/tests/tools/test_stock_source_adapters.py
index da6269ce..61fa1ddb 100644
--- a/tests/tools/test_stock_source_adapters.py
+++ b/tests/tools/test_stock_source_adapters.py
@@ -1,4 +1,9 @@
-from tools.video.stock_sources import all_sources
+import sys
+import types
+
+import pytest
+
+from tools.video.stock_sources import SearchFilters, all_sources, get_source
from tools.video.stock_sources.unsplash import _build_download_url, _orientation_for_unsplash
from tools.video.stock_sources.wikimedia import (
_build_search_queries,
@@ -70,3 +75,147 @@ def test_unsplash_helpers_preserve_query_params():
assert "ixid=abc" in url
assert "w=1920" in url
assert "fm=jpg" in url
+
+
+# ---------------------------------------------------------------------
+# Transport-error contract (issue #511)
+#
+# `base.StockSource.search` states the rule: "Network errors should be
+# raised — the corpus builder catches and logs per-source so one flaky
+# API doesn't poison the whole run." An adapter that swallows the error
+# and returns `[]` is indistinguishable from a source that genuinely has
+# nothing to offer, so `direct_clip_search` reports `success: True,
+# clips_downloaded: 0, errors: []` on a run where every request failed.
+#
+# The two tests below pin both halves of the contract across *every*
+# registered adapter, so a new source cannot quietly reintroduce the bug.
+# ---------------------------------------------------------------------
+
+# Adapters that gate on a key before they reach the network. Without
+# these the key-gated sources would return early and the test would
+# assert nothing.
+_SOURCE_CREDENTIALS = {
+ "COVERR_API_KEY": "test-coverr",
+ "NARA_API_KEY": "test-nara",
+ "NASA_API_KEY": "test-nasa",
+ "PEXELS_API_KEY": "test-pexels",
+ "PIXABAY_API_KEY": "test-pixabay",
+ "POND5_API_KEY": "test-pond5",
+ "UNSPLASH_ACCESS_KEY": "test-unsplash",
+ "VIDEVO_API_KEY": "test-videvo",
+}
+
+# Adapters that still swallow transport failures, recorded as known
+# defects rather than encoded as expected behavior. `strict=True` means
+# the suite fails the moment one of them is fixed and left in this map.
+_STILL_SWALLOWS_TRANSPORT_ERRORS = {
+ "archive_org": "#511 follow-up: query cascade continues past a failed strategy",
+ "wikimedia": "#511 follow-up: query cascade continues past a failed strategy",
+ "pond5_pd": "#511 follow-up: API failure falls through to the web fallback",
+}
+
+
+class TransportError(Exception):
+ """Distinct failure type so the assertion cannot pass by accident."""
+
+
+class _EmptyOkResponse:
+ """A genuine 200 that simply carries no results."""
+
+ status_code = 200
+ text = "<html><body></body></html>"
+ content = b""
+
+ def raise_for_status(self):
+ return None
+
+ def json(self):
+ return {}
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *_exc):
+ return False
+
+
+class _EmptySoup:
+ """Stand-in for `BeautifulSoup`: parses anything, finds nothing."""
+
+ def __init__(self, *_args, **_kwargs):
+ pass
+
+ def select(self, *_args, **_kwargs):
+ return []
+
+ def select_one(self, *_args, **_kwargs):
+ return None
+
+ def find_all(self, *_args, **_kwargs):
+ return []
+
+ def find(self, *_args, **_kwargs):
+ return None
+
+
+def _install_fake_transport(monkeypatch, fake_get):
+ """Point every adapter's lazy `import requests` at `fake_get`.
+
+ `bs4` is stubbed alongside it: it is an optional dependency (the
+ scraping adapters report `is_available() is False` without it), and
+ they import it at the top of `search()`. Left alone, those adapters
+ would fail on the import rather than on the request, and the test
+ would pass for the wrong reason wherever bs4 is not installed.
+ """
+ requests_stub = types.ModuleType("requests")
+ requests_stub.get = fake_get
+ monkeypatch.setitem(sys.modules, "requests", requests_stub)
+
+ bs4_stub = types.ModuleType("bs4")
+ bs4_stub.BeautifulSoup = _EmptySoup
+ monkeypatch.setitem(sys.modules, "bs4", bs4_stub)
+
+ for var, value in _SOURCE_CREDENTIALS.items():
+ monkeypatch.setenv(var, value)
+
+
+def _adapter_names():
+ return [source.name for source in all_sources()]
+
+
+def _transport_error_params():
+ params = []
+ for name in _adapter_names():
+ reason = _STILL_SWALLOWS_TRANSPORT_ERRORS.get(name)
+ marks = [pytest.mark.xfail(strict=True, reason=reason)] if reason else []
+ params.append(pytest.param(name, marks=marks, id=name))
+ return params
+
+
[email protected]("source_name", _transport_error_params())
+def test_search_propagates_transport_errors(source_name, monkeypatch):
+ def boom(*_args, **_kwargs):
+ raise TransportError("simulated connection reset")
+
+ _install_fake_transport(monkeypatch, boom)
+
+ with pytest.raises(TransportError):
+ get_source(source_name).search(
+ "ocean waves", SearchFilters(kind="any", per_page=5)
+ )
+
+
[email protected]("source_name", _adapter_names(), ids=_adapter_names())
+def test_search_returns_empty_when_the_source_has_no_results(
+ source_name, monkeypatch
+):
+ # The other half of the contract: `[]` still has to mean "nothing
+ # matched". A 200 with an empty payload must not raise.
+ _install_fake_transport(monkeypatch, lambda *_a, **_k: _EmptyOkResponse())
+
+ assert (
+ get_source(source_name).search(
+ "ocean waves", SearchFilters(kind="any", per_page=5)
+ )
+ == []
+ )