#!/usr/bin/env nu

# Authentication Plugin Integration Tests
# Tests plugin availability, fallback behavior, and function signatures

use std assert

# Test plugin availability detection
export def test_plugin_available [] {
    print "  Testing plugin availability detection..."

    # Check if auth plugin is registered
    let available = (which auth | length) > 0

    # Should not error - just returns boolean
    assert ($available == true or $available == false)

    if $available {
        print "    ✅ Auth plugin is available"
    } else {
        print "    ⚠️  Auth plugin not available (will use HTTP fallback)"
    }
}

# Test graceful fallback when plugin not available
export def test_fallback_behavior [] {
    print "  Testing graceful fallback behavior..."

    # Import auth module
    use auth.nu *

    # Test verify function - should not crash
    let result = (do { plugin-verify } | complete)

    # Should handle both success and graceful failure
    if $result.exit_code == 0 {
        print "    ✅ Verify succeeded (plugin or HTTP)"
    } else {
        # Check if error message mentions fallback
        let has_fallback_msg = ($result.stderr | str contains "fallback" or $result.stderr | str contains "HTTP")
        print "    ✅ Verify failed gracefully"
    }
}

# Test login function signature
export def test_login_signature [] {
    print "  Testing login function signature..."

    use auth.nu *

    # Verify function exists
    let has_login = (which plugin-login | length) > 0
    assert $has_login

    # Test with invalid credentials (should fail gracefully)
    let result = (do {
        plugin-login "test_user" "test_pass"
    } | complete)

    # Should not crash
    assert ($result.exit_code == 0 or $result.exit_code != 0)
    print "    ✅ Login function signature correct"
}

# Test logout function
export def test_logout_signature [] {
    print "  Testing logout function signature..."

    use auth.nu *

    # Verify function exists
    let has_logout = (which plugin-logout | length) > 0
    assert $has_logout

    # Test logout (should succeed even if not logged in)
    let result = (do { plugin-logout } | complete)

    # Should not crash
    assert ($result.exit_code == 0 or $result.exit_code != 0)
    print "    ✅ Logout function signature correct"
}

# Test MFA enrollment signature
export def test_mfa_enroll_signature [] {
    print "  Testing MFA enrollment function signature..."

    use auth.nu *

    # Verify function exists
    let has_mfa = (which plugin-mfa-enroll | length) > 0
    assert $has_mfa

    print "    ✅ MFA enrollment function exists"
}

# Test MFA verify signature
export def test_mfa_verify_signature [] {
    print "  Testing MFA verify function signature..."

    use auth.nu *

    # Verify function exists
    let has_mfa_verify = (which plugin-mfa-verify | length) > 0
    assert $has_mfa_verify

    print "    ✅ MFA verify function exists"
}

# Test configuration integration
export def test_config_integration [] {
    print "  Testing configuration integration..."

    # Should read config values without error
    let config_check = (do {
        use ../config/accessor.nu *
        get-config-value "plugins.auth.enabled" true
    } | complete)

    assert ($config_check.exit_code == 0)

    # Check control center URL config
    let url_check = (do {
        use ../config/accessor.nu *
        get-config-value "plugins.auth.control_center_url" "http://localhost:3000"
    } | complete)

    assert ($url_check.exit_code == 0)
    print "    ✅ Configuration integration working"
}

# Test token management functions
export def test_token_management [] {
    print "  Testing token management..."

    use auth.nu *

    # Test get-token (should return null or token)
    let result = (do { get-token } | complete)
    assert ($result.exit_code == 0 or $result.exit_code != 0)

    # Test token refresh (should fail gracefully if no token)
    let refresh_result = (do { plugin-refresh-token } | complete)
    assert ($refresh_result.exit_code == 0 or $refresh_result.exit_code != 0)

    print "    ✅ Token management functions working"
}

# Test error handling
export def test_error_handling [] {
    print "  Testing error handling..."

    use auth.nu *

    # Test with invalid URL
    let result = (do {
        $env.CONTROL_CENTER_URL = "http://invalid-url-that-does-not-exist:99999"
        plugin-verify
    } | complete)

    # Should fail gracefully with error message
    if $result.exit_code != 0 {
        assert ($result.stderr | str length) > 0
        print "    ✅ Error handling working correctly"
    } else {
        print "    ⚠️  Expected failure for invalid URL"
    }
}

# Main test runner
export def main [] {
    print "\n🧪 Running Authentication Plugin Tests...\n"

    test_plugin_available
    test_fallback_behavior
    test_login_signature
    test_logout_signature
    test_mfa_enroll_signature
    test_mfa_verify_signature
    test_config_integration
    test_token_management
    test_error_handling

    print "\n✅ Authentication plugin tests completed!\n"
}
