scripts-test-stack-detector
#!/usr/bin/env python3 """
title: "Import by executing the module" component_type: script version: "1.0.0" audience: contributor status: stable summary: "Test script for stack-detector.py" keywords: ['api', 'database', 'detector', 'generation', 'git'] tokens: ~500 created: 2025-12-22 updated: 2025-12-22 script_name: "test-stack-detector.py" language: python executable: true usage: "python3 scripts/test-stack-detector.py [options]" python_version: "3.10+" dependencies: [] modifies_files: false network_access: false requires_auth: false
Test script for stack-detector.py"""
import sys from pathlib import Path
Import by executing the module
with open(Path(file).parent / "stack-detector.py") as f: code = f.read()
namespace = {} exec(code, namespace) CoditectStackDetector = namespace['CoditectStackDetector'] TechnologyStack = namespace['TechnologyStack']
Test programmatic API
print("=" * 70) print("CODITECT Stack Detector - Programmatic API Test") print("=" * 70)
repo_root = Path.cwd() config_path = repo_root / "config" / "stack-commands.json"
detector = CoditectStackDetector(repo_root, config_path)
Test 1: Stack Detection
print("\n[Test 1] Stack Detection") stack = detector.detect_all() print(f"✓ Languages: {sorted(stack.languages)}") print(f"✓ Frameworks: {sorted(stack.frameworks)}") print(f"✓ Databases: {sorted(stack.databases)}") print(f"✓ Cloud: {sorted(stack.cloud_providers)}") print(f"✓ Build Tools: {sorted(stack.build_tools)}") print(f"✓ Version Control: {sorted(stack.version_control)}")
Test 2: Command Generation
print("\n[Test 2] Command Allowlist Generation") allowed_commands = detector.generate_allowed_commands(stack) print(f"✓ Generated {len(allowed_commands)} allowed commands") print(f" Sample: {sorted(list(allowed_commands))[:10]}")
Test 3: Pattern Generation
print("\n[Test 3] Command Pattern Generation") patterns = detector.generate_command_patterns(stack) print(f"✓ Generated {len(patterns)} command patterns") if patterns: print(f" Patterns: {sorted(patterns)}")
Test 4: Profile Generation
print("\n[Test 4] Security Profile Generation") profile = detector.generate_profile(force_refresh=False) print(f"✓ Profile version: {profile['version']}") print(f"✓ Created: {profile['created_at']}") print(f"✓ Expires: {profile['expires_at']}") print(f"✓ Project hash: {profile['project_hash'][:16]}...")
Test 5: Command Permission Checking
print("\n[Test 5] Command Permission Checking") test_cases = [ ("pytest", True), ("python3 manage.py migrate", True), ("npm run build", True), ("git status", True), ("shutdown -h now", False), ("dd if=/dev/zero", False), ("unknown-command", False), ]
passed = 0 failed = 0 for cmd, expected_allowed in test_cases: allowed = detector.is_command_allowed(cmd) status = "✓" if allowed == expected_allowed else "✗" result = "ALLOWED" if allowed else "BLOCKED" expected = "ALLOWED" if expected_allowed else "BLOCKED"
if allowed == expected_allowed:
passed += 1
print(f" {status} {cmd:30} → {result} (expected {expected})")
else:
failed += 1
print(f" {status} {cmd:30} → {result} (expected {expected}) FAIL!")
Test 6: Project Hash
print("\n[Test 6] Project Hash Computation") hash1 = detector.compute_project_hash() print(f"✓ Initial hash: {hash1[:16]}...") hash2 = detector.compute_project_hash() print(f"✓ Recomputed hash: {hash2[:16]}...") if hash1 == hash2: print("✓ Hashes are deterministic") else: print("✗ Hash computation is non-deterministic - FAIL!") failed += 1
Summary
print("\n" + "=" * 70) print("Test Summary") print("=" * 70) print(f"Passed: {passed}") print(f"Failed: {failed}")
if failed == 0: print("\n✅ All tests passed!") sys.exit(0) else: print(f"\n❌ {failed} tests failed!") sys.exit(1)