foreach(name persistence database database_rollback backup models recent_items conversation_actions media_store)
    add_executable(test_${name} test_${name}.cpp)
    target_link_libraries(test_${name} PRIVATE signal-storage Qt5::Core)
    target_compile_options(test_${name} PRIVATE -Wall -Wextra -Wpedantic)
    add_test(NAME ${name} COMMAND test_${name})
endforeach()

# Compile the production engine and run the actual launcher against a fake
# Java entry point. No Signal account, upstream runtime, Lomiri or network needed.
add_executable(test_engine test_engine.cpp ../src/Engine.cpp)
target_link_libraries(test_engine PRIVATE signal-storage Qt5::Core)
target_compile_options(test_engine PRIVATE -Wall -Wextra -Wpedantic)
target_compile_definitions(test_engine PRIVATE
   SIGNAL_BUILD_ENGINE_DIR="${CMAKE_CURRENT_BINARY_DIR}/runtime-fixture"
   SIGNAL_LAUNCHER_SOURCE="${CMAKE_SOURCE_DIR}/tools/run-signal-cli"
)
add_test(NAME engine COMMAND test_engine)
set_tests_properties(engine PROPERTIES TIMEOUT 30)

# Production publication state tests do not require Qt or a live Signal account.
find_package(Threads REQUIRED)
add_executable(test_qr_state test_qr_state.cpp)
target_include_directories(test_qr_state PRIVATE ../src)
target_link_libraries(test_qr_state PRIVATE Threads::Threads)
target_compile_options(test_qr_state PRIVATE -Wall -Wextra -Wpedantic)
foreach(case initial publish-before-notify legacy-reproduction replace cancel snapshot-isolation reentrant utf8 shared-lifetime concurrent)
   add_test(NAME qr-state-${case} COMMAND test_qr_state ${case})
   set_tests_properties(qr-state-${case} PROPERTIES TIMEOUT 15)
endforeach()

# Real Qt Quick Image / production image provider / production QR component.
# Deliberately required, not silently skipped by a successful native test run.
find_package(Qt5 5.15 REQUIRED COMPONENTS Gui Qml Quick)
find_library(QRENCODE_TEST_LIBRARY NAMES qrencode REQUIRED)
add_executable(test_qr test_qr.cpp ../src/QrProvider.cpp ../src/QrProvider.h)
target_include_directories(test_qr PRIVATE ../src)
target_link_libraries(test_qr PRIVATE Qt5::Gui Qt5::Qml Qt5::Quick ${QRENCODE_TEST_LIBRARY} Threads::Threads)
target_compile_options(test_qr PRIVATE -Wall -Wextra -Wpedantic)
target_compile_definitions(test_qr PRIVATE SIGNAL_QR_QML_FIXTURE="${CMAKE_CURRENT_SOURCE_DIR}/QrFixture.qml")
add_test(NAME qr-qml COMMAND test_qr)
set_tests_properties(qr-qml PROPERTIES TIMEOUT 30 ENVIRONMENT "QT_QPA_PLATFORM=offscreen;QT_QUICK_BACKEND=software")

# Production scrolling components with real Qt Quick geometry/flick physics.
# Host-only Lomiri visual stubs are never included in resources.qrc or the app.
add_executable(test_sheets test_sheets.cpp)
target_link_libraries(test_sheets PRIVATE Qt5::Gui Qt5::Qml Qt5::Quick)
target_compile_options(test_sheets PRIVATE -Wall -Wextra -Wpedantic)
target_compile_definitions(test_sheets PRIVATE
   SIGNAL_SHEET_FIXTURE="${CMAKE_CURRENT_SOURCE_DIR}/SheetFixture.qml"
   SIGNAL_SHEET_STUBS="${CMAKE_CURRENT_SOURCE_DIR}/qml-stubs"
)
add_test(NAME sheets-qml COMMAND test_sheets)
set_tests_properties(sheets-qml PROPERTIES TIMEOUT 20 ENVIRONMENT "QT_QPA_PLATFORM=offscreen;QT_QUICK_BACKEND=software")

# Actual Qt-independent avatar selection policy. Also compiled by the portable
# Python test runner; these cases do not imply that QImageReader or Signal ran.
add_executable(test_avatar_policy test_avatar_policy.cpp)
target_include_directories(test_avatar_policy PRIVATE ../src)
target_compile_options(test_avatar_policy PRIVATE -Wall -Wextra -Wpedantic)
foreach(case preference fallback legacy group retry columns all-combinations)
   add_test(NAME avatar-policy-${case} COMMAND test_avatar_policy ${case})
