*** Settings ***
Documentation       Filesystem Resource Type YAML Load Tests
Library             Process
Library             OperatingSystem

*** Variables ***
${PYTHON}             python
${FS_MOUNT_YAML}      ${CURDIR}/../examples/resource-types/fs-mount.yaml
${FS_FILE_YAML}       ${CURDIR}/../examples/resource-types/fs-file.yaml
${FS_DIR_YAML}        ${CURDIR}/../examples/resource-types/fs-directory.yaml

*** Test Cases ***
Load Fs Mount Built-in YAML And Validate
    [Documentation]    Load fs-mount.yaml via schema loader and assert key fields
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.resource.schema import ResourceTypeConfigSchema
    ...    schema = ResourceTypeConfigSchema.from_yaml_file("${FS_MOUNT_YAML}")
    ...    assert schema.name == "fs-mount", 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 schema.user_addable is True, f"user_addable: {schema.user_addable}"
    ...    assert len(schema.cli_args) == 1, f"cli_args: {len(schema.cli_args)}"
    ...    assert schema.cli_args[0].name == "path", f"cli_arg: {schema.cli_args[0].name}"
    ...    assert len(schema.parent_types) == 0, f"parent_types: {schema.parent_types}"
    ...    assert "fs-directory" in schema.child_types, f"child_types: {schema.child_types}"
    ...    assert schema.handler == "cleveragents.resource.handlers.fs_mount"
    ...    assert schema.auto_discovery is not None, "auto_discovery missing"
    ...    assert schema.auto_discovery.get("enabled") is True
    ...    print("fs-mount built-in YAML validated successfully")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    fs-mount load failed: ${result.stderr}
    Should Contain    ${result.stdout}    fs-mount built-in YAML validated successfully

Load Fs File Built-in YAML And Validate
    [Documentation]    Load fs-file.yaml via schema loader and assert key fields
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.resource.schema import ResourceTypeConfigSchema
    ...    schema = ResourceTypeConfigSchema.from_yaml_file("${FS_FILE_YAML}")
    ...    assert schema.name == "fs-file", 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 schema.user_addable is False, f"user_addable: {schema.user_addable}"
    ...    assert len(schema.cli_args) == 0, f"cli_args: {len(schema.cli_args)}"
    ...    assert "fs-directory" in schema.parent_types
    ...    assert len(schema.child_types) == 0, f"child_types: {schema.child_types}"
    ...    assert schema.handler == "cleveragents.resource.handlers.fs_file"
    ...    print("fs-file built-in YAML validated successfully")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    fs-file load failed: ${result.stderr}
    Should Contain    ${result.stdout}    fs-file built-in YAML validated successfully

Load All Fs Built-in YAMLs As Domain Models
    [Documentation]    Load all fs 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"
    ...    check("${FS_MOUNT_YAML}", "fs-mount")
    ...    check("${FS_FILE_YAML}", "fs-file")
    ...    check("${FS_DIR_YAML}", "fs-directory")
    ...    print("All fs 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}    All fs built-in domain models validated successfully

*** Keywords ***
