# Prefer a system GoogleTest (Debian build images ship libgtest-dev); fall back
# to FetchContent so the test suite also configures on distros without it
# (e.g. the Void Linux dev host).
find_package(GTest QUIET)
if(NOT GTest_FOUND)
    message(STATUS "GTest not found on the system - fetching googletest")
    include(FetchContent)
    FetchContent_Declare(googletest
        GIT_REPOSITORY https://github.com/google/googletest.git
        GIT_TAG        v1.15.2)
    set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
    FetchContent_MakeAvailable(googletest)
endif()
include(GoogleTest)

add_executable(test_smoke test_smoke.cpp)
target_link_libraries(test_smoke PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_smoke)

add_executable(test_config test_config.cpp)
target_link_libraries(test_config PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_config)

add_executable(test_errors test_errors.cpp)
target_link_libraries(test_errors PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_errors)

add_executable(test_filters test_filters.cpp)
target_link_libraries(test_filters PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_filters)

add_executable(test_db_gateway test_db_gateway.cpp)
target_link_libraries(test_db_gateway PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_db_gateway)

add_executable(test_settings test_settings.cpp)
target_link_libraries(test_settings PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_settings)

add_executable(test_stores test_stores.cpp)
target_link_libraries(test_stores PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_stores)

add_executable(test_registry test_registry.cpp)
target_link_libraries(test_registry PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_registry)

add_executable(test_embeddings test_embeddings.cpp)
target_link_libraries(test_embeddings PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_embeddings)

add_executable(test_apikey test_apikey.cpp)
target_link_libraries(test_apikey PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_apikey)

add_executable(test_auth test_auth.cpp)
target_link_libraries(test_auth PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_auth)

add_executable(test_rate_limiter test_rate_limiter.cpp)
target_link_libraries(test_rate_limiter PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_rate_limiter)

add_executable(test_captcha test_captcha.cpp)
target_link_libraries(test_captcha PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_captcha)

add_executable(test_api_integration test_api_integration.cpp)
target_link_libraries(test_api_integration PRIVATE vectorapi_core GTest::gtest_main)
target_compile_definitions(test_api_integration PRIVATE SVAPI_API_DOCS_DIR="${CMAKE_SOURCE_DIR}/api")
gtest_discover_tests(test_api_integration)

add_executable(test_project_settings test_project_settings.cpp)
target_link_libraries(test_project_settings PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_project_settings)

add_executable(test_outbound_http test_outbound_http.cpp)
target_link_libraries(test_outbound_http PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_outbound_http)

add_executable(test_principal test_principal.cpp)
target_link_libraries(test_principal PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_principal)

# The two function-runtime units below are pure: no database client, no I/O. They
# compile their sources directly instead of linking vectorapi_core, so these two
# targets still build and run on a host where smartbotic-db-client cannot compile
# (see CLAUDE.md, "The Void dev host can no longer build the C++ natively").
add_executable(test_functions_descriptor
    test_functions_descriptor.cpp
    ${CMAKE_SOURCE_DIR}/src/functions/descriptor.cpp
    ${CMAKE_SOURCE_DIR}/src/functions/cron.cpp
    ${CMAKE_SOURCE_DIR}/src/functions/timezone.cpp)
target_include_directories(test_functions_descriptor PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_functions_descriptor PRIVATE
    nlohmann_json::nlohmann_json GTest::gtest_main)
gtest_discover_tests(test_functions_descriptor)

add_executable(test_functions_net_guard
    test_functions_net_guard.cpp
    ${CMAKE_SOURCE_DIR}/src/functions/net_guard.cpp)
target_include_directories(test_functions_net_guard PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_functions_net_guard PRIVATE GTest::gtest_main)
gtest_discover_tests(test_functions_net_guard)

# Pure half of the function registry (record shape + the code-push merge rule);
# no database client, so it builds on the Void dev host too.
add_executable(test_function_record
    test_function_record.cpp
    ${CMAKE_SOURCE_DIR}/src/function_record.cpp
    ${CMAKE_SOURCE_DIR}/src/apikey.cpp)
target_include_directories(test_function_record PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_function_record PRIVATE
    nlohmann_json::nlohmann_json OpenSSL::Crypto GTest::gtest_main)
gtest_discover_tests(test_function_record)

# The run-log store against a link-time fake database: the test defines the few
# Client members the store calls, so this target must NOT link vectorapi_core,
# which would bring the real client back and fail to link on the dev host. It
# needs the client's headers only.
add_executable(test_function_run_log
    test_function_run_log.cpp
    ${CMAKE_SOURCE_DIR}/src/function_run_log.cpp)
