test_s6_chat_interface.py 3.72 KB
"""
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"]))