#!/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 '
' not in html:
errors.append("TC3: renderMsg() missing 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()