test_s2_session_history.py
3.73 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
"""RED test for S2: 修复 session 历史记录不显示
Tests:
TC1: API returns messages with proper format
TC2: Empty conversation shows proper empty state
TC3: Code blocks render with proper block-level styling (dark bg, monospace)
"""
import sys, os, json, subprocess, urllib.parse
BASE = "http://127.0.0.1:5001"
HTML_PATH = "/data/data/com.termux/files/home/wechat-viewer/templates/aichat.html"
def read_html():
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)
return r.stdout
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: API returns messages properly ---
conv = curl_post("/api/conversations", {"title": "test-s2-tc1"})
if not conv.get("ok"):
errors.append(f"TC1: Failed to create conversation: {conv}")
else:
conv_id = conv["data"]["id"]
msg1 = curl_post(f"/api/conversations/{conv_id}/messages", {"content": "Hello, write Python code"})
if not msg1.get("ok"):
errors.append(f"TC1: Failed to add user message: {msg1}")
msgs = curl_get(f"/api/conversations/{conv_id}/messages")
if not msgs.get("ok"):
errors.append(f"TC1: Failed to get messages: {msgs}")
elif not msgs.get("data"):
errors.append("TC1: Messages list is empty")
else:
m = msgs["data"][0]
for key in ["id", "role", "content", "created_at"]:
if key not in m:
errors.append(f"TC1: Message missing key '{key}'")
print(f" TC1: got {len(msgs['data'])} messages ✓")
# --- TC2: Empty conversation ---
empty_conv = curl_post("/api/conversations", {"title": "test-s2-tc2-empty"})
if empty_conv.get("ok"):
empty_msgs = curl_get(f"/api/conversations/{empty_conv['data']['id']}/messages")
if not empty_msgs.get("ok"):
errors.append(f"TC2: Empty conversation API failed")
elif len(empty_msgs.get("data") or []) != 0:
errors.append(f"TC2: Empty conversation should have 0 messages")
else:
print(" TC2: empty returns [] ✓")
# --- TC3: Code block rendering ---
html = read_html()
# Check code block handling exists
if '```' not in html:
errors.append("TC3: renderMsg() missing code block pattern (```) handler")
if '<pre><code>' not in html:
errors.append("TC3: renderMsg() missing <pre><code> wrapper for code blocks")
# Check msg-bubble pre styling for dark background
if '.msg-bubble pre' not in html:
errors.append("TC3: missing .msg-bubble pre CSS (dark code block background)")
# Check loadMessages handles empty state properly
if '开始对话吧' not in html:
errors.append("REG: missing empty state message '开始对话吧'")
# Check selectConversation calls loadMessages
if 'loadMessages' not in html:
errors.append("REG: loadMessages function missing from aichat.html")
if errors:
print("\nRED: Tests FAILED")
for e in errors:
print(f" ✗ {e}")
sys.exit(1)
else:
print("\nGREEN: All checks passed")
sys.exit(0)
if __name__ == '__main__':
main()