# SPDX-License-Identifier: GPL-3.0-or-later
set(REALMRUN_TEST_MODULES vfs pcx bmp pal records gamedata sage_res wav ogg rul sha256 manifest frame_pacing seats seat_overlay touch endian input_backends controller mapper mixer movie movie_audio probe redraw map_scroll viewport scenario codepatch map_render rng rules stack movement fog city game combat codepatch_city codepatch_units codepatch_misc hero spell ruin campaign combat_effects quest diplomacy diplo_save diplo_game diplo_bribe ruin_search campaign_game campaign_smoke savegame session ai game_attack ai_onesearch frontend_session)

foreach(mod ${REALMRUN_TEST_MODULES})
    add_executable(test_${mod} test_${mod}.c)
    target_link_libraries(test_${mod} PRIVATE realmrun_core)
    target_compile_options(test_${mod} PRIVATE -Wall -Wextra)
    realmrun_apply_tune(test_${mod})
    add_test(NAME ${mod} COMMAND test_${mod})
endforeach()

# UI engine test (T-M3-03): links realmrun_ui (the SAGE screen/widget engine)
# on top of realmrun_core. Pure C11, no SDL; synthetic fixtures at test runtime.
add_executable(test_ui test_ui.c)
target_link_libraries(test_ui PRIVATE realmrun_ui)
target_compile_options(test_ui PRIVATE -Wall -Wextra)
realmrun_apply_tune(test_ui)
add_test(NAME ui COMMAND test_ui)

# Font + menu + setup-screen + touch-UI tests (T-M3-04a / T-M3-05a / T-M3-touch):
# the fixed-cell font, the menu flow, the new-game setup controller (D-018) and
# the touch gesture -> UI action wiring (letterbox/rotation coords, lock-out
# guard); plus the TURN.PCX HUD banner-art integration (recon 2026-11-08).
# All link realmrun_ui; pure C11, no SDL.
foreach(mod font menu setup_screen touch_ui banner_art)
    add_executable(test_${mod} test_${mod}.c)
    target_link_libraries(test_${mod} PRIVATE realmrun_ui)
    target_compile_options(test_${mod} PRIVATE -Wall -Wextra)
    realmrun_apply_tune(test_${mod})
    add_test(NAME ${mod} COMMAND test_${mod})
endforeach()

# Credits-screen test (BACKLOG 12l): the docs/CREDITS.md-mirroring credit table,
# its scrollable window and the settings-screen W3CP attribution line. Links
# realmrun_ui; pure C11, no SDL. REALMRUN_SOURCE_DIR lets the test read
# docs/CREDITS.md and assert the W3CP attribution/URL stay in sync with the doc.
add_executable(test_credits_screen test_credits_screen.c)
target_link_libraries(test_credits_screen PRIVATE realmrun_ui)
target_compile_options(test_credits_screen PRIVATE -Wall -Wextra)
target_compile_definitions(test_credits_screen PRIVATE
    REALMRUN_SOURCE_DIR="${CMAKE_SOURCE_DIR}")
realmrun_apply_tune(test_credits_screen)
add_test(NAME credits_screen COMMAND test_credits_screen)

# Diplomatic Report test (T-M5-04b / BACKLOG 12c): the view-model over the
# read-only src/core/diplomacy state (bands/labels, self + non-live sides
# skipped, relation shown, F-03 band edge at ire 0), the UNKNOWN colour
# eligibility (trixy Q2) and the scrollable screen. Links realmrun_ui; pure C11,
# no SDL.
add_executable(test_diplo_report test_diplo_report.c)
target_link_libraries(test_diplo_report PRIVATE realmrun_ui)
target_compile_options(test_diplo_report PRIVATE -Wall -Wextra)
realmrun_apply_tune(test_diplo_report)
add_test(NAME diplo_report COMMAND test_diplo_report)

# Victory-view test (T-M5-05 / BACKLOG 12g): the city-conquest Victory view-model
# (vanilla 3 lines vs the W3CP F-08 "print-conquered-city" level line, level edge
# cases, owner wording, bounds/NULL safety). Links realmrun_ui; pure C11, no SDL.
add_executable(test_victory_view test_victory_view.c)
target_link_libraries(test_victory_view PRIVATE realmrun_ui)
target_compile_options(test_victory_view PRIVATE -Wall -Wextra)
realmrun_apply_tune(test_victory_view)
add_test(NAME victory_view COMMAND test_victory_view)

# Playable front-end HUD test (T-M6-05c / S-M6-05): strip values, side colours
# and icons, stack marker culling and fog filter, select_stack dirty rects,
# key/button action table incl. modal-aware Return, peace queue ascending order
# + decline mask + peace_pending, and idle sync = 0 dirty rects. Links
# realmrun_ui; pure C11, no SDL.
add_executable(test_hud test_hud.c)
target_link_libraries(test_hud PRIVATE realmrun_ui)
target_compile_options(test_hud PRIVATE -Wall -Wextra)
realmrun_apply_tune(test_hud)
add_test(NAME hud COMMAND test_hud)

