#!/bin/sh

mkdir -p debian/test-home/.config/dconf/
mkdir -p debian/test-home/.cache/dconf/
export HOME=$(pwd)/debian/test-home

xvfb-run -- dbus-run-session -- /bin/sh <<'EOF'
export XAUTHORITY=/dev/null
export LOMIRI_CONTENT_HUB_TESTING=1
export LOMIRI_CONTENT_HUB_LOGGING_LEVEL=2
export QT_LOGGING_RULES="*.debug=true"
export QT_MESSAGE_PATTERN="[%{time process} %{type}] %{appname} %{category} %{function} - %{message}"

lomiri-content-hub-service &
trap "kill -9 $! 2>/dev/null" EXIT TERM QUIT INT

# wait for lomiri-content-hub service to be fully responsive
sleep 2

FAILED=

lomiri-content-hub-test-sharer &
pid=$!
if ! timeout -s 9 30 lomiri-content-hub-send "content:?pkg=lomiri-content-hub-test-sharer&handler=share&text=text"; then
    FAILED="${FAILED:+${FAILED} }share"
fi
kill -9 $pid 2>/dev/null

lomiri-content-hub-test-importer &
pid=$!
if ! timeout -s 9 30 lomiri-content-hub-send "content:?pkg=lomiri-content-hub-test-importer&handler=export&text=text"; then
    FAILED="${FAILED:+${FAILED} }import"
fi
timeout -s 9 30 lomiri-content-hub-send "content:?pkg=lomiri-content-hub-test-importer&handler=export&url=file:///etc/issue"
if [ $? -ne 1 ]; then
    FAILED="${FAILED:+${FAILED} }reject-files"
fi
kill -9 $pid 2>/dev/null

if [ -z "$FAILED" ]; then
    echo "All tests passed"
    exit 0
else
    echo "$FAILED failed"
    exit 1
fi
EOF
