#!/bin/sh
set -eu

root="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
test_root="$(mktemp -d "${TMPDIR:-/tmp}/dalo-workflow-test.XXXXXX")"

cleanup() {
  rm -rf "$test_root"
}
trap cleanup EXIT INT TERM

version_check="$({
  awk '
    /^[[:space:]]+test .*node -p.*package.json/ {
      sub(/^[[:space:]]+/, "")
      print
      found = 1
    }
    END { if (!found) exit 1 }
  ' "$root/.github/workflows/publish.yml"
})"

version_check_script="$test_root/publish-version-check.sh"
{
  printf '%s\n' 'set -eu'
  printf '%s\n' 'version="$1"'
  printf '%s\n' "$version_check"
} > "$version_check_script"

(
  cd "$root/npm"
  version="$(node -p 'require("./package.json").version')"
  bash "$version_check_script" "$version"
)

workflow="$root/.github/workflows/publish.yml"
ci_workflow="$root/.github/workflows/ci.yml"
release_config="$root/release-please-config.json"

job_body() {
  awk -v job="$1" '
    $0 == "  " job ":" { found = 1; in_job = 1; next }
    in_job && /^  [A-Za-z0-9_-]+:/ { exit }
    in_job { print }
    END { if (!found) exit 1 }
  ' "$workflow"
}

workflow_step_run() {
  awk -v name="$1" '
    $0 == "      - name: " name { found = 1; in_step = 1; next }
    in_step && $0 == "        run: |" { in_run = 1; next }
    in_run && /^          / { sub(/^          /, ""); print; next }
    in_run { exit }
    END { if (!found || !in_run) exit 1 }
  ' "$workflow"
}

test "$(node -p 'require(process.argv[1]).packages["."].draft' "$release_config")" = true
test "$(node -p 'require(process.argv[1]).packages["."]["force-tag-creation"]' "$release_config")" = true
test "$(node -p 'require(process.argv[1]).packages["."]["extra-files"].includes("npm/package.json")' "$release_config")" = true
test "$(node -p 'require(process.argv[1]).packages["."]["extra-files"].filter(entry => entry.path === "npm/package-lock.json").length' "$release_config")" = 2
# release-please keeps the version shown on dalo.sh in step with the crate.
test "$(node -p 'require(process.argv[1]).packages["."]["extra-files"].some(entry => entry.type === "generic" && entry.path === "site/index.html")' "$release_config")" = true
(
  cd "$root/npm"
  npm run check-version
)
drifted_package="$test_root/package.json"
sed 's/"version": "[^"]*"/"version": "0.0.0"/' "$root/npm/package.json" > "$drifted_package"
if DALO_PACKAGE_JSON="$drifted_package" node "$root/npm/scripts/check-version.js" >/dev/null 2>&1; then
  echo "npm version check accepted a drifted package manifest" >&2
  exit 1
fi

artifacts_job="$(job_body release-artifacts)"
final_release_job="$(job_body publish-github-release)"
crate_job="$(job_body publish-crate)"
npm_job="$(job_body publish-npm)"
homebrew_job="$(job_body update-homebrew)"
release_please_job="$(job_body release-please)"

ci_job_body() {
  awk -v job="$1" '
    $0 == "  " job ":" { found = 1; in_job = 1; next }
    in_job && /^  [A-Za-z0-9_-]+:/ { exit }
    in_job { print }
    END { if (!found) exit 1 }
  ' "$ci_workflow"
}

ci_test_job="$(ci_job_body test)"
release_targets_job="$(ci_job_body release-targets)"
coverage_job="$(ci_job_body coverage)"
msrv_job="$(ci_job_body msrv)"
audit_job="$(ci_job_body audit)"

