*** Settings ***
Documentation       Binding Resolution Service Smoke Tests
Library             Process
Library             OperatingSystem

*** Variables ***
${PYTHON}           python

*** Test Cases ***
Contextual Binding Resolves Single Resource
    [Documentation]    Resolve a contextual slot with one matching linked resource
    ${script}=    Catenate    SEPARATOR=\n
    ...    from unittest.mock import MagicMock
    ...    from cleveragents.core.exceptions import NotFoundError
    ...    from cleveragents.domain.models.core.resource import Resource, PhysVirt
    ...    from cleveragents.domain.models.core.resource_type import ResourceTypeSpec, ResourceKind, SandboxStrategy
    ...    from cleveragents.domain.models.core.project import NamespacedProject, LinkedResource
    ...    from cleveragents.domain.models.core.tool import Tool, ToolSource, ToolType, ResourceSlot, ResourceAccessMode, BindingMode
    ...    from cleveragents.application.services.binding_resolution_service import BindingResolutionService
    ...    registry = MagicMock()
    ...    res = Resource(resource_id="01HGZ6FE0AQDYTR4BX00000001", name=None, resource_type_name="git-checkout", classification=PhysVirt.PHYSICAL)
    ...    registry.show_resource = lambda x: res
    ...    registry.show_type = lambda x: ResourceTypeSpec(name="git-checkout", resource_kind=ResourceKind.PHYSICAL, sandbox_strategy=SandboxStrategy.NONE, built_in=True)
    ...    project = NamespacedProject(name="proj", namespace="local", linked_resources=[LinkedResource(resource_id="01HGZ6FE0AQDYTR4BX00000001", alias="repo")])
    ...    tool = Tool(name="local/reader", description="Read", source=ToolSource.BUILTIN, tool_type=ToolType.TOOL, resource_slots=[ResourceSlot(name="repo", resource_type="git-checkout", access=ResourceAccessMode.READ_WRITE, binding=BindingMode.CONTEXTUAL)])
    ...    svc = BindingResolutionService(registry)
    ...    results = svc.resolve(tool, project)
    ...    assert len(results) == 1, f"Expected 1 result, got {len(results)}"
    ...    assert results[0].binding_mode == "contextual"
    ...    assert results[0].resource_id == "01HGZ6FE0AQDYTR4BX00000001"
    ...    assert not results[0].deferred
    ...    print("Contextual binding resolution passed")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    Contextual binding test failed: ${result.stderr}
    Should Contain    ${result.stdout}    Contextual binding resolution passed

Static Binding Resolves Named Resource
    [Documentation]    Resolve a static slot bound to a named resource
    ${script}=    Catenate    SEPARATOR=\n
    ...    from unittest.mock import MagicMock
    ...    from cleveragents.core.exceptions import NotFoundError
    ...    from cleveragents.domain.models.core.resource import Resource, PhysVirt
    ...    from cleveragents.domain.models.core.resource_type import ResourceTypeSpec, ResourceKind, SandboxStrategy
    ...    from cleveragents.domain.models.core.project import NamespacedProject
    ...    from cleveragents.domain.models.core.tool import Tool, ToolSource, ToolType, ResourceSlot, ResourceAccessMode, BindingMode
    ...    from cleveragents.application.services.binding_resolution_service import BindingResolutionService
    ...    registry = MagicMock()
    ...    res = Resource(resource_id="01HGZ6FE0AQDYTR4BX00000020", name="local/shared-config", resource_type_name="fs-directory", classification=PhysVirt.PHYSICAL)
    ...    registry.show_resource = lambda x: res
    ...    registry.show_type = lambda x: ResourceTypeSpec(name="fs-directory", resource_kind=ResourceKind.PHYSICAL, sandbox_strategy=SandboxStrategy.NONE, built_in=True)
    ...    project = NamespacedProject(name="proj", namespace="local")
    ...    tool = Tool(name="local/deployer", description="Deploy", source=ToolSource.BUILTIN, tool_type=ToolType.TOOL, resource_slots=[ResourceSlot(name="config_dir", resource_type="fs-directory", access=ResourceAccessMode.READ_ONLY, binding=BindingMode.STATIC, static_resource="local/shared-config")])
    ...    svc = BindingResolutionService(registry)
    ...    results = svc.resolve(tool, project)
    ...    assert len(results) == 1
    ...    assert results[0].binding_mode == "static"
    ...    assert results[0].resource_id == "01HGZ6FE0AQDYTR4BX00000020"
    ...    assert not results[0].deferred
    ...    print("Static binding resolution passed")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    Static binding test failed: ${result.stderr}
    Should Contain    ${result.stdout}    Static binding resolution passed

Parameter Binding Is Deferred
    [Documentation]    Parameter binding returns a deferred result
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.domain.models.core.project import NamespacedProject
    ...    from cleveragents.domain.models.core.tool import Tool, ToolSource, ToolType, ResourceSlot, ResourceAccessMode, BindingMode
    ...    from cleveragents.application.services.binding_resolution_service import BindingResolutionService
    ...    from unittest.mock import MagicMock
    ...    registry = MagicMock()
    ...    project = NamespacedProject(name="proj", namespace="local")
    ...    tool = Tool(name="local/builder", description="Build", source=ToolSource.BUILTIN, tool_type=ToolType.TOOL, resource_slots=[ResourceSlot(name="target", resource_type="fs-directory", access=ResourceAccessMode.READ_WRITE, binding=BindingMode.PARAMETER)])
    ...    svc = BindingResolutionService(registry)
    ...    results = svc.resolve(tool, project)
    ...    assert len(results) == 1
    ...    assert results[0].binding_mode == "parameter"
    ...    assert results[0].deferred is True
    ...    assert results[0].resource_id is None
    ...    print("Parameter binding deferred passed")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}
    Should Be Equal As Integers    ${result.rc}    0    Parameter binding test failed: ${result.stderr}
    Should Contain    ${result.stdout}    Parameter binding deferred passed

*** Keywords ***
