IntegraChat / test_simple.py
nothingworry's picture
working Tenant ID
c509b44
raw
history blame
4.28 kB
"""
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())