import re, os, sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
base = os.path.dirname(os.path.abspath(__file__))
html = open(os.path.join(base, 'index_main.html'), encoding='utf-8').read()

REN = {
    "nyu-africana-ma": ("Africana Studies (MA)", "非洲裔研究文学硕士（MA）", "details/nyu-africana-ma.html"),
    "nyu-classics-ma": ("Classics (MA)", "古典学文学硕士（MA）", "details/nyu-classics-ma.html"),
    "uchicago-maph": ("Master of Arts Program in the Humanities (MAPH)", "人文研究文学硕士（MAPH）", "details/uchicago-maph.html"),
    "col-french-ma": ("French (MA, free-standing)", "法语文学硕士（MA，独立学位）", "details/col-french-ma.html"),
    "col-italian-ma": ("Italian (MA, free-standing)", "意大利语文学硕士（MA，独立学位）", "details/col-italian-ma.html"),
    "nyu-german-ma": ("German Thought and Literature (MA)", "德语思想与文学文学硕士（MA）", "details/nyu-german-ma.html"),
    "ucla-slavic-ma": ("Slavic, East European and Eurasian Languages and Cultures (MA)", "斯拉夫、东欧与欧亚语言文化文学硕士（MA）", "details/ucla-slavic-ma.html"),
    "col-german-ma": ("Germanic Languages (MA, free-standing)", "日耳曼语言文学硕士（MA，独立学位）", "details/col-german-ma.html"),
}

vdm = re.search(r'const VERIFIED_DETAILS=new Set\(\[(.*?)\]\)', html, re.S)
vd = set(re.findall(r'"([^"]+)"', vdm.group(1)))

prog = re.search(r'const PROGRAMS\s*=\s*\[(.*?)\];', html, re.S).group(1)
blocks = dict(re.findall(r'\{id:"([^"]+)"([\s\S]*?)(?=\{id:"|$)', prog))

all_ok = True
for bid, (old, new, url) in REN.items():
    rest = blocks[bid]
    nm = re.search(r'n:"([^"]*)"', rest).group(1)
    em = re.search(r'en:"([^"]*)"', rest).group(1)
    disk = os.path.exists(os.path.join(base, url.replace('/', os.sep)))
    invd = url in vd
    n_ok = nm == new
    old_gone = f'n:"{old}"' not in html
    en_kept = bool(em)
    ok = n_ok and old_gone and disk and invd and en_kept
    all_ok &= ok
    print(f"{bid}: n={'OK' if n_ok else 'FAIL'} 旧串残留={not old_gone} 磁盘={disk} VD={invd} en保留={en_kept}")

# 剩余全英文 n
en_only = []
for bid, rest in blocks.items():
    nm = re.search(r'n:"([^"]*)"', rest)
    n = nm.group(1) if nm else ''
    if n and re.fullmatch(r'[A-Za-z0-9\s\-\&\(\)\.,/\'’·]+', n.strip()):
        en_only.append((bid, n.strip()))
print("\n剩余全英文 n:", len(en_only))
for x in en_only: print(" ", x[0], "|", x[1])

print("\nALL OK" if all_ok else "\nHAS FAIL")
