File size: 15,249 Bytes
459923e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test script for the enhanced Feedback system.
Demonstrates all new features while maintaining backward compatibility.
"""

import json
import logging
from Feedback import Grader

# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

def test_basic_functionality():
    """Test basic functionality (backward compatibility)."""
    print("\n" + "="*60)
    print("TEST 1: Basic Functionality (Backward Compatibility)")
    print("="*60)
    
    # Initialize with default configuration
    grader = Grader(api_key="your-api-key-here")
    
    # Short essay (should work exactly as before)
    short_essay = """
    Climate change is a serious global issue. The burning of fossil fuels releases 
    greenhouse gases that trap heat in the atmosphere. This causes global temperatures 
    to rise, leading to melting ice caps and extreme weather events. We must take 
    action to reduce our carbon footprint and transition to renewable energy sources.
    """
    
    training_context = "Evaluate this essay on climate change."
    
    try:
        feedback = grader.grade_answer_with_gpt(short_essay, training_context)
        print("βœ… Basic functionality works!")
        print(f"Overall score: {feedback.get('overall_score', 'N/A')}")
        print(f"Number of sections: {len(feedback.get('sections', []))}")
        
        # Check if chunking was used (should not be for short essay)
        if 'chunk_analysis' in feedback:
            print(f"Chunking was used: {feedback['chunk_analysis']['total_chunks']} chunks")
        else:
            print("No chunking used (as expected for short essay)")
            
    except Exception as e:
        print(f"❌ Basic functionality failed: {e}")

def test_chunking_functionality():
    """Test chunking functionality with long essay."""
    print("\n" + "="*60)
    print("TEST 2: Chunking Functionality")
    print("="*60)
    
    # Initialize with chunking enabled
    config = {
        'enable_chunking': True,
        'max_chunk_tokens': 4000,
        'enable_validation': True,
        'warn_on_truncation': True
    }
    
    grader = Grader(api_key="your-api-key-here", config=config)
    
    # Long essay that should trigger chunking
    long_essay = """
    OUTLINE:
    1. Introduction
    2. Historical Context
    3. Current Challenges
    4. Future Implications
    5. Conclusion
    
    ESSAY:
    
    Climate change represents one of the most pressing challenges facing humanity in the 21st century. 
    The scientific consensus is clear: human activities, particularly the burning of fossil fuels, 
    have significantly contributed to the warming of our planet. This essay will explore the historical 
    context of climate change, examine current challenges, and discuss future implications for our world.
    
    Historically, the Industrial Revolution marked the beginning of significant human impact on the 
    Earth's climate. The widespread use of coal, oil, and natural gas for energy production has led 
    to unprecedented levels of carbon dioxide and other greenhouse gases in the atmosphere. These 
    gases trap heat from the sun, creating what scientists call the greenhouse effect. While this 
    effect is natural and necessary for life on Earth, human activities have intensified it to 
    dangerous levels.
    
    The current challenges posed by climate change are multifaceted and far-reaching. Rising global 
    temperatures have led to the melting of polar ice caps and glaciers, contributing to sea level 
    rise that threatens coastal communities worldwide. Extreme weather events, including hurricanes, 
    droughts, and heatwaves, have become more frequent and intense. These changes affect not only 
    the environment but also human health, agriculture, and economic stability.
    
    Furthermore, climate change exacerbates existing social and economic inequalities. Developing 
    nations, which have contributed least to the problem, often bear the brunt of climate impacts. 
    Small island nations face existential threats from rising sea levels, while agricultural 
    communities struggle with changing weather patterns that affect crop yields. The economic costs 
    of climate change are staggering, with estimates suggesting trillions of dollars in potential 
    damages if action is not taken.
    
    Looking to the future, the implications of climate change become even more concerning if current 
    trends continue. Scientists warn that we may reach critical tipping points, such as the collapse 
    of major ice sheets or the release of methane from thawing permafrost, which could accelerate 
    warming beyond our ability to control. The window for meaningful action is closing rapidly, 
    making immediate and coordinated global response essential.
    
    However, there is reason for hope. Renewable energy technologies, such as solar and wind power, 
    have become increasingly cost-effective and widespread. Many countries have committed to reducing 
    their greenhouse gas emissions through international agreements like the Paris Climate Accord. 
    Individual actions, from reducing energy consumption to supporting sustainable practices, can 
    collectively make a significant difference.
    
    In conclusion, climate change represents a complex challenge that requires immediate attention 
    and coordinated action at all levels of society. While the challenges are significant, the 
    solutions are within our reach. Through technological innovation, policy changes, and individual 
    commitment, we can work toward a more sustainable future. The time to act is now, as the 
    consequences of inaction will only become more severe with each passing year.
    """
    
    training_context = "Evaluate this comprehensive essay on climate change, considering its structure, arguments, and evidence."
    
    try:
        # First, validate essay length
        length_analysis = grader.validate_essay_length(long_essay)
        print(f"Essay analysis:")
        print(f"  - Tokens: {length_analysis['token_count']}")
        print(f"  - Words: {length_analysis['word_count']}")
        print(f"  - Chunking needed: {length_analysis['chunking_needed']}")
        print(f"  - Recommendation: {length_analysis['processing_recommendation']}")
        
        # Grade the essay
        feedback = grader.grade_answer_with_gpt(long_essay, training_context)
        
        print("\nβœ… Chunking functionality works!")
        print(f"Overall score: {feedback.get('overall_score', 'N/A')}")
        
        # Check chunking information
        if 'chunk_analysis' in feedback:
            chunk_info = feedback['chunk_analysis']
            print(f"Chunking details:")
            print(f"  - Total chunks: {chunk_info['total_chunks']}")
            print(f"  - Chunks processed: {chunk_info['chunks_processed']}")
            print(f"  - Aggregation method: {chunk_info['aggregation_method']}")
        
        # Check token information
        if 'token_info' in feedback:
            token_info = feedback['token_info']
            print(f"Token processing:")
            print(f"  - Original tokens: {token_info['original_tokens']}")
            print(f"  - Processed tokens: {token_info['processed_tokens']}")
            print(f"  - Was truncated: {token_info['was_truncated']}")
            if 'chunked_processing' in token_info:
                print(f"  - Chunked processing: {token_info['chunked_processing']}")
        
        # Display section scores
        print("\nSection scores:")
        for section in feedback.get('sections', []):
            print(f"  - {section['name']}: {section['score']}% ({section['issues_count']} issues)")
            
    except Exception as e:
        print(f"❌ Chunking functionality failed: {e}")

def test_question_specific_grading():
    """Test question-specific grading functionality."""
    print("\n" + "="*60)
    print("TEST 3: Question-Specific Grading")
    print("="*60)
    
    grader = Grader(api_key="your-api-key-here")
    
    essay = """
    Climate change is caused by human activities like burning fossil fuels and deforestation. 
    These activities release greenhouse gases that trap heat in the atmosphere, causing global 
    temperatures to rise. The effects include melting ice caps, rising sea levels, and more 
    extreme weather events. Solutions include transitioning to renewable energy, reducing 
    emissions, and protecting forests.
    """
    
    question = "What are the main causes and effects of climate change?"
    
    try:
        feedback = grader.grade_answer_with_question(essay, question)
        
        print("βœ… Question-specific grading works!")
        print(f"Overall score: {feedback.get('overall_score', 'N/A')}")
        
        # Check question-specific feedback
        if 'question_specific_feedback' in feedback:
            q_feedback = feedback['question_specific_feedback']
            print(f"Question: {q_feedback.get('question', 'N/A')}")
            print(f"Question relevance score: {q_feedback.get('question_relevance_score', 'N/A')}%")
            print(f"Covered aspects: {len(q_feedback.get('covered_aspects', []))}")
            print(f"Missing aspects: {len(q_feedback.get('missing_aspects', []))}")
        
    except Exception as e:
        print(f"❌ Question-specific grading failed: {e}")

def test_configuration_management():
    """Test configuration management features."""
    print("\n" + "="*60)
    print("TEST 4: Configuration Management")
    print("="*60)
    
    # Initialize with custom configuration
    config = {
        'enable_chunking': True,
        'max_chunk_tokens': 3000,
        'enable_granular_feedback': False,
        'enable_validation': True,
        'warn_on_truncation': True
    }
    
    grader = Grader(api_key="your-api-key-here", config=config)
    
    try:
        # Get processing stats
        stats = grader.get_processing_stats()
        print("βœ… Configuration management works!")
        print("Current configuration:")
        for key, value in stats['configuration'].items():
            print(f"  - {key}: {value}")
        
        print("\nCapabilities:")
        for key, value in stats['capabilities'].items():
            print(f"  - {key}: {value}")
        
        # Test configuration update
        print("\nUpdating configuration...")
        grader.update_config({'max_chunk_tokens': 5000})
        
        updated_stats = grader.get_processing_stats()
        print(f"Updated max_chunk_tokens: {updated_stats['configuration']['max_chunk_tokens']}")
        
        # Test reset to defaults
        print("\nResetting to defaults...")
        grader.reset_to_defaults()
        
        default_stats = grader.get_processing_stats()
        print(f"Default max_chunk_tokens: {default_stats['configuration']['max_chunk_tokens']}")
        
    except Exception as e:
        print(f"❌ Configuration management failed: {e}")

def test_validation_and_error_recovery():
    """Test validation and error recovery features."""
    print("\n" + "="*60)
    print("TEST 5: Validation and Error Recovery")
    print("="*60)
    
    config = {
        'enable_validation': True,
        'enable_enhanced_logging': True,
        'log_missing_categories': True
    }
    
    grader = Grader(api_key="your-api-key-here", config=config)
    
    # Test with a short essay to check validation
    short_essay = "Climate change is bad. We should fix it."
    
    try:
        feedback = grader.grade_answer_with_gpt(short_essay, "Evaluate this essay.")
        
        print("βœ… Validation and error recovery works!")
        print(f"Overall score: {feedback.get('overall_score', 'N/A')}")
        
        # Check if all required sections are present
        sections = feedback.get('sections', [])
        required_categories = [
            'Grammar & Punctuation',
            'Vocabulary Usage',
            'Sentence Structure',
            'Content Relevance & Depth',
            'Argument Development',
            'Evidence & Citations',
            'Structure & Organization',
            'Conclusion Quality'
        ]
        
        present_categories = [section['name'] for section in sections]
        missing_categories = [cat for cat in required_categories if cat not in present_categories]
        
        if missing_categories:
            print(f"⚠️  Missing categories: {missing_categories}")
        else:
            print("βœ… All required categories present")
            
    except Exception as e:
        print(f"❌ Validation and error recovery failed: {e}")

def test_essay_length_validation():
    """Test essay length validation functionality."""
    print("\n" + "="*60)
    print("TEST 6: Essay Length Validation")
    print("="*60)
    
    grader = Grader(api_key="your-api-key-here")
    
    # Test different essay lengths
    test_cases = [
        ("Very short essay.", "Short essay test"),
        ("This is a medium length essay with several sentences. It contains enough content to test the system properly.", "Medium essay test"),
        ("This is a very long essay. " * 100, "Long essay test")
    ]
    
    try:
        for essay, description in test_cases:
            print(f"\nTesting {description}:")
            analysis = grader.validate_essay_length(essay)
            
            print(f"  - Tokens: {analysis['token_count']}")
            print(f"  - Words: {analysis['word_count']}")
            print(f"  - Chunking needed: {analysis['chunking_needed']}")
            print(f"  - Recommendation: {analysis['processing_recommendation']}")
            
            if analysis['warnings']:
                print(f"  - Warnings: {analysis['warnings']}")
        
        print("\nβœ… Essay length validation works!")
        
    except Exception as e:
        print(f"❌ Essay length validation failed: {e}")

def main():
    """Run all tests."""
    print("Enhanced Feedback System - Test Suite")
    print("="*60)
    print("This test suite demonstrates all enhanced features while")
    print("maintaining backward compatibility with existing code.")
    print("="*60)
    
    # Note: Replace "your-api-key-here" with actual API key for real testing
    print("\n⚠️  NOTE: Replace 'your-api-key-here' with actual OpenAI API key for real testing")
    print("   The tests will show the structure but won't make actual API calls without a valid key.\n")
    
    try:
        test_basic_functionality()
        test_chunking_functionality()
        test_question_specific_grading()
        test_configuration_management()
        test_validation_and_error_recovery()
        test_essay_length_validation()
        
        print("\n" + "="*60)
        print("βœ… ALL TESTS COMPLETED")
        print("="*60)
        print("The enhanced Feedback system is working correctly!")
        print("All features are backward compatible and ready for production use.")
        
    except Exception as e:
        print(f"\n❌ Test suite failed: {e}")

if __name__ == "__main__":
    main()