test_s5_chat_page.py 2.46 KB
#!/usr/bin/env python3
"""Test S5: AI chat page with conversation list."""
import sys, os, subprocess, unittest

sys.path.insert(0, os.path.expanduser("~/wechat-viewer"))

class TestChatPage(unittest.TestCase):
    """Test the /chat AI chat page."""

    def test_chat_route_exists(self):
        """@app.route('/chat') should exist in app.py."""
        app_path = os.path.expanduser("~/wechat-viewer/app.py")
        code = open(app_path).read()
        # Check route exists but NOT the /chat/<talker> variant
        self.assertIn("@app.route(\"/chat\")", code.replace("@app.route(\"/chat/\")", ""),
                     "/chat route must exist in app.py")

    def test_aichat_template_has_sidebar(self):
        """aichat.html should contain conversation sidebar elements."""
        tmpl_path = os.path.expanduser("~/wechat-viewer/templates/aichat.html")
        self.assertTrue(os.path.exists(tmpl_path), "aichat.html template must exist")
        if os.path.exists(tmpl_path):
            content = open(tmpl_path).read()
            self.assertIn("新建对话", content, "Should have new conversation button")
            self.assertIn("sidebar", content.lower(), "Should have sidebar element")
            self.assertIn("conversation", content.lower(), "Should reference conversations")

    def test_chat_page_returns_200(self):
        """curl /chat should return HTTP 200."""
        # Check if Flask is running
        r = subprocess.run(
            ["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}",
             "http://127.0.0.1:5000/chat", "--connect-timeout", "3", "--max-time", "5"],
            capture_output=True, text=True, timeout=10
        )
        code = r.stdout.strip()
        # Flask may not be running, but the route should exist in code
        self.assertIn(code, ("200", "000"), f"Expected 200 or 000 (not running), got {code}")


if __name__ == '__main__':
    # Quick pre-check
    app_path = os.path.expanduser("~/wechat-viewer/app.py")
    code = open(app_path).read()
    has_route = '@app.route("/chat")' in code.replace('@app.route("/chat/")', '')
    tmpl_path = os.path.expanduser("~/wechat-viewer/templates/aichat.html")
    has_template = os.path.exists(tmpl_path)
    
    print(f"/chat route exists: {has_route}")
    print(f"aichat.html exists: {has_template}")
    
    if not has_route or not has_template:
        print("RED: Missing /chat route or aichat.html template — test expected to fail")
        sys.exit(1)
    
    unittest.main(verbosity=2)