set(test_prefix memgraph__unit__)

find_package(fmt REQUIRED)
find_package(Threads REQUIRED)
find_package(antlr4-runtime REQUIRED)

include_directories(${antlr4-runtime_INCLUDE_DIR})

if(NOT TARGET memgraph__unit)
    add_custom_target(memgraph__unit)
endif()

add_library(memgraph_unit_main OBJECT main.cpp)
target_link_libraries(memgraph_unit_main mg-memory mg-utils GTest::gtest GTest::gmock Threads::Threads dl atomic)
target_precompile_headers(memgraph_unit_main PRIVATE
    <gtest/gtest.h>
)



# Test utilities
add_library(storage_test_utils storage_test_utils.cpp)
target_link_libraries(storage_test_utils mg::storage)

add_library(disk_test_utils disk_test_utils.cpp)
target_link_libraries(disk_test_utils mg::storage)

# Header-only helpers shared across storage test files
# (parameterized DDL abort/ghost/restore tests).
add_library(ddl_abort_helpers INTERFACE)
target_sources(ddl_abort_helpers INTERFACE ddl_abort_helpers.hpp)
target_link_libraries(ddl_abort_helpers INTERFACE mg::storage GTest::gtest)
target_include_directories(ddl_abort_helpers INTERFACE ${CMAKE_SOURCE_DIR})

find_package(Boost REQUIRED CONFIG COMPONENTS filesystem)

# Test integrations-kafka
add_library(kafka-mock STATIC kafka_mock.cpp)
target_link_libraries(kafka-mock mg-utils RdKafka::rdkafka++ Threads::Threads GTest::gtest)

# Include directories are intentionally not set, because kafka-mock isn't meant to be used apart from unit tests
add_unit_test(integrations_kafka_consumer
    SOURCES integrations_kafka_consumer.cpp kafka_mock.cpp
    LINK_TARGETS kafka-mock mg-integrations-kafka
)

add_unit_test(mgp_kafka_c_api
    SOURCES mgp_kafka_c_api.cpp
    LINK_TARGETS mg-query mg-integrations-kafka
)

add_unit_test(mgp_trans_c_api
    SOURCES mgp_trans_c_api.cpp
    LINK_TARGETS mg-query
)

# Test mg-query
add_unit_test(bfs_single_node
    SOURCES bfs_single_node.cpp
    LINK_TARGETS mg-query mg-glue disk_test_utils
)

add_unit_test(bfs_fine_grained
    SOURCES bfs_fine_grained.cpp
    LINK_TARGETS mg-query mg-glue disk_test_utils
)

add_unit_test(crc_accumulator
        SOURCES crc_accumulator.cpp
        LINK_TARGETS mg-utils
)

add_unit_test(kshortest_fine_grained
    SOURCES kshortest_fine_grained.cpp
    LINK_TARGETS mg-query mg-glue disk_test_utils
)

add_unit_test(kshortest_general
    SOURCES kshortest_general.cpp
    LINK_TARGETS mg-query mg-glue disk_test_utils
)

add_unit_test(cypher_main_visitor
    SOURCES cypher_main_visitor.cpp
    LINK_TARGETS mg-query
)

add_unit_test(event_map
        SOURCES event_map.cpp
        LINK_TARGETS mg-events mg-utils
)


add_unit_test(interpreter
    SOURCES interpreter.cpp ${CMAKE_SOURCE_DIR}/src/glue/communication.cpp
    LINK_TARGETS mg-communication mg-query mg-glue disk_test_utils
)

add_unit_test(query_plan_pruning_bfs
    SOURCES query_plan_pruning_bfs.cpp
    LINK_TARGETS mg-query
)

add_unit_test(plan_pretty_print
    SOURCES plan_pretty_print.cpp
    LINK_TARGETS mg-query disk_test_utils
)

add_unit_test(query_cost_estimator
    SOURCES query_cost_estimator.cpp
    LINK_TARGETS mg-query
)

add_unit_test(glue_communication
    SOURCES glue_communication.cpp
    LINK_TARGETS mg-query mg-glue disk_test_utils
)

add_unit_test(query_dump
    SOURCES query_dump.cpp ${CMAKE_SOURCE_DIR}/src/glue/communication.cpp
    LINK_TARGETS mg-communication mg-query disk_test_utils
)

add_unit_test(query_expression_evaluator
    SOURCES query_expression_evaluator.cpp
    LINK_TARGETS mg-query disk_test_utils
)

add_unit_test(query_frame_change
    SOURCES query_frame_change.cpp
    LINK_TARGETS mg-query
)

add_unit_test(query_cache
    SOURCES query_cache.cpp
    LINK_TARGETS mg-query
)

add_unit_test(query_plan
    SOURCES query_plan.cpp
    LINK_TARGETS mg-query
)

add_unit_test(query_plan_orderby_index
    SOURCES query_plan_orderby_index.cpp
    LINK_TARGETS mg-query
)

add_unit_test(query_parallel_plan
    SOURCES query_parallel_plan.cpp
    LINK_TARGETS mg-query
)

