Spaces:
Sleeping
Sleeping
File size: 11,065 Bytes
c509b44 78b6d7b c509b44 |
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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# IntegraChat Testing Guide
This guide explains how to test all the new features and improvements in IntegraChat.
## Prerequisites
1. **Install Dependencies**
```bash
pip install -r requirements.txt
```
2. **Environment Setup**
- Create a `.env` file or set environment variables
- Optional: Set up Ollama for LLM testing
- Optional: Set up Supabase for production analytics
## Test Structure
### 1. Unit Tests
Run unit tests for individual components:
```bash
# Run all unit tests
pytest backend/tests/
# Run specific test files
pytest backend/tests/test_analytics_store.py -v
pytest backend/tests/test_enhanced_admin_rules.py -v
pytest backend/tests/test_api_endpoints.py -v
# Run with coverage
pytest backend/tests/ --cov=backend/api --cov-report=html
```
### 2. Integration Tests
Test API endpoints with the FastAPI test client:
```bash
pytest backend/tests/test_api_endpoints.py -v
```
**Note**: Some integration tests may fail if MCP servers or LLM are not running. That's expected.
### 3. Manual Testing Scripts
Create test data and verify functionality manually:
#### A. Test Analytics Store
```bash
python -c "
from backend.api.storage.analytics_store import AnalyticsStore
import time
store = AnalyticsStore()
# Log tool usage
store.log_tool_usage('test_tenant', 'rag', latency_ms=150, tokens_used=500, success=True)
store.log_tool_usage('test_tenant', 'web', latency_ms=80, success=True)
# Log red-flag violation
store.log_redflag_violation(
'test_tenant',
'rule1',
'.*password.*',
'high',
'password123',
confidence=0.95
)
# Log RAG search
store.log_rag_search('test_tenant', 'test query', hits_count=5, avg_score=0.85, top_score=0.92)
# Log agent query
store.log_agent_query('test_tenant', 'test message', intent='rag', tools_used=['rag', 'llm'], total_tokens=1000)
# Get stats
print('Tool Usage:', store.get_tool_usage_stats('test_tenant'))
print('Violations:', store.get_redflag_violations('test_tenant'))
print('Activity:', store.get_activity_summary('test_tenant'))
print('RAG Quality:', store.get_rag_quality_metrics('test_tenant'))
"
```
#### B. Test Admin Rules with Regex
```bash
python -c "
from backend.api.storage.rules_store import RulesStore
import re
store = RulesStore()
# Add rule with regex pattern
store.add_rule(
'test_tenant',
'Block password queries',
pattern='.*password.*|.*pwd.*',
severity='high',
description='Blocks password-related queries'
)
# Get detailed rules
rules = store.get_rules_detailed('test_tenant')
print('Rules:', rules)
# Test regex matching
pattern = rules[0]['pattern']
regex = re.compile(pattern, re.IGNORECASE)
test_text = 'What is my password?'
match = regex.search(test_text)
print(f'Match for \"{test_text}\": {match is not None}')
"
```
## API Endpoint Testing
### Using curl
#### 1. Test Analytics Endpoints
```bash
# Overview
curl -X GET "http://localhost:8000/analytics/overview?days=30" \
-H "x-tenant-id: test_tenant"
# Tool Usage
curl -X GET "http://localhost:8000/analytics/tool-usage?days=30" \
-H "x-tenant-id: test_tenant"
# RAG Quality
curl -X GET "http://localhost:8000/analytics/rag-quality?days=30" \
-H "x-tenant-id: test_tenant"
# Red Flags
curl -X GET "http://localhost:8000/analytics/redflags?limit=50&days=30" \
-H "x-tenant-id: test_tenant"
```
#### 2. Test Admin Endpoints
```bash
# Add rule with regex and severity
curl -X POST "http://localhost:8000/admin/rules" \
-H "x-tenant-id: test_tenant" \
-H "Content-Type: application/json" \
-d '{
"rule": "Block password queries",
"pattern": ".*password.*",
"severity": "high",
"description": "Blocks password-related queries"
}'
# Get detailed rules
curl -X GET "http://localhost:8000/admin/rules?detailed=true" \
-H "x-tenant-id: test_tenant"
# Get violations
curl -X GET "http://localhost:8000/admin/violations?limit=50&days=30" \
-H "x-tenant-id: test_tenant"
# Get tool logs
curl -X GET "http://localhost:8000/admin/tools/logs?tool_name=rag&days=7" \
-H "x-tenant-id: test_tenant"
```
#### 3. Test Agent Endpoints
```bash
# Agent chat (normal)
curl -X POST "http://localhost:8000/agent/message" \
-H "Content-Type: application/json" \
-d '{
"tenant_id": "test_tenant",
"message": "What is the company policy?",
"temperature": 0.0
}'
# Agent debug
curl -X POST "http://localhost:8000/agent/debug" \
-H "Content-Type: application/json" \
-d '{
"tenant_id": "test_tenant",
"message": "What is the company policy?",
"temperature": 0.0
}'
# Agent plan
curl -X POST "http://localhost:8000/agent/plan" \
-H "Content-Type: application/json" \
-d '{
"tenant_id": "test_tenant",
"message": "What is the company policy?",
"temperature": 0.0
}'
```
### Using Python requests
Create a test script `test_api_manual.py`:
```python
import requests
import json
BASE_URL = "http://localhost:8000"
TENANT_ID = "test_tenant"
headers = {"x-tenant-id": TENANT_ID}
# Test analytics
print("Testing Analytics Endpoints...")
response = requests.get(f"{BASE_URL}/analytics/overview?days=30", headers=headers)
print(f"Overview: {response.status_code} - {json.dumps(response.json(), indent=2)}")
response = requests.get(f"{BASE_URL}/analytics/tool-usage?days=30", headers=headers)
print(f"Tool Usage: {response.status_code} - {json.dumps(response.json(), indent=2)}")
# Test admin rules
print("\nTesting Admin Rules...")
response = requests.post(
f"{BASE_URL}/admin/rules",
headers=headers,
json={
"rule": "Block password queries",
"pattern": ".*password.*",
"severity": "high"
}
)
print(f"Add Rule: {response.status_code} - {json.dumps(response.json(), indent=2)}")
response = requests.get(
f"{BASE_URL}/admin/rules?detailed=true",
headers=headers
)
print(f"Get Rules: {response.status_code} - {json.dumps(response.json(), indent=2)}")
# Test agent endpoints
print("\nTesting Agent Endpoints...")
response = requests.post(
f"{BASE_URL}/agent/plan",
json={
"tenant_id": TENANT_ID,
"message": "What is the company policy?",
"temperature": 0.0
}
)
print(f"Agent Plan: {response.status_code} - {json.dumps(response.json(), indent=2)}")
```
Run it:
```bash
python test_api_manual.py
```
## End-to-End Testing Workflow
### Step 1: Start Backend Services
```bash
# Terminal 1: Start FastAPI backend
cd backend/api
uvicorn main:app --port 8000 --reload
# Terminal 2: Start unified MCP server (rag/web/admin tools)
python backend/mcp_server/server.py
# Optional: Start Ollama for LLM
ollama serve
```
### Step 2: Generate Test Data
Run the analytics and rules tests to populate the database:
```bash
pytest backend/tests/test_analytics_store.py -v
pytest backend/tests/test_enhanced_admin_rules.py -v
```
### Step 3: Test Agent Flow
1. **Add some admin rules:**
```bash
curl -X POST "http://localhost:8000/admin/rules" \
-H "x-tenant-id: test_tenant" \
-H "Content-Type: application/json" \
-d '{"rule": "Block password queries", "pattern": ".*password.*", "severity": "high"}'
```
2. **Send a query that triggers red-flag:**
```bash
curl -X POST "http://localhost:8000/agent/message" \
-H "Content-Type: application/json" \
-d '{"tenant_id": "test_tenant", "message": "What is my password?"}'
```
3. **Check violations were logged:**
```bash
curl -X GET "http://localhost:8000/admin/violations" \
-H "x-tenant-id: test_tenant"
```
4. **Send normal queries and check analytics:**
```bash
curl -X POST "http://localhost:8000/agent/message" \
-H "Content-Type: application/json" \
-d '{"tenant_id": "test_tenant", "message": "What is the company policy?"}'
curl -X GET "http://localhost:8000/analytics/overview" \
-H "x-tenant-id: test_tenant"
```
5. **Use debug endpoint to see reasoning:**
```bash
curl -X POST "http://localhost:8000/agent/debug" \
-H "Content-Type: application/json" \
-d '{"tenant_id": "test_tenant", "message": "What is the company policy?"}'
```
### Step 4: Verify Database
Check that data is being stored:
```bash
# SQLite databases are in data/ directory
sqlite3 data/analytics.db "SELECT * FROM tool_usage_events LIMIT 10;"
sqlite3 data/analytics.db "SELECT * FROM redflag_violations LIMIT 10;"
sqlite3 data/admin_rules.db "SELECT * FROM admin_rules;"
```
## Testing Checklist
### Analytics Store
- [ ] Tool usage logging works
- [ ] Red-flag violations are logged
- [ ] RAG search events are logged with quality metrics
- [ ] Agent query events are logged
- [ ] Stats can be filtered by time
- [ ] Multiple tenants are isolated
### Admin Rules
- [ ] Rules can be added with regex patterns
- [ ] Severity levels work (low/medium/high/critical)
- [ ] Rules without pattern use rule text
- [ ] Disabled rules are not returned
- [ ] Multiple tenants are isolated
- [ ] Regex patterns actually match correctly
### API Endpoints
- [ ] `/analytics/overview` returns correct data
- [ ] `/analytics/tool-usage` returns stats
- [ ] `/analytics/rag-quality` returns metrics
- [ ] `/admin/rules` accepts regex/severity
- [ ] `/admin/violations` returns violations
- [ ] `/admin/tools/logs` returns tool usage
- [ ] `/agent/debug` returns reasoning trace
- [ ] `/agent/plan` returns tool selection plan
- [ ] Missing tenant_id returns 400
### Integration
- [ ] Agent orchestrator logs to analytics
- [ ] Red-flag detector logs violations
- [ ] Tool calls are tracked
- [ ] Multi-step workflows are logged
- [ ] Errors are logged correctly
## Common Issues
### Database Not Found
- Ensure `data/` directory exists
- Analytics store will create it automatically
### Tests Fail Due to Missing Services
- Some tests require MCP servers or LLM to be running
- Mock these services or skip tests if services unavailable
- Unit tests should work without external services
### Import Errors
- Ensure you're running from project root
- Check that `backend/` is in Python path
- Install all dependencies: `pip install -r requirements.txt`
## Performance Testing
For large-scale testing:
```python
# Load test analytics store
from backend.api.storage.analytics_store import AnalyticsStore
import time
store = AnalyticsStore()
tenant_id = "load_test_tenant"
start = time.time()
for i in range(1000):
store.log_tool_usage(tenant_id, "rag", latency_ms=100 + i % 50)
elapsed = time.time() - start
print(f"Logged 1000 events in {elapsed:.2f}s ({1000/elapsed:.0f} events/sec)")
# Query performance
start = time.time()
stats = store.get_tool_usage_stats(tenant_id)
elapsed = time.time() - start
print(f"Query took {elapsed*1000:.2f}ms")
```
## Next Steps
1. **Add more test cases** for edge cases
2. **Set up CI/CD** to run tests automatically
3. **Add performance benchmarks** for analytics queries
4. **Create integration test suite** that spins up all services
5. **Add E2E tests** using Playwright or Selenium for frontend
For questions or issues, check the test files in `backend/tests/` or refer to the main README.md.
|