*** Settings ***
Documentation       Resource Repository CRUD Integration Test
Library             Process
Library             OperatingSystem

*** Test Cases ***
ResourceTypeRepository Create And Get
    [Documentation]    Create a resource type and retrieve it by name
    ${script}=    Catenate    SEPARATOR=\n
    ...    from sqlalchemy import create_engine, event
    ...    from sqlalchemy.orm import sessionmaker
    ...    from cleveragents.infrastructure.database.models import Base, ResourceTypeModel
    ...    from cleveragents.infrastructure.database.repositories import ResourceTypeRepository
    ...    from cleveragents.domain.models.core.resource_type import ResourceTypeSpec, ResourceKind, SandboxStrategy
    ...    engine = create_engine("sqlite:///:memory:")
    ...    @event.listens_for(engine, "connect")
    ...    def _fk(conn, _): conn.cursor().execute("PRAGMA foreign_keys=ON")
    ...    Base.metadata.create_all(engine)
    ...    factory = sessionmaker(bind=engine)
    ...    repo = ResourceTypeRepository(factory)
    ...    spec = ResourceTypeSpec(name="robot/test-type", description="Robot test", resource_kind=ResourceKind.PHYSICAL, sandbox_strategy=SandboxStrategy.GIT_WORKTREE, user_addable=True, cli_args=[], parent_types=[], child_types=[], auto_discovery=None, equivalence=None, handler=None, capabilities={"read": True, "write": True, "sandbox": True, "checkpoint": False}, built_in=False)
    ...    repo.create(spec)
    ...    result = repo.get("robot/test-type")
    ...    assert result is not None, "Resource type not found"
    ...    assert result.name == "robot/test-type", f"Name mismatch: {result.name}"
    ...    assert result.description == "Robot test"
    ...    print("ResourceTypeRepository create+get passed")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    ResourceType repo test failed: ${result.stderr}
    Should Contain    ${result.stdout}    ResourceTypeRepository create+get passed

ResourceRepository Create And Resolve
    [Documentation]    Create a resource and resolve it by name and ULID
    ${script}=    Catenate    SEPARATOR=\n
    ...    from datetime import datetime, UTC
    ...    from sqlalchemy import create_engine, event
    ...    from sqlalchemy.orm import sessionmaker
    ...    from cleveragents.infrastructure.database.models import Base
    ...    from cleveragents.infrastructure.database.repositories import ResourceTypeRepository, ResourceRepository
    ...    from cleveragents.domain.models.core.resource_type import ResourceTypeSpec, ResourceKind, SandboxStrategy
    ...    from cleveragents.domain.models.core.resource import Resource, PhysVirt, ResourceCapabilities
    ...    engine = create_engine("sqlite:///:memory:")
    ...    @event.listens_for(engine, "connect")
    ...    def _fk(conn, _): conn.cursor().execute("PRAGMA foreign_keys=ON")
    ...    Base.metadata.create_all(engine)
    ...    factory = sessionmaker(bind=engine)
    ...    rt_repo = ResourceTypeRepository(factory)
    ...    res_repo = ResourceRepository(factory)
    ...    spec = ResourceTypeSpec(name="robot/res-type", description="Type", resource_kind=ResourceKind.PHYSICAL, sandbox_strategy=SandboxStrategy.NONE, user_addable=True, cli_args=[], parent_types=[], child_types=[], auto_discovery=None, equivalence=None, handler=None, capabilities={"read": True, "write": True, "sandbox": True, "checkpoint": False}, built_in=False)
    ...    rt_repo.create(spec)
    ...    res = Resource(resource_id="01HGZ6FE0AQDYTR4BX00000001", name="robot/test-res", resource_type_name="robot/res-type", classification=PhysVirt.PHYSICAL, description="Test", properties={}, location="/tmp/test", content_hash=None, sandbox_strategy=None, capabilities=ResourceCapabilities(), created_at=datetime.now(tz=UTC), updated_at=datetime.now(tz=UTC))
    ...    res_repo.create(res)
    ...    by_name = res_repo.resolve_namespaced_name("robot/test-res")
    ...    assert by_name is not None, "Resolve by name failed"
    ...    by_ulid = res_repo.resolve_namespaced_name("01HGZ6FE0AQDYTR4BX00000001")
    ...    assert by_ulid is not None, "Resolve by ULID failed"
    ...    assert by_name.resource_id == by_ulid.resource_id
    ...    print("ResourceRepository create+resolve passed")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    Resource repo test failed: ${result.stderr}
    Should Contain    ${result.stdout}    ResourceRepository create+resolve passed

*** Keywords ***
