-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqa_test_runner.py
More file actions
565 lines (442 loc) · 21.4 KB
/
qa_test_runner.py
File metadata and controls
565 lines (442 loc) · 21.4 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#!/usr/bin/env python3
"""
Comprehensive QA test runner for session stop hook timeline functionality.
Implements the testing strategy outlined in SESSION_STOP_HOOK_QA_STRATEGY.md
"""
import json
import os
import sys
import time
import tempfile
import subprocess
import shutil
from pathlib import Path
from typing import Dict, List, Tuple, Any
from datetime import datetime
# Add hooks directory to path
sys.path.insert(0, '/home/bryan/dashboard/.claude/hooks')
class QATestResult:
def __init__(self, name: str, passed: bool, details: str = "", duration: float = 0.0):
self.name = name
self.passed = passed
self.details = details
self.duration = duration
self.timestamp = datetime.now()
class StopHookQARunner:
def __init__(self, verbose: bool = True):
self.verbose = verbose
self.test_results: List[QATestResult] = []
self.failed_tests: List[str] = []
self.temp_dirs: List[Path] = []
def log(self, message: str, level: str = "INFO"):
"""Logging with optional verbosity control."""
if self.verbose:
timestamp = datetime.now().strftime("%H:%M:%S")
print(f"[{timestamp}] {level}: {message}")
def cleanup_temp_dirs(self):
"""Clean up temporary directories created during testing."""
for temp_dir in self.temp_dirs:
if temp_dir.exists():
shutil.rmtree(temp_dir, ignore_errors=True)
self.temp_dirs.clear()
def create_test_environment(self, name: str) -> Path:
"""Create isolated test environment."""
temp_dir = Path(tempfile.mkdtemp(prefix=f"qa_test_{name}_"))
self.temp_dirs.append(temp_dir)
return temp_dir
def run_test(self, test_func, name: str) -> QATestResult:
"""Run individual test with timing and error handling."""
self.log(f"Running test: {name}")
start_time = time.time()
try:
passed, details = test_func()
duration = time.time() - start_time
result = QATestResult(name, passed, details, duration)
if passed:
self.log(f"✅ {name} - PASSED ({duration:.3f}s)")
else:
self.log(f"❌ {name} - FAILED ({duration:.3f}s): {details}")
self.failed_tests.append(name)
except Exception as e:
duration = time.time() - start_time
details = f"Exception: {str(e)}"
result = QATestResult(name, False, details, duration)
self.log(f"💥 {name} - ERROR ({duration:.3f}s): {details}")
self.failed_tests.append(name)
self.test_results.append(result)
return result
# ========== UNIT TESTS ==========
def test_timeline_basic_creation(self) -> Tuple[bool, str]:
"""Test basic PROJECT_STATUS.md creation."""
from stop import update_project_status_timeline
test_dir = self.create_test_environment("basic_creation")
test_summary = "**Session Summary**: Basic test session. Tools: Read, Write."
result = update_project_status_timeline(str(test_dir), test_summary)
if not result:
return False, "Timeline update returned False"
status_file = test_dir / "PROJECT_STATUS.md"
if not status_file.exists():
return False, "PROJECT_STATUS.md file not created"
content = status_file.read_text()
if test_summary not in content:
return False, "Test summary not found in file content"
return True, f"File created with {len(content)} characters"
def test_timeline_append_functionality(self) -> Tuple[bool, str]:
"""Test appending to existing PROJECT_STATUS.md."""
from stop import update_project_status_timeline
test_dir = self.create_test_environment("append_test")
# Create initial file
first_summary = "**Session Summary**: First session. Tools: Read."
result1 = update_project_status_timeline(str(test_dir), first_summary)
# Append second entry
second_summary = "**Session Summary**: Second session. Tools: Write."
result2 = update_project_status_timeline(str(test_dir), second_summary)
if not (result1 and result2):
return False, "One or both timeline updates failed"
status_file = test_dir / "PROJECT_STATUS.md"
content = status_file.read_text()
entry_count = content.count("## Session Export -")
if entry_count < 2:
return False, f"Expected 2+ entries, found {entry_count}"
if first_summary not in content or second_summary not in content:
return False, "One or both summaries missing from content"
return True, f"Successfully appended, {entry_count} total entries"
def test_error_handling_invalid_directory(self) -> Tuple[bool, str]:
"""Test error handling with invalid directory."""
from stop import update_project_status_timeline
invalid_path = "/nonexistent/invalid/path/that/should/not/exist"
test_summary = "**Session Summary**: Error test."
result = update_project_status_timeline(invalid_path, test_summary)
if result:
return False, "Should have failed with invalid directory"
return True, "Correctly handled invalid directory"
def test_session_analysis_empty(self) -> Tuple[bool, str]:
"""Test session analysis with no data."""
from stop import analyze_session_activity, generate_summary
# Use non-existent session ID
analysis = analyze_session_activity("nonexistent_session_id")
summary = generate_summary(analysis)
if not summary:
return False, "Summary generation failed for empty session"
expected_tools = set()
if analysis["tools_used"] != expected_tools:
return False, f"Expected empty tools, got {analysis['tools_used']}"
return True, f"Generated summary: '{summary}'"
def test_summary_generation_patterns(self) -> Tuple[bool, str]:
"""Test different summary generation patterns."""
from stop import generate_summary
test_cases = [
{
"name": "UI Component Work",
"analysis": {
"tools_used": {"Magic", "Write"},
"files_modified": {"Button.tsx", "Form.vue"},
"commands_run": [],
"last_prompt": "create a button component",
"key_actions": [],
"test_results": None,
"errors_encountered": False
},
"expected_contains": ["component", "UI"]
},
{
"name": "Documentation Work",
"analysis": {
"tools_used": {"Write", "Edit"},
"files_modified": {"README.md", "api.md"},
"commands_run": [],
"last_prompt": "update the documentation",
"key_actions": [],
"test_results": None,
"errors_encountered": False
},
"expected_contains": ["doc"]
},
{
"name": "Hook Development",
"analysis": {
"tools_used": {"Edit", "Bash"},
"files_modified": {"hooks/stop.py"},
"commands_run": ["python test.py"],
"last_prompt": "fix the stop hook",
"key_actions": [],
"test_results": None,
"errors_encountered": False
},
"expected_contains": ["hook"]
}
]
for case in test_cases:
summary = generate_summary(case["analysis"])
summary_lower = summary.lower()
match_found = any(expected in summary_lower for expected in case["expected_contains"])
if not match_found:
return False, f"{case['name']}: Expected one of {case['expected_contains']} in '{summary}'"
return True, "All summary patterns generated correctly"
# ========== INTEGRATION TESTS ==========
def test_full_hook_simulation(self) -> Tuple[bool, str]:
"""Test full hook execution simulation."""
test_dir = self.create_test_environment("full_hook")
# Create mock session data
session_data = {
"session_id": "test_session_full_hook",
"working_dir": str(test_dir)
}
# Create mock session logs
session_log_dir = Path.home() / ".claude" / "sessions" / session_data["session_id"]
session_log_dir.mkdir(parents=True, exist_ok=True)
try:
# Create mock pre_tool_use.json
mock_events = [
{
"tool_name": "Edit",
"tool_input": {"file_path": str(test_dir / "test.py")},
"timestamp": "2024-01-01T12:00:00"
},
{
"tool_name": "Bash",
"tool_input": {"command": "python test.py"},
"timestamp": "2024-01-01T12:01:00"
}
]
with open(session_log_dir / "pre_tool_use.json", 'w') as f:
json.dump(mock_events, f)
# Create mock user_prompt_submit.json
mock_prompts = [
{
"prompt": "Please help me test the session hook functionality",
"timestamp": "2024-01-01T12:00:00"
}
]
with open(session_log_dir / "user_prompt_submit.json", 'w') as f:
json.dump(mock_prompts, f)
# Test the analysis and timeline update
from stop import analyze_session_activity, generate_summary, update_project_status_timeline
analysis = analyze_session_activity(session_data["session_id"])
summary = generate_summary(analysis)
detailed_summary = f"**Session Summary**: {summary}. Tools used: {', '.join(list(analysis['tools_used']))}."
result = update_project_status_timeline(str(test_dir), detailed_summary)
if not result:
return False, "Timeline update failed"
# Verify the result
status_file = test_dir / "PROJECT_STATUS.md"
if not status_file.exists():
return False, "PROJECT_STATUS.md not created"
content = status_file.read_text()
if "Edit" not in content or "Bash" not in content:
return False, "Expected tools not found in timeline"
return True, f"Full hook simulation successful, summary: '{summary}'"
finally:
# Cleanup mock session logs
if session_log_dir.exists():
shutil.rmtree(session_log_dir, ignore_errors=True)
def test_performance_large_session(self) -> Tuple[bool, str]:
"""Test performance with large session data."""
test_dir = self.create_test_environment("performance")
from stop import update_project_status_timeline
# Create large summary (simulating complex session)
large_summary = "**Session Summary**: " + "Large session test. " * 100
large_summary += f"Tools used: {', '.join([f'Tool{i}' for i in range(50)])}. "
large_summary += f"Files: {', '.join([f'file{i}.py' for i in range(20)])}."
start_time = time.time()
result = update_project_status_timeline(str(test_dir), large_summary)
duration = time.time() - start_time
if not result:
return False, "Timeline update failed for large session"
# Performance threshold: should complete within 100ms for large sessions
if duration > 0.1:
return False, f"Performance too slow: {duration:.3f}s (threshold: 0.1s)"
return True, f"Large session processed in {duration:.3f}s"
# ========== EDGE CASE TESTS ==========
def test_special_characters_in_paths(self) -> Tuple[bool, str]:
"""Test handling of special characters in file paths."""
from stop import update_project_status_timeline
# Create directory with special characters
test_dir = self.create_test_environment("special_chars")
special_subdir = test_dir / "test with spaces & símböls"
special_subdir.mkdir()
test_summary = "**Session Summary**: Testing special characters. Files: file with spaces.txt, file_with_üñíčødé.js"
result = update_project_status_timeline(str(special_subdir), test_summary)
if not result:
return False, "Failed to handle special characters in directory path"
status_file = special_subdir / "PROJECT_STATUS.md"
if not status_file.exists():
return False, "PROJECT_STATUS.md not created in special character directory"
# Verify content can be read back
try:
content = status_file.read_text(encoding='utf-8')
if test_summary not in content:
return False, "Special character content not preserved"
except UnicodeDecodeError:
return False, "Unicode decode error when reading back content"
return True, "Special characters handled correctly"
def test_concurrent_timeline_updates(self) -> Tuple[bool, str]:
"""Test concurrent timeline updates (simulated)."""
from stop import update_project_status_timeline
test_dir = self.create_test_environment("concurrent")
# Simulate rapid sequential updates (threading would be more complex)
summaries = [
"**Session Summary**: Concurrent test 1. Tools: Read.",
"**Session Summary**: Concurrent test 2. Tools: Write.",
"**Session Summary**: Concurrent test 3. Tools: Edit.",
]
results = []
for i, summary in enumerate(summaries):
result = update_project_status_timeline(str(test_dir), summary)
results.append(result)
time.sleep(0.01) # Small delay to simulate timing
if not all(results):
return False, f"Not all updates succeeded: {results}"
# Verify all entries are present
status_file = test_dir / "PROJECT_STATUS.md"
content = status_file.read_text()
entry_count = content.count("## Session Export -")
if entry_count < 3:
return False, f"Expected 3 entries, found {entry_count}"
# Check all summaries are present
for summary in summaries:
if summary not in content:
return False, f"Summary missing: {summary[:50]}..."
return True, f"All {len(summaries)} concurrent updates successful"
def test_malformed_session_data(self) -> Tuple[bool, str]:
"""Test handling of malformed session data."""
from stop import analyze_session_activity
# Create session with malformed JSON
session_id = "malformed_test_session"
session_log_dir = Path.home() / ".claude" / "sessions" / session_id
session_log_dir.mkdir(parents=True, exist_ok=True)
try:
# Create malformed JSON file
with open(session_log_dir / "pre_tool_use.json", 'w') as f:
f.write('{"invalid": json data without closing brace')
# Should not crash
analysis = analyze_session_activity(session_id)
# Should return empty/default analysis
if analysis["tools_used"] or analysis["files_modified"]:
return False, "Should have returned empty analysis for malformed data"
return True, "Malformed session data handled gracefully"
finally:
if session_log_dir.exists():
shutil.rmtree(session_log_dir, ignore_errors=True)
# ========== TEST ORCHESTRATION ==========
def run_unit_tests(self) -> int:
"""Run all unit tests."""
self.log("🧪 Running Unit Tests", "PHASE")
unit_tests = [
(self.test_timeline_basic_creation, "Timeline Basic Creation"),
(self.test_timeline_append_functionality, "Timeline Append Functionality"),
(self.test_error_handling_invalid_directory, "Error Handling Invalid Directory"),
(self.test_session_analysis_empty, "Session Analysis Empty"),
(self.test_summary_generation_patterns, "Summary Generation Patterns"),
]
passed = 0
for test_func, name in unit_tests:
result = self.run_test(test_func, f"Unit: {name}")
if result.passed:
passed += 1
self.log(f"Unit Tests: {passed}/{len(unit_tests)} passed")
return passed
def run_integration_tests(self) -> int:
"""Run all integration tests."""
self.log("🔗 Running Integration Tests", "PHASE")
integration_tests = [
(self.test_full_hook_simulation, "Full Hook Simulation"),
(self.test_performance_large_session, "Performance Large Session"),
]
passed = 0
for test_func, name in integration_tests:
result = self.run_test(test_func, f"Integration: {name}")
if result.passed:
passed += 1
self.log(f"Integration Tests: {passed}/{len(integration_tests)} passed")
return passed
def run_edge_case_tests(self) -> int:
"""Run all edge case tests."""
self.log("⚠️ Running Edge Case Tests", "PHASE")
edge_tests = [
(self.test_special_characters_in_paths, "Special Characters in Paths"),
(self.test_concurrent_timeline_updates, "Concurrent Timeline Updates"),
(self.test_malformed_session_data, "Malformed Session Data"),
]
passed = 0
for test_func, name in edge_tests:
result = self.run_test(test_func, f"Edge Case: {name}")
if result.passed:
passed += 1
self.log(f"Edge Case Tests: {passed}/{len(edge_tests)} passed")
return passed
def generate_report(self) -> bool:
"""Generate comprehensive test report."""
total_tests = len(self.test_results)
passed_tests = sum(1 for r in self.test_results if r.passed)
total_duration = sum(r.duration for r in self.test_results)
print("\n" + "=" * 80)
print("📊 SESSION STOP HOOK QA TEST REPORT")
print("=" * 80)
print(f"Test Execution Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"Total Tests Run: {total_tests}")
print(f"Tests Passed: {passed_tests}")
print(f"Tests Failed: {len(self.failed_tests)}")
print(f"Success Rate: {(passed_tests/total_tests)*100:.1f}%")
print(f"Total Duration: {total_duration:.3f}s")
print(f"Average Test Duration: {total_duration/total_tests:.3f}s")
if self.failed_tests:
print(f"\n❌ FAILED TESTS ({len(self.failed_tests)}):")
for test_name in self.failed_tests:
failed_result = next((r for r in self.test_results if r.name == test_name), None)
if failed_result:
print(f" • {test_name}: {failed_result.details}")
print(f"\n📈 PERFORMANCE SUMMARY:")
slowest_tests = sorted(self.test_results, key=lambda r: r.duration, reverse=True)[:5]
for i, test in enumerate(slowest_tests, 1):
status = "✅" if test.passed else "❌"
print(f" {i}. {status} {test.name}: {test.duration:.3f}s")
success = len(self.failed_tests) == 0
if success:
print(f"\n🎉 ALL TESTS PASSED! Session stop hook is ready for production.")
print(f"✅ Timeline functionality is working correctly")
print(f"✅ Error handling is robust")
print(f"✅ Performance is within acceptable limits")
else:
print(f"\n⚠️ SOME TESTS FAILED - Review required before production deployment")
print(f"📋 Review failed tests and fix issues")
print(f"🔄 Re-run tests after fixes")
print("=" * 80)
return success
def run_all_tests(self) -> bool:
"""Run comprehensive test suite."""
print("🚀 Starting Session Stop Hook Comprehensive QA Testing")
print("=" * 80)
try:
# Run test phases
unit_passed = self.run_unit_tests()
integration_passed = self.run_integration_tests()
edge_passed = self.run_edge_case_tests()
# Generate final report
return self.generate_report()
finally:
# Always cleanup
self.cleanup_temp_dirs()
def main():
"""Main entry point for QA testing."""
import argparse
parser = argparse.ArgumentParser(description="Session Stop Hook QA Test Runner")
parser.add_argument("--verbose", "-v", action="store_true",
help="Enable verbose output")
parser.add_argument("--quick", "-q", action="store_true",
help="Run only essential tests (faster)")
args = parser.parse_args()
runner = StopHookQARunner(verbose=args.verbose)
if args.quick:
# Quick test mode - run only critical tests
print("🏃 Running Quick Test Mode")
runner.run_test(runner.test_timeline_basic_creation, "Quick: Basic Creation")
runner.run_test(runner.test_timeline_append_functionality, "Quick: Append Functionality")
runner.run_test(runner.test_full_hook_simulation, "Quick: Full Simulation")
success = runner.generate_report()
else:
# Full test suite
success = runner.run_all_tests()
return 0 if success else 1
if __name__ == "__main__":
sys.exit(main())