if(NOT VOID_ENABLE_TESTING)
    message("Tests disabled")
    return()
endif()

enable_testing()
# Get the list of modules that were actually successfully created
get_property(ACTIVE_MODULES GLOBAL PROPERTY VOID_MODULES)

# ------------------------------------------------------------------------------
# PART 1: Internal "Kitchen Sink" Test (One Executable to rule them all)
# ------------------------------------------------------------------------------
file(GLOB_RECURSE INTERNAL_SOURCES CONFIGURE_DEPENDS "internal/*.cpp")

if(INTERNAL_SOURCES)
    add_executable(VoidInternalTests ${INTERNAL_SOURCES})
    
    # Always link the Testing framework
    target_link_libraries(VoidInternalTests PRIVATE Void::Testing)
    
    # Link against EVERY active module in the project
    foreach(mod ${ACTIVE_MODULES})
        # Avoid self-linking if Testing is in the list
        if(NOT mod STREQUAL "Testing")
            target_link_libraries(VoidInternalTests PRIVATE Void::${mod})
        endif()
    endforeach()

    add_test(NAME AllInternalTests COMMAND VoidInternalTests)
endif()

# ------------------------------------------------------------------------------
# PART 2: Standalone Tests (Isolated Executables per Module)
# ------------------------------------------------------------------------------
foreach(mod ${ACTIVE_MODULES})
    # Look for a subdirectory in standalone/ that matches the module name
    set(STANDALONE_MOD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/standalone/${mod}")
    
    if(IS_DIRECTORY ${STANDALONE_MOD_DIR})
        file(GLOB test_files CONFIGURE_DEPENDS "${STANDALONE_MOD_DIR}/*.cpp")
        
        foreach(test_src ${test_files})
            get_filename_component(test_file_name ${test_src} NAME_WE)
            set(EXE_NAME "test_standalone_${mod}_${test_file_name}")
            
            add_executable(${EXE_NAME} ${test_src})
            # Only links against its specific module
            target_link_libraries(${EXE_NAME} PRIVATE Void::${mod})
            
            add_test(NAME ${EXE_NAME} COMMAND ${EXE_NAME})
        endforeach()
    endif()
endforeach()
