File size: 4,283 Bytes
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
"""
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())