#!/usr/bin/env bash
#
# Tests for the package-manifest render scripts (Scoop + Homebrew).
#
# Validates that each render script emits a well-formed manifest for given
# inputs, and that the inputs land in the right places. Dependency-light:
# requires node (always in the CI image) for JSON parsing; uses jq and ruby for
# extra checks when present (both available on the dev box). No network.
#
# Run: scripts/test-package-renders.sh
set -euo pipefail

here=$(cd "$(dirname "$0")" && pwd)
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

fail() { echo "FAIL: $*" >&2; exit 1; }
pass() { echo "ok: $*"; }

# Distinct dummy hashes so we can assert each lands in the right slot.
VER=9.9.9
H_WIN_X64=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa01
H_WIN_ARM=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa02
H_MAC_ARM=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa03
H_MAC_X64=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa04
H_LIN_ARM=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa05
H_LIN_X64=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa06

# ---- Scoop ----------------------------------------------------------------
scoop=$tmp/rdbms-playground.json
# Pass a leading 'v' to confirm it gets stripped.
"$here/render-scoop-manifest.sh" "v$VER" "$H_WIN_X64" "$H_WIN_ARM" > "$scoop"

node -e 'JSON.parse(require("fs").readFileSync(process.argv[1],"utf8"))' "$scoop" \
  || fail "scoop manifest is not valid JSON"
pass "scoop manifest parses as JSON"

# Field-level assertions via node (no jq dependency).
check_json() { # <jsexpr> <expected>
  local got
  got=$(node -e '
    const m = JSON.parse(require("fs").readFileSync(process.argv[1],"utf8"));
    process.stdout.write(String(eval(process.argv[2])));
  ' "$scoop" "$1")
  [ "$got" = "$2" ] || fail "scoop: $1 = '$got', expected '$2'"
}
check_json 'm.version'                          "$VER"
check_json 'm.bin'                              "rdbms-playground.exe"
check_json 'm.architecture["64bit"].hash'       "$H_WIN_X64"
check_json 'm.architecture["arm64"].hash'       "$H_WIN_ARM"
check_json 'm.checkver.jsonpath'                '$.tag_name'
grep -q "rdbms-playground-v$VER-x86_64-pc-windows-gnu.exe#/rdbms-playground.exe" "$scoop" \
  || fail "scoop: x64 url/fragment missing"
grep -q "rdbms-playground-v$VER-aarch64-pc-windows-gnullvm.exe#/rdbms-playground.exe" "$scoop" \
  || fail "scoop: arm64 url/fragment missing"
pass "scoop manifest fields correct"

if command -v jq >/dev/null 2>&1; then
  jq -e . "$scoop" >/dev/null || fail "scoop: jq rejected the manifest"
  pass "scoop manifest valid per jq"
fi

# ---- Homebrew -------------------------------------------------------------
formula=$tmp/rdbms-playground.rb
"$here/render-homebrew-formula.sh" "$VER" "$H_MAC_ARM" "$H_MAC_X64" "$H_LIN_ARM" "$H_LIN_X64" > "$formula"

if command -v ruby >/dev/null 2>&1; then
  ruby -c "$formula" >/dev/null || fail "homebrew formula is not valid Ruby"
  pass "homebrew formula parses as Ruby"
else
  echo "warn: ruby not present — skipping formula syntax check" >&2
fi

# Each hash must appear exactly once (right asset → right slot), the version
# must be present, and #{version} must survive verbatim for brew's test block.
for pair in \
  "aarch64-apple-darwin:$H_MAC_ARM" \
  "x86_64-apple-darwin:$H_MAC_X64" \
  "aarch64-unknown-linux-musl:$H_LIN_ARM" \
  "x86_64-unknown-linux-musl:$H_LIN_X64"; do
  target=${pair%%:*}; hash=${pair#*:}
  grep -q "rdbms-playground-v$VER-$target\"" "$formula" || fail "homebrew: url for $target missing"
  grep -q "sha256 \"$hash\"" "$formula"                 || fail "homebrew: sha256 for $target missing"
done
grep -q 'version "'"$VER"'"' "$formula"          || fail "homebrew: version line missing"
grep -q 'assert_match "rdbms-playground #{version}"' "$formula" \
  || fail "homebrew: test block #{version} interpolation was mangled"
grep -q 'rdbms-playground-v9.9.9-x86_64-pc-windows' "$formula" \
  && fail "homebrew: formula unexpectedly references a Windows asset"
pass "homebrew formula fields correct"

echo "all render tests passed"
