
include(CTest)

set(KAZVTEST_RESPATH ${CMAKE_CURRENT_SOURCE_DIR}/resources)
configure_file(kazvtest-respath.hpp.in kazvtest-respath.hpp)

function(libkazv_add_tests)
  set(options "")
  set(oneValueArgs "")
  set(multiValueArgs EXTRA_LINK_LIBRARIES EXTRA_INCLUDE_DIRECTORIES)
  cmake_parse_arguments(PARSE_ARGV 0 libkazv_add_tests "${options}" "${oneValueArgs}" "${multiValueArgs}")
  foreach(test_source ${libkazv_add_tests_UNPARSED_ARGUMENTS})
    string(REGEX REPLACE "\\.cpp$" "" test_executable "${test_source}")
    string(REGEX REPLACE "/|\\\\" "--" test_executable "${test_executable}")
    message(STATUS "Test ${test_executable} added")
    add_executable("${test_executable}" "${test_source}")
    target_link_libraries("${test_executable}"
      PRIVATE Catch2::Catch2WithMain
      Threads::Threads
      ${libkazv_add_tests_EXTRA_LINK_LIBRARIES}
    )

    target_include_directories(
      "${test_executable}"
      PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
      ${CMAKE_CURRENT_SOURCE_DIR}
      ${CMAKE_CURRENT_SOURCE_DIR}/..
      ${libkazv_add_tests_EXTRA_INCLUDE_DIRECTORIES}
    )

    target_compile_definitions("${test_executable}" PRIVATE CATCH_CONFIG_ENABLE_ALL_STRINGMAKERS)

    add_test(NAME "${test_executable}" COMMAND "${test_executable}" "--allow-running-no-tests" "~[needs-internet]")
  endforeach()
endfunction()

libkazv_add_tests(
  event-test.cpp
  cursorutiltest.cpp
  base/serialization-test.cpp
  base/types-test.cpp
  base/immer-utils-test.cpp
  base/json-utils-test.cpp
  EXTRA_LINK_LIBRARIES kazvbase
)

add_library(client-test-lib SHARED client/client-test-util.cpp)
target_link_libraries(client-test-lib PUBLIC kazvjob kazvclient)
libkazv_add_tests(
  client/discovery-test.cpp
  client/sync-test.cpp
  client/content-test.cpp
  client/paginate-test.cpp
  client/storage-actions-test.cpp
  client/util-test.cpp
  client/serialization-test.cpp
  client/encrypted-file-test.cpp
  client/sdk-test.cpp
  client/thread-safety-test.cpp
  client/room-test.cpp
  client/random-generator-test.cpp
  client/profile-test.cpp
  client/kick-test.cpp
  client/ban-test.cpp
  client/join-test.cpp
  client/keys-test.cpp
  client/device-ops-test.cpp
  client/send-test.cpp
  client/encryption-test.cpp
  client/redact-test.cpp
  client/tagging-test.cpp
  client/account-data-test.cpp
  client/room/room-actions-test.cpp
  client/room/local-echo-test.cpp
  client/room/event-relationships-test.cpp
  client/room/member-membership-test.cpp
  client/room/purge-test.cpp
  client/push-rules-desc-test.cpp
  client/notification-handler-test.cpp
  client/validator-test.cpp
  client/power-levels-desc-test.cpp
  client/client-test.cpp
  client/create-room-test.cpp
  client/device-list-tracker-test.cpp
  client/device-list-tracker-benchmark-test.cpp
  client/room/read-receipt-test.cpp
  client/room/undecrypted-events-test.cpp
  client/encryption-benchmark-test.cpp
  client/login-test.cpp
  client/logout-test.cpp
  client/room/pinned-events-test.cpp
  client/get-versions-test.cpp
  client/alias-test.cpp
  client/encode-test.cpp
  client/maybe-add-save-events-trigger-benchmark-test.cpp
  client/verification-processing-test.cpp
  client/room/local-draft.cpp
  EXTRA_LINK_LIBRARIES kazvclient kazveventemitter kazvjob client-test-lib kazvtestfixtures
  EXTRA_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/client
)

libkazv_add_tests(
  basejobtest.cpp
  kazvjobtest.cpp
  file-desc-test.cpp
  EXTRA_LINK_LIBRARIES kazvbase kazvjob
)

libkazv_add_tests(
  promise-test.cpp
  EXTRA_LINK_LIBRARIES kazvbase kazvjob kazvstore
)

libkazv_add_tests(
  event-emitter-test.cpp
  EXTRA_LINK_LIBRARIES kazvbase kazveventemitter
)

libkazv_add_tests(
  crypto-test.cpp
  crypto/inbound-group-session-test.cpp
  crypto/outbound-group-session-test.cpp
  crypto/session-test.cpp
  crypto/key-export-test.cpp
  crypto/verification-process-test.cpp
  crypto/verification-utils-test.cpp
  crypto/verification-tracker-test.cpp
  EXTRA_LINK_LIBRARIES kazvcrypto
)

libkazv_add_tests(
  store-test.cpp
  EXTRA_LINK_LIBRARIES kazvstore kazvjob
)
