test_s5_rename.py 3.45 KB
#!/usr/bin/env python3
"""RED test for S5: Session 管理增强

Tests:
  TC1: renderConversations has no duplicate function declaration
  TC2: Inline rename (startRename) exists and PUTs to API
  TC3: Delete conversation (deleteConversation) exists with confirmation
  TC4: Session info persistence (what's the actual bug?)
"""
import sys, os, json, subprocess

HTML_PATH = "/data/data/com.termux/files/home/wechat-viewer/templates/aichat.html"
BASE = "http://127.0.0.1:5001"

def read_html():
    r = subprocess.run(["su", "-c", "cat " + HTML_PATH], capture_output=True, text=True, timeout=10)
    return r.stdout

def curl_get(path):
    r = subprocess.run(["curl", "-s", BASE + path], capture_output=True, text=True, timeout=15)
    return json.loads(r.stdout) if r.stdout else {}

def curl_post(path, data):
    body = json.dumps(data)
    r = subprocess.run(["curl", "-s", "-X", "POST", BASE + path,
        "-H", "Content-Type: application/json", "-d", body], capture_output=True, text=True, timeout=15)
    return json.loads(r.stdout) if r.stdout else {}

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

    # TC1: No duplicate function renderConversations
    # Count occurrences of "function renderConversations"
    func_count = html.count("function renderConversations")
    if func_count > 1:
        errors.append("TC1: Duplicate renderConversations function (count=" + str(func_count) + ") - JS error")

    # TC2: startRename with PUT API
    if "function startRename" not in html:
        errors.append("TC2: startRename function missing")
    if "method: 'PUT'" not in html:
        errors.append("TC2: startRename missing PUT API call for rename")
    if "/api/conversations/" not in html:
        errors.append("TC2: startRename missing API path")

    # TC3: deleteConversation with confirm
    if "function deleteConversation" not in html:
        errors.append("TC3: deleteConversation function missing")
    if "confirm" not in html:
        errors.append("TC3: deleteConversation missing confirmation dialog")
    if "DELETE" not in html:
        errors.append("TC3: deleteConversation missing DELETE method")

    # TC4: rename conversation via API
    conv = curl_post("/api/conversations", {"title": "test s5 rename old"})
    if not conv.get("ok"):
        errors.append("TC4: Cannot create test conversation")
    else:
        conv_id = conv["data"]["id"]
        # Test PUT rename
        r2 = subprocess.run(["curl", "-s", "-X", "PUT", BASE + "/api/conversations/" + str(conv_id),
            "-H", "Content-Type: application/json",
            "-d", '{"title": "test s5 renamed"!r}'],  # wrong, let me fix
            capture_output=True, text=True, timeout=15)
    # Actually, let me just verify the endpoint accepts PUT
    rename = subprocess.run(["curl", "-s", "-X", "PUT",
        BASE + "/api/conversations/" + str(conv_id),
        "-H", "Content-Type: application/json",
        "-d", '{"title":"test s5 renamed"}'],
        capture_output=True, text=True, timeout=15)
    rename_data = json.loads(rename.stdout) if rename.stdout else {}
    if rename_data.get("ok"):
        print("  TC4: PUT rename succeeded ✓")
    else:
        errors.append("TC4: PUT /api/conversations/<id> rename failed: " + str(rename_data))

    if errors:
        print("\nRED: " + str(len(errors)) + " failures")
        for e in errors:
            print("  FAIL: " + e)
        sys.exit(1)
    else:
        print("\nGREEN: All checks passed")
        sys.exit(0)

if __name__ == '__main__':
    main()