target_include_directories(test_function_run_log PRIVATE
    ${CMAKE_SOURCE_DIR}/src
    $<TARGET_PROPERTY:smartbotic::db-client,INTERFACE_INCLUDE_DIRECTORIES>)
target_link_libraries(test_function_run_log PRIVATE
    nlohmann_json::nlohmann_json GTest::gtest_main)
gtest_discover_tests(test_function_run_log)

# RunnerPool talks to nothing but httplib, so this target compiles its source
# directly rather than linking vectorapi_core, and still builds and runs on a
# host where smartbotic-db-client cannot compile.
add_executable(test_runner_client
    test_runner_client.cpp
    ${CMAKE_SOURCE_DIR}/src/runner_client.cpp)
target_include_directories(test_runner_client PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_compile_definitions(test_runner_client PRIVATE CPPHTTPLIB_OPENSSL_SUPPORT)
target_link_libraries(test_runner_client PRIVATE
    httplib::httplib nlohmann_json::nlohmann_json spdlog::spdlog
    OpenSSL::SSL OpenSSL::Crypto GTest::gtest_main)
gtest_discover_tests(test_runner_client)

# The cron engine: pure, so it builds and runs on the dev host. It is also what
# descriptor.cpp validates schedules with, so a schedule that stores and a
# schedule that fires cannot disagree.
add_executable(test_functions_cron
    test_functions_cron.cpp
    ${CMAKE_SOURCE_DIR}/src/functions/cron.cpp
    ${CMAKE_SOURCE_DIR}/src/functions/timezone.cpp)
target_include_directories(test_functions_cron PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_functions_cron PRIVATE GTest::gtest_main)
gtest_discover_tests(test_functions_cron)

# The IANA time zone seam. Pure apart from the host tz database, which is a
# declared runtime dependency of the packages.
add_executable(test_functions_timezone
    test_functions_timezone.cpp
    ${CMAKE_SOURCE_DIR}/src/functions/timezone.cpp)
target_include_directories(test_functions_timezone PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_functions_timezone PRIVATE GTest::gtest_main)
gtest_discover_tests(test_functions_timezone)

# Scheduler and event-watcher DECISIONS: pure, so no database client.
add_executable(test_function_scheduler
    test_function_scheduler.cpp
    ${CMAKE_SOURCE_DIR}/src/functions/descriptor.cpp
    ${CMAKE_SOURCE_DIR}/src/functions/cron.cpp
    ${CMAKE_SOURCE_DIR}/src/functions/timezone.cpp)
target_include_directories(test_function_scheduler PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_function_scheduler PRIVATE
    nlohmann_json::nlohmann_json GTest::gtest_main)
gtest_discover_tests(test_function_scheduler)

# The job queue against a link-time fake database: this target defines the few
# Client members the queue calls, including the version read and the
# compare-and-set that make claiming atomic, so it must NOT link vectorapi_core.
add_executable(test_function_jobs
    test_function_jobs.cpp
    ${CMAKE_SOURCE_DIR}/src/function_jobs.cpp
    ${CMAKE_SOURCE_DIR}/src/apikey.cpp)
target_include_directories(test_function_jobs PRIVATE
    ${CMAKE_SOURCE_DIR}/src
    $<TARGET_PROPERTY:smartbotic::db-client,INTERFACE_INCLUDE_DIRECTORIES>)
target_link_libraries(test_function_jobs PRIVATE
    nlohmann_json::nlohmann_json OpenSSL::Crypto GTest::gtest_main)
gtest_discover_tests(test_function_jobs)

# Secret name rules and the API view that must never carry a value.
add_executable(test_project_secrets
    test_project_secrets.cpp
    ${CMAKE_SOURCE_DIR}/src/project_secrets.cpp)
target_include_directories(test_project_secrets PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_project_secrets PRIVATE nlohmann_json::nlohmann_json GTest::gtest_main)
gtest_discover_tests(test_project_secrets)

# The secret store, same link-time fake database approach.
add_executable(test_project_secret_store
    test_project_secret_store.cpp
    ${CMAKE_SOURCE_DIR}/src/project_secrets.cpp
    ${CMAKE_SOURCE_DIR}/src/project_secret_store.cpp)
target_include_directories(test_project_secret_store PRIVATE
    ${CMAKE_SOURCE_DIR}/src
    $<TARGET_PROPERTY:smartbotic::db-client,INTERFACE_INCLUDE_DIRECTORIES>)
target_link_libraries(test_project_secret_store PRIVATE
    nlohmann_json::nlohmann_json GTest::gtest_main)
gtest_discover_tests(test_project_secret_store)
