"""Patch B-min IMPORTANT 1 — coordinator fallback sanitize must propagate semantic_constraints.

Static source-text test (full-stack integration is covered by canary). The fallback
sanitize call inside _generate_variation_in_loop's outer ModerationError handler
must receive semantic_constraints kwarg or the polarity guard is bypassed when the
inner generate_and_validate_scene retries are exhausted.
"""
import re
from pathlib import Path

# Resolve coordinator source relative to this test file: tests/services/__file__
# → repo_root/backend/app/services/scene_generation_coordinator.py.
# Using the test file location keeps the assertion robust to pytest cwd.
_COORD_SRC = (
    Path(__file__).resolve().parents[2]
    / "app" / "services" / "scene_generation_coordinator.py"
)


def test_coordinator_fallback_sanitize_propagates_constraints():
    src = _COORD_SRC.read_text(encoding="utf-8")
    # All sanitizer.sanitize(...) invocations in this file MUST include
    # semantic_constraints kwarg post-Patch B-min. Currently only the fallback
    # inside _generate_variation_in_loop's outer ModerationError handler exists;
    # any future sanitize() call added here without the kwarg is a defense bypass.
    sanitize_calls = re.findall(
        r"sanitizer\.sanitize\([^)]*\)",
        src,
        flags=re.DOTALL,
    )
    assert sanitize_calls, "no sanitizer.sanitize calls found — refactor likely"
    for call in sanitize_calls:
        assert "semantic_constraints" in call, (
            f"sanitizer.sanitize call missing semantic_constraints kwarg "
            f"(potential Patch B-min defense bypass): {call}"
        )
