# Tests use plain asserts + exit codes (no framework dep needed).
# Behavior tests — not just "it compiles" — are how milestones are judged (AGENTS.md).

add_executable(test_smoke test_smoke.cpp)
target_link_libraries(test_smoke PRIVATE voicecat::voicecat)
target_compile_features(test_smoke PRIVATE cxx_std_20)
add_test(NAME smoke COMMAND test_smoke)

# frame_codec has no third-party deps; runs under both skeleton and dev.
# Needs core/src on the include path to reach internal headers (protocol/, session/, etc.).
add_executable(test_frame_codec test_frame_codec.cpp)
target_link_libraries(test_frame_codec PRIVATE voicecat::voicecat)
target_compile_features(test_frame_codec PRIVATE cxx_std_20)
target_include_directories(test_frame_codec PRIVATE ${CMAKE_SOURCE_DIR}/core/src)
add_test(NAME frame_codec COMMAND test_frame_codec)

if(VOICECAT_USE_VCPKG_DEPS)
    set(VC_TEST_INTERNAL_INCLUDES
        ${CMAKE_SOURCE_DIR}/core/src
        ${CMAKE_SOURCE_DIR}/server/src
        ${CMAKE_BINARY_DIR}/core/generated)  # protobuf-generated headers

    add_executable(test_envelope test_envelope.cpp)
    target_link_libraries(test_envelope PRIVATE voicecat::voicecat)
    target_compile_features(test_envelope PRIVATE cxx_std_20)
    target_include_directories(test_envelope PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME envelope COMMAND test_envelope)

    add_executable(test_tls_loopback test_tls_loopback.cpp)
    target_link_libraries(test_tls_loopback PRIVATE voicecat::voicecat)
    target_compile_features(test_tls_loopback PRIVATE cxx_std_20)
    target_include_directories(test_tls_loopback PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME tls_loopback COMMAND test_tls_loopback)

    # Links voicecat::server (which pulls in voicecat::voicecat + all deps transitively).
    add_executable(test_m1_integration test_m1_integration.cpp)
    target_link_libraries(test_m1_integration PRIVATE voicecat::server)
    target_compile_features(test_m1_integration PRIVATE cxx_std_20)
    target_include_directories(test_m1_integration PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME m1_integration COMMAND test_m1_integration)
    set_tests_properties(m1_integration PROPERTIES TIMEOUT 60)

    # ── M2 unit tests ──────────────────────────────────────────────────────────

    add_executable(test_voice_frame test_voice_frame.cpp)
    target_link_libraries(test_voice_frame PRIVATE voicecat::voicecat)
    target_compile_features(test_voice_frame PRIVATE cxx_std_20)
    target_include_directories(test_voice_frame PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME voice_frame COMMAND test_voice_frame)

    add_executable(test_media_aead test_media_aead.cpp)
    target_link_libraries(test_media_aead PRIVATE voicecat::voicecat)
    target_compile_features(test_media_aead PRIVATE cxx_std_20)
    target_include_directories(test_media_aead PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME media_aead COMMAND test_media_aead)

    add_executable(test_opus_codec test_opus_codec.cpp)
    target_link_libraries(test_opus_codec PRIVATE voicecat::voicecat)
    target_compile_features(test_opus_codec PRIVATE cxx_std_20)
    target_include_directories(test_opus_codec PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME opus_codec COMMAND test_opus_codec)

    # PLC cap: after ~2s of pure PLC (no real packets), the mixer emits silence instead of
    # comfort noise — bounds the eternal-hiss failure mode (defense-in-depth for the
    # disconnect/LEFT fix). White-box AudioEngine test, no server needed.
    add_executable(test_plc_cap test_plc_cap.cpp)
    target_link_libraries(test_plc_cap PRIVATE voicecat::voicecat)
    target_compile_features(test_plc_cap PRIVATE cxx_std_20)
    target_include_directories(test_plc_cap PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME plc_cap COMMAND test_plc_cap)

    # Jitter-buffer bounded depth: across many talkspurt/silence cycles with a compressed sender
    # timeline + reordered stragglers, playout latency must stay bounded (no backward drift /
    # ratchet). White-box AudioEngine test, no server needed.
    add_executable(test_jitter_depth test_jitter_depth.cpp)
    target_link_libraries(test_jitter_depth PRIVATE voicecat::voicecat)
    target_compile_features(test_jitter_depth PRIVATE cxx_std_20)
    target_include_directories(test_jitter_depth PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME jitter_depth COMMAND test_jitter_depth)

    # External playback (iOS VPIO): the mixer-timer thread drives decode+mix with NO hardware
    # device and delivers the final mix to the mixed-output sink. White-box AudioEngine test.
    add_executable(test_external_playback test_external_playback.cpp)
    target_link_libraries(test_external_playback PRIVATE voicecat::voicecat)
    target_compile_features(test_external_playback PRIVATE cxx_std_20)
    target_include_directories(test_external_playback PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME external_playback COMMAND test_external_playback)

    # M2 exit criterion: two headless clients relay encrypted Opus frames via the SFU.
    add_executable(test_m2_voice test_m2_voice.cpp)
    target_link_libraries(test_m2_voice PRIVATE voicecat::server)
    target_compile_features(test_m2_voice PRIVATE cxx_std_20)
    target_include_directories(test_m2_voice PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME m2_voice COMMAND test_m2_voice)
    set_tests_properties(m2_voice PROPERTIES TIMEOUT 120)

    # Same exit criterion, but through the real C ABI (vc_client), not raw sockets —
    # proves stream_start/stop/UDP-binding in core/src/core/client.cpp actually work.
    add_executable(test_voice_client_abi test_voice_client_abi.cpp)
    target_link_libraries(test_voice_client_abi PRIVATE voicecat::server)
    target_compile_features(test_voice_client_abi PRIVATE cxx_std_20)
    target_include_directories(test_voice_client_abi PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME voice_client_abi COMMAND test_voice_client_abi)
    set_tests_properties(voice_client_abi PROPERTIES TIMEOUT 60)

    # M3 exit criterion: multi-stream (mic + desktop audio), independent per-stream
    # gain/mute/NS, per-channel Opus configurability, talk indicators -- all through the
    # real C ABI (vc_client), not raw sockets.
    add_executable(test_m3_multistream test_m3_multistream.cpp)
    target_link_libraries(test_m3_multistream PRIVATE voicecat::server)
    target_compile_features(test_m3_multistream PRIVATE cxx_std_20)
    target_include_directories(test_m3_multistream PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME m3_multistream COMMAND test_m3_multistream)
    set_tests_properties(m3_multistream PROPERTIES TIMEOUT 60)

    # Post-M3 follow-up: device enumeration, VAD/PTT input gate, true stereo playback mixing —
    # the items PROGRESS.md's M3 section explicitly carried forward as out of scope.
    add_executable(test_vad_ptt_devices test_vad_ptt_devices.cpp)
    target_link_libraries(test_vad_ptt_devices PRIVATE voicecat::server)
    target_compile_features(test_vad_ptt_devices PRIVATE cxx_std_20)
    target_include_directories(test_vad_ptt_devices PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME vad_ptt_devices COMMAND test_vad_ptt_devices)
    set_tests_properties(vad_ptt_devices PROPERTIES TIMEOUT 60)

    # M4: channel/user/stream snapshot getters (vc_list_channels/vc_list_users/
    # vc_list_user_streams) — through the real C ABI against a real in-process server.
    add_executable(test_channel_user_list_abi test_channel_user_list_abi.cpp)
    target_link_libraries(test_channel_user_list_abi PRIVATE voicecat::server)
    target_compile_features(test_channel_user_list_abi PRIVATE cxx_std_20)
    target_include_directories(test_channel_user_list_abi PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME channel_user_list_abi COMMAND test_channel_user_list_abi)
    set_tests_properties(channel_user_list_abi PROPERTIES TIMEOUT 60)

    # M4: TOFU server-identity gate (VC_EVENT_SERVER_IDENTITY / vc_confirm_server_identity /
    # vc_get_server_identity_display) — real TLS handshakes against real in-process servers.
    add_executable(test_tofu_flow test_tofu_flow.cpp)
    target_link_libraries(test_tofu_flow PRIVATE voicecat::server)
    target_compile_features(test_tofu_flow PRIVATE cxx_std_20)
    target_include_directories(test_tofu_flow PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME tofu_flow COMMAND test_tofu_flow)
    set_tests_properties(tofu_flow PROPERTIES TIMEOUT 90)

    # M5 Phase 1: permission enforcement + SetPermissionRequest + channel create.
    add_executable(test_m5_permissions test_m5_permissions.cpp)
    target_link_libraries(test_m5_permissions PRIVATE voicecat::server)
    target_compile_features(test_m5_permissions PRIVATE cxx_std_20)
    target_include_directories(test_m5_permissions PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME m5_permissions COMMAND test_m5_permissions)
    set_tests_properties(m5_permissions PROPERTIES TIMEOUT 60)

    # M5 Phase 2: kick/ban/move/server-mute.
    add_executable(test_m5_kick_ban_move_mute test_m5_kick_ban_move_mute.cpp)
    target_link_libraries(test_m5_kick_ban_move_mute PRIVATE voicecat::server)
    target_compile_features(test_m5_kick_ban_move_mute PRIVATE cxx_std_20)
    target_include_directories(test_m5_kick_ban_move_mute PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME m5_kick_ban_move_mute COMMAND test_m5_kick_ban_move_mute)
    set_tests_properties(m5_kick_ban_move_mute PROPERTIES TIMEOUT 90)

    # M5 Phase 3: in-app admin account management.
    add_executable(test_m5_admin_accounts test_m5_admin_accounts.cpp)
    target_link_libraries(test_m5_admin_accounts PRIVATE voicecat::server)
    target_compile_features(test_m5_admin_accounts PRIVATE cxx_std_20)
    target_include_directories(test_m5_admin_accounts PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME m5_admin_accounts COMMAND test_m5_admin_accounts)
    set_tests_properties(m5_admin_accounts PROPERTIES TIMEOUT 90)

    # M5 Phase 4: channel CRUD & password enforcement.
    add_executable(test_m5_channel_crud test_m5_channel_crud.cpp)
    target_link_libraries(test_m5_channel_crud PRIVATE voicecat::server)
    target_compile_features(test_m5_channel_crud PRIVATE cxx_std_20)
    target_include_directories(test_m5_channel_crud PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME m5_channel_crud COMMAND test_m5_channel_crud)
    set_tests_properties(m5_channel_crud PROPERTIES TIMEOUT 90)

    # Disconnect/timeout: server broadcasts UserEvent::LEFT on TCP drop (no more ghost
    # users or eternal PLC hiss on peers). Also covers the keepalive/reaper paths added
    # alongside the LEFT-broadcast fix.
    add_executable(test_disconnect_left test_disconnect_left.cpp)
    target_link_libraries(test_disconnect_left PRIVATE voicecat::server)
    target_compile_features(test_disconnect_left PRIVATE cxx_std_20)
    target_include_directories(test_disconnect_left PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME disconnect_left COMMAND test_disconnect_left)
    set_tests_properties(disconnect_left PROPERTIES TIMEOUT 90)

    # Reaper: half-open connections (no TCP EOF) are dropped after the configurable timeout,
    # peers get UserEvent::LEFT, the stale client gets disconnected. Uses a 2s timeout for
    # fast test turnaround (production default is 45s).
    add_executable(test_reaper_timeout test_reaper_timeout.cpp)
    target_link_libraries(test_reaper_timeout PRIVATE voicecat::server)
    target_compile_features(test_reaper_timeout PRIVATE cxx_std_20)
    target_include_directories(test_reaper_timeout PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME reaper_timeout COMMAND test_reaper_timeout)
    set_tests_properties(reaper_timeout PROPERTIES TIMEOUT 30)

    # DRED toggle: per-channel dred flag round-trips through protocol; encoder initialises
    # with DRED; PCM injection through DRED-enabled encode path runs without crash.
    add_executable(test_dred_toggle test_dred_toggle.cpp)
    target_link_libraries(test_dred_toggle PRIVATE voicecat::server)
    target_compile_features(test_dred_toggle PRIVATE cxx_std_20)
    target_include_directories(test_dred_toggle PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME dred_toggle COMMAND test_dred_toggle)
    set_tests_properties(dred_toggle PROPERTIES TIMEOUT 60)

    # External PCM feed/tap API:
    #   test_feed_pcm_round_trip — vc_stream_feed_pcm (mono), verified via pcm_sink.
    #   test_feed_pcm_stereo     — vc_stream_feed_pcm (stereo, Music Room), L != R assertion.
    #   test_pcm_sink            — vc_set_pcm_sink metadata (user_id / stream_id / sr) + disable.
    add_executable(test_external_pcm test_external_pcm.cpp)
    target_link_libraries(test_external_pcm PRIVATE voicecat::server)
    target_compile_features(test_external_pcm PRIVATE cxx_std_20)
    target_include_directories(test_external_pcm PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME external_pcm COMMAND test_external_pcm)
    set_tests_properties(external_pcm PROPERTIES TIMEOUT 90)

    # Non-20ms channel frame_ms: the fixed 48k/20ms engine clock is reframed to the channel's
    # Opus window before encoding. 40ms (accumulate) and 10ms (split) feed->sink round trips.
    add_executable(test_frame_ms_reframe test_frame_ms_reframe.cpp)
    target_link_libraries(test_frame_ms_reframe PRIVATE voicecat::server)
    target_compile_features(test_frame_ms_reframe PRIVATE cxx_std_20)
    target_include_directories(test_frame_ms_reframe PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME frame_ms_reframe COMMAND test_frame_ms_reframe)
    set_tests_properties(frame_ms_reframe PROPERTIES TIMEOUT 90)

    # Per-channel sample_rate as an Opus bandwidth cap (OPUS_SET_MAX_BANDWIDTH): a 7 kHz tone is
    # attenuated on an 8 kHz (narrowband) channel vs a 48 kHz (full-band) channel.
    add_executable(test_channel_samplerate test_channel_samplerate.cpp)
    target_link_libraries(test_channel_samplerate PRIVATE voicecat::server)
    target_compile_features(test_channel_samplerate PRIVATE cxx_std_20)
    target_include_directories(test_channel_samplerate PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME channel_samplerate COMMAND test_channel_samplerate)
    set_tests_properties(channel_samplerate PROPERTIES TIMEOUT 90)

    # Noise suppression: the RNNoise backend behind ApmProcessor actually denoises (docs/voice.md
    # §10-11). Links core only; reaches the internal audio/ headers for ApmProcessor::create().
    add_executable(test_noise_suppression test_noise_suppression.cpp)
    target_link_libraries(test_noise_suppression PRIVATE voicecat::voicecat)
    target_compile_features(test_noise_suppression PRIVATE cxx_std_20)
    target_include_directories(test_noise_suppression PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME noise_suppression COMMAND test_noise_suppression)

    # Receive-side NR through the decode loop: a stereo VOICE stream is actually denoised
    # (regression for the stereo-mic bypass), and a screen-audio share is left untouched.
    # White-box AudioEngine test; paced realtime feed, so it needs a generous timeout.
    add_executable(test_recv_noise_reduction test_recv_noise_reduction.cpp)
    target_link_libraries(test_recv_noise_reduction PRIVATE voicecat::voicecat)
    target_compile_features(test_recv_noise_reduction PRIVATE cxx_std_20)
    target_include_directories(test_recv_noise_reduction PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
    add_test(NAME recv_noise_reduction COMMAND test_recv_noise_reduction)
    set_tests_properties(recv_noise_reduction PROPERTIES TIMEOUT 90)
endif()
