# FILE: _St/test_verification_checklist.py | ROLE: 버전별 검증목록 회귀 테스트
from pathlib import Path
import re

ROOT = Path(__file__).resolve().parent


def test_v279_verification_checklist_and_injection_logs() -> None:
    checklist = (ROOT / "검증목록_v279.md").read_text(encoding="utf-8")
    numbers = re.findall(r"^\| (\d{2}) \| \[[^]]*\] \| `T-[^`]+` \|", checklist, flags=re.MULTILINE)
    assert numbers == [f"{n:02d}" for n in range(1, 62)]
    for test_id in [
        "T-AUTO-INDEXED-START-001",
        "T-AUTO-DELETED-ACCOUNT-001",
        "T-ACCOUNT-DB-FILE-001",
        "T-AUTO-CONTROL-STATE-001",
        "T-AUTO-TAB-ID-UI-001",
        "T-AUTO-ETA-001",
        "T-SIDE-AUTO-LABEL-TAB-001",
        "T-SIDE-ACCOUNT-CARD-SELECT-001",
        "T-MANUAL-DOWNLOAD-START-001",
        "T-EXT-RUNTIME-DIAG-001",
        "T-SIDE-CDP-GATE-001",
        "T-SIDE-POPUP-HANDOFF-001",
        "T-MEDIA-SAVE-SELECT-001",
        "T-AUTO-CACHE-RECOVERY-001",
        "T-AUTO-CDP-LAUNCH-001",
    ]:
        assert test_id in checklist
    assert "1/N 이상은 현재 페이지 종류와 관계없이 지정 계정 URL부터 직접 로드" in checklist
    assert "로그인·challenge·비공개·네트워크·Injection 지연은 삭제 오판 없음" in checklist
    assert "sort_database_selection.json" in checklist
    assert "Analyze8780·WS8771·수집계정 목록이 같은 선택 SQLite 사용" in checklist
    assert "0/N 비 Instagram은 running·자동대기 진입 없이 안내" in checklist
    assert "tab_id는 방식 3 개수 앞 1곳만 표시" in checklist
    assert "종료예상 = 남은 게시물×(저장 평균 + 다운 지연)" in checklist
    assert "저장값이 없으면 자동실행 사용 기본 체크" in checklist
    assert "Side 카드 IG 계정 클릭은 기존 계정추가 대상만 변경" in checklist
    assert "수동 MP4/JPG 버튼 클릭 순간 정확한 shortcode·asset_kind·media_index start 강조" in checklist
    assert "window.__IGBLUE_RUNTIME_DIAG_V276__.snapshot()" in checklist
    assert "unload→pagehide monkey patch 없음" in checklist
    assert "CDP:X에서는 자동실행 사용·시작·자동예약 입력을 비활성화" in checklist
    assert "popup이 동일 Instagram tab_id를 확인한 뒤에만 기존 Side 닫기" in checklist
    assert "Video 저장 / Image 저장 4가지 조합" in checklist
    assert "AD-03-E01 [예외추적]" in checklist
    assert "AD-03 [복구완료]" in checklist
    assert "Main actual shortcode가 snapshot과 다르면" in checklist
    assert "Down icon only=버튼만" in checklist
    assert "Use/Side/Main 체크값을 서로 독립 저장" in checklist
    assert "PUT /json/new 또는 Target.createTarget" in checklist
    assert "일반 Chrome User Data 프로세스는 종료하지 않음" in checklist

    inject = (ROOT / "IG_StYellow" / "js" / "instagram_inject.js").read_text(encoding="utf-8")
    for check_id in ["INJ-01", "INJ-02", "INJ-03", "INJ-04", "INJ-05"]:
        assert check_id in inject
    for field in [
        'schema:"igblue_dom_diagnostic_v268"',
        "media_map_count",
        "capture_counters",
        "injection_check_log",
    ]:
        assert field in inject


def test_v286_verification_checklist() -> None:
    checklist = (ROOT / "검증목록_v286.md").read_text(encoding="utf-8")
    numbers = re.findall(r"^\| (\d{2}) \| \[[^]]*\] \| `T-V286-[^`]+` \|", checklist, flags=re.MULTILINE)
    assert numbers == [f"{n:02d}" for n in range(1, 18)]
    for token in [
        "T-V286-FRESH-START-001",
        "T-V286-STALE-EXPLORE-001",
        "T-V286-CDP-CLOSE-001",
        "T-V286-USER-TAB-PRESERVE-001",
        "T-V286-WIN-ERROR-TAB-CLOSE-001",
        "OK_USER_TAB_PRESERVED",
        "Target.closeTarget",
        "chrome.tabs.remove",
        "자동 WS direct MP4/JPG",
    ]:
        assert token in checklist