printf '%s\n' "$ci_test_job" | grep -Fq 'cargo test --locked'
printf '%s\n' "$ci_test_job" | grep -Fq 'cargo clippy --locked --all-targets --all-features -- -D warnings'
printf '%s\n' "$ci_test_job" | grep -Fq 'cargo build --release --locked --target "${{ matrix.target }}"'
printf '%s\n' "$ci_test_job" | grep -Fq 'sh scripts/release-rehearsal.sh'
printf '%s\n' "$ci_test_job" | grep -Fq 'cargo test --release --locked --target "${{ matrix.target }}" --test performance -- --ignored noop_sync_should_not_regress_against_a_minimal_store --nocapture'
performance_command='cargo test --release --locked --test performance -- --ignored noop_sync_should_not_regress_against_a_minimal_store --nocapture'
for performance_document in "$root/CONTRIBUTING.md" "$root/.github/pull_request_template.md"; do
  grep -Fq "$performance_command" "$performance_document"
done

# The host test job covers the two native release targets. The dedicated job
# covers the three remaining targets, including native ARM execution and the
# static-musl release-binary smoke path.
for target in x86_64-unknown-linux-gnu aarch64-apple-darwin; do
  printf '%s\n' "$ci_test_job" | grep -Fq "$target"
done

for target in \
  aarch64-unknown-linux-gnu \
  x86_64-unknown-linux-musl \
  aarch64-unknown-linux-musl; do
  printf '%s\n' "$release_targets_job" | grep -Fq "$target"
done

# The Intel macOS build was discontinued with 1.0, so neither CI nor the
# publish matrix may reintroduce it: a built target that no channel installs is
# a promise nobody keeps.
if printf '%s\n' "$release_targets_job" | grep -Fq 'x86_64-apple-darwin'; then
  echo 'the CI release-target matrix still builds the discontinued Intel macOS target' >&2
  exit 1
fi

release_target_entry() {
  printf '%s\n' "$release_targets_job" | awk -v target="$1" '
    /^          - os:/ {
      if (entry ~ ("target: " target)) {
        found = 1
        print entry
        exit
      }
      entry = $0 ORS
      next
    }
    { entry = entry $0 ORS }
    END {
      if (!found) {
        if (entry ~ ("target: " target)) {
          print entry
        } else {
          exit 1
        }
      }
    }
  '
}

aarch64_gnu_entry="$(release_target_entry aarch64-unknown-linux-gnu)"
x86_64_musl_entry="$(release_target_entry x86_64-unknown-linux-musl)"
aarch64_musl_entry="$(release_target_entry aarch64-unknown-linux-musl)"

printf '%s\n' "$aarch64_gnu_entry" | grep -Fqx '          - os: ubuntu-24.04-arm'
printf '%s\n' "$aarch64_gnu_entry" | grep -Fqx '            builder: cargo'
printf '%s\n' "$aarch64_gnu_entry" | grep -Fqx '            test: cargo'
printf '%s\n' "$aarch64_gnu_entry" | grep -Fqx '            smoke: false'
printf '%s\n' "$x86_64_musl_entry" | grep -Fqx '          - os: ubuntu-latest'
printf '%s\n' "$x86_64_musl_entry" | grep -Fqx '            builder: cross'
printf '%s\n' "$x86_64_musl_entry" | grep -Fqx '            test: cross'
printf '%s\n' "$x86_64_musl_entry" | grep -Fqx '            smoke: true'
printf '%s\n' "$aarch64_musl_entry" | grep -Fqx '          - os: ubuntu-latest'
printf '%s\n' "$aarch64_musl_entry" | grep -Fqx '            builder: cross'
printf '%s\n' "$aarch64_musl_entry" | grep -Fqx '            test: none'
printf '%s\n' "$aarch64_musl_entry" | grep -Fqx '            smoke: false'

