# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: Copyright (C) 2026 Max A. Vilkki a.k.a. codise
#
# Unit tests (doctest + trompeloeil). They must not need HVSC; tests that use the local
# C64Music/ copy skip themselves when it's missing (CLAUDE.md).

add_executable(flipsid_tests
    test_main.cpp
    smoke_test.cpp
    engine_smoke_test.cpp
    hvsc_songlengths_test.cpp
    common_spsc_queue_test.cpp
    common_audio_ring_test.cpp
    common_triple_buffer_test.cpp
    audio_output_test.cpp
    recording_test.cpp
    engines_chip_presets_test.cpp
    engines_dither_test.cpp
    engines_factory_test.cpp
    engines_hvsc_test.cpp
    engines_filter_curves_test.cpp
    engines_lankila_presets_test.cpp
    engines_lankila_test.cpp
    engines_render_test.cpp
    engines_resid_rand_test.cpp
    engines_sid_sound_test.cpp
    engines_write_tee_test.cpp
    library_buglist_test.cpp
    library_hvsc_test.cpp
    library_index_test.cpp
    library_md5_test.cpp
    library_user_song_lengths_test.cpp
    library_scan_hvsc_test.cpp
    library_scan_test.cpp
    library_search_test.cpp
    library_service_test.cpp
    library_song_lengths_test.cpp
    library_stil_hvsc_test.cpp
    library_stil_test.cpp
    library_tree_filter_test.cpp
    library_tune_info_test.cpp
    command_line_test.cpp
    device_policy_test.cpp
    file_dialogs_test.cpp
    frame_scheduler_test.cpp
    log_test.cpp
    ui_format_test.cpp
    ui_player_frames_test.cpp
    ui_tab_strip_test.cpp
    ui_test.cpp
    ui_device_list_test.cpp
    ui_theme_test.cpp
    ui_theme_file_test.cpp
    ui_theme_editor_test.cpp
    player_file_name_subtune_test.cpp
    player_session_test.cpp
    player_song_end_test.cpp
    player_sid_stream_test.cpp
    player_test.cpp
    profiles_test.cpp
    profiles_store_test.cpp
    profiles_resolve_test.cpp
    liked_songs_test.cpp
    playlist_test.cpp
    bundled_playlists_test.cpp
    playlist_m3u_test.cpp
    tune_markers_test.cpp
    tune_overrides_test.cpp
    render_options_test.cpp
    settings_test.cpp
    c64_roms_test.cpp
    ui_search_test.cpp
    ui_settings_test.cpp
    ui_browser_tree_test.cpp
    ui_about_test.cpp
    ui_hardsid_setup_test.cpp
    ui_hvsc_test.cpp
    ui_library_hvsc_test.cpp
    ui_library_panels_test.cpp
    ui_menu_dim_test.cpp
    ui_playlist_m3u_test.cpp
    ui_tune_details_test.cpp
    mixer_controller_test.cpp
    mixer_protocol_test.cpp
    ultimate_device_api_test.cpp
    ultimate_device_info_test.cpp
    ultimate_http_test.cpp
    ultimate_service_test.cpp
    ultimate_sid_image_test.cpp
    sidout_sequences_test.cpp
    sidout_clock_test.cpp
    sidout_calibration_test.cpp
    sidout_stream_test.cpp
    sidout_write_ops_test.cpp
    sidout_plan_test.cpp
    sidout_scheduler_test.cpp
    sidout_service_test.cpp
    sid_out_view_test.cpp
    platform_power_test.cpp
    hardsid_protocol_test.cpp
    hardsid_device_test.cpp
    hvsc_download_test.cpp
    hvsc_install_test.cpp
    hvsc_unpack_test.cpp
    hvsc_updater_test.cpp
    hvsc_view_test.cpp
    trash_test.cpp
    ${PROJECT_SOURCE_DIR}/apps/flipsid/trash.cpp
)

target_link_libraries(flipsid_tests PRIVATE
    flipsid::version
    ext::doctest
    ext::trompeloeil
    ext::yyjson
    ext::ebur128
    ext::sidplayfp
    ext::resid # for resid/resid_rand.h
    ext::residfp2011 # for residfp2011/sid.h
    ext::residfp_internal # for the noise index, engines_write_tee_test.cpp
    flipsid::common
    flipsid::output
    flipsid::recording
    flipsid::engines
    flipsid::library
    flipsid::cli
    flipsid::log
    flipsid::ui
    flipsid::player
    flipsid::render_options
    flipsid::settings
    flipsid::profiles
    flipsid::playlist
    flipsid::ultimate
    flipsid::mixer
    flipsid::sidout
    flipsid::hardsid
    flipsid::hvsc
    flipsid::platform
)

# Where the HVSC tests start looking for C64Music/ (see hvsc_songlengths_test.cpp).
target_compile_definitions(flipsid_tests PRIVATE
    "FLIPSID_SOURCE_DIR=\"${PROJECT_SOURCE_DIR}\""
)

if(WIN32)
    # timeBeginPeriod() in test_main.cpp.
    target_link_libraries(flipsid_tests PRIVATE winmm)
endif()

flipsid_configure_target(flipsid_tests)

# One binary, two CTest entries. doctest's doctest_discover_tests() would list each case
# separately, but it runs the test binary as a post-build step, which breaks
# cross-compiling. Run the binary directly for per-case output and filtering
# (--test-case=..., --help).
add_test(NAME flipsid_tests COMMAND flipsid_tests --test-suite-exclude=hvsc)

# The tests that need a local HVSC copy get their own entry, so that without one CTest
# reports them as skipped instead of passed. The binary exits with 77 when every test that
# ran skipped itself (fixtures/skip.h); a failure still fails the entry. (Matching the
# output instead reported a failing HVSC test as skipped when another one had no HVSC.)
add_test(NAME flipsid_hvsc_tests COMMAND flipsid_tests --test-suite=hvsc)
set_tests_properties(flipsid_hvsc_tests PROPERTIES SKIP_RETURN_CODE 77)
