"""
tests/test_agents.py - Tests for agent profiles
"""

import sys
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(PROJECT_ROOT))

from agent_profiles import (
    get_profile,
    list_profiles,
    validate_action,
    FilesystemAccess,
    NetworkAccess
)


def test_profiles_exist():
    """Test that all profiles exist"""
    print("\n=== Test: profiles exist ===")
    profiles = ['coder', 'researcher', 'memory-manager', 'security-lab']
    
    for name in profiles:
        profile = get_profile(name)
        if profile:
            print(f"   ✓ {name} exists")
        else:
            print(f"   ✗ {name} missing")
            return False
    
    return True


def test_profile_properties():
    """Test profile properties"""
    print("\n=== Test: profile properties ===")
    coder = get_profile('coder')
    
    checks = [
        (coder.name == 'coder', 'name'),
        (len(coder.allowed_tools) > 0, 'allowed_tools'),
        (coder.filesystem_access == FilesystemAccess.WORKSPACE, 'filesystem'),
        (coder.network_access == NetworkAccess.PROXY, 'network'),
        (coder.approval_required == True, 'approval'),
    ]
    
    for check, name in checks:
        if check:
            print(f"   ✓ {name} correct")
        else:
            print(f"   ✗ {name} incorrect")
            return False
    
    return True


def test_validation():
    """Test action validation"""
    print("\n=== Test: action validation ===")
    coder = get_profile('coder')
    
    # Should be allowed
    allowed = validate_action(coder, 'read', 'workspace/file.py')
    if not allowed:
        print("   ✗ Coder should read workspace files")
        return False
    
    # Should not be allowed
    denied = validate_action(coder, 'delete', 'workspace/file.py')
    if denied:
        print("   ✗ Coder should not delete without approval")
        return False
    
    print("   ✓ Validation works correctly")
    return True


def test_list_profiles():
    """Test list_profiles function"""
    print("\n=== Test: list profiles ===")
    profiles = list_profiles()
    
    if len(profiles) == 4:
        print(f"   ✓ 4 profiles listed")
        return True
    else:
        print(f"   ✗ Expected 4 profiles, got {len(profiles)}")
        return False


if __name__ == '__main__':
    print("=" * 60)
    print("Agent Profiles Tests")
    print("=" * 60)
    
    tests = [
        test_profiles_exist,
        test_profile_properties,
        test_validation,
        test_list_profiles,
    ]
    
    results = []
    for test in tests:
        try:
            results.append(test())
        except Exception as e:
            print(f"ERROR: {e}")
            import traceback
            traceback.print_exc()
            results.append(False)
    
    print("\n" + "=" * 60)
    print(f"Results: {sum(results)}/{len(results)} passed")
    print("=" * 60)
    
    sys.exit(0 if all(results) else 1)
