set(TEST_TIMEOUT_SECONDS "" CACHE STRING "Limit runtime of each test to the number of seconds specified")

add_library(auto_test_support
  auto_test_support.c
  auto_test_support.h)
target_link_libraries(auto_test_support PRIVATE misc_tools)
if(TARGET toxcore_static)
  target_link_libraries(auto_test_support PRIVATE toxcore_static)
else()
  target_link_libraries(auto_test_support PRIVATE toxcore_shared)
endif()
if(TARGET pthreads4w::pthreads4w)
  target_link_libraries(auto_test_support PRIVATE pthreads4w::pthreads4w)
elseif(TARGET PThreads4W::PThreads4W)
  target_link_libraries(auto_test_support PRIVATE PThreads4W::PThreads4W)
elseif(TARGET Threads::Threads)
  target_link_libraries(auto_test_support PRIVATE Threads::Threads)
endif()

function(auto_test target)
  add_executable(auto_${target}_test ${target}_test.c)
  target_link_libraries(auto_${target}_test PRIVATE misc_tools auto_test_support)
  if(TARGET toxcore_static)
    target_link_libraries(auto_${target}_test PRIVATE toxcore_static)
  else()
    target_link_libraries(auto_${target}_test PRIVATE toxcore_shared)
  endif()
  if(TARGET pthreads4w::pthreads4w)
    target_link_libraries(auto_${target}_test PRIVATE pthreads4w::pthreads4w)
  elseif(TARGET PThreads4W::PThreads4W)
    target_link_libraries(auto_${target}_test PRIVATE PThreads4W::PThreads4W)
  elseif(TARGET Threads::Threads)
    target_link_libraries(auto_${target}_test PRIVATE Threads::Threads)
  endif()
  add_test(NAME ${target} COMMAND auto_${target}_test)
  set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}")
  # add the source dir as environment variable, so the testdata can be found
  set_tests_properties(${target} PROPERTIES ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw;srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
endfunction()

auto_test(TCP)
auto_test(announce)
auto_test(crypto)
#auto_test(dht)  # Doesn't work with UNITY_BUILD.
auto_test(encryptsave)
auto_test(file_saving)
auto_test(forwarding)
auto_test(invalid_tcp_proxy)
auto_test(invalid_udp_proxy)
auto_test(network)
auto_test(onion)
auto_test(save_compatibility)
auto_test(tox_dispatch)
auto_test(tox_new)
auto_test(tox_strncasecmp)
auto_test(version)

add_subdirectory(scenarios)

target_include_directories(auto_encryptsave_test SYSTEM PRIVATE ${LIBSODIUM_INCLUDE_DIRS})

if(NON_HERMETIC_TESTS)
  auto_test(tcp_relay)
endif()

if(PROXY_TEST)
  auto_test(proxy)
endif()
