#!/usr/bin/env bash
# Build and run the Qt app's tests (console asserts, no framework). The
# installer and Rust suites run from the root bin/check.
set -euo pipefail
cd "$(dirname "$0")/../tests"
# Everything runs on a private D-Bus session bus, so the tests' fake
# icloud-session never meets the desktop's bus or a real daemon.
if [[ "${ICLOUD_NOTES_PRIVATE_BUS:-}" != 1 ]]; then
  export ICLOUD_NOTES_PRIVATE_BUS=1
  exec dbus-run-session --config-file="$PWD/session-bus.conf" -- "$PWD/../bin/test" "$@"
fi
# The tests' stub engine answers vault-info with the real one (the app's
# view of the vault's state), built from this checkout.
cargo build --quiet --locked -p icloud-notes-sync --manifest-path "$PWD/../../Cargo.toml"
export ICLOUD_NOTES_SYNC_REAL="${CARGO_TARGET_DIR:-$PWD/../../target}/debug/icloud-notes-sync"
export QT_QPA_PLATFORM="${QT_QPA_PLATFORM:-offscreen}"
export NO_AT_BRIDGE=1 # the private bus has no accessibility bus to find
for t in guardrails_test backend_test sequence_test layout_test cli_test; do
  mkdir -p "build-$t"
  (cd "build-$t" && qmake6 "../$t.pro" && make -j"$(nproc)" && "./$t")
done

