cmake_minimum_required(VERSION 3.26)

include(CTest)
enable_testing()

# Find or fetch Google Test
find_package(GTest QUIET)
if(NOT GTest_FOUND)
    include(FetchContent)
    FetchContent_Declare(
        googletest
        GIT_REPOSITORY https://github.com/google/googletest.git
        GIT_TAG        v1.14.0
    )
    set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
    FetchContent_MakeAvailable(googletest)
endif()

# ── AuthData & record integrity tests ──
add_executable(test_authdata test_authdata.cpp)
target_include_directories(test_authdata PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_authdata PRIVATE
    authobj
    GTest::gtest_main
)
gtest_discover_tests(test_authdata)

# ── Session (local + cluster) tests ──
add_executable(test_session test_session.cpp)
target_include_directories(test_session PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_session PRIVATE
    authobj
    GTest::gtest_main
)
gtest_discover_tests(test_session)

# ── User / Group / GPO data integrity tests ──
add_executable(test_entities test_entities.cpp)
target_include_directories(test_entities PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_entities PRIVATE
    authobj
    GTest::gtest_main
)
gtest_discover_tests(test_entities)

# ── Backend storage tests (file-backed) ──
add_executable(test_backend test_backend.cpp)
target_include_directories(test_backend PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_backend PRIVATE
    authobj
    GTest::gtest_main
)
gtest_discover_tests(test_backend)

# ── SHA512 hash tests ──
add_executable(test_hash test_hash.cpp)
target_include_directories(test_hash PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_hash PRIVATE
    authobj
    GTest::gtest_main
)
gtest_discover_tests(test_hash)

# ── RecordIndex tests ──
add_executable(test_index test_index.cpp)
target_include_directories(test_index PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_index PRIVATE
    authobj
    GTest::gtest_main
)
gtest_discover_tests(test_index)

# ── Performance / runtime benchmarks (memory, storage, access times) ──
add_executable(test_performance test_performance.cpp)
target_include_directories(test_performance PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_performance PRIVATE
    authobj
    GTest::gtest_main
)
gtest_discover_tests(test_performance)

# ── Cluster-mode performance benchmarks ──
add_executable(test_cluster_performance test_cluster_performance.cpp)
target_include_directories(test_cluster_performance PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_cluster_performance PRIVATE
    authobj
    GTest::gtest_main
)
gtest_discover_tests(test_cluster_performance)

# ── Cluster operations (scrub, replicate, etc) ──
add_executable(test_cluster_ops test_cluster_ops.cpp)
target_include_directories(test_cluster_ops PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_cluster_ops PRIVATE
    authobj
    GTest::gtest_main
)
gtest_discover_tests(test_cluster_ops)
