#!/bin/sh
# Run the upstream integration tests that need nothing but the installed gtty
# binaries and an X display. They drive a private gtty over its XML-RPC, telnet
# and UDP ports the way real loggers do: CQRLOG polls it like its own fldigi
# interface, and a logged QSO is watched as QLog, KLog and WaveLogGate read it.
#
# The scripts are opt-in under ctest (GTTY_INTEGRATION=1) because a build tree
# normally has no display. Here they are run directly, and a SKIP exit (77) is
# reported as a failure: this test declares everything these scripts look
# for, so skipping would mean the testbed is broken, not that the test is
# inapplicable.
#
# Left out on purpose:
#   test_cqrlog's          it transmits a message and waits for the AFSK
#   Qso.test_trx_state_    modulator to drain; a testbed has no sound card, so
#   follows_the_transmitter gtty never unkeys and the test waits 40 s for
#                          receive. The rest of that script runs below.
#   test_not1mm,           drive gtty with Not1MM's own clients and GUI and
#   test_not1mm_gui        expect the whole message come out; that too needs
#                          working audio (the GUI part also needs PulseAudio
#                          for its virtual band).
#   test_tlf               its auto-QSY test demands the carrier offset back
#                          to the exact Hz, and its receive test decodes RTTY
#                          played through a virtual sound device; both fail
#                          intermittently (observed with build-tree and
#                          installed binaries alike)
#   test_spot_feed         same virtual sound device, and no CQ was spotted
#                          when run here
# Those still run upstream (ctest with GTTY_INTEGRATION=1).
set -u

export GTTY_BIN=/usr/bin/gtty
export GTTY_CLI=/usr/bin/gtty-cli
export NO_AT_BRIDGE=1
export GSETTINGS_BACKEND=memory

# xvfb-run only replaces DISPLAY. When the caller's session also has a Wayland
# display, GTK prefers it and the test windows open on the real desktop,
# outside the Xvfb the tests are supposed to be isolated in.
export GDK_BACKEND=x11
unset WAYLAND_DISPLAY

# GTK wants a runtime directory of its own; autopkgtest does not always give
# the test one.
if [ -z "${XDG_RUNTIME_DIR:-}" ]; then
    XDG_RUNTIME_DIR=$(mktemp -d)
    chmod 700 "$XDG_RUNTIME_DIR"
    export XDG_RUNTIME_DIR
fi

# autopkgtest runs the test in the unpacked source tree; being called from
# somewhere else (by hand) still works.
src=$PWD
if [ ! -d "$src/tests/integration" ]; then
    src=$(cd "$(dirname "$0")/../.." && pwd)
fi
cd "$src"

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

failed=
for t in test_cqrlog test_log_forwarding; do
    echo "=== $t"
    # Everything but test_cqrlog's transmit test, which needs a sound card.
    args=
    [ "$t" = test_cqrlog ] && args="Polling Qso.test_save_clears_the_log_row"
    status=0
    xvfb-run -a dbus-run-session -- python3 "tests/integration/$t.py" $args \
        >"$tmp/$t.log" 2>&1 || status=$?
    cat "$tmp/$t.log"
    [ "$status" -eq 0 ] && continue
    if [ "$status" -eq 77 ] || grep -q '^SKIP:' "$tmp/$t.log"; then
        echo "FAIL: $t skipped; it expects everything in debian/tests/control"
    fi
    failed="$failed $t"
done

if [ -n "$failed" ]; then
    echo "FAILED:$failed" >&2
    exit 1
fi
