test_s6_chat_interface.py
3.72 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
S6 聊天界面集成测试 — 验证 aichat.html 页面元素和 JS 函数
"""
import pytest
import subprocess
import sys
import os
import time
import json
# Path setup
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import local_db
import config
BASE_URL = "http://127.0.0.1:5000"
def get_page(path="/chat"):
"""Fetch a page from the running Flask server."""
r = subprocess.run(
["curl", "-s", "-o", "/dev/stdout", "-w", "\n%{http_code}",
f"{BASE_URL}{path}", "--connect-timeout", "5", "--max-time", "10"],
capture_output=True, text=True, timeout=15
)
lines = r.stdout.strip().split("\n")
status_code = lines[-1].strip()
html = "\n".join(lines[:-1])
return html, status_code
def test_chat_page_accessible():
"""Test /chat page returns 200."""
html, status = get_page("/chat")
assert status == "200", f"Expected 200, got {status}"
def test_msg_list_element_exists():
"""Test msgList DOM element exists."""
html, _ = get_page("/chat")
assert 'id="msgList"' in html, "msgList element not found"
assert 'id="messagesContainer"' in html or 'id="msgList"' in html, \
"chat messages container not found"
def test_msg_input_exists():
"""Test input box exists."""
html, _ = get_page("/chat")
assert 'id="msgInput"' in html or 'id="userInput"' in html, \
"input box not found"
def test_send_button_exists():
"""Test send button exists."""
html, _ = get_page("/chat")
assert 'id="sendBtn"' in html or 'id="sendButton"' in html or \
'onclick="sendMessage' in html or 'addEventListener' in html, \
"send button not found"
def test_load_messages_function_exists():
"""Test loadMessages / selectConversation function exists."""
html, _ = get_page("/chat")
assert 'function loadMessages' in html or \
'function selectConversation' in html or \
'loadMessages(' in html or \
'fetch(' in html, \
"message loading function not found"
def test_send_message_function_exists():
"""Test sendMessage function exists."""
html, _ = get_page("/chat")
assert 'function sendMessage' in html or 'sendMessage(' in html, \
"sendMessage function not found"
def test_sse_streaming_integration():
"""Test SSE/streaming integration exists."""
html, _ = get_page("/chat")
assert 'EventSource' in html or 'stream' in html or 'text/event-stream' in html or \
'/api/conversations' in html, \
"SSE streaming integration not found"
def test_no_js_escape_bugs():
"""Test no style backslash triple-escape bugs."""
html, _ = get_page("/chat")
assert "style=\\\\\\\\'" not in html, "STYLE BACKSLASH BUG: triple backslash in style attribute"
assert '\\\\\\\\\\\\\\\\\\\\\\"' not in html, "TRIPLE ESCAPE BUG in JS strings"
def test_no_onclick_escape_bugs():
"""Test no onclick escape bugs."""
html, _ = get_page("/chat")
# Check for onclick with backslash-quote patterns
for pattern in ["onclick=\"toggleContent(", "onclick=\"\\\\'", "onclick='\\\\\""]:
assert pattern not in html, f"ONCLICK ESCAPE BUG: found pattern: {pattern}"
def test_conversation_list_in_sidebar():
"""Test sidebar has conversation list."""
html, _ = get_page("/chat")
assert 'id="convList"' in html or 'loadConversations' in html, \
"conversation list in sidebar not found"
def test_new_chat_button():
"""Test new conversation button exists."""
html, _ = get_page("/chat")
assert 'newConversation' in html or 'new-chat-btn' in html or \
'新建对话' in html, \
"new chat button not found"
if __name__ == "__main__":
# Run tests with -v
sys.exit(pytest.main([__file__, "-v", "--tb=short"]))