# tests 子工程最低 CMake 版本要求（与主工程保持一致）
cmake_minimum_required(VERSION 3.10)

# 预留：测试通用源码列表（当前未直接使用，可用于后续统一链接）
set(DTU_TEST_COMMON_SOURCES
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/ascii.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/lcd.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/key.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/menu/app/menu.c"
    "${CMAKE_SOURCE_DIR}/src/TCP/tcp.c"
    "${CMAKE_SOURCE_DIR}/src/thread_utils.c"
    "${CMAKE_SOURCE_DIR}/src/remoteDisplay.c"
)

# 封装测试目标创建逻辑：
# - add_executable: 生成测试可执行文件
# - target_include_directories: 注入项目头文件路径
# - target_link_libraries: 按平台链接系统库
# - add_test: 注册到 CTest，支持 ctest 统一执行
function(add_dtu_test test_name)
    add_executable(${test_name} ${ARGN})
    target_sources(${test_name} PRIVATE
        "${CMAKE_SOURCE_DIR}/src/common/utf8.c"
    )
    # 测试目标可见的头文件目录
    target_include_directories(${test_name} PRIVATE
        "${CMAKE_SOURCE_DIR}/include"
        "${CMAKE_SOURCE_DIR}/src"
        "${CMAKE_SOURCE_DIR}/src/TCP"
        "${CMAKE_SOURCE_DIR}/src/Drv"
        "${CMAKE_SOURCE_DIR}/src/Drv/lcd"
        "${CMAKE_SOURCE_DIR}/src/common"
    )
    # 平台差异：Windows 需要 ws2_32，非 Windows 使用 pthread
    if(WIN32)
        target_link_libraries(${test_name} PRIVATE ws2_32)
    else()
        target_link_libraries(${test_name} PRIVATE pthread)
    endif()
    # 将测试程序注册为 CTest 用例，名称与目标名一致
    add_test(NAME ${test_name} COMMAND ${test_name})
endfunction()

# ------------------------------------------------------------
# Smoke 测试：验证测试框架本身可用
# ------------------------------------------------------------
add_dtu_test(test_smoke tests_smoke.c)

# ------------------------------------------------------------
# P0：高价值纯逻辑测试
# ------------------------------------------------------------
add_dtu_test(
    test_p0_remote_display
    test_p0_remote_display.c
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/lcd.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/lcd_draw.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/lcd_text.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/ascii.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/key.c"
    "${CMAKE_SOURCE_DIR}/src/TCP/tcp.c"
    "${CMAKE_SOURCE_DIR}/src/thread_utils.c"
)
add_dtu_test(
    test_p0_utf8_hz12_get
    test_p0_utf8_hz12_get.c
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/ascii.c"
)

# ------------------------------------------------------------
# P1：业务核心计算/状态流转测试
# ------------------------------------------------------------

# ------------------------------------------------------------
# P1：lcd基本测试
# ------------------------------------------------------------
add_dtu_test(
    test_p1_lcd_basic
    test_p1_lcd_basic.c
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/lcd.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/lcd_draw.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/lcd_text.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/ascii.c"
)
# ------------------------------------------------------------
# P1：菜单管理器测试
# ------------------------------------------------------------
add_dtu_test(
    test_p1_page_manager
    test_p1_page_manager.c
    "${CMAKE_SOURCE_DIR}/src/Drv/pages/page_manager.c"
)
# ------------------------------------------------------------
# P1：菜单测试
# ------------------------------------------------------------
add_dtu_test(
    test_p1_menu
    test_p1_menu.c
    "${CMAKE_SOURCE_DIR}/src/Drv/menu/app/menu.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/pages/page_manager.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/pages/menu/model.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/pages/menu/view.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/pages/menu/presenter.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/pages/menu/page.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/pages/global/global_state.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/pages/global/renderer_lcd.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/lcd_text.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/lcd.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/lcd_draw.c"
    "${CMAKE_SOURCE_DIR}/src/Drv/lcd/ascii.c"
)
target_compile_definitions(test_p1_menu PRIVATE UNIT_TEST)
# ------------------------------------------------------------
# P1：key测试
# ------------------------------------------------------------
add_dtu_test(
    test_p1_key
    test_p1_key.c
    "${CMAKE_SOURCE_DIR}/src/Drv/key.c"
)
# ------------------------------------------------------------
# P2：集成测试（网络回环等）
# ------------------------------------------------------------
add_dtu_test(
    test_p2_tcp_loopback
    test_p2_tcp_loopback.c
    "${CMAKE_SOURCE_DIR}/src/TCP/tcp.c"
    "${CMAKE_SOURCE_DIR}/src/thread_utils.c"
)