printf '%s\n' "$release_targets_job" | grep -Fq 'runs-on: ${{ matrix.os }}'
printf '%s\n' "$release_targets_job" | grep -Fq 'cross test --locked --target "${{ matrix.target }}" --lib'
printf '%s\n' "$release_targets_job" | grep -Fq 'cargo test --locked --target "${{ matrix.target }}"'
printf '%s\n' "$release_targets_job" | grep -Fq 'target/${{ matrix.target }}/release/dalo'
printf '%s\n' "$ci_test_job" | grep -Fq 'tests/deb-package-ci.sh'
printf '%s\n' "$release_targets_job" | grep -Fq 'tests/deb-package-ci.sh'
printf '%s\n' "$release_targets_job" | grep -Fq '"$binary" init --store "$test_root/store"'
printf '%s\n' "$release_targets_job" | grep -Fq '"$binary" sync --store "$test_root/store" --dry-run'
sh "$root/scripts/check-workflow-pins.sh" "$root/.github/workflows" > /dev/null

# The macOS test entry and the comment above it kept contradicting each other.
printf '%s\n' "$ci_test_job" | grep -Fq 'Keep macos-latest as the macOS test runner'
printf '%s\n' "$ci_test_job" | grep -Fqx '          - os: macos-latest'
if printf '%s\n' "$ci_test_job" | grep -Fq -e '- os: macos-14'; then
  echo 'the macOS test entry must stay on macos-latest; macos-14 rejects the hook fixtures' >&2
  exit 1
fi

# One coverage threshold, read from `coverage-threshold` by everything that
# states it.
coverage_threshold="$(cat "$root/coverage-threshold")"
case "$coverage_threshold" in
  ''|*[!0-9.]*)
    echo "coverage-threshold must hold a plain number, found '$coverage_threshold'" >&2
    exit 1
    ;;
esac
coverage_command='cargo llvm-cov --workspace --all-features --summary-only --fail-under-lines "$(cat coverage-threshold)"'
for coverage_document in "$ci_workflow" "$root/CONTRIBUTING.md" "$root/.github/pull_request_template.md"; do
  grep -Fq "$coverage_command" "$coverage_document" || {
    echo "$coverage_document does not read the gate from coverage-threshold" >&2
    exit 1
  }
done
printf '%s\n' "$coverage_job" | grep -Fq "$coverage_command" || {
  echo 'the CI coverage job no longer reads the gate from coverage-threshold' >&2
  exit 1
}
if grep -Fq "fail-under-lines $coverage_threshold" "$ci_workflow" "$root/CONTRIBUTING.md"; then
  echo 'the coverage threshold must be read from coverage-threshold, not restated' >&2
  exit 1
fi

# The MSRV job takes its toolchain from `rust-version` so the number lives in
# Cargo.toml alone.
msrv="$(sed -n 's/^rust-version = "\(.*\)"/\1/p' "$root/Cargo.toml" | head -n 1)"
test -n "$msrv"
printf '%s\n' "$msrv_job" | grep -Fq 'steps.msrv.outputs.version'
if printf '%s\n' "$msrv_job" | grep -Fq "$msrv"; then
  echo 'the MSRV job must read rust-version from Cargo.toml, not restate it' >&2
  exit 1
fi

# The pull request checklist, the CONTRIBUTING preflight, and the CI jobs are
# one contract. Every command below has to appear in all three.
pr_template="$root/.github/pull_request_template.md"
contributing="$root/CONTRIBUTING.md"

for shared_command in \
  'cargo fmt --check' \
  'cargo test --locked' \
  'sh tests/install.sh' \
  'sh tests/docs.sh' \
  'sh tests/workflows.sh' \
  'npm ci && npm run check-version && npm test' \
  'cargo clippy --locked --all-targets --all-features -- -D warnings' \
  'cargo build --release --locked' \
  'RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features'; do
  printf '%s\n' "$ci_test_job" | grep -Fq "$shared_command" || {
    echo "the CI test job no longer runs: $shared_command" >&2
    exit 1
  }
  grep -Fq "$shared_command" "$pr_template" || {
    echo "the pull request checklist is missing: $shared_command" >&2
    exit 1
  }
  grep -Fq "$shared_command" "$contributing" || {
    echo "CONTRIBUTING.md is missing: $shared_command" >&2
    exit 1
  }
