|
| 1 | +"""API-key gates and the SDK runtime patches. |
| 2 | +
|
| 3 | +The provider refactor will relax the hard ``OPENROUTER_API_KEY`` requirement, |
| 4 | +so these pin the *current* gate behaviour to make any change deliberate and |
| 5 | +visible. The patch tests guard the end-to-end-callability fixes the README |
| 6 | +depends on (data-URL images, video-download auth, the /audio/speech route). |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import base64 |
| 12 | +import io |
| 13 | + |
| 14 | +import pytest |
| 15 | +from PIL import Image |
| 16 | + |
| 17 | +# ───── missing-key gates ───────────────────────────────────────────── |
| 18 | + |
| 19 | + |
| 20 | +async def test_article_to_reel_errors_without_key(monkeypatch): |
| 21 | + monkeypatch.delenv("OPENROUTER_API_KEY", raising=False) |
| 22 | + import reel_af.app as app |
| 23 | + |
| 24 | + result = await app.article_to_reel(url="https://example.com/x") |
| 25 | + assert result == {"error": "OPENROUTER_API_KEY not set in env."} |
| 26 | + |
| 27 | + |
| 28 | +async def test_topic_to_reel_errors_without_key(monkeypatch): |
| 29 | + monkeypatch.delenv("OPENROUTER_API_KEY", raising=False) |
| 30 | + import reel_af.app as app |
| 31 | + |
| 32 | + result = await app.topic_to_reel(topic="the placebo effect") |
| 33 | + assert result == {"error": "OPENROUTER_API_KEY not set in env."} |
| 34 | + |
| 35 | + |
| 36 | +def test_cli_require_key_raises_when_unset(monkeypatch): |
| 37 | + monkeypatch.delenv("OPENROUTER_API_KEY", raising=False) |
| 38 | + import reel_af.cli as cli |
| 39 | + |
| 40 | + with pytest.raises(SystemExit): |
| 41 | + cli._require_key() |
| 42 | + |
| 43 | + |
| 44 | +def test_cli_require_key_passes_when_set(monkeypatch): |
| 45 | + monkeypatch.setenv("OPENROUTER_API_KEY", "sk-or-test") |
| 46 | + import reel_af.cli as cli |
| 47 | + |
| 48 | + cli._require_key() # must not raise |
| 49 | + |
| 50 | + |
| 51 | +# ───── sdk patches ─────────────────────────────────────────────────── |
| 52 | + |
| 53 | + |
| 54 | +def test_generate_speech_method_is_added(): |
| 55 | + from agentfield.media_providers import OpenRouterProvider |
| 56 | + |
| 57 | + import reel_af.sdk_patches # noqa: F401 |
| 58 | + |
| 59 | + assert hasattr(OpenRouterProvider, "generate_speech") |
| 60 | + |
| 61 | + |
| 62 | +def test_generate_video_is_patched(): |
| 63 | + from agentfield.media_providers import OpenRouterProvider |
| 64 | + |
| 65 | + import reel_af.sdk_patches # noqa: F401 |
| 66 | + |
| 67 | + assert getattr(OpenRouterProvider.generate_video, "__reel_af_patched__", False) is True |
| 68 | + |
| 69 | + |
| 70 | +def test_image_output_save_decodes_data_urls(tmp_path): |
| 71 | + from agentfield.multimodal_response import ImageOutput |
| 72 | + |
| 73 | + import reel_af.sdk_patches # noqa: F401 |
| 74 | + |
| 75 | + # A 4x4 red PNG encoded as a data: URL — Gemini returns these. |
| 76 | + buf = io.BytesIO() |
| 77 | + Image.new("RGB", (4, 4), (255, 0, 0)).save(buf, format="PNG") |
| 78 | + raw = buf.getvalue() |
| 79 | + data_url = "data:image/png;base64," + base64.b64encode(raw).decode() |
| 80 | + |
| 81 | + out = tmp_path / "decoded.png" |
| 82 | + ImageOutput(url=data_url).save(out) |
| 83 | + |
| 84 | + assert out.read_bytes() == raw # decoded locally, no network fetch |
| 85 | + |
| 86 | + |
| 87 | +def test_apply_all_is_idempotent(): |
| 88 | + from agentfield.media_providers import OpenRouterProvider |
| 89 | + |
| 90 | + import reel_af.sdk_patches as patches |
| 91 | + |
| 92 | + before = OpenRouterProvider.generate_video |
| 93 | + patches.apply_all() # second application |
| 94 | + patches.apply_all() # third |
| 95 | + after = OpenRouterProvider.generate_video |
| 96 | + |
| 97 | + assert after is before # no re-wrapping |
| 98 | + assert getattr(after, "__reel_af_patched__", False) is True |
| 99 | + assert hasattr(OpenRouterProvider, "generate_speech") |
0 commit comments