# syntax = docker/dockerfile:1.11-labs

FROM input AS rust-sdk-integration
ARG sys_name
ARG sys_version
ARG feat_set
ARG rust_target
ARG rust_toolchain
ARG cargo_profile
ARG RUSTUP_HOME
ARG CARGO_HOME
ARG CARGO_TARGET
ARG MRSDK_TARGET_DIR="/usr/src/matrix-rust-sdk/target"
ARG mrsdk_target_share
#ARG mrsdk_ref="integration"
ARG mrsdk_ref="tuwunel-changes"
ARG mrsdk_test_args=""
ARG mrsdk_test_opts=""
ARG mrsdk_skip_list=""
ARG mrsdk_parallel=2
ARG mrsdk_startup_delay="10s"
ARG mrsdk_testee="/usr/bin/tuwunel"

WORKDIR /usr/src
ADD --link https://github.com/matrix-construct/matrix-rust-sdk.git#${mrsdk_ref} matrix-rust-sdk

WORKDIR /etc
COPY <<EOF tuwunel.toml
    [global]
    admin_room_notices = false
    allow_device_name_federation = true
    allow_guest_registration = true
    allow_legacy_media = true
    allow_public_room_directory_over_federation = true
    allow_public_room_directory_without_auth = true
    allow_registration = true
    create_admin_room = false
    error_on_unknown_config_opts = true
    ip_range_denylist = []
    log = "debug,tuwunel=trace,h2=warn,hyper=warn"
    log_colors = false
    log_guest_registrations = false
    log_span_events = "NONE"
    log_thread_ids = true
    media_compat_file_link = false
    media_startup_check = true
    query_trusted_key_servers_first = false
    query_trusted_key_servers_first_on_join = false
    rocksdb_log_level = "debug"
    rocksdb_max_log_files = 1
    rocksdb_paranoid_file_checks = true
    rocksdb_recovery_mode = 0
    trusted_servers = []
    url_preview_domain_contains_allowlist = ["*"]
    url_preview_domain_explicit_denylist = ["*"]
    yes_i_am_very_very_sure_i_want_an_open_registration_server_prone_to_abuse = true
EOF

WORKDIR /usr/lib
COPY --link --from=install /usr/lib .

WORKDIR /usr/bin
COPY --link --from=install /usr/bin/tuwunel .

WORKDIR /usr/src/matrix-rust-sdk
SHELL ["/bin/bash", "-c"]
ENV RUST_BACKTRACE="full"
ENV TUWUNEL_CONFIG="/etc/tuwunel.toml"
ENV TUWUNEL_DATABASE_PATH="/var/db/tuwunel"
ENV TUWUNEL_SERVER_NAME="localhost"
ENV TUWUNEL_PORT="[8448]"
ENV HOMESERVER_URL="http://localhost:8448"
RUN \
--mount=type=cache,dst=${RUSTUP_HOME}/downloads,sharing=shared \
--mount=type=cache,dst=${CARGO_HOME}/registry,sharing=shared \
--mount=type=cache,dst=${CARGO_HOME}/git,sharing=shared \
--mount=type=cache,dst=${MRSDK_TARGET_DIR},id=${mrsdk_target_share},sharing=locked \
<<EOF
    set -eux

    nohup ${mrsdk_testee[@]} 1> /var/log/tuwunel.log &
    PID=$!; trap "sleep 10s; set +e; kill -QUIT ${PID}; wait ${PID}" EXIT
    sleep "${mrsdk_startup_delay}"

    rustup run ${rust_toolchain} \
        cargo test \
            --locked \
            --release \
            "--color=always" \
            "--features=default" \
            "--target=${rust_target}" \
            "--target-dir=${MRSDK_TARGET_DIR}" \
            "--package=matrix-sdk-integration-testing" \
            ${mrsdk_test_args[@]} \
            -- \
            "--color=always" \
            "--test-threads=${mrsdk_parallel}" \
            ${mrsdk_skip_list[@]} \
            ${mrsdk_test_opts[@]} \
            ;
EOF
