File size: 7,906 Bytes
bed5cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/bash

# Create .env file
cat > /home/ubuntu/cursor-rules-generator/.env << EOL
# App settings
DEBUG=True
PORT=5000

# API keys (these are placeholders, real keys should be provided by users)
GEMINI_API_KEY=
OPENAI_API_KEY=
OPENROUTER_API_KEY=
EOL

# Create a test script to validate the backend functionality
cat > /home/ubuntu/cursor-rules-generator/test_backend.py << EOL
"""
Test script for Cursor Rules Generator backend functionality.
"""

import os
import sys
import json
from backend.llm.factory import LLMAdapterFactory
from backend.engine.rule_generator import RuleGenerator
from backend.config.settings import Settings

def test_factory():
    """Test the LLM adapter factory."""
    print("Testing LLM adapter factory...")
    
    factory = LLMAdapterFactory()
    
    # Test getting supported providers
    providers = factory.get_supported_providers()
    print(f"Supported providers: {providers}")
    
    # Test creating adapters
    for provider_name in providers.keys():
        try:
            adapter = factory.create_adapter(provider_name)
            print(f"Successfully created adapter for {provider_name}")
        except Exception as e:
            print(f"Failed to create adapter for {provider_name}: {e}")
    
    print("LLM adapter factory test completed.")
    print("-" * 50)

def test_rule_generator():
    """Test the rule generator."""
    print("Testing rule generator...")
    
    rule_generator = RuleGenerator()
    
    # Test getting rule types
    rule_types = rule_generator.get_rule_types()
    print(f"Supported rule types: {[rt['id'] for rt in rule_types]}")
    
    # Test creating a basic rule without LLM
    try:
        rule = rule_generator._create_basic_rule(
            rule_type="Always",
            description="Test rule",
            content="This is a test rule content.",
            parameters={
                "globs": "",
                "referenced_files": "@test-file.ts"
            }
        )
        print("Successfully created a basic rule:")
        print(rule)
    except Exception as e:
        print(f"Failed to create a basic rule: {e}")
    
    print("Rule generator test completed.")
    print("-" * 50)

def main():
    """Run all tests."""
    print("Starting backend functionality tests...")
    print("=" * 50)
    
    test_factory()
    test_rule_generator()
    
    print("=" * 50)
    print("All tests completed.")

if __name__ == "__main__":
    main()
EOL

# Create a test script to validate the frontend functionality
cat > /home/ubuntu/cursor-rules-generator/test_frontend.py << EOL
"""
Test script for Cursor Rules Generator frontend functionality.
"""

import os
import sys
import http.server
import socketserver
import threading
import webbrowser
import time

def start_server():
    """Start a simple HTTP server to serve the frontend files."""
    os.chdir("frontend")
    
    PORT = 8000
    Handler = http.server.SimpleHTTPRequestHandler
    
    with socketserver.TCPServer(("", PORT), Handler) as httpd:
        print(f"Serving frontend at http://localhost:{PORT}")
        httpd.serve_forever()

def main():
    """Run frontend tests."""
    print("Starting frontend functionality tests...")
    print("=" * 50)
    
    # Check if frontend files exist
    if not os.path.exists("frontend/index.html"):
        print("Error: frontend/index.html not found.")
        return
    
    if not os.path.exists("frontend/css/styles.css"):
        print("Error: frontend/css/styles.css not found.")
        return
    
    if not os.path.exists("frontend/js/script.js"):
        print("Error: frontend/js/script.js not found.")
        return
    
    print("Frontend files found.")
    
    # Start the server in a separate thread
    server_thread = threading.Thread(target=start_server)
    server_thread.daemon = True
    server_thread.start()
    
    # Wait for the server to start
    time.sleep(1)
    
    # Open the browser
    print("Opening browser to test frontend...")
    webbrowser.open("http://localhost:8000")
    
    print("Frontend server started. Press Ctrl+C to stop.")
    
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print("Server stopped.")
    
    print("=" * 50)
    print("Frontend test completed.")

if __name__ == "__main__":
    main()
EOL

# Create a test script to validate the Hugging Face deployment
cat > /home/ubuntu/cursor-rules-generator/test_huggingface.py << EOL
"""
Test script for Cursor Rules Generator Hugging Face deployment.
"""

import os
import sys
import importlib.util

def test_gradio_app():
    """Test the Gradio app for Hugging Face deployment."""
    print("Testing Gradio app for Hugging Face deployment...")
    
    # Check if the app.py file exists
    if not os.path.exists("huggingface/app.py"):
        print("Error: huggingface/app.py not found.")
        return
    
    # Check if requirements.txt exists
    if not os.path.exists("huggingface/requirements.txt"):
        print("Error: huggingface/requirements.txt not found.")
        return
    
    # Check if README.md exists
    if not os.path.exists("huggingface/README.md"):
        print("Error: huggingface/README.md not found.")
        return
    
    # Check if DEPLOYMENT.md exists
    if not os.path.exists("huggingface/DEPLOYMENT.md"):
        print("Error: huggingface/DEPLOYMENT.md not found.")
        return
    
    print("All required files for Hugging Face deployment found.")
    
    # Try to import gradio to check if it's installed
    try:
        import gradio
        print("Gradio is installed.")
    except ImportError:
        print("Warning: Gradio is not installed. Cannot fully test the app.")
        print("To install Gradio, run: pip install gradio")
        return
    
    # Try to load the app module to check for syntax errors
    try:
        spec = importlib.util.spec_from_file_location("app", "huggingface/app.py")
        app_module = importlib.util.module_from_spec(spec)
        
        # We don't actually execute the module to avoid launching the app
        # spec.loader.exec_module(app_module)
        
        print("Hugging Face app module loaded successfully (syntax check passed).")
    except Exception as e:
        print(f"Error loading Hugging Face app module: {e}")
    
    print("Hugging Face deployment test completed.")
    print("-" * 50)

def main():
    """Run all tests."""
    print("Starting Hugging Face deployment tests...")
    print("=" * 50)
    
    test_gradio_app()
    
    print("=" * 50)
    print("All tests completed.")

if __name__ == "__main__":
    main()
EOL

# Create a main test script to run all tests
cat > /home/ubuntu/cursor-rules-generator/run_tests.py << EOL
"""
Main test script for Cursor Rules Generator.
"""

import os
import sys
import subprocess

def run_test(test_script, description):
    """Run a test script and print the result."""
    print(f"Running {description}...")
    print("=" * 50)
    
    result = subprocess.run([sys.executable, test_script], capture_output=True, text=True)
    
    print(result.stdout)
    
    if result.returncode != 0:
        print(f"Error running {description}:")
        print(result.stderr)
    else:
        print(f"{description} completed successfully.")
    
    print("=" * 50)
    print()

def main():
    """Run all tests."""
    print("Starting Cursor Rules Generator tests...")
    print()
    
    # Test backend functionality
    run_test("test_backend.py", "backend functionality tests")
    
    # Test Hugging Face deployment
    run_test("test_huggingface.py", "Hugging Face deployment tests")
    
    # Note: We don't automatically run the frontend test as it opens a browser
    print("To test the frontend, run: python test_frontend.py")
    
    print("All tests completed.")

if __name__ == "__main__":
    main()
EOL

echo "Test scripts created successfully."