videogen/tests/reelforge/test_stories_fetch.py
caelum0x d56639a594
Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

78 lines
2.6 KiB
Python

"""Unit: stories.fetch — URL normalization + guarded default_fetch (no network)."""
from __future__ import annotations
import httpx
import pytest
from app.domains.stories.fetch import (
RedditFetchError,
default_fetch,
normalize_thread_url,
)
# --------------------------------------------------------------------------- #
# normalize_thread_url
# --------------------------------------------------------------------------- #
def test_normalize_appends_json():
out = normalize_thread_url("https://www.reddit.com/r/AITA/comments/abc/title/")
assert out == "https://www.reddit.com/r/AITA/comments/abc/title.json"
def test_normalize_keeps_existing_json_and_drops_query():
out = normalize_thread_url("https://reddit.com/r/x/comments/abc/t.json?utm=1")
assert out == "https://reddit.com/r/x/comments/abc/t.json"
def test_normalize_rejects_non_http_scheme():
with pytest.raises(RedditFetchError) as ei:
normalize_thread_url("ftp://reddit.com/r/x/comments/abc")
assert ei.value.status_code == 400
def test_normalize_rejects_non_reddit_host():
with pytest.raises(RedditFetchError) as ei:
normalize_thread_url("https://evil.example/r/x/comments/abc")
assert ei.value.status_code == 400
# --------------------------------------------------------------------------- #
# default_fetch (httpx monkeypatched — never hits the network)
# --------------------------------------------------------------------------- #
class _Resp:
def __init__(self, status_code: int, text: str) -> None:
self.status_code = status_code
self.text = text
def test_default_fetch_returns_body(monkeypatch):
captured: dict = {}
def _get(url, **kwargs):
captured["url"] = url
captured["headers"] = kwargs.get("headers", {})
return _Resp(200, '{"ok": true}')
monkeypatch.setattr(httpx, "get", _get)
body = default_fetch("https://reddit.com/x.json")
assert body == '{"ok": true}'
assert captured["headers"]["User-Agent"] == "ReelForgeBot/1.0 (+stories)"
def test_default_fetch_maps_4xx_to_502(monkeypatch):
monkeypatch.setattr(httpx, "get", lambda url, **kw: _Resp(404, "nope"))
with pytest.raises(RedditFetchError) as ei:
default_fetch("https://reddit.com/x.json")
assert ei.value.status_code == 502
def test_default_fetch_maps_timeout_to_502(monkeypatch):
def _boom(url, **kwargs):
raise RuntimeError("timeout")
monkeypatch.setattr(httpx, "get", _boom)
with pytest.raises(RedditFetchError) as ei:
default_fetch("https://reddit.com/x.json")
assert ei.value.status_code == 502