set(WORKSPACE "${CMAKE_CURRENT_SOURCE_DIR}")

if (USE_HIP)
  file(GLOB TEST_SOURCES "*.cpp")
  hipify_files(TEST_SOURCES)

  file(RELATIVE_PATH EXAMPLE_REL_PATH "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}")
  set(WORKSPACE "${CMAKE_BINARY_DIR}/${EXAMPLE_REL_PATH}")
endif()

add_executable(rdma_transport_test ${WORKSPACE}/rdma_transport_test.cpp)
target_link_libraries(rdma_transport_test PUBLIC transfer_engine gflags::gflags glog::glog)
# add_test(NAME rdma_transport_test COMMAND rdma_transport_test)

add_executable(transport_uint_test ${WORKSPACE}/transport_uint_test.cpp)
target_link_libraries(transport_uint_test PUBLIC transfer_engine gtest gtest_main )
add_test(NAME transport_uint_test COMMAND transport_uint_test)

add_executable(rdma_transport_test2 ${WORKSPACE}/rdma_transport_test2.cpp)
target_link_libraries(rdma_transport_test2 PUBLIC transfer_engine gtest gtest_main )
# add_test(NAME rdma_transport_test2 COMMAND rdma_transport_test2)

add_executable(rdma_loopback_test ${WORKSPACE}/rdma_loopback_test.cpp)
target_link_libraries(rdma_loopback_test PUBLIC transfer_engine gtest gtest_main )
# add_test(NAME rdma_loopback_test COMMAND rdma_loopback_test)

# This test verifies endpoint re-establishment in RDMATransport.
# Intended for manual testing only.
add_executable(rdma_endpoint_reestablish_test ${WORKSPACE}/rdma_endpoint_reestablish_test.cpp)
target_link_libraries(rdma_endpoint_reestablish_test PUBLIC transfer_engine gtest gtest_main )

if (USE_CXL)
    add_executable(cxl_transport_test ${WORKSPACE}/cxl_transport_test.cpp)
    target_link_libraries(cxl_transport_test PUBLIC transfer_engine gtest gtest_main )
    add_test(NAME cxl_transport_test COMMAND cxl_transport_test)
endif()

if (USE_NVMEOF)
    add_executable(nvmeof_transport_test ${WORKSPACE}/nvmeof_transport_test.cpp)
    target_link_libraries(nvmeof_transport_test PUBLIC transfer_engine gtest gtest_main )
    # add_test(NAME nvmeof_transport_test COMMAND nvmeof_transport_test)
endif()

if (USE_TCP)
add_executable(tcp_transport_test ${WORKSPACE}/tcp_transport_test.cpp)
target_link_libraries(tcp_transport_test PUBLIC transfer_engine gtest gtest_main )
add_test(NAME tcp_transport_test COMMAND tcp_transport_test)
endif()

if (USE_MNNVL)
    add_executable(nvlink_transport_test ${WORKSPACE}/nvlink_transport_test.cpp)
    target_link_libraries(nvlink_transport_test PUBLIC transfer_engine gtest gtest_main )
    add_test(NAME nvlink_transport_test COMMAND nvlink_transport_test)
endif()

if (USE_UBSHMEM)
    add_executable(ubshmem_transport_test ${WORKSPACE}/ubshmem_transport_test.cpp)
    target_link_libraries(ubshmem_transport_test PUBLIC transfer_engine gtest gtest_main ascendcl)
    add_test(NAME ubshmem_transport_test COMMAND ubshmem_transport_test)
endif()

if (USE_EFA)
    add_executable(efa_transport_test ${WORKSPACE}/efa_transport_test.cpp)
    target_link_libraries(efa_transport_test PUBLIC transfer_engine gtest gtest_main)
    add_test(NAME efa_transport_test COMMAND efa_transport_test)
endif()

# UB transport test with URMA endpoint and mock support
if (USE_UB)
    add_executable(ub_transport_test ${WORKSPACE}/ub_transport_test.cpp)
    target_link_libraries(ub_transport_test PUBLIC transfer_engine gtest gtest_main glog::glog pthread)
    target_include_directories(ub_transport_test PRIVATE ${urma_INCLUDE_DIR})
    add_test(NAME ub_transport_test COMMAND ub_transport_test)
endif()

add_executable(transfer_metadata_test ${WORKSPACE}/transfer_metadata_test.cpp)
target_link_libraries(transfer_metadata_test PUBLIC transfer_engine gtest gtest_main)
add_test(NAME transfer_metadata_test COMMAND transfer_metadata_test)

add_executable(topology_test ${WORKSPACE}/topology_test.cpp)
target_link_libraries(topology_test PUBLIC transfer_engine gtest gtest_main)
add_test(NAME topology_test COMMAND topology_test)

add_executable(memory_location_test ${WORKSPACE}/memory_location_test.cpp)
target_link_libraries(memory_location_test PUBLIC transfer_engine gtest gtest_main)
add_test(NAME memory_location_test COMMAND memory_location_test)

add_executable(common_test ${WORKSPACE}/common_test.cpp)
target_link_libraries(common_test PUBLIC transfer_engine gtest gtest_main)
add_test(NAME common_test COMMAND common_test)

if (USE_ASCEND_DIRECT)
    # AscendDirectTransport unit test with mock implementations
    # Mock implementations are included in the test file via anonymous namespace
    add_executable(ascend_direct_transport_test
        ${WORKSPACE}/ascend_direct_transport_test.cpp
    )
    target_link_libraries(ascend_direct_transport_test PUBLIC
        transfer_engine
        gtest
        gtest_main
        metadef
    )
    # Allow shared library undefined symbols (Ascend libraries have circular deps)
    set_target_properties(ascend_direct_transport_test PROPERTIES
        LINK_FLAGS "-Wl,--allow-shlib-undefined"
    )

    add_test(NAME ascend_direct_transport_test COMMAND ascend_direct_transport_test)
endif()

# Multi-protocol transport test (only when ENABLE_MULTI_PROTOCOL is ON)
if (ENABLE_MULTI_PROTOCOL)
    add_executable(mp_transport_test ${WORKSPACE}/mp_transport_test.cpp)
    target_link_libraries(mp_transport_test PUBLIC transfer_engine gtest gtest_main)
    add_test(NAME mp_transport_test COMMAND mp_transport_test)
endif()
