*** Settings ***
Documentation    Integration tests for Plugin Architecture Framework
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment

*** Variables ***
${HELPER}    ${CURDIR}/helper_plugin_architecture.py

*** Test Cases ***
PluginLoader Loads Valid Class
    [Documentation]    Verify PluginLoader can import a class from module:ClassName
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    load-class    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plugin-load-class-ok

PluginLoader Rejects Disallowed Module Prefix
    [Documentation]    Verify PluginLoader security prefix allowlist
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    prefix-security    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plugin-prefix-security-ok

PluginLoader Protocol Validation Pass
    [Documentation]    Verify protocol validation succeeds for conforming class
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    protocol-pass    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plugin-protocol-pass-ok

PluginLoader Protocol Validation Fail
    [Documentation]    Verify protocol validation fails for non-conforming class
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    protocol-fail    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plugin-protocol-fail-ok

PluginManager Lifecycle
    [Documentation]    Verify register -> activate -> deactivate lifecycle
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    lifecycle    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plugin-lifecycle-ok

PluginManager Config-Driven Registration
    [Documentation]    Verify config-driven plugin registration
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    config-registration    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plugin-config-registration-ok

PluginManager Error Handling
    [Documentation]    Verify error transitions and exception types
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    error-handling    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plugin-error-handling-ok

DI Container PluginManager Resolution
    [Documentation]    Verify DI container resolves PluginManager singleton
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    di-resolution    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plugin-di-resolution-ok

PluginManager Thread Safety
    [Documentation]    Verify concurrent plugin registration is thread-safe
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    thread-safety    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plugin-thread-safety-ok

PluginManager Extension Points
    [Documentation]    Verify extension point registration and listing
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    extension-points    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plugin-extension-points-ok

PluginLoader Entry Point Security - Disallowed Prefix Not Loaded
    [Documentation]    Security regression test for issue #7476.
    ...
    ...                Verifies that load_from_entry_points does NOT call ep.load()
    ...                for entry points whose module prefix is not in the allowlist.
    ...                This ensures arbitrary code execution is prevented even when
    ...                a malicious package registers an entry point.
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    entry-point-security    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plugin-entry-point-security-ok
