videogen/tests/reelforge/test_openai_image.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

86 lines
2.8 KiB
Python

"""OpenAI image adapter — real OpenAI Images API (Bearer, url or b64 result)."""
from __future__ import annotations
import pytest
from app.domains.gateway.openai_image import openai_generate_image
from app.domains.routing.providers.kling_fal import KlingError
class _Resp:
def __init__(self, status_code=200, body=None):
self.status_code = status_code
self._body = body or {}
self.headers = {}
def json(self):
return self._body
class FakeSession:
def __init__(self, script):
self._script = list(script)
self.calls = []
def request(self, method, url, *, headers=None, json=None, timeout=None):
self.calls.append({"method": method, "url": url, "headers": headers, "json": json})
return self._script.pop(0)
def _gen(session, model="gpt-image-1", **kw):
return openai_generate_image(model, "a red panda", api_key="sk-secret", session=session,
sleep=lambda s: None, **kw)
def test_hosted_url_returned_and_bearer_endpoint():
sess = FakeSession([_Resp(200, {"data": [{"url": "https://oai/img.png"}]})])
url = _gen(sess, model="dall-e-3")
assert url == "https://oai/img.png"
call = sess.calls[0]
assert call["method"] == "POST"
assert call["url"] == "https://api.openai.com/v1/images/generations"
assert call["headers"]["Authorization"] == "Bearer sk-secret"
assert call["json"]["model"] == "dall-e-3" and call["json"]["prompt"] == "a red panda"
def test_b64_becomes_data_url():
sess = FakeSession([_Resp(200, {"data": [{"b64_json": "AAAABBBB"}]})])
url = _gen(sess)
assert url == "data:image/png;base64,AAAABBBB"
def test_prompt_required():
with pytest.raises(KlingError) as ei:
openai_generate_image("gpt-image-1", "", api_key="sk", session=FakeSession([]))
assert ei.value.kind == "validation"
def test_missing_key_is_auth():
with pytest.raises(KlingError) as ei:
openai_generate_image("gpt-image-1", "x", api_key="", session=FakeSession([]))
assert ei.value.kind == "auth"
def test_auth_error_maps():
with pytest.raises(KlingError) as ei:
_gen(FakeSession([_Resp(401, {"error": {"message": "bad key"}})]))
assert ei.value.kind == "auth"
def test_bad_request_is_validation():
with pytest.raises(KlingError) as ei:
_gen(FakeSession([_Resp(400, {"error": {"message": "content policy"}})]))
assert ei.value.kind == "validation"
def test_rate_limit_retries_then_succeeds():
sess = FakeSession([_Resp(429, {}), _Resp(200, {"data": [{"url": "https://oai/ok.png"}]})])
assert _gen(sess) == "https://oai/ok.png" and len(sess.calls) == 2
def test_empty_data_is_provider_error():
with pytest.raises(KlingError) as ei:
_gen(FakeSession([_Resp(200, {"data": []})]))
assert ei.value.kind == "provider"