*** Settings ***
Documentation     Integration tests for exception handling module
Resource          common.resource
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Test Cases ***
Classify Validation Error
    [Documentation]    Verify ValidationError classifies as 422
    ${result}=    Run Process    ${PYTHON}    ${WORKSPACE}/robot/helper_security_exceptions.py
    ...    classify    validation
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    code=422

Classify Database Error
    [Documentation]    Verify DatabaseError classifies as 522
    ${result}=    Run Process    ${PYTHON}    ${WORKSPACE}/robot/helper_security_exceptions.py
    ...    classify    database
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    code=522

Redact Secret Key
    [Documentation]    Verify api_key is redacted
    ${result}=    Run Process    ${PYTHON}    ${WORKSPACE}/robot/helper_security_exceptions.py
    ...    redact    api_key    sk-secret12345
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    ***REDACTED***

Wrap Bare Exception
    [Documentation]    Verify bare Exception is wrapped in CleverAgentsError
    ${result}=    Run Process    ${PYTHON}    ${WORKSPACE}/robot/helper_security_exceptions.py
    ...    wrap    unexpected-crash
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    is_ca=True

Redact Non-Secret Key
    [Documentation]    Verify non-secret values pass through
    ${result}=    Run Process    ${PYTHON}    ${WORKSPACE}/robot/helper_security_exceptions.py
    ...    redact    plan_id    01PLAN001
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    01PLAN001
    Should Not Contain    ${result.stdout}    REDACTED

Helper No Args
    [Documentation]    Verify helper exits with error when no args given
    ${result}=    Run Process    ${PYTHON}    ${WORKSPACE}/robot/helper_security_exceptions.py
    Should Be Equal As Integers    ${result.rc}    1
    Should Contain    ${result.stdout}    Usage

Helper Unknown Command
    [Documentation]    Verify helper exits with error for unknown command
    ${result}=    Run Process    ${PYTHON}    ${WORKSPACE}/robot/helper_security_exceptions.py
    ...    bogus
    Should Be Equal As Integers    ${result.rc}    1
    Should Contain    ${result.stdout}    Unknown command

Helper Classify Missing Type
    [Documentation]    Verify classify with no type arg prints usage
    ${result}=    Run Process    ${PYTHON}    ${WORKSPACE}/robot/helper_security_exceptions.py
    ...    classify
    Should Be Equal As Integers    ${result.rc}    1
    Should Contain    ${result.stdout}    Usage

Helper Redact Missing Value
    [Documentation]    Verify redact with only one arg prints usage
    ${result}=    Run Process    ${PYTHON}    ${WORKSPACE}/robot/helper_security_exceptions.py
    ...    redact    api_key
    Should Be Equal As Integers    ${result.rc}    1
    Should Contain    ${result.stdout}    Usage

Helper Wrap Missing Message
    [Documentation]    Verify wrap with no message arg prints usage
    ${result}=    Run Process    ${PYTHON}    ${WORKSPACE}/robot/helper_security_exceptions.py
    ...    wrap
    Should Be Equal As Integers    ${result.rc}    1
    Should Contain    ${result.stdout}    Usage
