"""GROUNDING-V2 §4a — text-first 는 **글만** 받는 검색 스펙이 필요하다.

★기존 helper 는 늘 `["image","text"]` 를 요구했다. 그대로 쓰면
①안 쓸 사진 값을 매번 치르고 ②claim ↔ source 결속이 사진 caption 쪽으로
흐려진다 (Codex).
"""
from app.modules.pipeline.search_grounded_ref import (
    SEARCH_IMAGE_RESULTS,
    build_web_search_tool,
)


class TestTheDefaultDoesNotChange:
    """★지금 부르는 세 자리는 사진이 필요해서 부른다. 기본을 바꾸면 조용히 잃는다."""

    def test_default_still_asks_for_images(self):
        spec = build_web_search_tool()
        assert spec["search_content_types"] == ["image", "text"]
        assert spec["image_settings"]["caption"] is True
        assert spec["image_settings"]["max_results"] == SEARCH_IMAGE_RESULTS

    def test_an_explicit_max_results_still_lands(self):
        assert build_web_search_tool(3)["image_settings"]["max_results"] == 3


class TestTextOnlyDropsTheImageBlock:
    def test_text_only_omits_the_content_types_key(self):
        """★`["text"]` 를 명시하지 않고 **키 자체를 뺀다** — 그것이 provider
        기본이고 공식 예시도 그렇다 (Codex)."""
        assert "search_content_types" not in build_web_search_tool(
            want_images=False)

    def test_text_only_carries_no_image_settings(self):
        """★`image_settings` 를 남겨 두면 provider 가 사진을 붙여 보낸다."""
        assert "image_settings" not in build_web_search_tool(want_images=False)

    def test_max_results_is_ignored_when_text_only(self):
        assert build_web_search_tool(9, want_images=False) == \
            build_web_search_tool(1, want_images=False)

    def test_it_is_still_a_web_search_tool(self):
        assert build_web_search_tool(want_images=False)["type"] == "web_search"


class TestExistingCallersStillGetImages:
    """★AST 가 아니라 **호출부가 실제로 만드는 스펙**을 본다."""

    def test_the_three_image_callers_are_unchanged(self):
        # 세 자리 모두 want_images 를 안 넘긴다 = 기본값 = 사진 포함
        import inspect

        from app.modules.pipeline import search_grounded_ref, typology_prior

        for mod in (search_grounded_ref, typology_prior):
            src = inspect.getsource(mod)
            for line in src.splitlines():
                if "build_web_search_tool(" in line and "def " not in line:
                    assert "want_images" not in line, line
