*** Settings ***
Documentation    Prompt Injection Mitigation — integration smoke tests
Library          OperatingSystem
Library          Process
Resource         ${CURDIR}/common.resource

Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment

*** Variables ***
${HELPER_SCRIPT}    robot/helper_prompt_injection_mitigation.py
${SRC_DIR}          ${CURDIR}/../src

*** Test Cases ***
Sanitizer Rejects Injection Pattern
    [Tags]    security  prompt-injection
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    test-injection-rejection
    ...    cwd=${WORKSPACE}    env:PYTHONPATH=${SRC_DIR}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    PASS

Sanitizer Escapes HTML Entities
    [Tags]    security  prompt-injection
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    test-html-escape
    ...    cwd=${WORKSPACE}    env:PYTHONPATH=${SRC_DIR}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    PASS

Sanitizer Strips Control Characters
    [Tags]    security  prompt-injection
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    test-control-chars
    ...    cwd=${WORKSPACE}    env:PYTHONPATH=${SRC_DIR}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    PASS

Boundary Markers Wrap User Content
    [Tags]    security  prompt-injection
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    test-boundary-markers
    ...    cwd=${WORKSPACE}    env:PYTHONPATH=${SRC_DIR}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    PASS

System Prompt Augmentation
    [Tags]    security  prompt-injection
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    test-system-augment
    ...    cwd=${WORKSPACE}    env:PYTHONPATH=${SRC_DIR}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    PASS

Sanitize And Wrap Convenience
    [Tags]    security  prompt-injection
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    test-sanitize-and-wrap
    ...    cwd=${WORKSPACE}    env:PYTHONPATH=${SRC_DIR}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    PASS

Schema Validation Passes Valid Input
    [Tags]    security  prompt-injection
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    test-schema-valid
    ...    cwd=${WORKSPACE}    env:PYTHONPATH=${SRC_DIR}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    PASS

Schema Validation Rejects Invalid Input
    [Tags]    security  prompt-injection
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    test-schema-invalid
    ...    cwd=${WORKSPACE}    env:PYTHONPATH=${SRC_DIR}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    PASS

Capability Enforcement Blocks Write In ReadOnly
    [Tags]    security  prompt-injection
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    test-capability-readonly
    ...    cwd=${WORKSPACE}    env:PYTHONPATH=${SRC_DIR}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    PASS

Unsafe Tool Gating
    [Tags]    security  prompt-injection
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    test-unsafe-gating
    ...    cwd=${WORKSPACE}    env:PYTHONPATH=${SRC_DIR}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    PASS
