"""The Transport seam — runtime_checkable structural conformance.

Proves the seam is transport-agnostic: a fake that never imports gRPC
satisfies both Protocols structurally.
"""

from __future__ import annotations

from tests.conftest import FakeGrpcTransport

import mgf.vm.sdk as sdk
from mgf.vm.sdk.transport.base import (
    AsyncTransport,
    Transport,
    TransportCancelled,
    TransportConnectionError,
    TransportError,
    TransportTimeout,
)


def test_fake_satisfies_transport_protocol() -> None:
    assert isinstance(FakeGrpcTransport(), Transport)


def test_arbitrary_object_is_not_a_transport() -> None:
    assert not isinstance(object(), Transport)


class _AsyncStub:
    async def submit_op(
        self,
        kind: object,
        payload: object,
        *,
        client_op_id: str,
        token: object | None = None,
    ) -> object:
        raise NotImplementedError

    async def get_op(self, op_id: object) -> object:
        raise NotImplementedError

    def watch_op(self, op_id: object) -> object:
        raise NotImplementedError

    async def read(self, query: object) -> object:
        raise NotImplementedError

    def watch_stream(self, subject: object) -> object:
        raise NotImplementedError

    async def aclose(self) -> None:
        raise NotImplementedError


def test_async_stub_satisfies_async_transport_protocol() -> None:
    assert isinstance(_AsyncStub(), AsyncTransport)


def test_sync_fake_is_not_async_transport() -> None:
    # The sync fake lacks aclose; structural check must reject it.
    assert not isinstance(FakeGrpcTransport(), AsyncTransport)


def test_transport_reexports_core_error_tree() -> None:
    # The seam re-exports core's errors; it must NOT redefine them. They are
    # the same objects as mgf.vm.core.exceptions.
    from mgf.vm.core import exceptions as core_exc

    assert TransportError is core_exc.TransportError
    assert TransportConnectionError is core_exc.TransportConnectionError
    assert TransportTimeout is core_exc.TransportTimeout
    assert TransportCancelled is core_exc.TransportCancelled


def test_public_surface_exports() -> None:
    for name in (
        "Transport",
        "AsyncTransport",
        "VmManagerClient",
        "AsyncVmManagerClient",
        "OperationHandle",
    ):
        assert name in sdk.__all__
        assert hasattr(sdk, name)


def test_operation_handle_is_reexported_from_core() -> None:
    from mgf.vm.core.contracts import OperationHandle as CoreHandle

    assert sdk.OperationHandle is CoreHandle
