File size: 4,377 Bytes
27bbd47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test script for the cache system
"""

import os
from deepseek_caller import DeepSeekCaller
from stats_logger import StatsLogger
from config import DEEPSEEK_API_KEY

def test_deepseek_connection():
    """Test if DeepSeek API is accessible"""
    print("=" * 70)
    print("🧪 TEST 1: DeepSeek API Connection")
    print("=" * 70)

    if not DEEPSEEK_API_KEY:
        print(" DEEPSEEK_API_KEY not found in environment")
        print("   Set it with: export DEEPSEEK_API_KEY='your-key'")
        return False

    try:
        caller = DeepSeekCaller()
        is_connected = caller.test_connection()

        if is_connected:
            print(" DeepSeek API is accessible")
            return True
        else:
            print(" DeepSeek API test failed")
            return False

    except Exception as e:
        print(f" Error: {e}")
        return False

def test_feedback_generation():
    """Test feedback generation"""
    print("\n" + "=" * 70)
    print("🧪 TEST 2: Feedback Generation")
    print("=" * 70)

    caller = DeepSeekCaller()

    test_context = {
        "theme": "Prime Number Check",
        "difficulty": "beginner",
        "error_category": "Incorrect Base Case Return Value",
        "instructions": "Write a function to check if a number is prime",
        "code": """
int is_prime(int n) {
    if (n <= 1) {
        return 1;  // Bug: should return 0
    }
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return 0;
    }
    return 1;
}
""",
        "test_cases_scope": ["Input: 1", "Input: 2", "Input: 17"],
        "failed_tests": ["Input: 1 (returns 1, expected 0)"]
    }

    print(" Generating feedback...")
    result = caller.generate_feedback(test_context)

    if result.get('feedback'):
        print(" Feedback generated successfully!")
        print("\n Metrics:")
        print(f"   Tokens (prompt): {result['tokens_prompt']}")
        print(f"   Tokens (completion): {result['tokens_completion']}")
        print(f"   Total tokens: {result['tokens_total']}")
        print(f"   Generation time: {result['generation_time_ms']:.0f} ms")
        print("\n Feedback:")
        print(f"   {result['feedback'][:200]}...")
        return True
    else:
        print(f" Error: {result.get('error')}")
        return False

def test_stats_logger():
    """Test stats logging"""
    print("\n" + "=" * 70)
    print("🧪 TEST 3: Stats Logger")
    print("=" * 70)

    logger = StatsLogger()

    # Test query log
    test_query = {
        "query_id": "test-123",
        "status": "hit",
        "similarity_score": 0.15,
        "confidence": 0.95,
        "response_time_ms": 123.45,
        "theme": "Test Theme",
        "error_category": "Test Error",
        "difficulty": "beginner",
        "deepseek_tokens": 0,
        "cache_size": 100
    }

    try:
        logger.log_query(test_query)
        print(" Query logged successfully")

        # Read back
        stats = logger.read_stats(limit=1)
        if stats:
            print(f" Read back: {stats[-1]['query_id']}")
        else:
            print("  No stats found (empty file)")

        return True

    except Exception as e:
        print(f" Error: {e}")
        return False

def main():
    print(" TESTING CACHE SYSTEM COMPONENTS")
    print()

    results = []

    # Test 1: API Connection
    results.append(("DeepSeek API", test_deepseek_connection()))

    # Test 2: Feedback Generation
    if results[0][1]:  # Only if API works
        results.append(("Feedback Generation", test_feedback_generation()))
    else:
        print("\n⏭  Skipping feedback generation test (API unavailable)")

    # Test 3: Stats Logger
    results.append(("Stats Logger", test_stats_logger()))

    # Summary
    print("\n" + "=" * 70)
    print(" TEST SUMMARY")
    print("=" * 70)

    for test_name, passed in results:
        status = " PASS" if passed else " FAIL"
        print(f"{status:12} {test_name}")

    total_tests = len(results)
    passed_tests = sum(1 for _, passed in results if passed)

    print()
    print(f"Total: {passed_tests}/{total_tests} tests passed")

    if passed_tests == total_tests:
        print(" All tests passed! System is ready.")
        return 0
    else:
        print("  Some tests failed. Check configuration.")
        return 1

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