*** Settings ***
Documentation       Virtual Core Resource Type Integration Tests
Library             Process
Library             OperatingSystem

*** Variables ***
${PYTHON}           python
${FILE_YAML}        ${CURDIR}/../examples/resource-types/file.yaml
${DIR_YAML}         ${CURDIR}/../examples/resource-types/directory.yaml
${COMMIT_YAML}      ${CURDIR}/../examples/resource-types/commit.yaml
${BRANCH_YAML}      ${CURDIR}/../examples/resource-types/branch.yaml
${TAG_YAML}         ${CURDIR}/../examples/resource-types/tag.yaml
${TREE_YAML}        ${CURDIR}/../examples/resource-types/tree.yaml

*** Test Cases ***
Load All Virtual Core YAMLs And Validate Schema
    [Documentation]    Load all 6 virtual core YAMLs via schema loader and assert key fields
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.resource.schema import ResourceTypeConfigSchema
    ...    VIRTUAL_TYPES = {
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"file": "${FILE_YAML}",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"directory": "${DIR_YAML}",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"commit": "${COMMIT_YAML}",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"branch": "${BRANCH_YAML}",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"tag": "${TAG_YAML}",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"tree": "${TREE_YAML}",
    ...    }
    ...    for name, path in VIRTUAL_TYPES.items():
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}schema = ResourceTypeConfigSchema.from_yaml_file(path)
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert schema.name == name, f"name mismatch: {schema.name} != {name}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert schema.resource_kind == "virtual", f"kind: {schema.resource_kind}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert schema.sandbox_strategy == "none", f"strategy: {schema.sandbox_strategy}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert schema.user_addable is False, f"addable: {schema.user_addable}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert schema.built_in is True, f"built_in: {schema.built_in}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert schema.equivalence is not None, f"equivalence missing for {name}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert len(schema.cli_args) == 0, f"cli_args not empty for {name}"
    ...    print("All 6 virtual core YAMLs validated successfully")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    Virtual core YAML load failed: ${result.stderr}
    Should Contain    ${result.stdout}    All 6 virtual core YAMLs validated successfully

Load Virtual Core YAMLs As Domain Models
    [Documentation]    Load all virtual core YAMLs as ResourceTypeSpec domain models
    ${script}=    Catenate    SEPARATOR=\n
    ...    import yaml
    ...    from cleveragents.domain.models.core.resource_type import ResourceTypeSpec, ResourceKind
    ...    PATHS = {
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"file": "${FILE_YAML}",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"directory": "${DIR_YAML}",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"commit": "${COMMIT_YAML}",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"branch": "${BRANCH_YAML}",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"tag": "${TAG_YAML}",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}"tree": "${TREE_YAML}",
    ...    }
    ...    for name, path in PATHS.items():
    ...    ${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 == ResourceKind.VIRTUAL
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}cli_dict = spec.as_cli_dict()
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert "equivalence" in cli_dict, f"no equivalence in cli_dict for {name}"
    ...    print("All 6 virtual core 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 6 virtual core domain models validated successfully

Verify Virtual Types In Bootstrap Registration
    [Documentation]    Verify all 6 virtual core types are in BUILTIN_TYPES
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.application.services._resource_registry_data import BUILTIN_TYPES
    ...    names = [d["name"] for d in BUILTIN_TYPES]
    ...    expected = ["file", "directory", "commit", "branch", "tag", "tree"]
    ...    for vt in expected:
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert vt in names, f"{vt} not in builtin types: {names}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}entry = next(d for d in BUILTIN_TYPES if d["name"] == vt)
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert entry["resource_kind"] == "virtual", f"{vt} kind: {entry['resource_kind']}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert entry["user_addable"] is False, f"{vt} addable: {entry['user_addable']}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert entry["sandbox_strategy"] == "none", f"{vt} strategy: {entry['sandbox_strategy']}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert entry.get("equivalence") is not None, f"{vt} missing equivalence"
    ...    print("All 6 virtual core types present in bootstrap registration")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    Bootstrap verification failed: ${result.stderr}
    Should Contain    ${result.stdout}    All 6 virtual core types present in bootstrap registration

Verify BUILTIN_NAMES Includes Virtual Types
    [Documentation]    Verify ResourceTypeSpec.BUILTIN_NAMES includes virtual core types
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.domain.models.core.resource_type import ResourceTypeSpec
    ...    expected = {"file", "directory", "commit", "branch", "tag", "tree"}
    ...    for name in expected:
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert name in ResourceTypeSpec.BUILTIN_NAMES, f"{name} not in BUILTIN_NAMES"
    ...    print("All virtual core types in BUILTIN_NAMES")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    BUILTIN_NAMES check failed: ${result.stderr}
    Should Contain    ${result.stdout}    All virtual core types in BUILTIN_NAMES

Virtual Types Appear In CLI Resource Type List
    [Documentation]    Verify virtual core types are listed via the CLI resource type list path
    ${script}=    Catenate    SEPARATOR=\n
    ...    import json
    ...    from sqlalchemy import create_engine
    ...    from sqlalchemy.orm import sessionmaker
    ...    from cleveragents.infrastructure.database.models import Base
    ...    from cleveragents.application.services.resource_registry_service import ResourceRegistryService
    ...    engine = create_engine("sqlite:///:memory:")
    ...    Base.metadata.create_all(engine)
    ...    service = ResourceRegistryService(session_factory=sessionmaker(bind=engine))
    ...    service.bootstrap_builtin_types()
    ...    types = service.list_types()
    ...    type_names = [t.name for t in types]
    ...    expected = ["file", "directory", "commit", "branch", "tag", "tree"]
    ...    for vt in expected:
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert vt in type_names, f"{vt} not in listed types: {type_names}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}spec = next(t for t in types if t.name == vt)
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert str(spec.resource_kind) == "virtual", f"{vt} kind: {spec.resource_kind}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert spec.user_addable is False, f"{vt} user_addable: {spec.user_addable}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert spec.equivalence is not None, f"{vt} missing equivalence"
    ...    print(f"CLI list path: all 6 virtual types present and correctly typed")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    CLI list path verification failed: ${result.stderr}
    Should Contain    ${result.stdout}    all 6 virtual types present

Virtual Types Are Rejected By Register Resource
    [Documentation]    Verify virtual types cannot be user-added via register_resource
    ${script}=    Catenate    SEPARATOR=\n
    ...    from sqlalchemy import create_engine
    ...    from sqlalchemy.orm import sessionmaker
    ...    from cleveragents.infrastructure.database.models import Base
    ...    from cleveragents.application.services.resource_registry_service import ResourceRegistryService
    ...    engine = create_engine("sqlite:///:memory:")
    ...    Base.metadata.create_all(engine)
    ...    service = ResourceRegistryService(session_factory=sessionmaker(bind=engine))
    ...    service.bootstrap_builtin_types()
    ...    try:
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}service.register_resource("file", name="test-file")
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}raise AssertionError("Expected ValidationError but register_resource succeeded")
    ...    except Exception as e:
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}assert "not user-addable" in str(e).lower(), f"Wrong error: {e}"
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}print(f"Correctly rejected: {e}")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    Register rejection test failed: ${result.stderr}
    Should Contain    ${result.stdout}    Correctly rejected

*** Keywords ***