add_unit_test(query_plan_accumulate_aggregate
    SOURCES query_plan_accumulate_aggregate.cpp
    LINK_TARGETS mg-query mg-glue disk_test_utils
)

add_unit_test(query_plan_bag_semantics
    SOURCES query_plan_bag_semantics.cpp
    LINK_TARGETS mg-query mg-glue disk_test_utils
)

add_unit_test(query_plan_create_set_remove_delete
    SOURCES query_plan_create_set_remove_delete.cpp
    LINK_TARGETS mg-query mg-glue disk_test_utils
)

add_unit_test(query_plan_edge_cases
    SOURCES query_plan_edge_cases.cpp ${CMAKE_SOURCE_DIR}/src/glue/communication.cpp
    LINK_TARGETS mg-communication mg-query disk_test_utils
)

add_unit_test(query_plan_match_filter_return
    SOURCES query_plan_match_filter_return.cpp
    LINK_TARGETS mg-query mg-glue disk_test_utils
)

add_unit_test(query_plan_operator_to_string
    SOURCES query_plan_operator_to_string.cpp
    LINK_TARGETS mg-query disk_test_utils
)

add_unit_test(query_plan_read_write_typecheck
    SOURCES query_plan_read_write_typecheck.cpp ${CMAKE_SOURCE_DIR}/src/query/plan/read_write_type_checker.cpp
    LINK_TARGETS mg-query disk_test_utils
)

# ============================================================================
# plan_v2 unit tests - Layer 1: Pure algebra (no egraph, no planner)
# ============================================================================
add_unit_test(query_plan_v2_variable_set
    SOURCES query_plan_v2_variable_set.cpp
    LINK_TARGETS mg-query mg-planner mg::query::plan_v2::test_support
)

add_unit_test(query_plan_v2_algebra
    SOURCES query_plan_v2_algebra.cpp
    LINK_TARGETS mg-query mg-planner mg::query::plan_v2::test_support
)

# ============================================================================
# plan_v2 unit tests - Layer 2: egraph -> v1 reconstruction + resolver semantics
# ============================================================================
add_unit_test(query_plan_v2_converter
    SOURCES query_plan_v2_converter.cpp
    LINK_TARGETS mg-query mg-planner mg::query::plan_v2::test_support
)

# ============================================================================
# plan_v2 unit tests - Layer 3: Cardinality estimation (no full pipeline)
# ============================================================================
add_unit_test(query_plan_v2_cardinality
    SOURCES query_plan_v2_cardinality.cpp
    LINK_TARGETS mg-query mg-planner mg::query::plan_v2::test_support
)

# ============================================================================
# plan_v2 unit tests - Layer 3: Analysis framework (constant identity + lattice)
# ============================================================================
add_unit_test(query_plan_v2_analysis
    SOURCES query_plan_v2_analysis.cpp
    LINK_TARGETS mg-query mg-planner
)

# ============================================================================
# plan_v2 unit tests - Layer 3: Full extraction pipeline + rewrites
# ============================================================================
add_unit_test(query_plan_v2_rewrites
    SOURCES query_plan_v2_rewrites.cpp
    LINK_TARGETS mg-query mg-planner
)

add_unit_test(query_plan_v2_pipeline
    SOURCES query_plan_v2_pipeline.cpp
    LINK_TARGETS mg-query mg-planner
)

# ============================================================================
# plan_v2 unit tests - Layer 4: End-to-end DML (CREATE/SET/REMOVE/DELETE)
# ============================================================================
add_unit_test(query_plan_v2_create_set_remove_delete
    SOURCES query_plan_v2_create_set_remove_delete.cpp
    LINK_TARGETS mg-query mg-glue disk_test_utils
)

add_unit_test(query_pretty_print
    SOURCES query_pretty_print.cpp
    LINK_TARGETS mg-query disk_test_utils
)

add_unit_test(query_trigger
    SOURCES query_trigger.cpp
    LINK_TARGETS mg-query mg-glue disk_test_utils
)

add_unit_test(query_serialization_property_value
    SOURCES query_serialization_property_value.cpp
    LINK_TARGETS mg-query
)

add_unit_test(query_streams
    SOURCES query_streams.cpp
    LINK_TARGETS mg-query kafka-mock disk_test_utils
)

add_unit_test(transaction_queue
    SOURCES transaction_queue.cpp
    LINK_TARGETS mg-communication mg-query mg-glue disk_test_utils
)

add_unit_test(transaction_queue_multiple
    SOURCES transaction_queue_multiple.cpp
    LINK_TARGETS mg-communication mg-query mg-glue disk_test_utils
)

add_unit_test(query_plan_printer_race
    SOURCES query_plan_printer_race.cpp
    LINK_TARGETS mg-communication mg-query mg-glue disk_test_utils
)


# Test query functions
add_unit_test(query_function_mgp_module
    SOURCES query_function_mgp_module.cpp
    LINK_TARGETS mg-query
    INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
)


# Test query/procedure
add_unit_test(query_procedure_mgp_type
    SOURCES query_procedure_mgp_type.cpp
    LINK_TARGETS mg-query disk_test_utils
    INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
)


