from __future__ import annotations
import json
import re
from pathlib import Path
ROOT = Path(__file__).resolve().parent
SOURCE = (ROOT / 'ws8771_main.py').read_text(encoding='utf-8')
CONFIG = json.loads((ROOT / 'ws8771_config.json').read_text(encoding='utf-8'))

def test_ws_version_and_direct_finalize_handlers() -> None:
    assert 'WS_BUILD_VERSION = "2.3.8.225"' in SOURCE
    assert 'def handle_ig_media_exists' in SOURCE
    assert 'def handle_ig_media_downloaded' in SOURCE
    assert 'def generate_account_html_report' in SOURCE

def test_mouse_origin_heart_modules_are_removed() -> None:
    for name in [
        'ws8771_heart_client.py', 'ws8771_ig_auto_click_download.py',
        'ws8771_ig_auto_download.py', 'ws8771_origin_crosscheck.py'
    ]:
        assert not (ROOT / name).exists(), name
    forbidden = [
        'MOUSEEVENTF_LEFTDOWN', 'mouse_left_click(', 'mouse_move_smooth(',
        'SetCursorPos', 'GetCursorPos', 'WindowFromPoint',
        'ig_rtc_auto_click_once', 'ig_rtc_mouse_sync', 'ig_rtc_hover_status',
        'hert8772position', 'ig_dom_calibration'
    ]
    for token in forbidden:
        assert token not in SOURCE, token

def test_config_has_no_retired_mouse_state() -> None:
    for key in ['user_mouse_idle_resume_sec', 'user_mouse_move_threshold_px', 'ig_dom_calibration', 'hert8772position', 'mouse_action_rest_sec']:
        assert key not in CONFIG
    actions = set((CONFIG.get('thread_queue_mode') or {}).get('actions') or [])
    assert 'ig_rtc_auto_click_once' not in actions
    assert {'ig_media_exists', 'ig_media_downloaded'}.issubset(actions)


def test_ws_records_shared_media_saved_after_file_finalize() -> None:
    for token in [
        "from Shared.storage import record_downloaded_file",
        "def record_shared_media_saved",
        "MEDIA_SAVED_OK",
        "storage_ok",
        "client_event_id",
    ]:
        assert token in SOURCE, token
