#!/usr/bin/env bash
# KVT fork: tests for the scripts in packaging/kvt (no network, no secrets).
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
fail() { echo "FAIL: $*" >&2; exit 1; }
sha="$(printf 'a%.0s' $(seq 64))"
commit="$(printf '0%.0s' $(seq 40))"

# PKGBUILD: version, tag, checksum, provides/conflicts, marker file.
"$HERE/publish-aur.sh" --render v0.6.1-kvt.2 "$sha" "$work/aur"
p="$work/aur/PKGBUILD"
grep -qx 'pkgver=0.6.1.kvt.2' "$p" || fail "pkgver"
grep -qx '_tag=v0.6.1-kvt.2' "$p" || fail "_tag"
grep -qx "sha256sums=('$sha')" "$p" || fail "sha256sums"
grep -qx "provides=('rusty-teams')" "$p" || fail "provides"
grep -q "^conflicts=('rusty-teams'" "$p" || fail "conflicts"
grep -q '/usr/lib/rusty-teams/package-managed' "$p" || fail "package-managed marker"
grep -q 'rusty-teams.desktop' "$p" || fail "desktop file"
# shellcheck disable=SC1090
( source "$p"; [[ "${source[0]}" == "rusty-teams-0.6.1-kvt.2-linux-x86_64.tar.gz::https://git.kvt.nl/api/v1/projects/ict/rusty-teams/releases/v0.6.1-kvt.2/assets/rusty-teams-0.6.1-kvt.2-linux-x86_64.tar.gz" ]] ) || fail "source url"
! "$HERE/publish-aur.sh" --render v0.6.1-kvt.2 nothex "$work/x" 2>/dev/null || fail "bad sha accepted"
! "$HERE/publish-aur.sh" --render 0.6 "$sha" "$work/x" 2>/dev/null || fail "bad tag accepted"
[[ "$(grep -c '^pkgver=' "$HERE/aur/PKGBUILD")" == 1 ]] || fail "template pkgver"

# No AUR key: a clear message and exit 0.
out="$(env -u AUR_SSH_PRIVATE_KEY -u AUR_SSH_PRIVATE_KEY_B64 "$HERE/publish-aur.sh" v0.6.1-kvt.2)" || fail "skip must exit 0"
grep -q 'overgeslagen' <<<"$out" || fail "skip message"

# Release assets: versioned names, sums; every release needs all four build assets.
mkdir -p "$work/r/dist/kvt"
cd "$work/r"
echo linux > dist/kvt/teams-linux-amd64
echo tar > dist/kvt/linux-x86_64.tar.gz
! "$HERE/release-assets.sh" sums v0.6.1-kvt.2 >/dev/null 2>&1 || fail "sums without windows accepted"
echo exe > dist/kvt/teams-windows-amd64.exe
! "$HERE/release-assets.sh" sums v0.6.1-kvt.2 >/dev/null 2>&1 || fail "sums without windows arm64 accepted"
echo exearm > dist/kvt/teams-windows-arm64.exe
"$HERE/release-assets.sh" sums v0.6.1-kvt.2 >/dev/null
[[ "$(wc -l < dist/kvt/SHA256SUMS)" == 4 ]] || fail "sums with all assets"
grep -q "  rusty-teams-0.6.1-kvt.2-linux-amd64$" dist/kvt/SHA256SUMS || fail "linux sum name"
grep -q "  rusty-teams-0.6.1-kvt.2-windows-arm64.exe$" dist/kvt/SHA256SUMS || fail "windows arm64 sum name"
(cd dist/kvt && cp teams-windows-amd64.exe rusty-teams-0.6.1-kvt.2-windows-amd64.exe &&
  grep windows-amd64 SHA256SUMS | sha256sum -c - >/dev/null) || fail "windows sum"
