test_s2_pagination.py 3.91 KB
"""RED test for S2: frontend pagination + sort controls in index.html"""
import urllib.request, json, sys

BASE = "http://127.0.0.1:5000"
passed = 0
failed = 0

def test(label, check_fn):
    global passed, failed
    try:
        with urllib.request.urlopen(f"{BASE}/", timeout=10) as r:
            html = r.read().decode('utf-8')
            ok, msg = check_fn(html)
            if ok:
                passed += 1
                print(f"  PASS  {label}: {msg}")
            else:
                failed += 1
                print(f"  FAIL  {label}: {msg}")
    except Exception as e:
        failed += 1
        print(f"  FAIL  {label}: {e}")

# TC1: Pagination HTML structure exists
def check_tc1(html):
    has_pagination = any(kw in html.lower() for kw in ['pagination', 'page-nav', 'paginator'])
    has_sort = ('sort' in html.lower() and '<select' in html)
    if not has_pagination:
        return False, "no pagination container found"
    if not has_sort:
        return False, "no sort dropdown found"
    return True, "pagination + sort found"

# TC2: JS functions exist
def check_tc2(html):
    found = []
    if 'goToPage' in html:
        found.append("goToPage")
    if 'currentPage' in html or 'current_page' in html:
        found.append("pageState")
    if 'totalPages' in html or 'total_pages' in html:
        found.append("totalPages")
    if len(found) >= 2:
        return True, ", ".join(found)
    return False, f"only found: {found}"

# TC3: No Jinja2 escaping issues
def check_tc3(html):
    # Check for common Jinja2 escape bugs
    bs_bug = chr(92) + chr(92) + chr(92) + chr(92) + "'"
    triple_bug = chr(92) + chr(92) + chr(92) + chr(92) + chr(92) + chr(92) + '"'
    da_bug = 'data-action=' + chr(92) + chr(92) + '"'
    onclick_bug = 'onclick=' + chr(92) + chr(92) + '"event'
    
    issues = []
    if bs_bug in html:
        issues.append("STYLE BACKSLASH BUG")
    if triple_bug in html:
        issues.append("TRIPLE ESCAPE BUG")
    if da_bug in html:
        issues.append("JINJA2 ESCAPED QUOTES")
    if onclick_bug in html:
        issues.append("INLINE ONCLICK ESCAPED QUOTES")
    if issues:
        return False, ", ".join(issues)
    return True, "JS/Jinja2 clean"

# TC4: Session count element exists
def check_tc4(html):
    if 'sessionCount' in html:
        return True, "sessionCount element exists"
    return False, "sessionCount element missing"

# TC5: API pagination works (S1)
def check_tc5(html):
    try:
        with urllib.request.urlopen(f"{BASE}/api/sessions?page=1&per_page=10", timeout=10) as r:
            d = json.loads(r.read())
            ok = d.get("ok")
            total = d.get("total", 0)
            page = d.get("page", 0)
            if ok and total > 0 and page == 1:
                return True, f"API ok total={total} per_page={d.get('per_page')}"
            return False, f"API fail ok={ok} total={total}"
    except Exception as e:
        return False, f"API error: {e}"

# TC6: Page 2 different data
def check_tc6(html):
    try:
        r1 = urllib.request.urlopen(f"{BASE}/api/sessions?page=1&per_page=10", timeout=10)
        d1 = json.loads(r1.read())
        ids1 = {s.get("talker") for s in d1.get("data", [])}
        r2 = urllib.request.urlopen(f"{BASE}/api/sessions?page=2&per_page=10", timeout=10)
        d2 = json.loads(r2.read())
        ids2 = {s.get("talker") for s in d2.get("data", [])}
        overlap = ids1 & ids2
        if overlap:
            return False, f"overlap: {overlap}"
        return True, f"page1={len(ids1)} page2={len(ids2)} no overlap"
    except Exception as e:
        return False, str(e)

print("=== S2 RED: frontend pagination + sort ===\n")

test("TC1: pagination HTML", check_tc1)
test("TC2: JS functions", check_tc2)
test("TC3: Jinja2 escaping", check_tc3)
test("TC4: sessionCount element", check_tc4)
test("TC5: API pagination (S1)", check_tc5)
test("TC6: page 1 vs page 2", check_tc6)

print(f"\n{passed}/{passed+failed} 通过")
sys.exit(0 if failed == 0 else 1)