File size: 6,400 Bytes
36ed60f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from playwright.sync_api import sync_playwright, expect
import time
from pathlib import Path
import sys
import os
import subprocess
import signal
import re

# Add project root to path
sys.path.insert(0, str(Path(__file__).parent.parent))

from e2e_tests.playwright_helpers import (
    wait_for_gradio,
    load_character,
    send_message,
    get_last_bot_message,
    get_all_messages,
    kill_port
)

def test_chat_functionality_playwright():
    """
    Test: Chat Functionality (Playwright)
    """
    print("\n" + "πŸ’¬" * 40)
    print("TEST: Chat Functionality (Playwright)")
    print("πŸ’¬" * 40)
    
    # Ensure port 7860 is free
    kill_port(7860)
    
    # Start Gradio server
    print("\nπŸš€ Starting Gradio server...")
    gradio_process = subprocess.Popen(
        ['python3', 'web/app_gradio.py'],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    )
    time.sleep(10) # Give server time to start
    
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        page = browser.new_page()
        
        try:
            page.goto("http://localhost:7860")
            wait_for_gradio(page)
            
            # Load Thorin
            print("\nπŸ“ Loading Thorin for chat tests...")
            load_character(page, "Thorin")
            
            # 1. Test Chat Input Exists
            print("\n" + "=" * 80)
            print("TEST 1: Chat Input Exists")
            print("=" * 80)
            expect(page.get_by_placeholder("Type your action")).to_be_visible()
            print("βœ… Chat input exists.")
            
            # 2. Test Send Button Exists
            print("\n" + "=" * 80)
            print("TEST 2: Send Button Exists")
            print("=" * 80)
            expect(page.get_by_role("button", name="Send")).to_be_visible()
            print("βœ… Send button exists.")
            
            # 3. Test Clear History Button Exists
            print("\n" + "=" * 80)
            print("TEST 3: Clear History Button Exists")
            print("=" * 80)
            expect(page.get_by_role("button", name="Clear History")).to_be_visible()
            print("βœ… Clear History button exists.")
            
            # 4. Test Send Simple Message
            print("\n" + "=" * 80)
            print("TEST 4: Send Simple Message")
            print("=" * 80)
            test_message = "I look around the tavern."
            send_message(page, test_message)
            
            # Verify user message appears in chat history
            expect(page.locator(".message").filter(has_text=test_message)).to_be_visible()
            gm_response = get_last_bot_message(page)
            print(f"   GM response preview: {gm_response[:100]}...")
            if "tavern" in gm_response.lower() or "look around" in gm_response.lower():
                print("βœ… GM responded to simple message.")
            else:
                print("⚠️  GM response unclear.")
            
            # 5. Test /help Command
            print("\n" + "=" * 80)
            print("TEST 5: /help Command")
            print("=" * 80)
            send_message(page, "/help")
            help_response = get_last_bot_message(page)
            expect(page.locator("body")).to_contain_text("Available Commands")
            print("βœ… /help command response verified.")
            
            # 6. Test /stats Command
            print("\n" + "=" * 80)
            print("TEST 6: /stats Command")
            print("=" * 80)
            send_message(page, "/stats")
            stats_response = get_last_bot_message(page)
            expect(page.locator("body")).to_contain_text("Thorin Stormshield")
            expect(page.locator("body")).to_contain_text("HP:")
            print("βœ… /stats command response verified.")
            
            # 7. Test /context Command
            print("\n" + "=" * 80)
            print("TEST 7: /context Command")
            print("=" * 80)
            send_message(page, "/context")
            context_response = get_last_bot_message(page)
            expect(page.locator("body")).to_contain_text("Current Context:")
            print("βœ… /context command response verified.")
            
            # 8. Test /rag Command
            print("\n" + "=" * 80)
            print("TEST 8: /rag Command")
            print("=" * 80)
            send_message(page, "/rag Fireball")
            rag_response = get_last_bot_message(page)
            expect(page.locator("body")).to_contain_text("RAG Search Results:")
            expect(page.locator("body")).to_contain_text("fireball")
            print("βœ… /rag command response verified.")
            
            # 9. Test Clear Chat History
            print("\n" + "=" * 80)
            print("TEST 9: Clear Chat History")
            print("=" * 80)
            # Send one more message to ensure history has content
            send_message(page, "another message")
            expect(page.locator(".message").count()).to_be_greater_than(1) # At least two messages
            
            page.get_by_role("button", name="Clear History").click()
            time.sleep(1) # Allow UI to update
            
            # Chatbot should be empty or just initial greeting
            all_chat_messages = get_all_messages(page) # Get all visible messages
            expect(len(all_chat_messages)).to_be_less_than_or_equal(1) # Initial greeting usually remains
            print("βœ… Chat history cleared.")
            
            # 10. Test Chat Input Clears After Send
            print("\n" + "=" * 80)
            print("TEST 10: Chat Input Clears")
            print("=" * 80)
            chat_input = page.get_by_placeholder("Type your action")
            send_message(page, "One final message.")
            expect(chat_input).to_have_value("") # Input should be empty
            print("βœ… Chat input clears after send.")
            
            print("\nβœ… All Chat Functionality Tests Completed!")
            
        except Exception as e:
            print(f"\n❌ Test Failed: {e}")
            page.screenshot(path="chat_failure.png")
            raise e
        finally:
            browser.close()
            print("πŸ›‘ Stopping Gradio...")
            gradio_process.terminate()
            gradio_process.wait()

if __name__ == "__main__":
    test_chat_functionality_playwright()