# ---------------------------------------------------------------------
# Test fixture modules: one known-good stub, two deliberately broken
#
# The output paths come from the top-level file, which defines them
# before adding this directory so that each is written in one place.
# ---------------------------------------------------------------------
foreach(fixture good missing_symbols bad_version)
    add_library(fixture_${fixture} MODULE fixtures/src/${fixture}.cpp)
    set_target_properties(fixture_${fixture} PROPERTIES
            PREFIX ""
            SUFFIX ".so"
            OUTPUT_NAME ${fixture}
            LIBRARY_OUTPUT_DIRECTORY ${DPM_TEST_FIXTURE_DIR}
    )
endforeach()

# ---------------------------------------------------------------------
# API test binary — drives libdpm-core.so through its public C API
# ---------------------------------------------------------------------
add_executable(test_core test_core.cpp)

target_link_libraries(test_core PRIVATE dpm-core)

target_compile_definitions(test_core PRIVATE
        TEST_FIXTURE_MODULES="${DPM_TEST_FIXTURE_DIR}"
        TEST_FIXTURE_CONF="${CMAKE_CURRENT_SOURCE_DIR}/fixtures/conf"
        DPM_CORE_VERSION_EXPECTED="${PROJECT_VERSION}"
)

set_target_properties(test_core PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY ${DPM_TEST_DIR}
        BUILD_RPATH "$ORIGIN/../lib"
)

foreach(fixture good missing_symbols bad_version)
    add_dependencies(test_core fixture_${fixture})
endforeach()

add_test(NAME core_api COMMAND test_core)

# ---------------------------------------------------------------------
# CLI end-to-end tests (the dpm binary is the cheapest full-stack client)
# ---------------------------------------------------------------------
add_test(NAME cli_help COMMAND dpm --help)
set_tests_properties(cli_help PROPERTIES PASS_REGULAR_EXPRESSION "Usage: dpm")

add_test(NAME cli_list
        COMMAND dpm -c ${CMAKE_CURRENT_SOURCE_DIR}/fixtures/conf
                    -m ${CMAKE_BINARY_DIR}/modules --list-modules)
set_tests_properties(cli_list PROPERTIES PASS_REGULAR_EXPRESSION "info")

add_test(NAME cli_info_version
        COMMAND dpm -c ${CMAKE_CURRENT_SOURCE_DIR}/fixtures/conf
                    -m ${CMAKE_BINARY_DIR}/modules -L INFO info version)
set_tests_properties(cli_info_version PROPERTIES
        PASS_REGULAR_EXPRESSION "libdpm-core Version: ${PROJECT_VERSION}")

add_test(NAME cli_module_not_found
        COMMAND dpm -c ${CMAKE_CURRENT_SOURCE_DIR}/fixtures/conf
                    -m ${CMAKE_BINARY_DIR}/modules nonexistent)
set_tests_properties(cli_module_not_found PROPERTIES WILL_FAIL TRUE)

add_test(NAME cli_rejects_invalid_module
        COMMAND dpm -c ${CMAKE_CURRENT_SOURCE_DIR}/fixtures/conf
                    -m ${DPM_TEST_FIXTURE_DIR} missing_symbols)
set_tests_properties(cli_rejects_invalid_module PROPERTIES WILL_FAIL TRUE)
