test_s3_new_conv.py 2.56 KB
#!/usr/bin/env python3
"""RED test for S3: 修复新建对话逻辑

Tests: deferred conversation creation flow
  TC1: newConversation() doesn't call POST (deferred creation)
  TC2: sendMessage with null currentConvId triggers POST + SSE
  TC3: After sending, conversation appears in list with auto-title
"""

import sys, os, json, subprocess

BASE = "http://127.0.0.1:5001"

def curl_get(path):
    r = subprocess.run(["curl", "-s", f"{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", f"{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():
    errors = []
    
    # TC1: Verify newConversation() in aichat.html has deferred pattern
    r = subprocess.run(["su", "-c", "cat /data/data/com.termux/files/home/wechat-viewer/templates/aichat.html"],
                       capture_output=True, text=True, timeout=10)
    html = r.stdout
    
    # Check newConversation sets currentConvId = null (deferred creation)
    if 'currentConvId = null' not in html:
        errors.append("TC1: newConversation() missing currentConvId = null (deferred creation)")
    
    # Check sendMessage has lazy-create logic
    if '!currentConvId' not in html:
        errors.append("TC1: sendMessage() missing !currentConvId check (lazy create)")
    
    # TC2: Create conversation via API (simulating sendMessage flow)
    conv = curl_post("/api/conversations", {"title": "test s3 flow"})
    if not conv.get("ok"):
        errors.append(f"TC2: POST /api/conversations failed: {conv}")
    else:
        conv_id = conv["data"]["id"]
        print(f"  TC2: Created conversation id={conv_id}")
        
        # TC3: Verify conversation appears in list
        convs = curl_get("/api/conversations")
        if not convs.get("ok"):
            errors.append(f"TC3: GET /api/conversations failed")
        else:
            found = any(c["id"] == conv_id for c in convs["data"])
            if not found:
                errors.append(f"TC3: Created conversation {conv_id} not in list")
            else:
                print(f"  TC3: Conversation {conv_id} found in list ✓")
    
    if errors:
        print(f"\nRED: {len(errors)} failures")
        for e in errors:
            print(f"  ✗ {e}")
        sys.exit(1)
    else:
        print("\nGREEN: All checks passed")
        sys.exit(0)

if __name__ == '__main__':
    main()