# Catch2, fetched and pinned rather than assumed to be pre-installed on the
# dev machine - keeps "clone + cmake --build" sufficient with no manual
# dependency install step (see PHASE_PLAN.md Phase 0 exit criteria).
include(FetchContent)
FetchContent_Declare(
    Catch2
    GIT_REPOSITORY https://github.com/catchorg/Catch2.git
    GIT_TAG v3.9.1
)
FetchContent_MakeAvailable(Catch2)

find_package(Threads REQUIRED)

add_executable(scavenger_core_tests
    test_version.cpp
    test_task_queue.cpp
    test_hw_poller.cpp
    test_scoring.cpp
    test_memory_manager.cpp
    test_ibackend.cpp
    test_numeric_buffer.cpp
    test_scheduler.cpp
    test_composite_backend.cpp
    # Phase M1: always compiled/run, like nvml_utilization_source.cpp
    # itself - gracefully skips (WARN, not FAIL) when no NVIDIA driver/GPU
    # is present, same pattern as the hardware-gated tests below use for a
    # missing model fixture.
    test_nvml_utilization_source.cpp
    # Phase M1: deterministic (FakeUtilizationSource-based) merge-logic
    # coverage - no hardware dependency at all, runs everywhere.
    test_combined_utilization_source.cpp
)
target_link_libraries(scavenger_core_tests PRIVATE
    scavenger_core
    Catch2::Catch2WithMain
    Threads::Threads
)

# Phase 4: a real (not mocked) LlamaVulkanBackend correctness test - only
# buildable/linkable when SCAVENGER_BUILD_BACKENDS is ON, same gate as
# core/src/backends/ itself (see core/CMakeLists.txt) and scheduler_demo
# (core/tools/CMakeLists.txt). Kept out of scavenger_core_tests' always-on
# source list above so the rest of the Catch2 suite keeps building/running
# in CI with no Intel SDKs installed, per this file's own stated design.
if(SCAVENGER_BUILD_BACKENDS)
    target_sources(scavenger_core_tests PRIVATE
        test_llama_vulkan_backend.cpp
        test_openvino_backend.cpp
    )
    # test_llama_vulkan_backend.cpp needs to find models/stories260K.gguf
    # (repo-root-relative, gitignored test fixture - see models/README.md)
    # regardless of ctest's actual working directory - passed as a compile
    # definition rather than assumed relative to CWD.
    target_compile_definitions(scavenger_core_tests PRIVATE
        SCAVENGER_REPO_ROOT="${CMAKE_SOURCE_DIR}")

    # Phase 5, Tier 2: direct UsmTensorPool coverage, only meaningful (and
    # only compilable - it includes openvino_usm_pool.hpp directly) when
    # this library was built with the USM path, same gate as
    # openvino_usm_pool.cpp itself. test_openvino_backend.cpp's own
    # Tier-2-specific test cases are already #ifdef-guarded inside that one
    # file instead, since it's unconditionally compiled either way.
    if(SCAVENGER_ENABLE_TIER2_USM)
        target_sources(scavenger_core_tests PRIVATE test_openvino_usm_pool.cpp)
    endif()

    # Phase M1: LlamaCudaBackend real-hardware test, same
    # SCAVENGER_REPO_ROOT fixture-path definition as
    # test_llama_vulkan_backend.cpp above (already set unconditionally
    # inside this SCAVENGER_BUILD_BACKENDS block).
    if(SCAVENGER_BUILD_CUDA)
        target_sources(scavenger_core_tests PRIVATE test_llama_cuda_backend.cpp)
    endif()
endif()

# Phase 4 image-generation extension: same real-backend-not-just-MockBackend
# test pattern as test_llama_vulkan_backend.cpp above, gated behind
# SCAVENGER_BUILD_IMAGE_GEN instead (the OpenVINO GenAI SDK, not
# OpenVINO+llama.cpp).
if(SCAVENGER_BUILD_IMAGE_GEN)
    target_sources(scavenger_core_tests PRIVATE test_openvino_image_backend.cpp)
    target_compile_definitions(scavenger_core_tests PRIVATE
        SCAVENGER_REPO_ROOT="${CMAKE_SOURCE_DIR}")
endif()

list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
catch_discover_tests(scavenger_core_tests)
