*** Settings ***
Documentation       Deferred Physical Resource Type Integration Tests (#330)
Library             Process
Library             OperatingSystem

*** Variables ***
${EXAMPLES_DIR}     ${CURDIR}/../examples/resource-types

*** Test Cases ***
Load All Git Taxonomy YAMLs And Validate
    [Documentation]    Load all git object taxonomy YAMLs and validate key fields
    ${script}=    Catenate    SEPARATOR=\n
    ...    import yaml
    ...    from cleveragents.domain.models.core.resource_type import ResourceTypeSpec
    ...    types = [
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}("git", True, "none", ["git-checkout"]),
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}("git-remote", False, "none", ["git"]),
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}("git-branch", False, "none", ["git"]),
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}("git-tag", False, "none", ["git"]),
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}("git-commit", False, "none", ["git-branch", "git-tag", "git"]),
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}("git-tree", False, "none", ["git-commit", "git-tree"]),
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}("git-tree-entry", False, "none", ["git-tree"]),
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}("git-stash", False, "none", ["git"]),
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}("git-submodule", False, "none", ["git"]),
    ...    ]
    ...    for name, addable, sandbox, parents in types:
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}path = f"${EXAMPLES_DIR}/{name}.yaml"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}with open(path) as f:
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}config = yaml.safe_load(f)
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}spec = ResourceTypeSpec.from_config(config)
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert spec.name == name, f"name: {spec.name}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert spec.resource_kind.value == "physical", f"kind: {spec.resource_kind}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert spec.sandbox_strategy.value == sandbox, f"sandbox: {spec.sandbox_strategy}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert spec.user_addable == addable, f"addable: {spec.user_addable}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert spec.built_in is True, f"built_in: {spec.built_in}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}for p in parents:
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}assert p in spec.parent_types, f"{name}: missing parent {p}"
    ...    print("All git taxonomy YAMLs validated successfully")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    timeout=60s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    Git taxonomy load failed: ${result.stderr}
    Should Contain    ${result.stdout}    All git taxonomy YAMLs validated successfully

Load Filesystem Link YAMLs And Validate
    [Documentation]    Load fs-symlink and fs-hardlink YAMLs and validate
    ${script}=    Catenate    SEPARATOR=\n
    ...    import yaml
    ...    from cleveragents.domain.models.core.resource_type import ResourceTypeSpec
    ...    for name in ["fs-symlink", "fs-hardlink"]:
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}path = f"${EXAMPLES_DIR}/{name}.yaml"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}with open(path) as f:
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}config = yaml.safe_load(f)
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}spec = ResourceTypeSpec.from_config(config)
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert spec.name == name, f"name: {spec.name}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert spec.resource_kind.value == "physical"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert spec.sandbox_strategy.value == "copy_on_write"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert spec.user_addable is False
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert spec.built_in is True
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert "fs-directory" in spec.parent_types
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert len(spec.child_types) == 0
    ...    print("Filesystem link YAMLs validated successfully")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    timeout=60s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    FS link load failed: ${result.stderr}
    Should Contain    ${result.stdout}    Filesystem link YAMLs validated successfully

Assert Git Taxonomy In BUILTIN_NAMES
    [Documentation]    Verify all deferred physical types are in BUILTIN_NAMES
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.domain.models.core.resource_type import ResourceTypeSpec
    ...    expected = [
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"git", "git-remote", "git-branch", "git-tag",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"git-commit", "git-tree", "git-tree-entry",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"git-stash", "git-submodule",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"fs-symlink", "fs-hardlink",
    ...    ]
    ...    for name in expected:
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert name in ResourceTypeSpec.BUILTIN_NAMES, f"Missing: {name}"
    ...    print("All deferred physical types in BUILTIN_NAMES")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    timeout=60s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    BUILTIN_NAMES check failed: ${result.stderr}
    Should Contain    ${result.stdout}    All deferred physical types in BUILTIN_NAMES

Assert Git Taxonomy In Builtin Types Registry
    [Documentation]    Verify all deferred physical types are in BUILTIN_TYPES list
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.application.services._resource_registry_data import BUILTIN_TYPES
    ...    names = [t["name"] for t in BUILTIN_TYPES]
    ...    expected = [
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"git", "git-remote", "git-branch", "git-tag",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"git-commit", "git-tree", "git-tree-entry",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"git-stash", "git-submodule",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"fs-symlink", "fs-hardlink",
    ...    ]
    ...    for name in expected:
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert name in names, f"Missing from _BUILTIN_TYPES: {name}"
    ...    print("All deferred physical types in _BUILTIN_TYPES")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    timeout=60s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    _BUILTIN_TYPES check failed: ${result.stderr}
    Should Contain    ${result.stdout}    All deferred physical types in _BUILTIN_TYPES

*** Keywords ***
