*** Settings ***
Documentation       Regression tests for built-in git-checkout type bootstrap (bug #524).
...                 These tests verify that bootstrap_builtin_types() correctly seeds
...                 git-checkout into the registry and that resource registration succeeds
...                 after bootstrap.  Both tests call bootstrap explicitly and should pass.
Library             Process
Library             OperatingSystem
Resource            ${CURDIR}/common.resource

Suite Setup         Setup Test Environment
Suite Teardown      Cleanup Test Environment

*** Test Cases ***
Resource Add Git Checkout Should Not Fail With Type Not Found
    [Documentation]    Verify that after bootstrap, registering a git-checkout 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="git-checkout",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}name="local/test",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}location="/tmp/repo",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}description="Test git-checkout resource",
    ...    ${SPACE}${SPACE}${SPACE}${SPACE}properties={"branch": "main"},
    ...    )
    ...    assert resource.resource_type_name == "git-checkout", f"type: {resource.resource_type_name}"
    ...    assert resource.name == "local/test", f"name: {resource.name}"
    ...    print("resource add git-checkout succeeded after bootstrap")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    ...    msg=resource add git-checkout failed: ${result.stderr}
    Should Contain    ${result.stdout}    resource add git-checkout succeeded after bootstrap

Git Checkout Type Exists After Bootstrap
    [Documentation]    Directly call bootstrap_builtin_types() and verify git-checkout
    ...                is present in the type registry.
    ${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 "git-checkout" in registered, f"git-checkout not in registered: {registered}"
    ...    spec = service.show_type("git-checkout")
    ...    assert spec.name == "git-checkout", f"name mismatch: {spec.name}"
    ...    rk = spec.resource_kind.value if hasattr(spec.resource_kind, "value") else str(spec.resource_kind)
    ...    assert rk == "physical", f"kind mismatch: {rk}"
    ...    ss = spec.sandbox_strategy.value if hasattr(spec.sandbox_strategy, "value") else str(spec.sandbox_strategy)
    ...    assert ss == "git_worktree", f"sandbox mismatch: {ss}"
    ...    assert spec.user_addable is True, f"user_addable: {spec.user_addable}"
    ...    print("git-checkout bootstrap validated successfully")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    ...    msg=Bootstrap validation failed: ${result.stderr}
    Should Contain    ${result.stdout}    git-checkout bootstrap validated successfully
