cmake_minimum_required(VERSION 3.10)

set(TEST_FRAMEWORK_SOURCES
    test_framework.c
    fixtures/fixtures.c
    fixtures/http_request_builder.c
)

set(UNIT_TEST_SOURCES
    unit/test_http_parser.c
    unit/test_http_response.c
    unit/test_auth_basic.c
    unit/test_auth_digest.c
    unit/test_storage_path.c
    unit/test_user_database.c
    unit/test_lock_manager.c
)

set(INTEGRATION_WEBDAV_SOURCES
    integration/webdav_methods/test_options.c
    integration/webdav_methods/test_get.c
    integration/webdav_methods/test_put.c
    integration/webdav_methods/test_delete.c
    integration/webdav_methods/test_mkcol.c
    integration/webdav_methods/test_copy.c
    integration/webdav_methods/test_move.c
    integration/webdav_methods/test_propfind.c
    integration/webdav_methods/test_proppatch.c
    integration/webdav_methods/test_lock.c
    integration/webdav_methods/test_unlock.c
)

set(INTEGRATION_HTTP_SOURCES
    integration/http_methods/test_head.c
    integration/http_methods/test_post.c
)

set(INTEGRATION_AUTH_SOURCES
    integration/auth_flows/test_auth_basic_flow.c
    integration/auth_flows/test_auth_digest_flow.c
    integration/auth_flows/test_401_challenge.c
)

set(MANAGEMENT_SOURCES
    management/user_management/test_user_crud.c
    management/quota_management/test_quota.c
    management/cgi_settings/test_cgi_config.c
)

set(NWEDAV_LIB_SOURCES
    ../src/platform/pal.c
    ../src/platform/file.c
    ../src/platform/time.c
    ../src/platform/memory.c
    ../src/core/config.c
    ../src/core/logging.c
    ../src/core/server.c
    ../src/user/usermgr.c
    ../src/user/password.c
    ../src/http/parser.c
    ../src/http/response.c
    ../src/http/headers.c
    ../src/http/streaming.c
    ../src/http/webserve.c
    ../src/webdav/xml.c
    ../src/webdav/methods.c
    ../src/webdav/propfind.c
    ../src/webdav/proppatch.c
    ../src/webdav/lock.c
    ../src/storage/backend.c
    ../src/storage/properties.c
    ../src/storage/locks.c
    ../src/storage/pathmapping.c
    ../src/auth/auth.c
    ../src/cgi/cgi.c
    ../src/concurrency/threadpool.c
    ../src/concurrency/eventloop.c
    ../src/concurrency/connpool.c
)

add_executable(run_tests
    run_tests.c
    ${TEST_FRAMEWORK_SOURCES}
    ${UNIT_TEST_SOURCES}
    ${NWEDAV_LIB_SOURCES}
)

target_include_directories(run_tests PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/include
    ${CMAKE_SOURCE_DIR}/src
    ${SQLITE3_INCLUDE_DIRS}
    ${OPENSSL_INCLUDE_DIRS}
)

target_link_libraries(run_tests
    Threads::Threads
    ${SQLITE3_LIBRARIES}
    ${OPENSSL_LIBRARIES}
    ${PLATFORM_LIBS}
)

if(UUID_FOUND)
    target_include_directories(run_tests PRIVATE ${UUID_INCLUDE_DIRS})
    target_link_libraries(run_tests ${UUID_LIBRARIES})
endif()

# Integration tests disabled - need missing headers (request.h, handler.h) implemented
# add_executable(run_integration_tests
#     run_tests.c
#     ${TEST_FRAMEWORK_SOURCES}
#     ${INTEGRATION_WEBDAV_SOURCES}
#     ${INTEGRATION_HTTP_SOURCES}
#     ${INTEGRATION_AUTH_SOURCES}
#     ${NWEDAV_LIB_SOURCES}
# )

add_executable(run_management_tests
    run_tests.c
    ${TEST_FRAMEWORK_SOURCES}
    ${MANAGEMENT_SOURCES}
    ${NWEDAV_LIB_SOURCES}
)

target_include_directories(run_management_tests PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/include
    ${CMAKE_SOURCE_DIR}/src
    ${SQLITE3_INCLUDE_DIRS}
    ${OPENSSL_INCLUDE_DIRS}
)

target_link_libraries(run_management_tests
    Threads::Threads
    ${SQLITE3_LIBRARIES}
    ${OPENSSL_LIBRARIES}
    ${PLATFORM_LIBS}
)

if(UUID_FOUND)
    target_include_directories(run_management_tests PRIVATE ${UUID_INCLUDE_DIRS})
    target_link_libraries(run_management_tests ${UUID_LIBRARIES})
endif()

add_executable(run_all_tests
    run_tests.c
    ${TEST_FRAMEWORK_SOURCES}
    ${UNIT_TEST_SOURCES}
    ${MANAGEMENT_SOURCES}
    ${NWEDAV_LIB_SOURCES}
)

target_include_directories(run_all_tests PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/include
    ${CMAKE_SOURCE_DIR}/src
    ${SQLITE3_INCLUDE_DIRS}
    ${OPENSSL_INCLUDE_DIRS}
)

target_link_libraries(run_all_tests
    Threads::Threads
    ${SQLITE3_LIBRARIES}
    ${OPENSSL_LIBRARIES}
    ${PLATFORM_LIBS}
)

if(UUID_FOUND)
    target_include_directories(run_all_tests PRIVATE ${UUID_INCLUDE_DIRS})
    target_link_libraries(run_all_tests ${UUID_LIBRARIES})
endif()

add_test(NAME unit_tests COMMAND run_tests)
add_test(NAME management_tests COMMAND run_management_tests)
add_test(NAME all_tests COMMAND run_all_tests)

set_tests_properties(unit_tests PROPERTIES LABELS "unit")
set_tests_properties(management_tests PROPERTIES LABELS "management")
set_tests_properties(all_tests PROPERTIES LABELS "all")
