# 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)
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)
