*** Settings ***
Resource          ${CURDIR}/common.resource
Library           OperatingSystem
Library           Process

*** Variables ***
${PYTHON}         python
${SRC_DIR}        ${CURDIR}/..

*** Test Cases ***
Cost Entry Creation And Estimation
    [Documentation]    Verify CostEntry creation and cost estimation
    [Tags]    cost_controls    provider    model
    ${result}=    Run Process    ${PYTHON}    robot/helper_cost_controls.py    cost-entry
    ...    cwd=${SRC_DIR}
    Log Process Failure    ${result}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    cost-entry-ok

Provider Cost Table Lookup
    [Documentation]    Verify ProviderCostTable model lookup
    [Tags]    cost_controls    provider    model
    ${result}=    Run Process    ${PYTHON}    robot/helper_cost_controls.py    cost-table
    ...    cwd=${SRC_DIR}
    Log Process Failure    ${result}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    cost-table-ok

Cost Tracker Budget Enforcement
    [Documentation]    Verify CostTracker budget enforcement
    [Tags]    cost_controls    provider    budget
    ${result}=    Run Process    ${PYTHON}    robot/helper_cost_controls.py    cost-tracker
    ...    cwd=${SRC_DIR}
    Log Process Failure    ${result}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    cost-tracker-budget-ok

Fallback Selector No Providers
    [Documentation]    Verify FallbackSelector with no configured providers
    [Tags]    cost_controls    provider    fallback
    ${result}=    Run Process    ${PYTHON}    robot/helper_cost_controls.py    fallback-selector
    ...    cwd=${SRC_DIR}
    Log Process Failure    ${result}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    fallback-selector-ok

Settings Cost Control Defaults
    [Documentation]    Verify Settings cost control config defaults
    [Tags]    cost_controls    config    settings
    ${result}=    Run Process    ${PYTHON}    robot/helper_cost_controls.py    settings-defaults
    ...    cwd=${SRC_DIR}
    Log Process Failure    ${result}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    settings-defaults-ok

Cost Metadata Display Dict
    [Documentation]    Verify CostMetadata display dictionary
    [Tags]    cost_controls    provider    model
    ${result}=    Run Process    ${PYTHON}    robot/helper_cost_controls.py    cost-metadata
    ...    cwd=${SRC_DIR}
    Log Process Failure    ${result}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    cost-metadata-display-ok

Cost Tracker Concurrent Record Usage Is Thread Safe
    [Documentation]    Verify CostTracker.record_usage is thread-safe: concurrent calls must not lose cost increments
    [Tags]    cost_controls    provider    concurrency
    ${result}=    Run Process    ${PYTHON}    robot/helper_cost_controls.py    cost-tracker-concurrent
    ...    cwd=${SRC_DIR}
    Log Process Failure    ${result}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    cost-tracker-concurrent-ok

*** Keywords ***
Log Process Failure
    [Arguments]    ${result}
    Run Keyword If    ${result.rc} == 0    Return From Keyword
    Log To Console    Process failed with rc=${result.rc}
    Log To Console    STDOUT:${\n}${result.stdout}
    Log To Console    STDERR:${\n}${result.stderr}
