# Unit Tests for ReXGlue core components

add_executable(unit_tests
    audio/health_test.cpp
    audio/audio_system_test.cpp
    memory/heap_allocation_test.cpp
    memory/physical_host_offset_test.cpp
    graphics/command_ring_state_test.cpp
    graphics/resolve_route_test.cpp
    graphics/trace_capture_test.cpp
    graphics/trace_reader_test.cpp
    graphics/metal_profile_test.cpp
    kernel/critical_section_ledger_test.cpp
    kernel/critical_section_policy_test.cpp
    kernel/object_table_test.cpp
    core/cvar_test.cpp
    core/atomic_write_test.cpp
    core/fiber_test.cpp
    core/logging_test.cpp
    core/metal_performance_test.cpp
    core/sha256_test.cpp
    core/stream_test.cpp
    core/thread_native_exit_test.cpp
    core/byte_order_test.cpp
    core/chrono_test.cpp
    codegen/config_test.cpp
    codegen/template_registry_test.cpp
    ppc/import_function_test.cpp
    ppc/stack_test.cpp
    system/guest_path_test.cpp
    system/profile_persistence_test.cpp
    system/function_dispatcher_test.cpp
    codegen/manifest_test.cpp
    rexglue/migration_scan_test.cpp
    rexglue/ui_test.cpp
    ui/mouse_event_test.cpp
    ui/module_launch_coordinator_test.cpp
    input/input_system_test.cpp
    input/mnk_input_driver_test.cpp
    input/sdl_input_driver_test.cpp
)

if(APPLE)
    target_sources(unit_tests PRIVATE
        ui/macos_input_util_test.cpp
        ui/macos_key_translation_test.cpp
    )
endif()

if(REXGLUE_USE_METAL)
    target_sources(unit_tests PRIVATE
        ${PROJECT_SOURCE_DIR}/src/graphics/metal/edram_snapshot.cpp
        ${PROJECT_SOURCE_DIR}/src/graphics/metal/raw_color_sidecar.cpp
        graphics/metal_completion_packet_test.cpp
        graphics/metal_edram_snapshot_test.cpp
        graphics/metal_exact_output_merger_state_test.cpp
        graphics/metal_raw_color_sidecar_test.cpp
        graphics/metal_depth_float24_test.mm
        graphics/metal_exact_resolved_surface_test.cpp
        graphics/metal_goldeneye_postprocess_alias_test.cpp
        graphics/metal_mrt_route_test.cpp
        graphics/metal_shared_memory_test.mm
        graphics/metal_sampler_util_test.cpp
        graphics/metal_texture_cache_depth_test.mm
        ui/metal_presenter_capture_test.mm
    )
    target_link_libraries(unit_tests PRIVATE
        glslang::SPIRV
        xxHash::xxhash
        "-framework Metal"
        "-framework Foundation"
    )
endif()

target_sources(unit_tests PRIVATE
    ${PROJECT_SOURCE_DIR}/src/rexglue/commands/migration_scan.cpp
    ${PROJECT_SOURCE_DIR}/src/rexglue/ui/ui.cpp
    ${PROJECT_SOURCE_DIR}/src/rexglue/ui/progress.cpp
)

target_include_directories(unit_tests PRIVATE
    ${PROJECT_SOURCE_DIR}/include
    ${PROJECT_SOURCE_DIR}/src
)

target_link_libraries(unit_tests PRIVATE
    rexcore
    rexcodegen
    rexruntime
    Catch2::Catch2WithMain
)

# Strip Tracy/profiling instrumentation from test targets
target_compile_options(unit_tests PRIVATE
    -UTRACY_ENABLE -UREXGLUE_ENABLE_PROFILING
    -UREXGLUE_ENABLE_PERF_COUNTERS -UREXGLUE_PROFILE_GUEST_FUNCTIONS
)

set_target_properties(unit_tests PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:rexruntime>
)
if(UNIX AND NOT APPLE)
    set_target_properties(unit_tests PROPERTIES
        INSTALL_RPATH "$ORIGIN"
        BUILD_WITH_INSTALL_RPATH ON
    )
endif()

# Discover tests
include(Catch)
catch_discover_tests(unit_tests
    TEST_PREFIX "unit."
    PROPERTIES
        LABELS "unit"
        TIMEOUT 60
)
