*** Settings ***
Documentation       Resource type inheritance chain resolution and polymorphic type matching
Library             Process
Library             OperatingSystem
Library             Collections
Library             helper_resource_type_inheritance.py

*** Test Cases ***
Single Inheritance Chain Resolution
    [Documentation]    Resolve a two-level chain: devcontainer-instance -> container-instance -> resource
    ${chain}=    Resolve Inheritance Chain Keyword    devcontainer-instance
    Length Should Be    ${chain}    3
    Should Be Equal    ${chain}[0]    devcontainer-instance
    Should Be Equal    ${chain}[1]    container-instance
    Should Be Equal    ${chain}[2]    resource

Deep Chain Resolution
    [Documentation]    Resolve a four-level chain through the deep registry
    ${chain}=    Resolve Deep Chain    level-4
    Length Should Be    ${chain}    5
    Should Be Equal    ${chain}[0]    level-4
    Should Be Equal    ${chain}[4]    root

Root Type Returns Single Element Chain
    [Documentation]    A root type with no parent returns a one-element chain
    ${chain}=    Resolve Inheritance Chain Keyword    resource
    Length Should Be    ${chain}    1
    Should Be Equal    ${chain}[0]    resource

Polymorphic Subtype Check Positive
    [Documentation]    devcontainer-instance is a subtype of resource (transitive)
    ${result}=    Check Is Subtype Of    devcontainer-instance    resource
    Should Be True    ${result}

Polymorphic Subtype Check Negative
    [Documentation]    resource is NOT a subtype of devcontainer-instance
    ${result}=    Check Is Subtype Of    resource    devcontainer-instance
    Should Not Be True    ${result}

Polymorphic Identity Check
    [Documentation]    A type is always a subtype of itself
    ${result}=    Check Is Subtype Of    container-instance    container-instance
    Should Be True    ${result}

Deep Polymorphic Subtype Check
    [Documentation]    level-4 is a subtype of root through four levels
    ${result}=    Check Is Subtype Deep    level-4    root
    Should Be True    ${result}

Field Merging Scalars Override
    [Documentation]    Scalar fields from child override parent values
    ${fields}=    Resolve Merged Fields    leaf
    Should Be Equal    ${fields}[sandbox_strategy]    none
    Should Be Equal    ${fields}[resource_kind]    virtual

Field Merging Collections Additive
    [Documentation]    Collection fields merge additively by default
    ${fields}=    Resolve Merged Fields    mid
    ${child_types}=    Get From Dictionary    ${fields}    child_types
    Should Contain    ${child_types}    alpha
    Should Contain    ${child_types}    beta

Field Merging CLI Args Same Name Replacement
    [Documentation]    cli_args with matching name from child replace parent
    ${fields}=    Resolve Merged Fields    leaf
    ${cli_args}=    Get From Dictionary    ${fields}    cli_args
    # "path" from leaf replaces "path" from base; "tag" from mid preserved
    ${names}=    Evaluate    [a["name"] for a in $cli_args]
    Should Contain    ${names}    path
    Should Contain    ${names}    tag

Find All Subtypes Of Ancestor
    [Documentation]    find_subtypes returns all direct children of a wide registry
    ${subtypes}=    Find All Subtypes    ancestor
    Length Should Be    ${subtypes}    20

Circular Inheritance Detected
    [Documentation]    Circular chain raises ResourceTypeCircularInheritanceError
    ${err}=    Detect Circular Inheritance
    Should Be Equal    ${err}    ResourceTypeCircularInheritanceError

Depth Limit Exceeded Detected
    [Documentation]    Chain exceeding MAX_CHAIN_DEPTH raises depth error
    ${err}=    Detect Depth Exceeded
    Should Be Equal    ${err}    ResourceTypeInheritanceDepthError

Max Chain Depth Constant
    [Documentation]    MAX_CHAIN_DEPTH is exposed and equals 5
    ${depth}=    Get Max Chain Depth
    Should Be Equal As Integers    ${depth}    5

Standalone Helper Chain Resolve
    [Documentation]    Run helper as standalone script: chain-resolve
    ${result}=    Run Process    ${PYTHON}    robot/helper_resource_type_inheritance.py    chain-resolve
    ...    env:PYTHONPATH=src
    Should Be Equal As Integers    ${result.rc}    0    helper failed: ${result.stderr}
    Should Contain    ${result.stdout}    chain-resolve-ok

Standalone Helper Subtype Check
    [Documentation]    Run helper as standalone script: subtype-check
    ${result}=    Run Process    ${PYTHON}    robot/helper_resource_type_inheritance.py    subtype-check
    ...    env:PYTHONPATH=src
    Should Be Equal As Integers    ${result.rc}    0    helper failed: ${result.stderr}
    Should Contain    ${result.stdout}    subtype-check-ok

Standalone Helper Field Merge
    [Documentation]    Run helper as standalone script: field-merge
    ${result}=    Run Process    ${PYTHON}    robot/helper_resource_type_inheritance.py    field-merge
    ...    env:PYTHONPATH=src
    Should Be Equal As Integers    ${result.rc}    0    helper failed: ${result.stderr}
    Should Contain    ${result.stdout}    field-merge-ok
