#!/bin/sh
# autopkgtest: run the standalone JACK application of the rncbc.org
# "v1" synthesizer family (drumkv1, samplv1, synthv1, padthv1) with the
# full GUI inside Xvfb and verify that it
#
#   1. starts up and registers its JACK client with the expected audio
#      and MIDI ports,
#   2. actually renders audio for a played MIDI note (measured through
#      a second JACK client, see jack_probe.c),
#   3. terminates cleanly on SIGTERM.
#
# This smoke test exercises the Qt GUI, the JACK client integration,
# the MIDI input path, the audio engine and the shutdown handling.
#
# AI assisted code, reviewed and tested by snd@debian.org

set -eu

TESTDIR=$(cd "$(dirname "$0")" && pwd)
. "$TESTDIR/common.sh"

detect_app
NOTE=${RNCBC_NOTE:-$DEF_NOTE}
RUN_SECONDS=${RNCBC_SECONDS:-6}
MIN_PEAK=${RNCBC_MIN_PEAK:-0.001}

setup_env
trap cleanup EXIT
trap "cleanup; exit 130" INT
trap "cleanup; exit 143" TERM

log "building the JACK probe helper"
PROBE="$WORKDIR/jack_probe"
cc -O2 -o "$PROBE" "$TESTDIR/jack_probe.c" \
	$(pkgconf --cflags --libs jack) -lm \
	|| fail "cannot build jack_probe"

"$PROBE" make-wav "$WORKDIR/sample.wav" \
	|| fail "cannot generate the test sample"

set -- 
case $APP in
drumkv1)
	write_drumkv1_preset "$WORKDIR/sample.wav" "$NOTE" \
		"$WORKDIR/preset.xml"
	set -- "$WORKDIR/preset.xml"
	;;
samplv1)
	write_samplv1_preset "$WORKDIR/sample.wav" "$WORKDIR/preset.xml"
	set -- "$WORKDIR/preset.xml"
	;;
esac

start_xvfb
start_jack "$PROBE"

CLIENT="rncbc-${APP}-autopkgtest"
log "starting ${APP}_jack (JACK client '$CLIENT', note $NOTE)"
"${APP}_jack" -n "$CLIENT" "$@" >"$WORKDIR/app.log" 2>&1 &
APP_PID=$!

log "waiting for the JACK client and its ports to register"
registered=0
i=0
while [ "$i" -lt 90 ]; do
	if ! kill -0 "$APP_PID" 2>/dev/null; then
		wait "$APP_PID" 2>/dev/null || true
		fail "application terminated during startup"
	fi
	if "$PROBE" wait-client --client "$CLIENT" --timeout 2 >&2; then
		registered=1
		break
	fi
	i=$((i + 1))
done
[ "$registered" = 1 ] \
	|| fail "JACK client '$CLIENT' did not register a MIDI input and two audio outputs"

# Make sure the process is still healthy after registration.
kill -0 "$APP_PID" 2>/dev/null \
	|| fail "application terminated right after JACK registration"

log "feeding MIDI and measuring the rendered audio for ${RUN_SECONDS}s"
"$PROBE" run --note "$NOTE" --seconds "$RUN_SECONDS" \
	--dest-client "$CLIENT" --audio-client "$CLIENT" \
	--min-peak "$MIN_PEAK" \
	|| fail "application did not render any audio for MIDI note $NOTE"

log "requesting graceful shutdown (SIGTERM)"
kill -TERM "$APP_PID" 2>/dev/null || true

# Kill the application if it does not honour SIGTERM within 30 seconds,
# so that the test cannot hang forever on a broken shutdown path.
(
	i=0
	while [ "$i" -lt 150 ]; do
		sleep 0.2
		i=$((i + 1))
	done
	kill -KILL "$APP_PID" 2>/dev/null || true
) &
WATCHDOG_PID=$!

set +e
wait "$APP_PID" 2>/dev/null
APP_RC=$?
set -e
APP_PID=
kill "$WATCHDOG_PID" 2>/dev/null || true
wait "$WATCHDOG_PID" 2>/dev/null || true
WATCHDOG_PID=

if [ "$APP_RC" -eq 137 ]; then
	fail "application did not terminate within 30s after SIGTERM"
fi
[ "$APP_RC" -eq 0 ] \
	|| fail "application exited with status $APP_RC on SIGTERM"

log "PASS: standalone ${APP} started, rendered audio and shut down cleanly"