def test_v288_verification_checklist() -> None:
    checklist = (ROOT / "검증목록_v288.md").read_text(encoding="utf-8")
    numbers = re.findall(r"^\| (\d{2}) \| \[[^]]*\] \| `T-V288-[^`]+` \|", checklist, flags=re.MULTILINE)
    assert numbers == [f"{n:02d}" for n in range(1, 19)]
    for token in [
        "T-V288-LIKE-PREFIX-001",
        "T-V288-ACCOUNT-CHECK-001",
        "T-V288-DB-NUMBER-001",
        "T-V288-MEDIA-RANGE-001",
        "T-V288-POST-FILE-DELETE-001",
        "T-V288-SHARED-PATH-BLOCK-001",
        "tem%",
        "social_accounts",
        "Range",
        "50/100/200/10000",
    ]:
        assert token in checklist


def test_v295_verification_checklist() -> None:
    checklist = (ROOT / "검증목록_v295.md").read_text(encoding="utf-8")
    numbers = re.findall(r"^\| (\d{2}) \| \[[^]]*\] \| `T-V295-[^`]+` \|", checklist, flags=re.MULTILINE)
    assert numbers == [f"{n:02d}" for n in range(1, 15)]
    for token in [
        "T-V295-AUTO-CONNECTION-001",
        "T-V295-FLAT-SESSION-001",
        "Target.attachToTarget(flatten=true)",
        "cdp_port=8700",
        "Network.enable/response/loading/getResponseBody",
        "yjm_pmonit",
    ]:
        assert token in checklist


def test_v296_verification_checklist() -> None:
    checklist = (ROOT / "검증목록_v296.md").read_text(encoding="utf-8")
    numbers = re.findall(r"^\| (\d{2}) \| \[[^]]*\] \| `T-V296-[^`]+` \|", checklist, flags=re.MULTILINE)
    assert numbers == [f"{n:02d}" for n in range(1, 16)]
    for token in [
        "T-V296-PATH-CONTRACT-001",
        "T-V296-DUPLICATE-REGISTER-001",
        "T-V296-ANALYZE-LOCAL-001",
        "{shortcode}.{실제확장자}",
        "CACHE8701_CDP",
        "AUTO_SEEN_JPG",
        "post_stat_10m",
        "CI-06",
        "_thumbnail.*",
    ]:
        assert token in checklist


def test_v297_verification_checklist() -> None:
    checklist = (ROOT / "검증목록_v297.md").read_text(encoding="utf-8")
    numbers = re.findall(r"^\| (\d{2}) \| \[[^]]*\] \| `T-V297-[^`]+` \|", checklist, flags=re.MULTILINE)
    assert numbers == [f"{n:02d}" for n in range(1, 16)]
    for token in [
        "T-V297-NUMERIC-MEDIA-REMOVE-001",
        "T-V297-STDOWN-ALIAS-001",
        "T-V297-ASSET-ID-RESPONSE-001",
        "T-V297-EXPLORER-SELECT-001",
        "T-V297-PATH-GUARD-001",
        "/_StDown/instagram/{account}/{file}",
        "{shortcode}.{실제확장자}",
        "media_files",
        "Explorer `/select`",
        "post_stat_10m",
    ]:
        assert token in checklist


def test_v303_verification_checklist() -> None:
    checklist = (ROOT / "검증목록_v303.md").read_text(encoding="utf-8")
    numbers = re.findall(r"^\| (\d{2}) \| \[[^]]*\] \| `T-V303-[^`]+` \|", checklist, flags=re.MULTILINE)
    assert numbers == [f"{n:02d}" for n in range(1, 17)]
    for token in [
        "T-V303-CDP-SOURCE-001",
        "T-V303-CACHE-VIDEO-INDEPENDENT-001",
        "T-V303-SWIPE-PROTECT-001",
        "T-V303-EXPLORER-LINK-001",
        "Side/Background 상태, Main 표시 전용",
        "{shortcode}.{실제확장자}",
        "범위 안 Swipe, 경계 확인 후 다음 URL",
        "display:none",
    ]:
        assert token in checklist


def test_v304_verification_checklist() -> None:
    checklist = (ROOT / "검증목록_v304.md").read_text(encoding="utf-8")
    result = (ROOT / "검증결과_v304.md").read_text(encoding="utf-8")
    cache = (ROOT / "Cache8701" / "cache8701_main.py").read_text(encoding="utf-8")
    side = (ROOT / "IG_StYellow" / "side.js").read_text(encoding="utf-8")
    for token in [
        "OWNER active=false",
        "8701에 로깅",
        "cache8701_log_*.log",
        "총 20개",
        "CI-01 메타데이터",
        "CI-06 SQLite",
    ]:
        assert token in checklist
    for token in ["pytest: 111 passed", "IG Node `test_*.js`: 43/43 OK", "실제 Windows 신규 이미지 파일: UNKNOWN"]:
        assert token in result
    assert "def connection_can_watch" in cache
    assert 'id = "igblue_settings_cache8701_file_logging"' in side or '"igblue_settings_cache8701_file_logging"' in side
