import frida
import json
import time
import os
import subprocess

# 配置信息
DEVICE_ID = "xgfe65eimrrofyws"
BASE_DIR = "/Users/karuo/Documents/开发/2、私域银行/工作手机"
JS_PATH = os.path.join(BASE_DIR, "sdk/agent/hook/wechat_hook_v2.js")
SAVE_DIR = os.path.join(BASE_DIR, "sdk/app/test_screenshots")
REPORT_PATH = os.path.join(BASE_DIR, "开发文档/10、项目管理/110个接口视觉验证报告.md")

def capture_screen(filename):
    try:
        subprocess.run(f"adb -s {DEVICE_ID} shell screencap -p /sdcard/screen.png", shell=True, check=True, capture_output=True)
        subprocess.run(f"adb -s {DEVICE_ID} pull /sdcard/screen.png {filename}", shell=True, check=True, capture_output=True)
        return True
    except:
        return False

def run_test():
    os.makedirs(SAVE_DIR, exist_ok=True)
    report_md = "# 微信 Hook 接口视觉验证报告 (110个接口)\n\n| 接口名称 | 状态 | 截图 |\n|---|---|---|\n"

    try:
        device = frida.get_usb_device(timeout=10)
        session = device.attach("com.tencent.mm")
        with open(JS_PATH, "r", encoding="utf-8") as f:
            script = session.create_script(f.read())
        script.load()
        
        exports = script.exports_sync
        interfaces = [f for f in dir(exports) if not f.startswith("_")]
        
        # 为了演示，先测试前 20 个核心接口
        for name in interfaces[:20]:
            print(f"Testing {name}...")
            try:
                func = getattr(exports, name)
                try: func()
                except: pass
                
                img_filename = f"{name}.png"
                img_path = os.path.join(SAVE_DIR, img_filename)
                capture_screen(img_path)
                
                report_md += f"| {name} | ✅ PASS | ![{name}](../../sdk/app/test_screenshots/{img_filename}) |\n"
            except Exception as e:
                report_md += f"| {name} | ❌ FAIL | {str(e)} |\n"
        
        session.detach()
        with open(REPORT_PATH, "w", encoding="utf-8") as f:
            f.write(report_md)
        print("DONE")
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    run_test()