# Music test: the CD-track map and graceful-stub paths run always; the
# synthetic-OGG streamed-decode block only when the vorbis *encoder* is present
# at build time (used to generate a tiny OGG at test time - no binary audio in
# git, D-006). On the aarch64 cross build neither is present and the block is
# compiled out, so the test still builds and runs the map/stub assertions.
add_executable(test_music test_music.c)
target_link_libraries(test_music PRIVATE realmrun_core)
target_compile_options(test_music PRIVATE -Wall -Wextra)
realmrun_apply_tune(test_music)
if(VORBISENC_FOUND)
    target_compile_definitions(test_music PRIVATE REALMRUN_TEST_HAVE_VORBISENC)
    target_link_libraries(test_music PRIVATE PkgConfig::VORBISENC)
endif()
add_test(NAME music COMMAND test_music)

# Preset reader test (T-M7-02): parses the shipped presets/frontier-claw.toml,
# whose path is injected as a compile definition so the test reads the real
# file. Pure C11, no SDL; links realmrun_core.
add_executable(test_preset test_preset.c)
target_link_libraries(test_preset PRIVATE realmrun_core)
target_compile_options(test_preset PRIVATE -Wall -Wextra)
target_compile_definitions(test_preset PRIVATE
    REALMRUN_PRESET_FILE="${CMAKE_SOURCE_DIR}/presets/frontier-claw.toml")
realmrun_apply_tune(test_preset)
add_test(NAME preset COMMAND test_preset)

# Preset -> front-end config mapping test (T-M7-02b): maps the shipped
# presets/frontier-claw.toml and in-test variants onto the plain
# realmrun_frontend_config the SDL front-end consumes. Pure C11, no SDL window;
# links realmrun_core. Same shipped-file path define as the preset test.
add_executable(test_preset_apply test_preset_apply.c ../src/platform/sdl_preset.c)
target_link_libraries(test_preset_apply PRIVATE realmrun_core)
target_compile_options(test_preset_apply PRIVATE -Wall -Wextra)
target_compile_definitions(test_preset_apply PRIVATE
    REALMRUN_PRESET_FILE="${CMAKE_SOURCE_DIR}/presets/frontier-claw.toml")
realmrun_apply_tune(test_preset_apply)
add_test(NAME preset_apply COMMAND test_preset_apply)

# Host-side POSIX shell contract test also runs during ARM64 cross-builds.
find_program(REALMRUN_SH_EXECUTABLE NAMES sh)
if(REALMRUN_SH_EXECUTABLE)
    add_test(NAME install COMMAND "${REALMRUN_SH_EXECUTABLE}"
        "${CMAKE_CURRENT_SOURCE_DIR}/test_install.sh" "${CMAKE_SOURCE_DIR}")

    # Optional, opt-in release-packaging contract test (T-M7-03): runs a
    # native-only tools/package.sh into a temp dir and checks the allowlist
    # audit, licence-notice presence and determinism (two runs -> same sha256).
    # It does a full build, so it SKIPs (ctest SKIP, return code 77) unless
    # REALMRUN_TEST_PACKAGE=1 and the toolchain/time budget is present; it is
    # never part of the default run and needs no cross toolchain.
    add_test(NAME package COMMAND "${REALMRUN_SH_EXECUTABLE}"
        "${CMAKE_CURRENT_SOURCE_DIR}/test_package.sh" "${CMAKE_SOURCE_DIR}")
    set_tests_properties(package PROPERTIES
        SKIP_RETURN_CODE 77
        LABELS package)

    # Optional, opt-in resource-budget harness test (T-E-05a, D-019): runs
    # tools/perf-budget.sh (native MinSizeRel build + stripped engine size <= 2
    # MiB + peak RSS of headless synthetic-input runs <= 128 MiB). Does a full
    # build, so it SKIPs (ctest SKIP, return code 77) unless REALMRUN_TEST_PERF=1
    # and the toolchain/time budget is present; never part of the default run.
    add_test(NAME perf COMMAND "${REALMRUN_SH_EXECUTABLE}"
        "${CMAKE_CURRENT_SOURCE_DIR}/test_perf.sh" "${CMAKE_SOURCE_DIR}")
    set_tests_properties(perf PROPERTIES
        SKIP_RETURN_CODE 77
        LABELS perf)
endif()

# Synthetic CLI/session contract, including cross-emulated tools.
if(REALMRUN_SH_EXECUTABLE)
    add_executable(test_scn_fixture ../tools/perf_mkscn.c)
    target_include_directories(test_scn_fixture PRIVATE "${CMAKE_SOURCE_DIR}")
    target_compile_options(test_scn_fixture PRIVATE -Wall -Wextra)
    realmrun_apply_sanitize(test_scn_fixture)
    string(REPLACE ";" " " REALMRUN_SCN_EMULATOR "${CMAKE_CROSSCOMPILING_EMULATOR}")
    add_test(NAME scn_session COMMAND "${REALMRUN_SH_EXECUTABLE}"
        "${CMAKE_CURRENT_SOURCE_DIR}/test_scn_session.sh"
        "$<TARGET_FILE:realmrun-scn>" "$<TARGET_FILE:test_scn_fixture>"
        "${REALMRUN_SCN_EMULATOR}")
endif()
