#!/usr/bin/env bash
set -Eeuo pipefail

project_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
fixture_dir="$(mktemp -d)"
trap 'rm -rf "$fixture_dir"' EXIT

cat >"$fixture_dir/build.log" <<'EOF'
Installing packages
ERROR: No matching distribution found for example==9.9
authorization: bearer-super-secret
Worker exited with exit code 137
EOF

python3 "$project_dir/ci-rescue.py" "$fixture_dir/build.log" -o "$fixture_dir/report.md"
grep -q 'Dependency resolution' "$fixture_dir/report.md"
grep -q 'Memory exhaustion' "$fixture_dir/report.md"
grep -Fq 'authorization: [REDACTED]' "$fixture_dir/report.md"
if grep -q 'bearer-super-secret' "$fixture_dir/report.md"; then
  echo "secret redaction failed" >&2
  exit 1
fi

printf 'unrecognized failure\n' | python3 "$project_dir/ci-rescue.py" - >"$fixture_dir/unknown.md"
grep -q 'No known failure signature' "$fixture_dir/unknown.md"

echo "ci-rescue tests passed"
