#!/usr/bin/env bash
# tool/test.sh — flutter test wrapper with sqlite3 workaround.
#
# WHY THIS EXISTS:
#   BadNote vendors sqlite3 native binaries under vendor/sqlite3/ (selected via
#   the pubspec.yaml `hooks.user_defines.sqlite3.source: test-sqlite3` block).
#   On Linux the vendored file is `vendor/sqlite3/libsqlite3.x64.linux.so`.
#   Without pointing the dynamic linker at it, `flutter test` either falls back
#   to a system sqlite3 (wrong version / missing) or tries to download one at
#   build time (blocked behind the GFW on this machine).
#
#   Setting LD_LIBRARY_PATH to the vendor dir tells the linker to prefer the
#   vendored shared library.  The Flutter toolchain here (3.41.4 / Dart 3.10.8)
#   does NOT forward proxy env vars to build hooks, so LD_LIBRARY_PATH is the
#   reliable workaround for local Linux development.
#
#   On Windows CI the vendored sqlite3.x64.windows.dll is picked up
#   automatically by the native-asset build — no wrapper needed there.
#
# USAGE:
#   tool/test.sh                          # run all tests
#   tool/test.sh test/foo_test.dart       # run a specific test file
#   tool/test.sh --coverage               # pass any flutter test flags

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"

VENDOR_SQLITE="${PROJECT_ROOT}/vendor/sqlite3"

if [ ! -d "${VENDOR_SQLITE}" ]; then
  echo "WARNING: vendor/sqlite3/ not found at ${VENDOR_SQLITE}" >&2
  echo "         Proceeding without LD_LIBRARY_PATH override." >&2
else
  export LD_LIBRARY_PATH="${VENDOR_SQLITE}${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
fi

exec flutter test "$@"
