# Copyright (C) 2026 Joachim Welsch <https://welsch-software.com>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of AFS_Core, the synchronisation engine of Autofilesync.
#
# AFS_Core is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# AFS_Core is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# AFS_Core. If not, see <https://www.gnu.org/licenses/>.
cmake_minimum_required(VERSION 3.16)

# Builds standalone (cmake -S AFS_Core/test/Integrationstests) or as part of the
# top-level project with -DAFS_BUILD_TESTS=ON.
project(IntegrationtestProject LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -Wextra")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")

include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/AfsCore.cmake)
find_package(GTest REQUIRED)

enable_testing()

# _TEST opens up Synchronize, SyncFile and SyncFileSsh for the tests and changes
# their class layouts, so the tests get their own build of the core instead of
# the library the application links. No wxWidgets hooks here.
afs_core_add_library(afs_core_test DEFINITIONS _TEST)

set(SOURCES
    pch.cpp
    FileUtil.cpp
    DirUtil.cpp
    SmtpSink.cpp
    SyncFileTests.cpp
    SyncDirTests.cpp
    DifficultFilenameTests.cpp
    DifficultDirnameTests.cpp
    SyncItemTests.cpp
    ConfigTests.cpp
    CopyLogTests.cpp
    FileDeleteTests.cpp
    PasswordCryptoTests.cpp
    FakePhone.cpp
    DeviceTests.cpp
    LlamaClientTests.cpp
    HelpKnowledgeTests.cpp
    ScopedEnv.cpp
    FileDifferenceTests.cpp
    Libssh2InitTests.cpp
    SyncTreeTests.cpp
    TwoWaySyncTests.cpp
    SyncFlagTests.cpp
    LocalPermissionTests.cpp
    CopyFailureTests.cpp
    SyncSshFailureTests.cpp
    SymlinkTests.cpp
    SymlinkAsLinkTests.cpp
    SymlinkSshTests.cpp
    SymlinkCloudTests.cpp
    SymlinkFollowTests.cpp
    UnpluggedDriveTests.cpp
    MissingDirectoryTests.cpp
    LockedFileTests.cpp
    DiskFullTests.cpp
    ReadOnlyFilesystemTests.cpp
    WriteErrorTests.cpp
    SynchronizeTests.cpp
    SyncArbiterTests.cpp
    CheckPathTests.cpp
    LogAnalysisTests.cpp
    ModelLocationTests.cpp
    CloudFoldersTests.cpp
    RestoreTests.cpp
    WebDavTests.cpp
    GoogleDriveTests.cpp
    GoogleDriveLiveTests.cpp
    OneDriveTests.cpp
    DropboxTests.cpp
    S3Tests.cpp
    WebDavTestServer.cpp
    SyncProposalTests.cpp
    SyncPlanTests.cpp
    SyncStateTests.cpp
    SyncStateRunTests.cpp
    ChangeWatcherTests.cpp
    ConfigSummaryTests.cpp
    ItemStatusTests.cpp
    ScheduleTests.cpp
    AgeTests.cpp
    TargetEncryptionTests.cpp
    RestoreTreeTests.cpp
    GenerationsTests.cpp
    BinCleanupTests.cpp
    SyncSshFileTests.cpp
    SyncSshDirTests.cpp
    SyncSshTreeTests.cpp
    SyncSshTwoWayTests.cpp
    SyncSshToSshTests.cpp
    SshPathEncodingTests.cpp
    SshHostKeyTests.cpp
    SshLoginTests.cpp
    SshConnectTimeoutTests.cpp
    SshDirInspectorTests.cpp
    SshTest.cpp
    TestMain.cpp
)

# The tests of the general-purpose modules lie beside them in Utilities/test; they
# run in this program because Utilities is compiled into the core and its tests
# use the private temp directory and home of TestMain.cpp.
add_executable(Integrationtests ${SOURCES} ${UTILITIES_TEST_SOURCES})
target_include_directories(Integrationtests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
# The daemon's worker (backgroundsync/MyDaemon.cpp) is tested here as well: it
# is nothing but AFS_Core calls, and a test program of its own would have to
# repeat the private temp directory and home of TestMain.cpp. Skipped when
# AFS_Core is built without the rest of the repository.
set(AFS_DAEMON_DIR ${AFS_CORE_DIR}/../backgroundsync)
if(EXISTS ${AFS_DAEMON_DIR}/MyDaemon.cpp)
    target_sources(Integrationtests PRIVATE DaemonTests.cpp ${AFS_DAEMON_DIR}/MyDaemon.cpp)
    target_include_directories(Integrationtests PRIVATE ${AFS_DAEMON_DIR})
endif()
target_link_libraries(Integrationtests PRIVATE afs_core_test GTest::GTest)
# Root of AFS_Core: the fixtures find test/resources and test/tree below it.
target_compile_definitions(Integrationtests PRIVATE LIB_SOURCE_DIR="${AFS_CORE_DIR}")
# The translation tests read the catalogues beside the test binary, so a build
# of the test binary alone must compile them too - an empty locale directory
# makes the tests fail instead of skipping. The target exists only when the
# top-level project with msgfmt configures the build.
if(TARGET translations)
    add_dependencies(Integrationtests translations)
endif()

# Register each TEST() case individually with CTest instead of the binary as a
# whole. DISCOVERY_MODE PRE_TEST: the default POST_BUILD runs the binary from a
# script phase, which the Xcode generator schedules before code signing - macOS
# kills an unsigned binary at once. RUN_SERIAL: the tests share fixed rather
# than random directory paths and would disturb each other under real ctest
# parallelism.
include(GoogleTest)
gtest_discover_tests(Integrationtests DISCOVERY_MODE PRE_TEST PROPERTIES RUN_SERIAL TRUE)
