# //! 第一阶段只链接 Fcitx5 默认面板与 JSON IPC。
cmake_minimum_required(VERSION 3.16)
project(qingjian-fcitx5 VERSION 0.1.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Fcitx5Core 5.1.8 REQUIRED)
find_package(nlohmann_json 3.2 REQUIRED)
include(CTest)
set(QINGJIAN_SOURCES src/qingjian.cpp src/ipc/connection.cpp src/key/mapping.cpp)
add_library(qingjian MODULE src/factory.cpp ${QINGJIAN_SOURCES})
target_include_directories(qingjian PRIVATE src)
target_link_libraries(qingjian PRIVATE Fcitx5::Core nlohmann_json::nlohmann_json)
set_target_properties(qingjian PROPERTIES PREFIX "")
install(TARGETS qingjian LIBRARY DESTINATION lib/fcitx5)
install(FILES data/addon/qingjian.conf DESTINATION share/fcitx5/addon)
install(FILES data/inputmethod/qingjian.conf DESTINATION share/fcitx5/inputmethod)
if(BUILD_TESTING)
    find_package(Threads REQUIRED)
    foreach(test integration lifecycle real reentry privacy multiplex reentrant transport rescore)
        add_executable(test-${test} tests/${test}.cpp ${QINGJIAN_SOURCES})
        target_include_directories(test-${test} PRIVATE src tests)
        target_link_libraries(test-${test} PRIVATE Fcitx5::Core nlohmann_json::nlohmann_json Threads::Threads)
        target_compile_options(test-${test} PRIVATE -UNDEBUG)
    endforeach()
    set(QINGJIAN_SERVER_EXECUTABLE "${CMAKE_CURRENT_SOURCE_DIR}/../../../target/debug/qingjian-linux-server" CACHE FILEPATH "已构建的 Rust Server")
    foreach(test multiplex reentrant)
        target_compile_definitions(test-${test} PRIVATE QINGJIAN_SERVER_EXECUTABLE="${QINGJIAN_SERVER_EXECUTABLE}")
    endforeach()
    add_test(NAME shared-connection COMMAND test-multiplex)
    set_tests_properties(shared-connection PROPERTIES TIMEOUT 30)
    foreach(case old-version timeout malformed session)
        add_test(NAME transport-${case} COMMAND test-transport ${case})
        set_tests_properties(transport-${case} PROPERTIES TIMEOUT 5)
    endforeach()
    foreach(case vkeyboard-deactivate vkeyboard-focusout capability-destroy disconnect-destroy commit-reentry ui-reentry ui-destroy)
        add_test(NAME shared-${case} COMMAND test-reentrant ${case})
        set_tests_properties(shared-${case} PROPERTIES TIMEOUT 15)
    endforeach()
    add_test(NAME real-server COMMAND test-real "${QINGJIAN_SERVER_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/../../..")
    set_tests_properties(real-server PROPERTIES TIMEOUT 15)
    foreach(case password sensitive disable reset ui-reset focus click destroy display-failure)
        add_test(NAME reentry-${case} COMMAND test-reentry "${QINGJIAN_SERVER_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/../../.." ${case})
        set_tests_properties(reentry-${case} PROPERTIES TIMEOUT 15)
    endforeach()
    foreach(case sensitive-reset sensitive-focus sensitive-key password-reset disable-focus superseded-password superseded-disable superseded-sensitive)
        add_test(NAME privacy-${case} COMMAND test-privacy "${QINGJIAN_SERVER_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/../../.." ${case})
        set_tests_properties(privacy-${case} PROPERTIES TIMEOUT 15)
    endforeach()
    foreach(mode both inline window failure mismatch)
        add_test(NAME panel-${mode} COMMAND test-integration ${mode})
    endforeach()
    add_test(NAME panel-rescore COMMAND test-rescore)
    foreach(case focusout-inline focusout-panel focusout-client-owned sensitive password disabled empty capability-deactivate shift-empty)
        add_test(NAME lifecycle-${case} COMMAND test-lifecycle ${case})
    endforeach()
endif()
