test_s3_new_conv.py
2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/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()