done

printf '%s\n' "$audit_job" | grep -Fq 'cargo deny check' || {
  echo 'the CI audit job no longer runs cargo deny check' >&2
  exit 1
}
for documented_command in 'cargo deny check' 'git diff --check'; do
  for validation_document in "$pr_template" "$contributing"; do
    grep -Fq "$documented_command" "$validation_document" || {
      echo "$validation_document is missing: $documented_command" >&2
      exit 1
    }
  done
done

# Pull request titles feed release-please through the squashed commit subject.
pr_title_workflow="$root/.github/workflows/pr-title.yml"
grep -Fq 'amannn/action-semantic-pull-request@' "$pr_title_workflow"
for commit_type in feat fix docs test ci chore refactor; do
  grep -Fqx "            $commit_type" "$pr_title_workflow" || {
    echo "the pull request title check does not accept the '$commit_type' prefix" >&2
    exit 1
  }
  grep -Fq "$commit_type: " "$contributing" || {
    echo "CONTRIBUTING.md no longer documents the '$commit_type' prefix" >&2
    exit 1
  }
done

printf '%s\n' "$artifacts_job" | grep -Fqx '    needs: release-please'
printf '%s\n' "$artifacts_job" | grep -Fq "gh release view \"\$TAG_NAME\" --json isDraft --jq '.isDraft'"
printf '%s\n' "$artifacts_job" | grep -Fq 'GH_REPO: ${{ github.repository }}'
printf '%s\n' "$artifacts_job" | grep -Fq 'scripts/package-deb.sh'
printf '%s\n' "$artifacts_job" | grep -Fq "hashFiles('scripts/package-deb.sh') != ''"
printf '%s\n' "$artifacts_job" | grep -Fq 'dist/*.deb dist/*.sha256 dist/*.sigstore.json'
printf '%s\n' "$artifacts_job" | grep -Fq 'artifacts=(dist/*.tar.gz dist/*.deb)'
printf '%s\n' "$artifacts_job" | grep -Fq 'shopt -s nullglob'
printf '%s\n' "$release_please_job" | grep -Fq "inputs.recover_tag != ''"
printf '%s\n' "$release_please_job" | grep -Fq "gh release view \"\$TAG_NAME\" --json isDraft --jq '.isDraft'"
printf '%s\n' "$release_please_job" | grep -Fq 'GH_REPO: ${{ github.repository }}'
printf '%s\n' "$release_please_job" | grep -Fq 'echo "release_created=true" >> "$GITHUB_OUTPUT"'
printf '%s\n' "$release_please_job" | grep -Fq 'echo "tag_name=$TAG_NAME" >> "$GITHUB_OUTPUT"'
printf '%s\n' "$release_please_job" | grep -Fq 'echo "release_is_draft=$release_is_draft" >> "$GITHUB_OUTPUT"'
printf '%s\n' "$final_release_job" | grep -Fqx '    needs: [release-please, release-artifacts]'
printf '%s\n' "$final_release_job" | grep -Fq "needs.release-artifacts.result == 'success'"
printf '%s\n' "$final_release_job" | grep -Fq 'GH_REPO: ${{ github.repository }}'
printf '%s\n' "$final_release_job" | grep -Fq 'gh release edit "$TAG_NAME" --draft=false'
printf '%s\n' "$final_release_job" | grep -Fq 'for architecture in amd64 arm64; do'
printf '%s\n' "$final_release_job" | grep -Fq 'package="dalo_${version}_${architecture}.deb"'
printf '%s\n' "$final_release_job" | grep -Fq 'if [ -f scripts/package-deb.sh ]; then'
printf '%s\n' "$final_release_job" | grep -Fq 'legacy release has an unexpected partial Debian package set'
printf '%s\n' "$final_release_job" | grep -Fq 'for asset in "$package" "$package.sha256" "$package.sigstore.json"; do'
no_deb_assets="$test_root/no-deb-assets"
mkdir -p "$no_deb_assets/dist" "$no_deb_assets/bin"
touch "$no_deb_assets/dist/dalo-1.3.0-x86_64-unknown-linux-musl.tar.gz"
touch "$no_deb_assets/dist/dalo-1.3.0-x86_64-unknown-linux-musl.tar.gz.sha256"
cat > "$no_deb_assets/bin/cosign" <<'EOF'
#!/bin/sh
set -eu
test "$1" = sign-blob
test -f "$5"
touch "$4"
EOF
cat > "$no_deb_assets/bin/gh" <<'EOF'
#!/bin/sh
set -eu
if [ "$1" = release ] && [ "$2" = view ]; then
  printf '%s\n' true
  exit 0
