test_s1_send_button.py 3.15 KB
#!/usr/bin/env python3
"""RED test for S1: 修复发送按钮不可点击

Verifies the current aichat.html has the bugs:
  1. newConversation() explicitly enables sendBtn even when input is empty
  2. newConversation() doesn't clear msgInput value
"""

import sys, os, subprocess

HTML_PATH = "/data/data/com.termux/files/home/wechat-viewer/templates/aichat.html"

def read_html():
    """Read the HTML file (may need su for root-owned file)."""
    try:
        with open(HTML_PATH) as f:
            return f.read()
    except PermissionError:
        r = subprocess.run(["su", "-c", f"cat {HTML_PATH}"], capture_output=True, text=True, timeout=10)
        if r.returncode != 0:
            print(f"FAIL: Cannot read {HTML_PATH}: {r.stderr}")
            sys.exit(1)
        return r.stdout

def main():
    html = read_html()
    errors = []

    # --- Bug 1: newConversation() explicitly enables sendBtn ---
    # The bad pattern: document.getElementById('sendBtn').disabled = false;
    # inside newConversation() function
    # We use a heuristic: find the newConversation function body and check
    
    # Find newConversation function
    if 'newConversation' not in html:
        errors.append("BUG1: newConversation function not found in HTML")
    else:
        # Check if newConversation() has the bad line
        # We look for 'disabled = false' near newConversation context
        # The fix should remove the explicit disabled=false in newConversation
        # and instead set disabled=true (since input is cleared)
        
        # Find newConversation function start
        idx = html.index('function newConversation()')
        # Find the next function or end of script
        next_func = html.find('function ', idx + 10)
        if next_func == -1:
            conv_body = html[idx:]
        else:
            conv_body = html[idx:next_func]
        
        # BAD: has disabled = false inside newConversation
        if "sendBtn').disabled = false" in conv_body or 'sendBtn").disabled = false' in conv_body:
            errors.append("BUG1: newConversation() explicitly sets sendBtn.disabled=false (should be disabled when input empty)")
        
        # BAD: does NOT clear msgInput value  
        if "msgInput').value = ''" not in conv_body and 'msgInput").value = \'\'' not in conv_body:
            errors.append("BUG2: newConversation() does not clear msgInput value (old text may keep button enabled)")

    # --- Check: input handler exists (should be there) ---
    if "addEventListener('input'" not in html:
        errors.append("REG: input event listener missing (needed for auto-enable/disable)")
    
    # --- Check: deferred creation in sendMessage ---
    if '!currentConvId' not in html:
        errors.append("REG: deferred creation logic missing in sendMessage")

    # --- Jinja2 escape check ---
    if '\\\\\\\\' in html:
        errors.append("JINJA2: backslash bug detected")

    if errors:
        print("RED: Tests FAILED (bugs found — expected for RED phase)")
        for e in errors:
            print(f"  ✗ {e}")
        sys.exit(1)
    else:
        print("GREEN: All checks passed (no bugs found)")
        sys.exit(0)

if __name__ == '__main__':
    main()