# Tests for smartbotic-database

# Vector storage integration test
# Compiles memory_store.cpp directly — no dependency on the full service executable.
set(TEST_VECTOR_SOURCES
    test_vector_storage.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/memory_store.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/config/collection_config_manager.cpp
)

add_executable(test_vector_storage ${TEST_VECTOR_SOURCES})

target_include_directories(test_vector_storage PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src
)

# nlohmann/json
if(TARGET nlohmann_json::nlohmann_json)
    target_link_libraries(test_vector_storage PRIVATE nlohmann_json::nlohmann_json)
else()
    target_include_directories(test_vector_storage PRIVATE ${NLOHMANN_JSON_INCLUDE_DIRS})
endif()

# spdlog
if(TARGET spdlog::spdlog)
    target_link_libraries(test_vector_storage PRIVATE spdlog::spdlog)
else()
    target_link_libraries(test_vector_storage PRIVATE ${SPDLOG_LIBRARIES})
    target_include_directories(test_vector_storage PRIVATE ${SPDLOG_INCLUDE_DIRS})
endif()

# Threads (required by MemoryStore background threads)
find_package(Threads REQUIRED)
target_link_libraries(test_vector_storage PRIVATE Threads::Threads)

# View projection unit test
set(TEST_VIEWS_SOURCES
    test_views.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/views/projection.cpp
)

add_executable(test_views ${TEST_VIEWS_SOURCES})

target_include_directories(test_views PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src
)

# nlohmann/json
if(TARGET nlohmann_json::nlohmann_json)
    target_link_libraries(test_views PRIVATE nlohmann_json::nlohmann_json)
else()
    target_include_directories(test_views PRIVATE ${NLOHMANN_JSON_INCLUDE_DIRS})
endif()

# Per-collection timestamp precision test
set(TEST_TIMESTAMP_PRECISION_SOURCES
    test_timestamp_precision.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/memory_store.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/config/collection_config_manager.cpp
)

add_executable(test_timestamp_precision ${TEST_TIMESTAMP_PRECISION_SOURCES})

target_include_directories(test_timestamp_precision PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src
)

if(TARGET nlohmann_json::nlohmann_json)
    target_link_libraries(test_timestamp_precision PRIVATE nlohmann_json::nlohmann_json)
else()
    target_include_directories(test_timestamp_precision PRIVATE ${NLOHMANN_JSON_INCLUDE_DIRS})
endif()

if(TARGET spdlog::spdlog)
    target_link_libraries(test_timestamp_precision PRIVATE spdlog::spdlog)
else()
    target_link_libraries(test_timestamp_precision PRIVATE ${SPDLOG_LIBRARIES})
    target_include_directories(test_timestamp_precision PRIVATE ${SPDLOG_INCLUDE_DIRS})
endif()

find_package(Threads REQUIRED)
target_link_libraries(test_timestamp_precision PRIVATE Threads::Threads)

# Snapshot durability + fallback integration test
# Exercises SnapshotManager directly; pulls in memory_store.cpp, snapshot.cpp,
# and wal.cpp (for the crc32 helpers used by snapshot.cpp).
set(TEST_SNAPSHOT_DURABILITY_SOURCES
    test_snapshot_durability.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/memory_store.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/config/collection_config_manager.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/persistence/snapshot.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/persistence/wal.cpp
)

add_executable(test_snapshot_durability ${TEST_SNAPSHOT_DURABILITY_SOURCES})

target_include_directories(test_snapshot_durability PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src
)

if(TARGET nlohmann_json::nlohmann_json)
    target_link_libraries(test_snapshot_durability PRIVATE nlohmann_json::nlohmann_json)
else()
    target_include_directories(test_snapshot_durability PRIVATE ${NLOHMANN_JSON_INCLUDE_DIRS})
endif()

if(TARGET spdlog::spdlog)
    target_link_libraries(test_snapshot_durability PRIVATE spdlog::spdlog)
else()
    target_link_libraries(test_snapshot_durability PRIVATE ${SPDLOG_LIBRARIES})
    target_include_directories(test_snapshot_durability PRIVATE ${SPDLOG_INCLUDE_DIRS})
endif()

find_package(Threads REQUIRED)
target_link_libraries(test_snapshot_durability PRIVATE Threads::Threads)

# LZ4 — snapshot.cpp #includes <lz4.h> unless DISABLE_LZ4 is defined, so link it.
find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
    pkg_check_modules(TEST_LZ4 QUIET liblz4)
endif()
if(TEST_LZ4_FOUND)
    target_link_libraries(test_snapshot_durability PRIVATE ${TEST_LZ4_LIBRARIES})
    target_include_directories(test_snapshot_durability PRIVATE ${TEST_LZ4_INCLUDE_DIRS})
else()
    # Fallback: assume liblz4 is installed at a standard location
    target_link_libraries(test_snapshot_durability PRIVATE lz4)
endif()

# Register with CTest
enable_testing()
add_test(NAME vector_storage COMMAND test_vector_storage)
add_test(NAME views COMMAND test_views)
add_test(NAME timestamp_precision COMMAND test_timestamp_precision)
add_test(NAME snapshot_durability COMMAND test_snapshot_durability)