fi
if [ "$1" = release ] && [ "$2" = upload ]; then
  shift 2
  test "$1" = "$TAG_NAME"
  shift
  for asset in "$@"; do
    test "$asset" = --clobber && continue
    test -f "$asset"
    printf '%s\n' "$asset"
  done
  exit 0
fi
exit 1
EOF
chmod +x "$no_deb_assets/bin/cosign" "$no_deb_assets/bin/gh"
sign_script="$test_root/sign-release-assets.sh"
upload_script="$test_root/upload-release-assets.sh"
workflow_step_run 'Sign release assets' > "$sign_script"
workflow_step_run 'Upload artifact to release' > "$upload_script"
(
  cd "$no_deb_assets"
  PATH="$no_deb_assets/bin:$PATH" bash "$sign_script"
  PATH="$no_deb_assets/bin:$PATH" GH_TOKEN=stub GH_REPO=example/dalo TAG_NAME=dalo-v1.3.0 bash "$upload_script" > uploaded-assets.txt
  test "$(wc -l < uploaded-assets.txt | tr -d ' ')" = 3
  if grep -Fq '*.deb' uploaded-assets.txt; then
    echo 'release upload passed a literal unmatched Debian asset glob' >&2
    exit 1
  fi
)

# A package listing can exceed the pipe buffer after the matching entry.
# Early-exit grep turns that successful check into a broken pipe under pipefail.
deb_checks="$test_root/deb-checks"
mkdir -p "$deb_checks/bin" "$deb_checks/scripts"
printf '%s\n' '#!/bin/sh' 'exit 0' > "$deb_checks/scripts/package-deb.sh"
cat > "$deb_checks/bin/dpkg-deb" <<'EOF'
#!/bin/sh
set -eu
case "$1" in
  --field)
    case "$3" in
      Package) printf '%s\n' dalo ;;
      Version) printf '%s\n' 1.4.0 ;;
      Architecture) printf '%s\n' "$DEB_TEST_ARCH" ;;
      Depends) printf '%s\n' 'git, libc6 (>= 2.23)' ;;
      *) exit 1 ;;
    esac
    ;;
  --contents)
    awk -v missing="${DEB_TEST_MISSING_MANPAGE:-false}" 'BEGIN {
      print "./usr/bin/dalo"
      if (missing != "true") print "./usr/share/man/man1/dalo.1"
      for (i = 0; i < 100000; i++) print "./usr/share/doc/dalo/entry-" i
    }'
    ;;
  *) exit 1 ;;