json="$("$HERE/release-assets.sh" json v0.6.1-kvt.2 "$commit")"
python3 - "$json" <<'PY' || fail "release json"
import json, sys
body = json.loads(sys.argv[1])
names = {a["name"]: (a["job"], a["path"]) for a in body["assets"]}
assert body["name"] == "v0.6.1-kvt.2" and len(body["commit"]) == 40
assert names["rusty-teams-0.6.1-kvt.2-linux-amd64"] == ("linux_release", "dist/kvt/teams-linux-amd64")
assert names["rusty-teams-0.6.1-kvt.2-windows-amd64.exe"][0] == "windows_release"
assert names["rusty-teams-0.6.1-kvt.2-windows-arm64.exe"][0] == "windows_arm64_release"
assert names["SHA256SUMS"] == ("checksums", "dist/kvt/SHA256SUMS")
assert len(names) == 5
PY
rm dist/kvt/teams-linux-amd64
! "$HERE/release-assets.sh" sums v0.6.1-kvt.2 2>/dev/null || fail "missing linux binary accepted"

# CI cache helper: tarball round-trip (BlueGit caches files, not directories).
cachework="$(mktemp -d)"
mkdir -p "$cachework/cargo-home/registry/cache/demo" "$cachework/rustup-home/toolchains"
echo crate > "$cachework/cargo-home/registry/cache/demo/foo.crate"
echo rustc > "$cachework/rustup-home/toolchains/marker"
(cd "$cachework" && CARGO_HOME="$cachework/cargo-home" RUSTUP_HOME="$cachework/rustup-home"   "$HERE/ci-cache.sh" save linux)
[[ -f "$cachework/.ci-cache/cargo-linux.tar.gz" ]] || fail "cache archive missing"
rm -rf "$cachework/cargo-home" "$cachework/rustup-home"
(cd "$cachework" && CARGO_HOME="$cachework/cargo-home" RUSTUP_HOME="$cachework/rustup-home"   "$HERE/ci-cache.sh" restore linux)
[[ -f "$cachework/cargo-home/registry/cache/demo/foo.crate" ]] || fail "cache restore cargo"
[[ -f "$cachework/rustup-home/toolchains/marker" ]] || fail "cache restore rustup"
out="$(cd "$cachework" && "$HERE/ci-cache.sh" restore windows)"
grep -q 'Geen CI-cache' <<<"$out" || fail "missing windows cache message"
rm -rf "$cachework"

# AUR source package: the tag and commit are filled in, and .SRCINFO (made without makepkg)
# equals the committed one.
SRC="$HERE/../aur-source"
srcsha="$(printf 'e%.0s' $(seq 40))"
"$SRC/publish.sh" --render v0.6.1-kvt.4 e972869751c189818d20930bebdb6a0156b16db1 "$work/src" || fail "aur-source render"
diff -q "$work/src/PKGBUILD" "$SRC/PKGBUILD" >/dev/null || fail "aur-source PKGBUILD is not rendered for v0.6.1-kvt.4"
diff -q "$work/src/.SRCINFO" "$SRC/.SRCINFO" >/dev/null || fail "aur-source .SRCINFO differs from the committed one"
"$SRC/publish.sh" --render v0.7.0-kvt.1 "$srcsha" "$work/src2" || fail "aur-source render 2"
grep -qx 'pkgver=0.7.0.kvt.1' "$work/src2/PKGBUILD" || fail "aur-source pkgver"
grep -qx '_tag=v0.7.0-kvt.1' "$work/src2/PKGBUILD" || fail "aur-source _tag"
grep -qx "_commit=$srcsha" "$work/src2/PKGBUILD" || fail "aur-source _commit"
grep -qP '^\tpkgver = 0\.7\.0\.kvt\.1$' "$work/src2/.SRCINFO" || fail "aur-source .SRCINFO pkgver"
! "$SRC/publish.sh" --render 0.7 "$srcsha" "$work/x" 2>/dev/null || fail "aur-source bad tag accepted"
! "$SRC/publish.sh" --render v0.7.0-kvt.1 abc "$work/x" 2>/dev/null || fail "aur-source bad commit accepted"
out="$(env -u AUR_SSH_PRIVATE_KEY -u AUR_SSH_PRIVATE_KEY_B64 "$SRC/publish.sh" v0.7.0-kvt.1 "$srcsha")" || fail "aur-source skip must exit 0"
grep -q 'overgeslagen' <<<"$out" || fail "aur-source skip message"

echo "packaging/kvt tests: ok"
