#!/usr/bin/env bash
# pre-push: run the same checks as .github/workflows/ci.yml before the push
# reaches GitHub Actions. CI minutes cost money — fail locally first.
#
# Opt in to the integration suite by exporting SURQL_PRE_PUSH_INTEGRATION=1
# and booting the v3.0.5 container:
#
#   docker run -d -p 8000:8000 --name surrealdb surrealdb/surrealdb:v3.0.5 \
#     start --user root --pass root memory
#
# Bypass (rarely): `git push --no-verify` — only when explicitly authorised.

set -euo pipefail

echo "[pre-push] cargo fmt --all --check"
cargo fmt --all --check

echo "[pre-push] cargo clippy --all-targets --all-features -- -D warnings"
cargo clippy --all-targets --all-features -- -D warnings

echo "[pre-push] cargo test --lib --all-features"
cargo test --lib --all-features

echo "[pre-push] cargo test --doc --all-features"
cargo test --doc --all-features

if [[ "${SURQL_PRE_PUSH_INTEGRATION:-0}" == "1" ]]; then
  echo "[pre-push] cargo test --test '*' --all-features (SURQL_PRE_PUSH_INTEGRATION=1)"
  cargo test --test '*' --all-features -- --test-threads=1
else
  echo "[pre-push] skipping integration tests (set SURQL_PRE_PUSH_INTEGRATION=1 to run)"
fi

echo "[pre-push] all checks passed"