esac
EOF
chmod +x "$deb_checks/bin/dpkg-deb"
deb_check_script="$test_root/build-debian-package.sh"
workflow_step_run 'Build Debian package' > "$deb_check_script"
(
  cd "$deb_checks"
  for architecture in amd64 arm64; do
    case "$architecture" in
      amd64) target=x86_64-unknown-linux-gnu ;;
      arm64) target=aarch64-unknown-linux-gnu ;;
    esac
    PATH="$deb_checks/bin:$PATH" TAG_NAME=dalo-v1.4.0 TARGET="$target" DEB_TEST_ARCH="$architecture" \
      bash -e -o pipefail "$deb_check_script"
  done
  if PATH="$deb_checks/bin:$PATH" TAG_NAME=dalo-v1.4.0 TARGET=x86_64-unknown-linux-gnu \
    DEB_TEST_ARCH=amd64 DEB_TEST_MISSING_MANPAGE=true bash -e -o pipefail "$deb_check_script"; then
    echo 'release package validation accepted a missing manpage' >&2
    exit 1
  fi
)
printf '%s\n' "$crate_job" | grep -Fq 'https://crates.io/api/v1/crates/dalo/${version}'
printf '%s\n' "$crate_job" | grep -Fq 'is already published on crates.io'
printf '%s\n' "$crate_job" | grep -Fq 'rust-lang/crates-io-auth-action@'
printf '%s\n' "$crate_job" | grep -Fq 'id-token: write'
printf '%s\n' "$crate_job" | grep -Fq 'CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}'
if printf '%s\n' "$crate_job" | grep -Fq 'secrets.CARGO_REGISTRY_TOKEN'; then
  echo 'crates.io publishing must use only the short-lived OIDC credential' >&2
  exit 1
fi
if printf '%s\n' "$crate_job" | grep -Fq 'continue-on-error:'; then
  echo 'crates.io authentication must fail closed' >&2
  exit 1
fi
if printf '%s\n' "$crate_job" | grep -Fq 'no crates.io credential'; then
  echo 'the obsolete long-lived credential fallback guard is still present' >&2
  exit 1
fi
printf '%s\n' "$npm_job" | grep -Fq 'npm view "getdalo@${version}" version'
printf '%s\n' "$npm_job" | grep -Fq 'is already published on npm'
printf '%s\n' "$npm_job" | grep -Fq 'if test -f package-lock.json'
printf '%s\n' "$npm_job" | grep -Fq 'No package-lock.json or dependencies in legacy release; skipping npm install'
printf '%s\n' "$npm_job" | grep -Fq 'package-lock.json is missing for an npm package with dependencies'
if printf '%s\n' "$npm_job" | grep -Fq 'npm version "$version"'; then
  echo 'npm publish must validate the release manifest without rewriting its version' >&2
  exit 1
fi
printf '%s\n' "$homebrew_job" | grep -Fq 'sort -V | tail -n 1'
printf '%s\n' "$homebrew_job" | grep -Fq 'not dispatching ${version}'

# Every job that needs a version parses the release tag inline. Run each of
# those parsers instead of trusting them: a drifted one shipped the
# `dalo-dalo-v0.4.0-*` archives, and the 0.x to 1.0.0 step is the next chance
# for one of them to disagree with the rest.
tag_parsers="$test_root/tag-parsers"
awk '
  /^[[:space:]]*case "\$TAG_NAME" in$/ { collecting = 1 }
  collecting {
    line = $0
    sub(/^[[:space:]]+/, "", line)
    print line
  }
  collecting && /^[[:space:]]*esac$/ { print "###END###"; collecting = 0 }
' "$workflow" > "$tag_parsers"

tag_parser_count="$(grep -c '^###END###$' "$tag_parsers" || true)"
if [ "$tag_parser_count" -lt 6 ]; then
  echo "expected every publish.yml job to parse the release tag, found $tag_parser_count parsers" >&2
  exit 1
fi

tag_parser() {
  awk -v want="$1" '
    BEGIN { position = 1 }
    /^###END###$/ { position += 1; next }
    position == want { print }
  ' "$tag_parsers"
}

