Spaces:
Sleeping
Sleeping
| """ | |
| Simple standalone test script - can be run directly without pytest | |
| Usage: | |
| python test_simple.py | |
| """ | |
| import sys | |
| from pathlib import Path | |
| # Setup paths | |
| backend_dir = Path(__file__).parent / "backend" | |
| sys.path.insert(0, str(backend_dir)) | |
| root_dir = Path(__file__).parent | |
| sys.path.insert(0, str(root_dir)) | |
| def test_analytics_store(): | |
| """Test AnalyticsStore""" | |
| print("\n" + "="*60) | |
| print("Testing AnalyticsStore") | |
| print("="*60) | |
| try: | |
| from api.storage.analytics_store import AnalyticsStore | |
| store = AnalyticsStore() | |
| tenant_id = "test_simple" | |
| # Log some events | |
| print("β Logging tool usage...") | |
| store.log_tool_usage(tenant_id, "rag", latency_ms=150, tokens_used=500, success=True) | |
| store.log_tool_usage(tenant_id, "web", latency_ms=80, success=True) | |
| print("β Logging red-flag violation...") | |
| store.log_redflag_violation( | |
| tenant_id, "rule1", ".*password.*", "high", | |
| "password123", confidence=0.95 | |
| ) | |
| print("β Logging RAG search...") | |
| store.log_rag_search(tenant_id, "test query", hits_count=5, avg_score=0.85) | |
| # Get stats | |
| print("\nπ Tool Usage Stats:") | |
| stats = store.get_tool_usage_stats(tenant_id) | |
| print(f" RAG: {stats.get('rag', {})}") | |
| print(f" Web: {stats.get('web', {})}") | |
| print("\nπ¨ Violations:") | |
| violations = store.get_redflag_violations(tenant_id) | |
| print(f" Count: {len(violations)}") | |
| if violations: | |
| print(f" First: {violations[0]['severity']} - {violations[0]['matched_text']}") | |
| print("\nβ AnalyticsStore test PASSED!") | |
| return True | |
| except Exception as e: | |
| print(f"\nβ AnalyticsStore test FAILED: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| def test_admin_rules(): | |
| """Test Admin Rules with regex""" | |
| print("\n" + "="*60) | |
| print("Testing Admin Rules (Regex & Severity)") | |
| print("="*60) | |
| try: | |
| from api.storage.rules_store import RulesStore | |
| import re | |
| store = RulesStore() | |
| tenant_id = "test_simple" | |
| # Add rule with regex | |
| print("β Adding rule with regex pattern...") | |
| store.add_rule( | |
| tenant_id, | |
| "Block password queries", | |
| pattern=".*password.*", | |
| severity="high", | |
| description="Blocks password queries" | |
| ) | |
| # Get detailed rules | |
| rules = store.get_rules_detailed(tenant_id) | |
| print(f"\nπ Rules found: {len(rules)}") | |
| if rules: | |
| rule = rules[0] | |
| print(f" Pattern: {rule['pattern']}") | |
| print(f" Severity: {rule['severity']}") | |
| print(f" Description: {rule['description']}") | |
| # Test regex | |
| print("\nπ§ͺ Testing regex pattern...") | |
| regex = re.compile(rule['pattern'], re.IGNORECASE) | |
| test_cases = [ | |
| ("What is my password?", True), | |
| ("Regular text", False) | |
| ] | |
| for text, should_match in test_cases: | |
| match = regex.search(text) is not None | |
| status = "β" if match == should_match else "β" | |
| print(f" {status} '{text}' -> {match} (expected {should_match})") | |
| print("\nβ Admin Rules test PASSED!") | |
| return True | |
| except Exception as e: | |
| print(f"\nβ Admin Rules test FAILED: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| def main(): | |
| """Run all tests""" | |
| print("\nπ IntegraChat Simple Tests") | |
| print("="*60) | |
| results = [] | |
| results.append(test_analytics_store()) | |
| results.append(test_admin_rules()) | |
| # Summary | |
| print("\n" + "="*60) | |
| print("Test Summary") | |
| print("="*60) | |
| passed = sum(results) | |
| total = len(results) | |
| print(f"Tests Passed: {passed}/{total}") | |
| if passed == total: | |
| print("β All tests passed!") | |
| return 0 | |
| else: | |
| print("β Some tests failed") | |
| return 1 | |
| if __name__ == "__main__": | |
| exit(main()) | |