"""C3 — detail_steps focus rewrite 폐기 regression test (fix-critical-1 Tier α #3).

_strip_invalid_sids() helper unit-test (G1-G4, G6) + helper-output·source-level
canary (G5, G7). no live LLM, NO VLM — pure helper + source 문자열 검사.
"""
from pathlib import Path

import app.core.steps.detail_steps as _detail_steps_mod
from app.core.steps.detail_steps import _strip_invalid_sids

_SRC = Path(_detail_steps_mod.__file__).read_text(encoding="utf-8")


def test_g1_t2i_prompt_invalid_id_possessive_removal():
    # 무효 ID + 후행 소유격 token-complete 제거 — orphan 's 0
    assert _strip_invalid_sids("focus on C08's hand", {"C08"}) == "focus on hand"


def test_g2_representative_moment_invalid_id_possessive_removal():
    # representative_moment(sfp) path 동일 helper — 선두 무효 ID 소유격 제거
    assert (
        _strip_invalid_sids("C08's lantern by the doorway", {"C08"})
        == "lantern by the doorway"
    )


def test_g3_composite_id_possessive_removal():
    # composite ID(C##O##) 를 한 단위로 제거
    assert _strip_invalid_sids("a man C08O02's coat", {"C08O02"}) == "a man coat"


def test_g4_three_digit_id_preservation():
    # 무효 set 밖 유효 3-digit ID(P123) 보존, prefix misalign 0
    assert _strip_invalid_sids("P123 holds C12", {"C12"}) == "P123 holds"


def test_g5_no_semantic_injection():
    # helper 출력 + detail_steps.py source 어디에도 "the figure's" 삽입 0
    assert "the figure's" not in _strip_invalid_sids("focus on C08's hand", {"C08"})
    assert "the figure's" not in _SRC
    assert "focus on the figure's" not in _SRC


def test_g6_local_normalization_and_no_match_noop():
    # (a) 무매치 no-op — clean prompt 불변 (blind trim 0)
    assert _strip_invalid_sids("  clean prompt  ", {"C08"}) == "  clean prompt  "
    # (b) far-whitespace 보존 — 제거 위치에서 떨어진 double space 불변
    assert (
        _strip_invalid_sids("alpha  beta C08's gamma", {"C08"})
        == "alpha  beta gamma"
    )


def test_g7_cleanup_loop_removed():
    # :2872-2882 3-regex cleanup loop 폐기 증명 — old literal 0
    assert "빈 ID 패턴 정리" not in _SRC
    assert r"focus on\s+'s" not in _SRC
    assert r"\s+'s\s+" not in _SRC