parser_number=1
while [ "$parser_number" -le "$tag_parser_count" ]; do
  parser_script="$test_root/tag-parser-${parser_number}.sh"
  {
    printf '%s\n' 'set -eu'
    printf '%s\n' 'TAG_NAME="$1"'
    tag_parser "$parser_number"
    printf '%s\n' 'printf "%s\n" "${version-}"'
  } > "$parser_script"

  for accepted_tag in \
    'dalo-v1.0.0 1.0.0' \
    'v1.0.0 1.0.0' \
    'dalo-v10.11.12 10.11.12' \
    'dalo-v0.16.0 0.16.0'; do
    tag="${accepted_tag% *}"
    expected_version="${accepted_tag#* }"
    if ! parsed_version="$(sh "$parser_script" "$tag")"; then
      echo "publish.yml tag parser $parser_number rejected the release tag '$tag'" >&2
      exit 1
    fi
    if [ -n "$parsed_version" ] && [ "$parsed_version" != "$expected_version" ]; then
      echo "publish.yml tag parser $parser_number read '$parsed_version' from '$tag', expected '$expected_version'" >&2
      exit 1
    fi
  done

  for rejected_tag in '1.0.0' 'refs/tags/dalo-v1.0.0' 'getdalo-v1.0.0' ''; do
    if sh "$parser_script" "$rejected_tag" >/dev/null 2>&1; then
      echo "publish.yml tag parser $parser_number accepted the unusable tag '$rejected_tag'" >&2
      exit 1
    fi
  done
  parser_number=$((parser_number + 1))
done

# The tap bump refuses to move Homebrew backwards. `sort -V`, not string order,
# is what makes 1.0.0 win over 0.16.0.
homebrew_gate="$({
  awk '
    /^[[:space:]]*newest_version=/ { collecting = 1 }
    collecting {
      line = $0
      sub(/^[[:space:]]+/, "", line)
      print line
      found = 1
    }
    collecting && /^[[:space:]]*fi$/ { exit }
    END { if (!found) exit 1 }
  ' "$workflow"
})"

homebrew_gate_script="$test_root/homebrew-gate.sh"
{
  printf '%s\n' 'set -eu'
  printf '%s\n' 'current_version="$1"'
  printf '%s\n' 'version="$2"'
  printf '%s\n' "$homebrew_gate"
  printf '%s\n' 'printf "%s\n" dispatch'
} > "$homebrew_gate_script"

test "$(sh "$homebrew_gate_script" 0.16.0 1.0.0)" = dispatch
test "$(sh "$homebrew_gate_script" 0.9.0 0.10.0)" = dispatch
test "$(sh "$homebrew_gate_script" 1.0.0 1.0.1)" = dispatch
sh "$homebrew_gate_script" 1.0.0 1.0.0 | grep -Fq 'already references dalo 1.0.0'
sh "$homebrew_gate_script" 1.0.0 0.16.0 | grep -Fq 'already references dalo 1.0.0'

# The major has to come from a `Release-As:` footer: `bump-minor-pre-major`
# turns every `feat!:` below 1.0.0 into a minor bump instead.
test "$(node -p 'require(process.argv[1]).packages["."]["bump-minor-pre-major"]' "$release_config")" = true
grep -Fq 'Release-As: 1.0.0' "$root/docs/ci.md" || {
  echo 'docs/ci.md no longer documents the Release-As footer that produces the major' >&2
  exit 1
}

for downstream_job in "$crate_job" "$npm_job" "$homebrew_job"; do
  printf '%s\n' "$downstream_job" | grep -Fq "needs.release-please.outputs.release_is_draft == 'false'"
done
for target in \
  x86_64-unknown-linux-gnu \
  aarch64-unknown-linux-gnu \
  x86_64-unknown-linux-musl \
  aarch64-unknown-linux-musl \
  aarch64-apple-darwin; do
  printf '%s\n' "$final_release_job" | grep -Fq "$target"
done

# Five targets, not six: the finalisation gate must not wait for an Intel macOS
# asset that the build matrix no longer produces.
for discontinued_job in "$artifacts_job" "$final_release_job"; do
  if printf '%s\n' "$discontinued_job" | grep -Fq 'x86_64-apple-darwin'; then
    echo 'publish.yml still expects the discontinued Intel macOS target' >&2
    exit 1
  fi