add_unit_test(query_procedure_mgp_module
    SOURCES query_procedure_mgp_module.cpp
    LINK_TARGETS mg-query
    INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
)


if (MG_PYTHON_SUPPORT)
add_unit_test(query_procedure_py_module
    SOURCES query_procedure_py_module.cpp
    LINK_TARGETS mg-query disk_test_utils
    CUSTOM_MAIN
    INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
)
endif()


add_unit_test(query_procedures_mgp_graph
    SOURCES query_procedures_mgp_graph.cpp
    LINK_TARGETS mg-query storage_test_utils disk_test_utils
    INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
)

add_unit_test(query_virtual_graph
    SOURCES query_virtual_graph.cpp
    LINK_TARGETS mg-query
    INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
)

add_unit_test(query_procedure_text_search_query_fields
    SOURCES query_procedure_text_search_query_fields.cpp
    INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
)


# END query/procedure
add_unit_test(query_profile
    SOURCES query_profile.cpp
    LINK_TARGETS mg-query
)

add_unit_test(query_graph_free
    SOURCES query_graph_free.cpp
    LINK_TARGETS mg-query
)

add_unit_test(query_required_privileges
    SOURCES query_required_privileges.cpp
    LINK_TARGETS mg-query
)

add_unit_test(query_semantic
    SOURCES query_semantic.cpp
    LINK_TARGETS mg-query disk_test_utils
)

add_unit_test(query_variable_start_planner
    SOURCES query_variable_start_planner.cpp
    LINK_TARGETS mg-query mg-glue disk_test_utils
)

add_unit_test(stripped
    SOURCES stripped.cpp
    LINK_TARGETS mg-query
)

add_unit_test(typed_value
    SOURCES typed_value.cpp
    LINK_TARGETS mg-query disk_test_utils
)

# Test mg-communication
add_unit_test(bolt_chunked_decoder_buffer
    SOURCES bolt_chunked_decoder_buffer.cpp
    LINK_TARGETS mg-communication
)

add_unit_test(bolt_chunked_encoder_buffer
    SOURCES bolt_chunked_encoder_buffer.cpp
    LINK_TARGETS mg-communication
)

add_unit_test(bolt_decoder
    SOURCES bolt_decoder.cpp
    LINK_TARGETS mg-communication
)

add_unit_test(bolt_encoder
    SOURCES bolt_encoder.cpp ${CMAKE_SOURCE_DIR}/src/glue/communication.cpp
    LINK_TARGETS mg-communication mg-query disk_test_utils
)

add_unit_test(bolt_session
    SOURCES bolt_session.cpp
    LINK_TARGETS mg-communication mg-utils mg-storage-v2
)

add_unit_test(communication_buffer
    SOURCES communication_buffer.cpp
    LINK_TARGETS mg-communication mg-utils
)

add_unit_test(network_timeouts
    SOURCES network_timeouts.cpp
    LINK_TARGETS mg-communication
)

# Test mg-kvstore
add_unit_test(kvstore
    SOURCES kvstore.cpp
    LINK_TARGETS mg-kvstore mg-utils
)

if (MG_ENTERPRISE)
add_unit_test(tenant_profiles
    SOURCES tenant_profiles.cpp ${CMAKE_SOURCE_DIR}/src/dbms/tenant_profiles.cpp
    LINK_TARGETS mg-kvstore mg-utils nlohmann_json::nlohmann_json
)
endif ()

# Test data structures
add_unit_test(ring_buffer
    SOURCES ring_buffer.cpp
    LINK_TARGETS mg-utils
)

# Test mg-io
add_unit_test(network_endpoint
    SOURCES network_endpoint.cpp
    LINK_TARGETS mg-io
)

add_unit_test(network_utils
    SOURCES network_utils.cpp
    LINK_TARGETS mg-io mg-utils
)

add_unit_test(socket
    SOURCES socket.cpp
    LINK_TARGETS mg-utils mg-io mg-events
)

# Test mg-utils
add_unit_test(utils_algorithm
    SOURCES utils_algorithm.cpp
    LINK_TARGETS mg-utils
)

# Hot/cold tenants: gatekeeper 4-state lifecycle machine
add_unit_test(hot_cold_gatekeeper
    SOURCES hot_cold_gatekeeper.cpp
    LINK_TARGETS mg-utils
)

# Hot/cold tenants: node-local suspend engine (HOT -> COLD teardown)
add_unit_test(hot_cold_suspend
    SOURCES hot_cold_suspend.cpp
    LINK_TARGETS mg-query mg-auth mg-glue mg-dbms
)

# Hot/cold tenants: node-local resume engine (COLD -> HOT, single-flight)
add_unit_test(hot_cold_resume
    SOURCES hot_cold_resume.cpp
    LINK_TARGETS mg-query mg-auth mg-glue mg-dbms
)


add_unit_test(utils_aws
    SOURCES utils_aws.cpp
    LINK_TARGETS mg-utils mg-aws
)

