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
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.
86 lines
3.4 KiB
Python
86 lines
3.4 KiB
Python
"""Ad Studio product-photo normalization — default behavior unchanged, SAM cutout opt-in."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
|
|
from app.domains.adstudio.images import MIN_EDGE, normalize_image, phrase_from_title
|
|
from app.domains.routing.providers.sam_segment import SamBox as RealSamBox
|
|
|
|
|
|
def _solid_jpeg(width: int, height: int, color=(10, 20, 30)) -> bytes:
|
|
from PIL import Image
|
|
|
|
img = Image.new("RGB", (width, height), color)
|
|
buf = io.BytesIO()
|
|
img.save(buf, "JPEG")
|
|
return buf.getvalue()
|
|
|
|
|
|
def test_default_unconfigured_behavior_is_unchanged(monkeypatch):
|
|
# No MODEL_API_KEY/SAM_API_KEY set anywhere: the default cutout lookup must be
|
|
# a pure no-op, so normalize_image's output is identical to the pre-SAM code.
|
|
monkeypatch.delenv("MODEL_API_KEY", raising=False)
|
|
monkeypatch.delenv("SAM_API_KEY", raising=False)
|
|
data = _solid_jpeg(400, 300)
|
|
with_default = normalize_image(data)
|
|
without_cutout = normalize_image(data, cutout=None)
|
|
assert with_default is not None and without_cutout is not None
|
|
assert with_default[0] == without_cutout[0]
|
|
assert with_default[1:] == (400, 300)
|
|
|
|
|
|
def test_cutout_none_disables_the_step_explicitly():
|
|
data = _solid_jpeg(300, 300)
|
|
bytes_out, w, h = normalize_image(data, cutout=None)
|
|
assert (w, h) == (300, 300)
|
|
|
|
|
|
def test_configured_cutout_crops_to_the_detected_box():
|
|
data = _solid_jpeg(400, 400)
|
|
box = RealSamBox(x1=100, y1=100, x2=300, y2=300, frame_width=400, frame_height=400)
|
|
result = normalize_image(data, cutout=lambda _data: box)
|
|
assert result is not None
|
|
_bytes, w, h = result
|
|
# Cropped (300-100)+padding on each side, then thumbnailed — strictly
|
|
# smaller than the original full 400x400 frame.
|
|
assert w < 400 and h < 400
|
|
|
|
|
|
def test_degenerate_box_falls_back_to_the_full_frame():
|
|
data = _solid_jpeg(400, 400)
|
|
# A box far too small to clear MIN_EDGE after cropping must never make an
|
|
# otherwise-usable photo unusable.
|
|
tiny = RealSamBox(x1=0, y1=0, x2=2, y2=2, frame_width=400, frame_height=400)
|
|
result = normalize_image(data, cutout=lambda _data: tiny)
|
|
assert result is not None
|
|
_bytes, w, h = result
|
|
assert w >= MIN_EDGE or h >= MIN_EDGE
|
|
|
|
|
|
def test_phrase_from_title_cuts_at_middle_dot_variant_suffix():
|
|
# Real storefront title shape (Oxhan Atelier, verified live 2026-11-02):
|
|
# a "·"-separated variant name trails the real category noun, and SAM 3.1
|
|
# grounds on "sweatshirt" but not on the variant name "Anka".
|
|
assert phrase_from_title("Very Turkish Time Sweatshirt · Anka") == "the sweatshirt"
|
|
|
|
|
|
def test_phrase_from_title_still_handles_existing_separators():
|
|
assert phrase_from_title("Nike Air Zoom Pegasus 40 Running Shoes") == "the shoes"
|
|
assert phrase_from_title("Premium Leather Wallet - Brown") == "the wallet"
|
|
assert phrase_from_title("Wireless Earbuds (White)") == "the earbuds"
|
|
|
|
|
|
def test_cutout_lookup_exception_degrades_gracefully():
|
|
data = _solid_jpeg(300, 300)
|
|
|
|
def _boom(_data):
|
|
raise RuntimeError("network error")
|
|
|
|
# normalize_image's own except-block only guards decode; the cutout callable
|
|
# itself must not raise into it uncaught from real wiring (sam_segment's
|
|
# default integration already swallows exceptions) — a raising custom
|
|
# cutout here is expected to propagate as a decode failure (None), matching
|
|
# "any failure in this step means skip this image" for the whole function.
|
|
assert normalize_image(data, cutout=_boom) is None
|