done

for release_job in "$artifacts_job" "$final_release_job" "$crate_job" "$npm_job" "$homebrew_job"; do
  printf '%s\n' "$release_job" | grep -Fq "needs.release-please.outputs.release_created == 'true'"
done

for downstream_job in "$crate_job" "$npm_job" "$homebrew_job"; do
  printf '%s\n' "$downstream_job" | grep -Fq 'publish-github-release'
done

package_files="$(cd "$root" && cargo package --list --allow-dirty)"
for excluded_prefix in '.github/' 'docs/' 'npm/' 'site/' 'video/'; do
  if printf '%s\n' "$package_files" | grep -q "^$excluded_prefix"; then
    echo "cargo package unexpectedly contains $excluded_prefix" >&2
    exit 1
  fi
done

# dalo.sh is assembled by site/build.mjs at deploy time: the workflow must run
# the build, in order, and upload the assembled tree rather than the sources.
pages_workflow="$root/.github/workflows/pages.yml"
pages_job="$(awk '/^  deploy:/{on=1} on && /^  [A-Za-z0-9_-]+:/ && !/^  deploy:/{exit} on{print}' "$pages_workflow")"
for site_build_command in \
  'corepack pnpm --dir site install --frozen-lockfile' \
  'node site/build.mjs --check' \
  'node site/build.mjs'; do
  printf '%s\n' "$pages_job" | grep -Fqx "          $site_build_command" || {
    echo "the Pages deploy job no longer runs: $site_build_command" >&2
    exit 1
  }
done
printf '%s\n' "$pages_job" | grep -Fqx '          path: ./site/build' || {
  echo 'the Pages deploy job must upload the assembled site/build tree' >&2
  exit 1
}
if printf '%s\n' "$pages_job" | grep -Fq 'Stamp site version'; then
  echo 'the sed-based version stamp is superseded by site/build.mjs; remove it' >&2
  exit 1
fi
build_line="$(printf '%s\n' "$pages_job" | grep -Fn -- '- name: Build site' | cut -d: -f1)"
check_line="$(printf '%s\n' "$pages_job" | grep -Fn 'node site/build.mjs --check' | cut -d: -f1)"
render_line="$(printf '%s\n' "$pages_job" | grep -Fnx '          node site/build.mjs' | cut -d: -f1)"
upload_line="$(printf '%s\n' "$pages_job" | grep -Fn -- '- name: Upload static site' | cut -d: -f1)"
test -n "$build_line" && test -n "$check_line" && test -n "$render_line" && test -n "$upload_line"
if ! { [ "$build_line" -lt "$check_line" ] && [ "$check_line" -lt "$render_line" ] && [ "$render_line" -lt "$upload_line" ]; }; then
  echo 'the Pages deploy job must check, then build, then upload, in that order' >&2
  exit 1
fi

# A stale committed site render must fail the pull request, not the Pages
# deploy on main: CI installs the site dependencies and runs the same check.
site_job="$(ci_job_body site)"
for site_check_command in \
  'corepack pnpm --dir site install --frozen-lockfile' \
  'node site/build.mjs --check'; do
  printf '%s\n' "$site_job" | grep -Fqx "          $site_check_command" || {
    echo "the CI site job no longer runs: $site_check_command" >&2
    exit 1
  }
done
printf '%s\n' "$site_job" | grep -Fq 'COREPACK_ENABLE_DOWNLOAD_PROMPT: "0"' || {
  echo 'the CI site job must disable the corepack download prompt' >&2
  exit 1
}
for validation_document in "$pr_template" "$contributing"; do
  grep -Fq 'node site/build.mjs --check' "$validation_document" || {
    echo "$validation_document is missing: node site/build.mjs --check" >&2
    exit 1
  }
done

echo "workflow checks passed"
