*** Settings ***
Documentation    End-to-end tests for Config Service with multi-level resolution
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment

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

*** Test Cases ***
Config Service Registers All Spec Keys
    [Documentation]    Verify that the registry contains exactly 105 keys (102 spec + 1 skills + 2 server stubs)
    [Tags]    tdd_issue    tdd_issue_4304    tdd_expected_fail

    ${result}=    Run Process    ${PYTHON}    ${HELPER}    registry-key-count    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    config-registry-key-count-ok

Config Resolution Returns Default
    [Documentation]    Verify that resolving a key with no overrides returns the default
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    resolve-default    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    config-resolution-default-ok

Config Resolution With Global Config
    [Documentation]    Verify that a value written to the TOML file overrides the default
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    resolve-global    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    config-resolution-global-ok

Config Resolution With Env Var
    [Documentation]    Verify that an env var overrides the global config value
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    resolve-env    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    config-resolution-env-ok

Config Resolution With CLI Override
    [Documentation]    Verify that cli_value has the highest priority in the chain
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    resolve-cli    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    config-resolution-cli-ok

Config Resolution Project Scope
    [Documentation]    Verify that project-scoped config is resolved via project_name
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    resolve-project    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    config-resolution-project-ok

Config Resolution Verbose Chain
    [Documentation]    Verify that verbose=True returns all 6 chain entries
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    resolve-verbose-chain    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    config-resolution-verbose-chain-ok

Config Env Var For Provider Keys
    [Documentation]    Verify provider.openai.api-key maps to OPENAI_API_KEY
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    env-var-provider    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    config-env-var-provider-ok

Config Validate Type Coercion
    [Documentation]    Verify str to int, float, and bool coercion via validate_type
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    type-coercion    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    config-type-coercion-ok

Config List Command Integration
    [Documentation]    Verify that ``agents config list`` outputs all registered keys
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    cli-list-integration    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    config-cli-list-integration-ok
