# Unit tests: sysfs/PCI helpers and public error strings (no XPU vendor runtime).

find_package(GTest REQUIRED)

add_library(dummy_plugin_ok SHARED dummy_plugin_ok.c)
set_target_properties(dummy_plugin_ok PROPERTIES OUTPUT_NAME "dummy_plugin_ok")

add_library(dummy_plugin_missing_type SHARED dummy_plugin_missing_type.c)
set_target_properties(dummy_plugin_missing_type PROPERTIES OUTPUT_NAME "dummy_plugin_missing_type")

add_executable(test_pci_sysfs test_pci_sysfs.cpp)
target_include_directories(test_pci_sysfs PRIVATE "${CMAKE_SOURCE_DIR}/common")
target_link_libraries(test_pci_sysfs PRIVATE kyml GTest::gtest_main)
add_test(NAME test_pci_sysfs COMMAND test_pci_sysfs)

add_executable(test_kyml_errstring test_kyml_errstring.cpp)
target_include_directories(test_kyml_errstring PRIVATE "${CMAKE_SOURCE_DIR}/include")
target_link_libraries(test_kyml_errstring PRIVATE kyml GTest::gtest_main)
add_test(NAME test_kyml_errstring COMMAND test_kyml_errstring)

add_executable(test_plugin_manager_verifySyms test_plugin_manager_verifySyms.cpp)
target_include_directories(test_plugin_manager_verifySyms PRIVATE "${CMAKE_SOURCE_DIR}/include")
target_link_libraries(test_plugin_manager_verifySyms PRIVATE kyml dl GTest::gtest_main)
target_compile_definitions(test_plugin_manager_verifySyms PRIVATE
	DUMMY_PLUGIN_OK_PATH="$<TARGET_FILE:dummy_plugin_ok>"
	DUMMY_PLUGIN_MISSING_TYPE_PATH="$<TARGET_FILE:dummy_plugin_missing_type>"
)
add_test(NAME test_plugin_manager_verifySyms COMMAND test_plugin_manager_verifySyms)

add_executable(test_kyml_api_smoke test_kyml_api_smoke.cpp)
target_include_directories(test_kyml_api_smoke PRIVATE "${CMAKE_SOURCE_DIR}/include")
target_link_libraries(test_kyml_api_smoke PRIVATE kyml GTest::gtest_main)
add_test(NAME test_kyml_api_smoke COMMAND test_kyml_api_smoke)

# HOUMO integration test: requires vendor runtime + proper whitelist/plugin dir on host.
add_executable(test_houmo_integration test_houmo_integration.c)
target_include_directories(test_houmo_integration PRIVATE "${CMAKE_SOURCE_DIR}/include")
target_link_libraries(test_houmo_integration PRIVATE kyml dl)
add_test(NAME test_houmo_integration COMMAND test_houmo_integration)
set_tests_properties(test_houmo_integration PROPERTIES SKIP_RETURN_CODE 3)
# Ensure we load the just-built, coverage-instrumented plugin from this build tree
# instead of any system-installed kyml plugins.
set_tests_properties(test_houmo_integration PROPERTIES
    ENVIRONMENT "KYML_KYML_PLUGIN_DIR=${CMAKE_BINARY_DIR}"
)

option(KYML_COVERAGE_REPORT "Enable gcovr coverage-report target" OFF)
if(KYML_COVERAGE_REPORT)
    find_program(GCOVR_EXECUTABLE gcovr)
    if (NOT GCOVR_EXECUTABLE)
        message(WARNING "gcovr not found: coverage target will not be generated. Install gcovr or set KYML_COVERAGE_REPORT=OFF.")
        return()
    endif()

    add_custom_target(coverage
        COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/coverage-report
        COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
        COMMAND ${GCOVR_EXECUTABLE} -r ${CMAKE_SOURCE_DIR}
            --exclude ${CMAKE_SOURCE_DIR}/third_party
            --exclude ${CMAKE_SOURCE_DIR}/tests
            --exclude ${CMAKE_SOURCE_DIR}/src
            --exclude ${CMAKE_BINARY_DIR}
            --xml -o ${CMAKE_BINARY_DIR}/coverage-report/coverage.xml
        COMMAND ${GCOVR_EXECUTABLE} -r ${CMAKE_SOURCE_DIR}
            --exclude ${CMAKE_SOURCE_DIR}/third_party
            --exclude ${CMAKE_SOURCE_DIR}/tests
            --exclude ${CMAKE_SOURCE_DIR}/src
            --exclude ${CMAKE_BINARY_DIR}
            --html --html-details -o ${CMAKE_BINARY_DIR}/coverage-report/coverage.html
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
        COMMENT "Generating coverage report (gcovr)"
    )
endif()
