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

# The index request grammar: pure, so it builds where the database client cannot.
add_executable(test_index_request test_index_request.cpp)
target_include_directories(test_index_request PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_index_request PRIVATE nlohmann_json::nlohmann_json GTest::gtest_main)
gtest_discover_tests(test_index_request)

# Header-only and pure, so it links neither the database client nor httplib.
add_executable(test_embed_budget test_embed_budget.cpp)
target_include_directories(test_embed_budget PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_embed_budget PRIVATE GTest::gtest_main)
gtest_discover_tests(test_embed_budget)

# fileNameRejection() is pure, but file_store.cpp also holds the store, which
# links the database client - so this links vectorapi_core like the rest.
# The database-failure mapping, driven through the FailureDetail overload: plain
# data in, ApiError out, so no database is needed.
add_executable(test_db_errors test_db_errors.cpp)
target_include_directories(test_db_errors PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_db_errors PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_db_errors)

# Pure, clock-injected: no database and no real sleeping.
add_executable(test_db_await test_db_await.cpp)
target_include_directories(test_db_await PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_db_await PRIVATE GTest::gtest_main)
gtest_discover_tests(test_db_await)

# Pure: no curl, no sockets, no mail server. The awkward cases - a literal
# containing CRLF, parens, NUL - are exactly the ones a real server produces
# and a line-based parser gets wrong.
# Pure header, so it needs no library - but it DOES need nlohmann, because the
# tests cross-check the validator against nlohmann's own dump().
add_executable(test_utf8_text test_utf8_text.cpp)
target_include_directories(test_utf8_text PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_utf8_text PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_utf8_text)

add_executable(test_trigger_body test_trigger_body.cpp)
target_include_directories(test_trigger_body PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_trigger_body PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_trigger_body)

add_executable(test_header_fields test_header_fields.cpp)
target_include_directories(test_header_fields PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_header_fields PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_header_fields)

add_executable(test_known_keys test_known_keys.cpp)
target_include_directories(test_known_keys PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_known_keys PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_known_keys)

add_executable(test_query_params test_query_params.cpp)
target_include_directories(test_query_params PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_query_params PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_query_params)

add_executable(test_vector_scan test_vector_scan.cpp)
target_include_directories(test_vector_scan PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_vector_scan PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_vector_scan)

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

# RFC 2047, lifted with the code from the sibling project and ported from its
# hand-rolled main() to gtest. Every case the original covered is kept: the
# value of taking a tested component is the tests.
add_executable(test_mime_words test_mime_words.cpp)
target_include_directories(test_mime_words PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_mime_words PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_mime_words)

add_executable(test_file_name test_file_name.cpp)
target_include_directories(test_file_name PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_file_name PRIVATE vectorapi_core GTest::gtest_main)
gtest_discover_tests(test_file_name)

# Document body shaping and the increment deltas: header-only and pure.
add_executable(test_invoke_envelope test_invoke_envelope.cpp)
target_include_directories(test_invoke_envelope PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_invoke_envelope PRIVATE nlohmann_json::nlohmann_json GTest::gtest_main)
gtest_discover_tests(test_invoke_envelope)

add_executable(test_documents_util test_documents_util.cpp)
target_include_directories(test_documents_util PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_documents_util PRIVATE nlohmann_json::nlohmann_json GTest::gtest_main)
gtest_discover_tests(test_documents_util)
