*** Settings ***
Documentation       Integration tests for built-in fs-mount type bootstrap (bug #2911).
...                 These tests verify that bootstrap_builtin_types() correctly seeds
...                 fs-mount into the registry and that resource registration succeeds
...                 after bootstrap.
Library             Process
Library             OperatingSystem
Resource            ${CURDIR}/common.resource

Suite Setup         Setup Test Environment
Suite Teardown      Cleanup Test Environment

*** Test Cases ***
Bootstrap Seeds Fs Mount Type Into Registry
    [Documentation]    Verify that bootstrap_builtin_types() seeds fs-mount into the
    ...                database so that show_type("fs-mount") succeeds.
    ${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:", echo=False)
    ...    Base.metadata.create_all(engine)
    ...    factory = sessionmaker(bind=engine, expire_on_commit=False)
    ...    service = ResourceRegistryService(session_factory=factory)
    ...    registered = [t.name for t in service.list_types()]
    ...    assert "fs-mount" in registered, f"fs-mount not in registered: {registered}"
    ...    spec = service.show_type("fs-mount")
    ...    assert spec.name == "fs-mount", f"name mismatch: {spec.name}"
    ...    kind = spec.resource_kind.value if hasattr(spec.resource_kind, "value") else str(spec.resource_kind)
    ...    assert kind == "physical", f"kind: {kind}"
    ...    strategy = spec.sandbox_strategy.value if hasattr(spec.sandbox_strategy, "value") else str(spec.sandbox_strategy)
    ...    assert strategy == "copy_on_write", f"strategy: {strategy}"
    ...    assert spec.user_addable is True, f"user_addable: {spec.user_addable}"
    ...    print("fs-mount bootstrap validated successfully")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    fs-mount bootstrap failed: ${result.stderr}
    Should Contain    ${result.stdout}    fs-mount bootstrap validated successfully

Fs Mount Has Correct Child Types After Bootstrap
    [Documentation]    Verify that fs-mount has fs-directory as a child type.
    ${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:", echo=False)
    ...    Base.metadata.create_all(engine)
    ...    factory = sessionmaker(bind=engine, expire_on_commit=False)
    ...    service = ResourceRegistryService(session_factory=factory)
    ...    spec = service.show_type("fs-mount")
    ...    assert "fs-directory" in spec.child_types, f"child_types: {spec.child_types}"
    ...    assert spec.parent_types == [], f"parent_types should be empty: {spec.parent_types}"
    ...    print("fs-mount child/parent types validated successfully")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    fs-mount child types check failed: ${result.stderr}
    Should Contain    ${result.stdout}    fs-mount child/parent types validated successfully

Resource Add Fs Mount Succeeds After Bootstrap
    [Documentation]    Verify that after bootstrap, registering an fs-mount resource
    ...                instance succeeds (no "Resource type not found" error).
    ${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:", echo=False)
    ...    Base.metadata.create_all(engine)
    ...    factory = sessionmaker(bind=engine, expire_on_commit=False)
    ...    service = ResourceRegistryService(session_factory=factory)
    ...    service.bootstrap_builtin_types()
    ...    resource = service.register_resource(
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}type_name="fs-mount",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}name="local/test-mount",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}location="/tmp",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}description="Test fs-mount resource",
    ...    )
    ...    assert resource.resource_type_name == "fs-mount", f"type: {resource.resource_type_name}"
    ...    assert resource.name == "local/test-mount", f"name: {resource.name}"
    ...    print("resource add fs-mount succeeded after bootstrap")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    resource add fs-mount failed: ${result.stderr}
    Should Contain    ${result.stdout}    resource add fs-mount succeeded after bootstrap

Fs Directory Has Fs Mount As Parent Type After Bootstrap
    [Documentation]    Verify that fs-directory lists fs-mount as an allowed parent type.
    ${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:", echo=False)
    ...    Base.metadata.create_all(engine)
    ...    factory = sessionmaker(bind=engine, expire_on_commit=False)
    ...    service = ResourceRegistryService(session_factory=factory)
    ...    spec = service.show_type("fs-directory")
    ...    assert "fs-mount" in spec.parent_types, f"fs-mount not in fs-directory parent_types: {spec.parent_types}"
    ...    print("fs-directory parent_types includes fs-mount")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    fs-directory parent types check failed: ${result.stderr}
    Should Contain    ${result.stdout}    fs-directory parent_types includes fs-mount