# Every iteration starts a thread per barrier participant, so ctest's default cost of one slot per
# test lets it be scheduled alongside a full machine's worth of other tests. Declaring the slots it
# needs keeps that from happening; where the machine has fewer, ctest runs this test on its own.
# PROCESSORS mirrors kN in utils_barrier.cpp and has to be changed with it.
add_unit_test(utils_barrier
    SOURCES utils_barrier.cpp
    LINK_TARGETS mg-utils
    TEST_PROPERTIES TIMEOUT 120 PROCESSORS 12
)

add_unit_test(utils_hot_mask
    SOURCES utils_hot_mask.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_priority_thread_pool
    SOURCES utils_priority_thread_pool.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_epoch_tracker
    SOURCES utils_epoch_tracker.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_exceptions
    SOURCES utils_exceptions.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_file
    SOURCES utils_file.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_page_cache_releaser
    SOURCES utils_page_cache_releaser.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_stat
    SOURCES utils_stat.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_math
    SOURCES utils_math.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_memory
    SOURCES utils_memory.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_memory_thread_safe
    SOURCES utils_memory_thread_safe.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_memory_tracker
    SOURCES utils_memory_tracker.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_query_memory_tracker
    SOURCES utils_query_memory_tracker.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(jemalloc_hook_stress_test
    SOURCES jemalloc_hook_stress_test.cpp
    LINK_TARGETS mg-memory mg-utils
)

add_unit_test(tls_destructor_registration
    SOURCES tls_destructor_registration.cpp
    LINK_TARGETS mg-memory mg-utils
)

add_unit_test(utils_on_scope_exit
    SOURCES utils_on_scope_exit.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_pointer_pack
    SOURCES utils_pointer_pack.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_rwlock
    SOURCES utils_rwlock.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_scheduler
    SOURCES utils_scheduler.cpp
    LINK_TARGETS mg-utils mg-flags mg-settings
)

add_unit_test(utils_signals
    SOURCES utils_signals.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_safe_string
    SOURCES utils_safe_string.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_string
    SOURCES utils_string.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_synchronized
    SOURCES utils_synchronized.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_timestamp
    SOURCES utils_timestamp.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(skip_list
    SOURCES skip_list.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(small_vector
    SOURCES small_vector.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_id_bitmap
    SOURCES utils_id_bitmap.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(skip_list_range_chunking
    SOURCES skip_list_range_chunking.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_mask_sensitive_information
    SOURCES utils_mask_sensitive_information.cpp
    LINK_TARGETS mg-utils fmt::fmt
)

add_unit_test(utils_file_locker
    SOURCES utils_file_locker.cpp
    LINK_TARGETS mg-utils fmt::fmt
)

add_unit_test(utils_thread_pool
    SOURCES utils_thread_pool.cpp
    LINK_TARGETS mg-utils fmt::fmt
)

add_unit_test(csv_csv_parsing
    SOURCES csv_csv_parsing.cpp
    LINK_TARGETS mg::csv
)

add_unit_test(query_jsonl_reader
    SOURCES query_jsonl_reader.cpp
    LINK_TARGETS mg-query
)

add_unit_test(utils_prefetched
    SOURCES utils_prefetched.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_byte_source
    SOURCES utils_byte_source.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_download_temp_file
    SOURCES utils_download_temp_file.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(query_download_failure_reporting
    SOURCES query_download_failure_reporting.cpp
    LINK_TARGETS mg-query mg-requests
)

add_unit_test(requests_transfer_stall
    SOURCES requests_transfer_stall.cpp
    LINK_TARGETS mg-requests mg-utils
)

add_unit_test(utils_async_timer
    SOURCES utils_async_timer.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(license
    SOURCES license.cpp
    LINK_TARGETS mg-utils mg-license mg-flags
)

add_unit_test(utils_settings
    SOURCES utils_settings.cpp
    LINK_TARGETS mg-utils mg-settings
)

add_unit_test(utils_shared_quota
    SOURCES utils_shared_quota.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_temporal
    SOURCES utils_temporal.cpp
    LINK_TARGETS mg-utils mg-flags
)

add_unit_test(utils_java_string_formatter
    SOURCES utils_java_string_formatter.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_resource_lock
    SOURCES utils_resource_lock.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(lru_cache
    SOURCES lru_cache.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(utils_static_vector
    SOURCES utils_static_vector.cpp
    LINK_TARGETS mg::utils
)

add_unit_test(utils_stack
    SOURCES utils_stack.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(exponential_backoff
    SOURCES exponential_backoff.cpp
    LINK_TARGETS mg-utils range-v3::range-v3
)

add_unit_test(utils_compressor
    SOURCES utils_compressor.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(file_replication_handler
    SOURCES file_replication_handler.cpp
    LINK_TARGETS mg-rpc mg-slk mg-storage-v2
)

add_unit_test(utils_observer
    SOURCES utils_observer.cpp
    LINK_TARGETS mg-utils
)

# Test mg::storage
add_unit_test(commit_log_v2
    SOURCES commit_log_v2.cpp
    LINK_TARGETS gflags mg-utils mg::storage
)

add_unit_test(property_value_v2
    SOURCES property_value_v2.cpp
    LINK_TARGETS mg::storage mg-utils
)

add_unit_test(storage_v2
    SOURCES storage_v2.cpp
    LINK_TARGETS mg::storage storage_test_utils disk_test_utils
)

add_unit_test(storage_v2_acc
    SOURCES storage_v2_acc.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(storage_v2_chunk_iterator
    SOURCES storage_v2_chunk_iterator.cpp
    LINK_TARGETS mg-query storage_test_utils
)

add_unit_test(db_accessor_chunked_test
    SOURCES db_accessor_chunked_test.cpp
    LINK_TARGETS mg-query storage_test_utils
)

add_unit_test(storage_v2_delta_container
    SOURCES storage_v2_delta_container.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(storage_v2_enum_store
    SOURCES storage_v2_enum_store.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(storage_v2_constraints
    SOURCES storage_v2_constraints.cpp
    LINK_TARGETS mg::storage mg-dbms disk_test_utils ddl_abort_helpers
)

if(ENABLE_JEMALLOC)
    add_unit_test(db_memory_tracking
        SOURCES db_memory_tracking.cpp
        LINK_TARGETS mg::storage mg-dbms
    )
endif()

add_unit_test(storage_v2_decoder_encoder
    SOURCES storage_v2_decoder_encoder.cpp
    LINK_TARGETS mg::storage
)

# Much the slowest test in the suite: it writes and recovers snapshots and WAL
# across every durability mode, and in a sanitized build it runs long enough that
# the general cap would leave it far less headroom than it leaves every other
# test. It keeps the signal settings, so a hang in it still dumps core.
add_unit_test(storage_v2_durability_inmemory
    SOURCES storage_v2_durability_inmemory.cpp
    LINK_TARGETS mg::storage mg-dbms storage_test_utils
    TEST_PROPERTIES TIMEOUT 1800
)

add_unit_test(storage_v2_create_snapshot
    SOURCES storage_v2_create_snapshot.cpp
    LINK_TARGETS mg::storage mg-dbms
)

add_unit_test(storage_v2_recover_snapshot
    SOURCES storage_v2_recover_snapshot.cpp
    LINK_TARGETS mg::storage mg-dbms
)

add_unit_test(storage_v2_retention.
        SOURCES storage_v2_retention.cpp
        LINK_TARGETS mg::storage
)

add_unit_test(storage_rocks
    SOURCES storage_rocks.cpp
    LINK_TARGETS mg::storage disk_test_utils
)

add_unit_test(storage_v2_disk
    SOURCES storage_v2_disk.cpp
    LINK_TARGETS mg::storage disk_test_utils
)

add_unit_test(clearing_old_disk_data
    SOURCES clearing_old_disk_data.cpp
    LINK_TARGETS mg::storage disk_test_utils
)

add_unit_test(storage_v2_edge_inmemory
    SOURCES storage_v2_edge_inmemory.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(storage_v2_light_edges
    SOURCES storage_v2_light_edges.cpp
    LINK_TARGETS mg::storage mg-dbms
)

add_unit_test(storage_v2_edge_ondisk
    SOURCES storage_v2_edge_ondisk.cpp
    LINK_TARGETS mg::storage disk_test_utils
)

add_unit_test(storage_v2_gc
    SOURCES storage_v2_gc.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(storage_v2_gc_index_sweep
    SOURCES storage_v2_gc_index_sweep.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(storage_v2_index_arming
    SOURCES storage_v2_index_arming.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(storage_v2_non_sequential_isolation
    SOURCES storage_v2_non_sequential_isolation.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(storage_v2_delta_correctness
    SOURCES storage_v2_delta_correctness.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(storage_v2_indices
    SOURCES storage_v2_indices.cpp
    LINK_TARGETS mg::storage mg-utils disk_test_utils ddl_abort_helpers
)

add_unit_test(storage_v2_index_entry_lifetime
    SOURCES storage_v2_index_entry_lifetime.cpp
    LINK_TARGETS mg::storage mg-utils
)

add_unit_test(storage_v2_object_removal
    SOURCES storage_v2_object_removal.cpp
    LINK_TARGETS mg::storage mg-utils
)

add_unit_test(storage_v2_name_id_mapper
    SOURCES storage_v2_name_id_mapper.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(storage_v2_property_store
    SOURCES storage_v2_property_store.cpp
    LINK_TARGETS mg::storage
    CUSTOM_MAIN
)

add_unit_test(storage_v2_wal_file
    SOURCES storage_v2_wal_file.cpp
    LINK_TARGETS mg::storage storage_test_utils fmt::fmt
)

add_unit_test(storage_v2_replication
    SOURCES storage_v2_replication.cpp
    LINK_TARGETS mg::storage mg-dbms fmt::fmt mg-repl_coord_glue
)

add_unit_test(storage_v2_error
    SOURCES storage_v2_error.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(storage_v2_isolation_level
    SOURCES storage_v2_isolation_level.cpp
    LINK_TARGETS mg::storage disk_test_utils
)

add_unit_test(storage_v2_show_storage_info
    SOURCES storage_v2_show_storage_info.cpp
    LINK_TARGETS mg::storage disk_test_utils
)

add_unit_test(storage_v2_get_info
    SOURCES storage_v2_get_info.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(database_get_info
    SOURCES database_get_info.cpp
    LINK_TARGETS mg::storage mg-glue mg-dbms disk_test_utils
)

add_unit_test(storage_v2_storage_mode
    SOURCES storage_v2_storage_mode.cpp
    LINK_TARGETS mg::storage storage_test_utils mg-query mg-glue
)

add_unit_test(storage_v2_schema_info
    SOURCES storage_v2_schema_info.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(storage_v2_config
    SOURCES storage_v2_config.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(replication_server
    SOURCES replication_server.cpp
    LINK_TARGETS mg::storage Boost::filesystem
)

add_custom_target(memgraph__unit__tls_certs_dir ALL
     COMMAND ${CMAKE_COMMAND} -E copy_directory
     ${CMAKE_CURRENT_SOURCE_DIR}/tls_certs
     ${CMAKE_CURRENT_BINARY_DIR}/tls_certs
     DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tls_certs)
 add_dependencies(memgraph__unit memgraph__unit__tls_certs_dir)
 add_dependencies(${test_unit_prefix}replication_server memgraph__unit__tls_certs_dir)

add_unit_test(cluster_client_ssl
    SOURCES cluster_client_ssl.cpp
    LINK_TARGETS mg-communication mg::io mg::utils Boost::filesystem
)
add_dependencies(${test_unit_prefix}cluster_client_ssl memgraph__unit__tls_certs_dir)

add_unit_test(tls_min_version
    SOURCES tls_min_version.cpp
    LINK_TARGETS mg-communication mg::io mg::utils Boost::filesystem
)
add_dependencies(${test_unit_prefix}tls_min_version memgraph__unit__tls_certs_dir)

if (MG_ENTERPRISE)
    add_unit_test(management_servers
        SOURCES management_servers.cpp
        LINK_TARGETS mg-coordination mg-repl_coord_glue mg-rpc Boost::filesystem
    )
    add_dependencies(${test_unit_prefix}management_servers memgraph__unit__tls_certs_dir)
endif ()



add_unit_test(replication_persistence_helper
    SOURCES replication_persistence_helper.cpp
    LINK_TARGETS mg::storage mg-repl_coord_glue
)

add_unit_test(auth_checker
    SOURCES auth_checker.cpp
    LINK_TARGETS mg-glue mg-auth mg-query disk_test_utils
)

add_unit_test(auth_handler
    SOURCES auth_handler.cpp
    LINK_TARGETS mg-glue mg-auth mg-query
)

if (MG_ENTERPRISE)
    add_unit_test(auth_models
        SOURCES auth_models.cpp
        LINK_TARGETS mg-glue mg-auth
    )
else ()
    add_unit_test(auth_models_comm
        SOURCES auth_models_comm.cpp
        LINK_TARGETS mg-glue mg-auth
    )
endif ()

if (MG_ENTERPRISE)
    add_unit_test(coordinator_sso_authenticator
        SOURCES coordinator_sso_authenticator.cpp
        LINK_TARGETS mg-glue mg-auth
    )
endif ()

add_unit_test(cpp_api
    SOURCES cpp_api.cpp
    LINK_TARGETS mg-query disk_test_utils
    INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
)


add_unit_test(vector_index
    SOURCES vector_index.cpp
    LINK_TARGETS mg::storage mg-glue ddl_abort_helpers
)

add_unit_test(vector_edge_index
    SOURCES vector_edge_index.cpp
    LINK_TARGETS mg::storage mg-glue ddl_abort_helpers
)

add_unit_test(text_index
    SOURCES text_index.cpp
    LINK_TARGETS mg::storage ddl_abort_helpers
)

# Test mg-auth
if (MG_ENTERPRISE)
    add_unit_test(auth
        SOURCES auth.cpp
        LINK_TARGETS mg-auth mg-license Boost::filesystem
    )

    file(GLOB _auth_kvstore_files "${CMAKE_CURRENT_SOURCE_DIR}/auth_kvstore/*")
    mg_add_stamped_custom_target(memgraph__unit__auth_kvstore_dir ALL
        STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}
        COMMANDS COMMAND ${CMAKE_COMMAND} -E copy_directory
            ${CMAKE_CURRENT_SOURCE_DIR}/auth_kvstore
            ${CMAKE_CURRENT_BINARY_DIR}/auth_kvstore
        DEPENDS ${_auth_kvstore_files})
    add_dependencies(memgraph__unit memgraph__unit__auth_kvstore_dir)
    add_dependencies(${test_unit_prefix}auth memgraph__unit__auth_kvstore_dir)
endif ()

# Test mg-slk
if (MG_ENTERPRISE)
    add_unit_test(slk_advanced
        SOURCES slk_advanced.cpp
        LINK_TARGETS mg::storage mg-replication mg-coordination mg-repl_coord_glue
    )
endif ()

add_unit_test(slk_core
    SOURCES slk_core.cpp
    LINK_TARGETS mg-slk gflags fmt::fmt
)

add_unit_test(slk_streams
    SOURCES slk_streams.cpp
    LINK_TARGETS mg-slk gflags fmt::fmt
)

# Test utilities
add_library(rpc_messages rpc_messages.cpp)
target_link_libraries(rpc_messages mg-rpc mg-slk)

# Test mg-rpc
add_unit_test(rpc
    SOURCES rpc.cpp
    LINK_TARGETS mg-rpc rpc_messages
)

# Test rpc-timeouts
if (MG_ENTERPRISE)
    add_unit_test(rpc_timeouts
        SOURCES rpc_timeouts.cpp
        LINK_TARGETS mg-rpc mg-coordination mg-replication mg-repl_coord_glue rpc_messages
    )
    add_dependencies(${test_unit_prefix}rpc_timeouts memgraph__unit__tls_certs_dir)
endif ()

# Test rpc-in-progress
add_unit_test(rpc_in_progress
    SOURCES rpc_in_progress.cpp
    LINK_TARGETS mg-rpc rpc_messages mg-storage-v2
)

# Test rpc progress heartbeat (sends only on recorded progress)
add_unit_test(rpc_progress_heartbeat
    SOURCES rpc_progress_heartbeat.cpp
    LINK_TARGETS mg-rpc rpc_messages mg-storage-v2
)

# Test rpc file framing (receive-buffer duplicate-file bug)
add_unit_test(rpc_file_framing
    SOURCES rpc_file_framing.cpp
    LINK_TARGETS mg-rpc rpc_messages mg-storage-v2
)

# Test replication-rpc-progress
add_unit_test(replication_rpc_progress
    SOURCES replication_rpc_progress.cpp
    LINK_TARGETS mg-rpc rpc_messages mg-storage-v2
)

# Test snapshot-rpc-progress
add_unit_test(snapshot_rpc_progress
    SOURCES snapshot_rpc_progress.cpp
    LINK_TARGETS mg-rpc rpc_messages mg-storage-v2
)

# Test cancellation of full-scan schema operations (index population, constraint validation)
add_unit_test(storage_v2_schema_cancellation
    SOURCES storage_v2_schema_cancellation.cpp
    LINK_TARGETS mg-storage-v2
)

add_unit_test(snapshot_progress
    SOURCES snapshot_progress.cpp
    LINK_TARGETS mg::storage
)

add_unit_test(rpc_versioning
    SOURCES rpc_versioning.cpp
    LINK_TARGETS mg-coordination mg-repl_coord_glue mg-rpc rpc_messages
)

add_unit_test(replication_state_writing
    SOURCES replication_state_writing.cpp
    LINK_TARGETS mg-replication mg-repl_coord_glue
)

# Test websocket

add_unit_test(monitoring
    SOURCES monitoring.cpp
    LINK_TARGETS mg-communication mg-auth Boost::headers
)

# Test multi-database
if (MG_ENTERPRISE)
    add_unit_test(dbms_database
        SOURCES dbms_database.cpp
        LINK_TARGETS mg::storage mg-query mg-glue mg-dbms
    )

    add_unit_test(dbms_handler
        SOURCES dbms_handler.cpp
        LINK_TARGETS mg::storage mg-query mg-glue mg-dbms
        CUSTOM_MAIN
    )

    add_unit_test(multi_tenancy
        SOURCES multi_tenancy.cpp
        LINK_TARGETS mg-query mg-auth mg-glue mg-dbms
    )
else ()
    add_unit_test(dbms_handler_community
        SOURCES dbms_handler_community.cpp
        LINK_TARGETS mg::storage mg-query mg-glue mg-dbms
        CUSTOM_MAIN
    )
endif ()

# Test distributed
add_unit_test(distributed_lamport_clock
    SOURCES distributed_lamport_clock.cpp
    LINK_TARGETS mg-distributed
    INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
)

add_unit_test(query_hint_provider
    SOURCES query_hint_provider.cpp
    LINK_TARGETS mg-query mg-glue
)


add_unit_test(counter
    SOURCES counter.cpp
    LINK_TARGETS mg-utils
)

add_unit_test(prometheus_metrics
    SOURCES prometheus_metrics.cpp
    LINK_TARGETS mg-metrics mg-dbms disk_test_utils
)

# Test coordination
if (MG_ENTERPRISE)
    add_unit_test(coordinator_utils
        SOURCES coordinator_utils.cpp
        LINK_TARGETS gflags mg-coordination mg-repl_coord_glue
        INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
    )

endif ()

# Test Raft log serialization
if (MG_ENTERPRISE)
    add_unit_test(coordinator_raft_log_serialization
        SOURCES coordinator_raft_log_serialization.cpp
        LINK_TARGETS gflags mg-coordination mg-repl_coord_glue
        INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
    )

endif ()

# Test CoordinatorClusterState
if (MG_ENTERPRISE)
    add_unit_test(coordinator_cluster_state
        SOURCES coordinator_cluster_state.cpp
        LINK_TARGETS gflags mg-coordination mg-repl_coord_glue
        INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
    )

endif ()

# Test coordinator state machine
if (MG_ENTERPRISE)
    add_unit_test(coordinator_state_machine
        SOURCES coordinator_state_machine.cpp
        LINK_TARGETS gflags mg-coordination mg-repl_coord_glue
        INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
    )

endif ()

if (MG_ENTERPRISE)
    add_unit_test(coordinator_state_manager
        SOURCES coordinator_state_manager.cpp
        LINK_TARGETS gflags mg-coordination mg-repl_coord_glue
        INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
    )

endif ()

if (MG_ENTERPRISE)
    add_unit_test(coordinator_log_store
        SOURCES coordinator_log_store.cpp
        LINK_TARGETS gflags mg-coordination mg-repl_coord_glue
        INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
    )

endif ()

# Test RAFT state
if (MG_ENTERPRISE)
    add_unit_test(coordinator_raft_state
        SOURCES coordinator_raft_state.cpp
        LINK_TARGETS gflags mg-coordination mg-repl_coord_glue
        INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
    )

endif ()

# Test coordinator global_read_only SET/SHOW setting path
if (MG_ENTERPRISE)
    add_unit_test(coordinator_global_read_only_setting
        SOURCES coordinator_global_read_only_setting.cpp
        LINK_TARGETS gflags mg-coordination mg-repl_coord_glue
        INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
    )

endif ()

# Test coordinator env flag handler
if (MG_ENTERPRISE)
    add_unit_test(coordinator_env_flag_handler
        SOURCES coordinator_env_flag_handler.cpp
        LINK_TARGETS gflags mg-flags
        INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
    )

endif ()

# Test coordinator instance context
if (MG_ENTERPRISE)
    add_unit_test(coordinator_instance_context
        SOURCES coordinator_instance_context.cpp
        LINK_TARGETS gflags mg-coordination
        INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
    )

endif ()

# Test coordinator instance aux
if (MG_ENTERPRISE)
    add_unit_test(coordinator_instance_aux
        SOURCES coordinator_instance_aux.cpp
        LINK_TARGETS gflags mg-coordination
        INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
    )

endif ()

# Test data instance context
if (MG_ENTERPRISE)
    add_unit_test(data_instance_context
        SOURCES data_instance_context.cpp
        LINK_TARGETS gflags mg-coordination
        INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
    )

endif ()

# Build all HA unit tests
if (MG_ENTERPRISE)
    add_custom_target(memgraph__unit__high_availability ALL
        DEPENDS memgraph__unit__coordinator_utils
        memgraph__unit__coordinator_raft_log_serialization
        memgraph__unit__coordinator_cluster_state
        memgraph__unit__coordinator_state_machine
        memgraph__unit__coordinator_state_manager
        memgraph__unit__coordinator_log_store
        memgraph__unit__coordinator_raft_state
        memgraph__unit__coordinator_global_read_only_setting
        memgraph__unit__coordinator_env_flag_handler
        memgraph__unit__coordinator_instance_context
        memgraph__unit__coordinator_instance_aux
    )

    add_custom_target(run_ha_tests
        COMMAND ctest -j4 -R memgraph__unit__coordinator_
    )

endif ()

# Test time to live
if (MG_ENTERPRISE)
    add_unit_test(ttl
        SOURCES ttl.cpp
        LINK_TARGETS mg-query disk_test_utils
        INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include
    )

endif ()

# Test database protector factory system
add_unit_test(database_protector_test
    SOURCES database_protector_test.cpp
    LINK_TARGETS mg-storage-v2
)

# Test community detection online
# avoid linking with shared library due to missing symbols
if (MG_ENTERPRISE)
    add_unit_test(community_detection_online
        SOURCES community_detection_online.cpp ${CMAKE_SOURCE_DIR}/query_modules/community_detection_module/algorithm_online/community_detection.cpp
        INCLUDE_DIRS
        ${CMAKE_SOURCE_DIR}/query_modules/community_detection_module
        ${CMAKE_SOURCE_DIR}/include
        ${CMAKE_SOURCE_DIR}/query_modules/mg_utility
    )
endif ()

# Test katz centrality online
# avoid linking with shared library due to missing symbols
if (MG_ENTERPRISE)
    add_unit_test(katz_centrality_online
        SOURCES katz_centrality_online.cpp ${CMAKE_SOURCE_DIR}/query_modules/katz_centrality_module/algorithm/katz.cpp
        INCLUDE_DIRS
        ${CMAKE_SOURCE_DIR}/query_modules/katz_centrality_module
        ${CMAKE_SOURCE_DIR}/include
        ${CMAKE_SOURCE_DIR}/query_modules/mg_utility
    )
endif ()

add_unit_test(safe_hardware_concurrency
    SOURCES safe_hardware_concurrency.cpp
    LINK_TARGETS mg-utils)

# Test log retention cleanup
add_unit_test(logging
    SOURCES logging.cpp
    LINK_TARGETS gflags mg-flags
)

add_unit_test(utils_queued_byte_source
    SOURCES utils_queued_byte_source.cpp
    LINK_TARGETS mg-utils
)
