cmake_minimum_required(VERSION 3.20)
project(LoSettingsMenuTests LANGUAGES CXX)
get_filename_component(REPO_ROOT "${CMAKE_CURRENT_LIST_DIR}/../../.." ABSOLUTE)
set(RUNTIME "${REPO_ROOT}/LostOdysseyRecomp")
find_package(Python3 COMPONENTS Interpreter REQUIRED)
find_package(Threads REQUIRED)
include(CTest)
add_executable(LoCompactionPromptTest ../compaction_prompt_test.cpp)
target_include_directories(LoCompactionPromptTest PRIVATE "${RUNTIME}")
target_compile_features(LoCompactionPromptTest PRIVATE cxx_std_20)
if(MSVC)
    target_compile_options(LoCompactionPromptTest PRIVATE /UNDEBUG /utf-8)
else()
    target_compile_options(LoCompactionPromptTest PRIVATE -UNDEBUG)
endif()
add_test(NAME compaction_prompt COMMAND LoCompactionPromptTest)
option(LO_MENU_SANITIZE "Enable Address/UndefinedBehavior sanitizers" OFF)
# One generated fixture per executable. Visual Studio generators attach a shared
# custom command to every consumer project and MSBuild runs them in parallel,
# so three targets writing one file race ("Permission denied").
function(lo_settings_interaction target)
    set(fixture "${CMAKE_CURRENT_BINARY_DIR}/${target}_fixture.cpp")
    add_custom_command(OUTPUT "${fixture}"
        COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_LIST_DIR}/generate_runtime_fixture.py"
            --root "${REPO_ROOT}" --output "${fixture}"
        DEPENDS generate_runtime_fixture.py "${RUNTIME}/settings/menu.cpp"
        VERBATIM)
    add_executable(${target} "${fixture}" "${RUNTIME}/settings/menu_render.cpp")
    target_compile_definitions(${target} PRIVATE ${ARGN})
endfunction()
lo_settings_interaction(LoSettingsInteraction LO_ENABLE_STREAMLINE_FG=1)
lo_settings_interaction(LoSettingsInteractionFsr LO_ENABLE_VULKAN_FSR_FG=1)
lo_settings_interaction(LoSettingsInteractionBoth LO_ENABLE_VULKAN_FSR_FG=1 LO_ENABLE_STREAMLINE_FG=1)
# XeSS-enabled builds add a seventh AA choice on Windows.
lo_settings_interaction(LoSettingsInteractionXess LO_ENABLE_STREAMLINE_FG=1 LO_HAS_XESS=1)
add_executable(LoSettingsRaster raster_test.cpp "${RUNTIME}/settings/menu_render.cpp")
add_executable(LoDebugOverlayTest ../debug_overlay_test.cpp "${RUNTIME}/debug/menu_overlay.cpp")
add_executable(LoDebugMenuInteractionTest ../debug_menu_interaction_test.cpp "${RUNTIME}/debug/menu_overlay.cpp")
set(encounter_fixture "${CMAKE_CURRENT_BINARY_DIR}/encounter_config_fixture.cpp")
add_custom_command(OUTPUT "${encounter_fixture}"
    COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_LIST_DIR}/generate_encounter_fixture.py"
        --root "${REPO_ROOT}" --output "${encounter_fixture}"
    DEPENDS generate_encounter_fixture.py "${RUNTIME}/settings/config.cpp" "${RUNTIME}/debug/no_encounters.cpp"
        "${RUNTIME}/gpu/loading_pacing.h"
    VERBATIM)
add_executable(LoEncounterConfigTest "${encounter_fixture}")
set(battle_exp_fixture "${CMAKE_CURRENT_BINARY_DIR}/battle_exp_fixture.cpp")
add_custom_command(OUTPUT "${battle_exp_fixture}"
    COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_LIST_DIR}/generate_battle_exp_fixture.py"
        --root "${REPO_ROOT}" --output "${battle_exp_fixture}"
    DEPENDS generate_battle_exp_fixture.py "${RUNTIME}/debug/battle_exp.cpp"
    VERBATIM)
add_executable(LoBattleExpTest "${battle_exp_fixture}")
foreach(target IN ITEMS LoSettingsInteraction LoSettingsInteractionFsr LoSettingsInteractionBoth LoSettingsInteractionXess LoSettingsRaster LoDebugOverlayTest LoDebugMenuInteractionTest LoEncounterConfigTest LoBattleExpTest)
    target_include_directories(${target} PRIVATE "${RUNTIME}")
    target_compile_features(${target} PRIVATE cxx_std_20)
    target_compile_definitions(${target} PRIVATE NOMINMAX)
    target_link_libraries(${target} PRIVATE Threads::Threads)
    if(MSVC)
        target_compile_options(${target} PRIVATE /utf-8 /UNDEBUG)
    else()
        target_compile_options(${target} PRIVATE -UNDEBUG)
        if(LO_MENU_SANITIZE)
            target_compile_options(${target} PRIVATE -fsanitize=address,undefined -fno-omit-frame-pointer)
            target_link_options(${target} PRIVATE -fsanitize=address,undefined)
        endif()
    endif()
endforeach()
add_test(NAME settings_interaction COMMAND LoSettingsInteraction)
add_test(NAME settings_interaction_fsr COMMAND LoSettingsInteractionFsr)
add_test(NAME settings_interaction_both COMMAND LoSettingsInteractionBoth)
add_test(NAME settings_interaction_xess COMMAND LoSettingsInteractionXess)
add_test(NAME settings_raster COMMAND LoSettingsRaster)
add_test(NAME debug_overlay COMMAND LoDebugOverlayTest)
add_test(NAME debug_menu_interaction COMMAND LoDebugMenuInteractionTest)
add_test(NAME encounter_config COMMAND LoEncounterConfigTest)
add_test(NAME battle_exp_hook COMMAND LoBattleExpTest)
add_test(NAME battle_exp_startup_on COMMAND LoBattleExpTest persisted-on)
set_tests_properties(settings_interaction settings_interaction_fsr settings_interaction_both settings_interaction_xess settings_raster PROPERTIES TIMEOUT 120)
set_tests_properties(debug_overlay debug_menu_interaction encounter_config battle_exp_hook battle_exp_startup_on PROPERTIES TIMEOUT 120)
