*** Settings ***
Documentation       Built-in Resource Type YAML Load Tests
Library             Process
Library             OperatingSystem

*** Variables ***
${GIT_CHECKOUT_YAML}    ${CURDIR}/../examples/resource-types/git-checkout.yaml
${FS_DIRECTORY_YAML}    ${CURDIR}/../examples/resource-types/fs-directory.yaml

*** Test Cases ***
Load Git Checkout Built-in YAML And Validate
    [Documentation]    Load git-checkout.yaml via schema loader and assert key fields
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.resource.schema import ResourceTypeConfigSchema
    ...    schema = ResourceTypeConfigSchema.from_yaml_file("${GIT_CHECKOUT_YAML}")
    ...    assert schema.name == "git-checkout", f"name: {schema.name}"
    ...    assert schema.resource_kind == "physical", f"kind: {schema.resource_kind}"
    ...    assert schema.sandbox_strategy == "git_worktree", f"strategy: {schema.sandbox_strategy}"
    ...    assert schema.built_in is True, f"built_in: {schema.built_in}"
    ...    assert len(schema.cli_args) == 2, f"cli_args: {len(schema.cli_args)}"
    ...    assert schema.handler == "cleveragents.resource.handlers.git_checkout"
    ...    print("git-checkout built-in YAML validated successfully")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    git-checkout load failed: ${result.stderr}
    Should Contain    ${result.stdout}    git-checkout built-in YAML validated successfully

Load Fs Directory Built-in YAML And Validate
    [Documentation]    Load fs-directory.yaml via schema loader and assert key fields
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.resource.schema import ResourceTypeConfigSchema
    ...    schema = ResourceTypeConfigSchema.from_yaml_file("${FS_DIRECTORY_YAML}")
    ...    assert schema.name == "fs-directory", f"name: {schema.name}"
    ...    assert schema.resource_kind == "physical", f"kind: {schema.resource_kind}"
    ...    assert schema.sandbox_strategy == "copy_on_write", f"strategy: {schema.sandbox_strategy}"
    ...    assert schema.built_in is True, f"built_in: {schema.built_in}"
    ...    assert len(schema.cli_args) == 1, f"cli_args: {len(schema.cli_args)}"
    ...    assert "git-checkout" in schema.parent_types
    ...    assert schema.handler == "cleveragents.resource.handlers.fs_directory:FsDirectoryHandler"
    ...    print("fs-directory built-in YAML validated successfully")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    fs-directory load failed: ${result.stderr}
    Should Contain    ${result.stdout}    fs-directory built-in YAML validated successfully

Load Both Built-in YAMLs As Domain Models
    [Documentation]    Load both built-in YAMLs as ResourceTypeSpec domain models
    ${script}=    Catenate    SEPARATOR=\n
    ...    import yaml
    ...    from cleveragents.domain.models.core.resource_type import ResourceTypeSpec
    ...    def check(path, expected_name):
    ...    ${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 == expected_name, f"name mismatch: {spec.name}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}cli_dict = spec.as_cli_dict()
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert "capabilities" in cli_dict, f"missing capabilities in cli_dict"
    ...    check("${GIT_CHECKOUT_YAML}", "git-checkout")
    ...    check("${FS_DIRECTORY_YAML}", "fs-directory")
    ...    print("Both built-in domain models validated successfully")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    Domain model load failed: ${result.stderr}
    Should Contain    ${result.stdout}    Both built-in domain models validated successfully

*** Keywords ***