endforeach()

# Actual Qt-independent rules for video still frames: darkness test, later
# positions, attempt limits. Does not run GStreamer or decode a file.
add_executable(test_poster_policy test_poster_policy.cpp)
target_include_directories(test_poster_policy PRIVATE ../src)
target_compile_options(test_poster_policy PRIVATE -Wall -Wextra -Wpedantic)
foreach(case black-and-bright colour-order small-subject padding-and-bounds choose-brightest positions attempt-limits scaled-size)
   add_test(NAME poster-policy-${case} COMMAND test_poster_policy ${case})
endforeach()

# Actual Qt-independent input rules of the Giphy picker. No request is made.
add_executable(test_giphy_policy test_giphy_policy.cpp)
target_include_directories(test_giphy_policy PRIVATE ../src)
target_compile_options(test_giphy_policy PRIVATE -Wall -Wextra -Wpedantic)
foreach(case api-key cdn-url query ids-and-pages gif-bytes)
   add_test(NAME giphy-policy-${case} COMMAND test_giphy_policy ${case})
endforeach()

# Actual Qt-independent input rules of the KLIPY picker. No request is made.
add_executable(test_klipy_policy test_klipy_policy.cpp)
target_include_directories(test_klipy_policy PRIVATE ../src)
target_compile_options(test_klipy_policy PRIVATE -Wall -Wextra -Wpedantic)
foreach(case api-key media-url query ids-pages gif-bytes)
   add_test(NAME klipy-policy-${case} COMMAND test_klipy_policy ${case})
endforeach()

add_executable(test_share_identity test_share_identity.cpp)
target_include_directories(test_share_identity PRIVATE ../src)
target_compile_options(test_share_identity PRIVATE -Wall -Wextra -Wpedantic)
foreach(case normal missing-env wrong-env wrong-label old-version no-guess path)
   add_test(NAME share-identity-${case} COMMAND test_share_identity ${case})
endforeach()

# Production QML with actual Qt geometry, pointer delivery and signal dispatch.
foreach(fixture ShareReview ContentImport ShareReceiver)
   add_executable(test_${fixture} test_share_ui.cpp)
   target_link_libraries(test_${fixture} PRIVATE Qt5::Gui Qt5::Qml Qt5::Quick)
   target_compile_options(test_${fixture} PRIVATE -Wall -Wextra -Wpedantic)
   target_compile_definitions(test_${fixture} PRIVATE
      SIGNAL_SHARE_FIXTURE="${CMAKE_CURRENT_SOURCE_DIR}/${fixture}Fixture.qml"
      SIGNAL_SHARE_STUBS="${CMAKE_CURRENT_SOURCE_DIR}/qml-stubs")
   add_test(NAME ${fixture}-qml COMMAND test_${fixture})
   set_tests_properties(${fixture}-qml PROPERTIES TIMEOUT 20 ENVIRONMENT "QT_QPA_PLATFORM=offscreen;QT_QUICK_BACKEND=software")
endforeach()

# Real glyph-run checks, not merely catalogue/GSUB inspection. The font is the
# same build-time compatibility copy that is installed in the Click package.
add_executable(test_emoji_font test_emoji_font.cpp)
target_link_libraries(test_emoji_font PRIVATE Qt5::Gui Qt5::Qml)
target_compile_options(test_emoji_font PRIVATE -Wall -Wextra -Wpedantic)
target_compile_definitions(test_emoji_font PRIVATE
    SIGNAL_EMOJI_FONT="${SIGNAL_EMOJI_FONT}"
    SIGNAL_EMOJI_CATALOGUE="${CMAKE_SOURCE_DIR}/qml/EmojiData.js")
add_test(NAME emoji-font COMMAND test_emoji_font)
set_tests_properties(emoji-font PROPERTIES TIMEOUT 30 ENVIRONMENT "QT_QPA_PLATFORM=offscreen")

# Native transport regression: 14 MiB binary (>16 MiB as JSON Base64),
# strict validation, chunk boundaries, write and size limits.
add_executable(test_attachment_base64 test_attachment_base64.cpp)
target_include_directories(test_attachment_base64 PRIVATE ../src)
target_compile_options(test_attachment_base64 PRIVATE -Wall -Wextra -Wpedantic)
add_test(NAME attachment-base64 COMMAND test_attachment_base64)
set_tests_properties(attachment-base64 PROPERTIES TIMEOUT 30)
