verify_chat_fast.py 3.32 KB
#!/usr/bin/env python3
"""Fast-forward verification for chat mobile adaptation task."""
import subprocess, sys

FAILURES = []
def check(name, condition):
    if condition:
        print(f"  [PASS] {name}")
    else:
        print(f"  [FAIL] {name}")
        FAILURES.append(name)

html, _ = subprocess.run(
    ["curl", "-s", "http://127.0.0.1:5000/chat", "--connect-timeout", "5", "--max-time", "10"],
    capture_output=True, text=True, timeout=15
).stdout, 0 if True else 1

# Fix: re-assign properly
r = subprocess.run(
    ["curl", "-s", "http://127.0.0.1:5000/chat", "--connect-timeout", "5", "--max-time", "10"],
    capture_output=True, text=True, timeout=15
)
html = r.stdout

print("=== S1: base.html integration ===")
check("Has topbar + menu-btn", 'class="menu-btn"' in html)
check("Has ☰ character", '☰' in html)
check("Has drawer overlay", 'class="overlay"' in html)
check("Has drawer div", 'class="drawer"' in html)
check("Has drawer-nav", 'class="drawer-nav"' in html)

print("\n=== S2: Mobile session drawer ===")
check("Has chat-sidebar", 'chatSidebar' in html)
check("Has mobile toggle button", 'chat-toggle-btn' in html)
check("Has toggleChatSidebar function", 'toggleChatSidebar' in html)
check("Has closeChatSidebar function", 'closeChatSidebar' in html)
check("Has overlay close onclick", 'closeChatSidebar()' in html)
check("Has @media mobile breakpoint", '@media' in html and '768' in html)
check("Sidebar transform translateX(-100%)", 'translateX(-100%)' in html)
check("Sidebar transition on transform", 'transition' in html and 'transform' in html)

print("\n=== S3: loadMessages + history ===")
check("Has loadMessages function", 'loadMessages' in html)
check("Has renderMsg function", 'renderMsg' in html)
check("Empty state '开始对话吧'", '开始对话吧' in html)
check("Empty state '选择一个对话'", '选择一个对话' in html)
check("Has selectConversation function", 'selectConversation' in html)
check("Has EventSource (SSE streaming)", 'EventSource' in html)
check("Has scrollToBottom function", 'scrollToBottom' in html)
check("User/assistant role rendering", "role === 'user'" in html)
check("Has deleteConversation function", 'deleteConversation' in html)

print("\n=== S4: Cross-cutting checks ===")
# Jinja2 safety
check("No Jinja2 escaped quotes in onclick (backslash)", 'onclick=\\"' not in html)
check("No style backslash bug", "style=\\'" not in html)
# API tests
import json
r2 = subprocess.run(
    ["curl", "-s", "http://127.0.0.1:5000/api/conversations", "--connect-timeout", "5", "--max-time", "10"],
    capture_output=True, text=True, timeout=15
)
data = json.loads(r2.stdout) if r2.stdout else {}
check("GET /api/conversations ok", data.get("ok"))
check("GET /api/conversations has data", isinstance(data.get("data"), list))

if data.get("data"):
    cid = data["data"][0]["id"]
    r3 = subprocess.run(
        ["curl", "-s", f"http://127.0.0.1:5000/api/conversations/{cid}/messages",
         "--connect-timeout", "5", "--max-time", "10"],
        capture_output=True, text=True, timeout=15
    )
    msg_data = json.loads(r3.stdout) if r3.stdout else {}
    check(f"GET /api/conversations/{cid}/messages ok", msg_data.get("ok"))

print(f"\n=== {len(FAILURES)} failure(s) ===")
if FAILURES:
    for f in FAILURES:
        print(f"  FAIL: {f}")
    sys.exit(1)
else:
    print("ALL FAST-FORWARD CHECKS PASSED")
    sys.exit(0)