# SPDX-License-Identifier: GPL-3.0-or-later
# ctest harness (L0-1). Plain C11 tests, no framework: the CHECK macro returns
# nonzero on first failure (NDEBUG-safe, unlike assert()).
add_executable(test_components test_components.c)
target_include_directories(test_components PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_components PRIVATE
  swat3_core swat3_vfs swat3_fmt swat3_render swat3_sim swat3_ai swat3_sa
  swat3_ui swat3_input swat3_net swat3_platform)
add_test(NAME components COMMAND test_components)

# L0-2: settings INI reader + fixed-tick accumulator. argv[1] = shipped ini.
add_executable(test_platform test_platform.c)
target_include_directories(test_platform PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_platform PRIVATE swat3_platform swat3_core m)
add_test(NAME platform
  COMMAND test_platform ${CMAKE_SOURCE_DIR}/config/defaults/settings.ini)

# L0-2 E2E smoke: window + loop headless via SDL dummy drivers, 3 ticks exit.
add_test(NAME platform_e2e_smoke COMMAND swat3port --ticks 3)
set_tests_properties(platform_e2e_smoke PROPERTIES
  ENVIRONMENT "SDL_VIDEODRIVER=dummy;SDL_AUDIODRIVER=dummy")

# L0-3 gate (MASTER-PLAN §8): test-pattern scene renders via BOTH present
# paths (GLES2 + software rasterizer) at 640x480 and 800x600. Part 1 runs
# the pure software rasterizer with no SDL video; part 2 drives both
# backends through SDL — offscreen video driver gives the opengles2 path a
# real EGL context headless (llvmpipe), dummy has none (AUTO->soft there).
add_executable(test_render test_render.c)
target_include_directories(test_render PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_render PRIVATE
  swat3_render swat3_core PkgConfig::SDL2)
add_test(NAME render COMMAND test_render)
# ASAN_OPTIONS=detect_leaks=0: bringing up EGL/llvmpipe (offscreen GLES2)
# leaves one-time Mesa driver allocations past SDL_Quit — LSan sees only
# those (<unknown module>, no engine frames). ASan memory-bug checks stay on.
set_tests_properties(render PROPERTIES
  ENVIRONMENT "SDL_VIDEODRIVER=offscreen;SDL_AUDIODRIVER=dummy;ASAN_OPTIONS=detect_leaks=0")
add_test(NAME render_e2e_gles2 COMMAND swat3port --ticks 2 --render gles2 --settings ${CMAKE_SOURCE_DIR}/config/defaults/settings.ini)
# detect_leaks=0: Mesa/EGL keeps ~56 bytes of one-time driver state past
# SDL_Quit — third-party, not engine memory (test_render keeps LSan on).
set_tests_properties(render_e2e_gles2 PROPERTIES
  ENVIRONMENT "SDL_VIDEODRIVER=offscreen;SDL_AUDIODRIVER=dummy;ASAN_OPTIONS=detect_leaks=0")

# L0-4 gate (MASTER-PLAN §4.1): input mapping tables (every gameplay action
# reachable on the pad), mouse/KB + controller paths, and the aim-assist
# acceptance skeleton: assist OFF ⇒ view deltas bit-identical to the mouse
# path; assist ON ⇒ same final pose is manually reachable and the hit test
# depends only on the pose (comfort, never outcomes). Scripted input via
# synthetic SDL events — no real devices needed (headless).
add_executable(test_input test_input.c)
target_include_directories(test_input PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_input PRIVATE
  swat3_input swat3_core PkgConfig::SDL2)
add_test(NAME input COMMAND test_input)
set_tests_properties(input PROPERTIES
  ENVIRONMENT "SDL_VIDEODRIVER=dummy;SDL_AUDIODRIVER=dummy")

# L0-5 gate (D-009, BACKLOG "determinism CI test"): same input sequence =>
# identical state hash (replay). Unit level: a scripted synthetic-input
# recording through the real input layer, hashed per tick by core/replay
# (FNV-1a/64 over canonical field bytes) — equality across independent
# runs, sensitivity to a one-count input change and to tick bucketing, the
# tick-clock replay (same frame-dt rhythm => same tick stream), and FNV
# known-answer vectors guarding the primitive itself.
add_executable(test_replay test_replay.c)
target_include_directories(test_replay PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_replay PRIVATE
  swat3_input swat3_core PkgConfig::SDL2)
add_test(NAME replay COMMAND test_replay)
set_tests_properties(replay PROPERTIES
  ENVIRONMENT "SDL_VIDEODRIVER=dummy;SDL_AUDIODRIVER=dummy")

# L0-5 engine level: --replay N drives a fixed synthetic script through the
# full platform loop TWICE (fresh input layer + tick clock per run) and
# exits nonzero on state-hash divergence. Doubles as the L0-6 board smoke
# (same command on the VIM1S, dummy driver or KMSDRM).
add_test(NAME replay_e2e
  COMMAND swat3port --replay 24 --settings ${CMAKE_SOURCE_DIR}/config/defaults/settings.ini)
set_tests_properties(replay_e2e PROPERTIES
  ENVIRONMENT "SDL_VIDEODRIVER=dummy;SDL_AUDIODRIVER=dummy")

# Family rule (AGENTS.md rule 4): C11 only — no C++ sources under src/ or tests/.
add_test(NAME no_cxx
  COMMAND ${CMAKE_COMMAND} -DSWAT3_ROOT=${CMAKE_SOURCE_DIR}
          -P ${CMAKE_SOURCE_DIR}/cmake/no_cxx.cmake)

# Clean-room scan (AGENTS.md rule 1) as part of every ctest run, label "clean".
add_test(NAME check_clean
  COMMAND /bin/sh ${CMAKE_SOURCE_DIR}/tools/check-clean.sh ${CMAKE_SOURCE_DIR})
set_tests_properties(check_clean PROPERTIES LABELS "clean")

# perf-check anchor (label "perf", MASTER-PLAN §7): the VIM1S budget table must
# stay documented; real budget measurements attach to this label from L2 on.
add_test(NAME perf_budgets_documented
  COMMAND ${CMAKE_COMMAND} -DSWAT3_ROOT=${CMAKE_SOURCE_DIR}
          -P ${CMAKE_SOURCE_DIR}/cmake/perf_budgets.cmake)
set_tests_properties(perf_budgets_documented PROPERTIES LABELS "perf")

# L1 gate: VFS + clean-room format readers (.res/.zip, .dat/.sse, .bmp/.tga, .scn, .gsm, .cmp).
add_executable(test_vfs_fmt test_vfs_fmt.c)
target_include_directories(test_vfs_fmt PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_vfs_fmt PRIVATE swat3_vfs swat3_fmt swat3_core m)
add_test(NAME vfs_fmt COMMAND test_vfs_fmt)

add_executable(test_install test_install.c)
target_compile_definitions(test_install PRIVATE SWAT3_ROOT_DIR="${CMAKE_SOURCE_DIR}")
add_test(NAME install COMMAND test_install)

add_executable(test_gameplay test_gameplay.c)
target_include_directories(test_gameplay PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_gameplay PRIVATE swat3_ui swat3_sim swat3_ai swat3_sa swat3_net swat3_render swat3_fmt swat3_vfs swat3_input swat3_core m)
add_test(NAME gameplay COMMAND test_gameplay)
set_tests_properties(gameplay PROPERTIES LABELS "perf")
