#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
TMP="$(mktemp -d "${XDG_CACHE_HOME:-$HOME/.cache}/overdeck/tests/fire-consumer-playbooks-XXXXXX")"
trap 'chmod -R u+w "$TMP" 2>/dev/null || true; rm -rf "$TMP"' EXIT
pass=0; fail=0
ok() { pass=$((pass + 1)); printf 'ok - %s\n' "$1"; }
bad() { fail=$((fail + 1)); printf 'not ok - %s\n' "$1"; }

# RED proof: a divergent file must refuse before any reset or deploy trigger.
mkdir -p "$TMP/divergent/deploy/packaging"; git -C "$TMP/divergent/deploy" init -q; git -C "$TMP/divergent/deploy" config user.email test@example.invalid; git -C "$TMP/divergent/deploy" config user.name test
printf 'landed\n' >"$TMP/divergent/deploy/file"; git -C "$TMP/divergent/deploy" add file; git -C "$TMP/divergent/deploy" commit -qm base; git -C "$TMP/divergent/deploy" branch -M main; git -C "$TMP/divergent/deploy" remote add origin "$TMP/divergent/deploy"; git -C "$TMP/divergent/deploy" fetch -q origin main:refs/remotes/origin/main
printf 'foreign\n' >"$TMP/divergent/deploy/file"
if OVERDECK_DEPLOY_DIR="$TMP/divergent/deploy" bash "$ROOT/playbooks/deploy-clone-dirty.sh" deploy-clone:overdeck-dirty >"$TMP/divergent/out" 2>"$TMP/divergent/err"; then bad 'RED divergent clone refuses'; elif grep -q 'divergent local files' "$TMP/divergent/err" && [[ "$(cat "$TMP/divergent/deploy/file")" == foreign ]]; then ok 'RED divergent clone refuses'; else bad 'RED divergent clone refuses'; fi

# GREEN: index-only dirt with identical bytes is restored, then only queues a deploy.
mkdir -p "$TMP/equivalent/deploy/packaging"; git -C "$TMP/equivalent/deploy" init -q; git -C "$TMP/equivalent/deploy" config user.email test@example.invalid; git -C "$TMP/equivalent/deploy" config user.name test
printf 'landed\n' >"$TMP/equivalent/deploy/file"; printf '#!/usr/bin/env bash\nprintf deploy-queued\n' >"$TMP/equivalent/deploy/packaging/deploy-local.sh"; chmod +x "$TMP/equivalent/deploy/packaging/deploy-local.sh"; git -C "$TMP/equivalent/deploy" add file packaging/deploy-local.sh; git -C "$TMP/equivalent/deploy" commit -qm base; git -C "$TMP/equivalent/deploy" branch -M main; git -C "$TMP/equivalent/deploy" remote add origin "$TMP/equivalent/deploy"; git -C "$TMP/equivalent/deploy" fetch -q origin main:refs/remotes/origin/main
git -C "$TMP/equivalent/deploy" update-index --chmod=+x file
if OVERDECK_DEPLOY_DIR="$TMP/equivalent/deploy" bash "$ROOT/playbooks/deploy-clone-dirty.sh" deploy-clone:overdeck-dirty >"$TMP/equivalent/out" 2>"$TMP/equivalent/err" && grep -q '"outcome":"shipped"' "$TMP/equivalent/out" && [[ -z "$(git -C "$TMP/equivalent/deploy" status --porcelain)" ]]; then ok 'GREEN equivalent clone restores and queues deploy'; else cat "$TMP/equivalent/err" >&2; bad 'GREEN equivalent clone restores and queues deploy'; fi

# RED proof: a runner name with no buildbox in it is declared not scriptable.
mkdir -p "$TMP/runner-nobox/bin"
if bash "$ROOT/playbooks/runner-offline.sh" gh-runner:runner-a:offline >"$TMP/runner-nobox/out" 2>"$TMP/runner-nobox/err"; then bad 'RED boxless runner name refuses'; elif grep -q 'does not encode a buildbox' "$TMP/runner-nobox/err"; then ok 'RED boxless runner name refuses'; else cat "$TMP/runner-nobox/err" >&2; bad 'RED boxless runner name refuses'; fi

# RED proof: zero units discovered on the box cannot report success.
mkdir -p "$TMP/runner/bin"
printf '#!/usr/bin/env bash\ncase "$*" in *list-unit-files*) exit 0 ;; *) exit 1 ;; esac\n' >"$TMP/runner/bin/ssh"; chmod +x "$TMP/runner/bin/ssh"
if PATH="$TMP/runner/bin:$PATH" bash "$ROOT/playbooks/runner-offline.sh" gh-runner:test-debian9-2:offline >"$TMP/runner/out" 2>"$TMP/runner/err"; then bad 'RED missing runner unit refuses'; elif grep -q 'cannot identify exactly one unit' "$TMP/runner/err"; then ok 'RED missing runner unit refuses'; else cat "$TMP/runner/err" >&2; bad 'RED missing runner unit refuses'; fi

# GREEN runner proof: unit discovered on the box, started there, listener live, GH online.
mkdir -p "$TMP/runner-green/bin"
cat >"$TMP/runner-green/bin/ssh" <<'STUB'
#!/usr/bin/env bash
case "$*" in
  *list-unit-files*) printf 'test-runner-2.service\n' ;;
  *"systemctl --user start test-runner-2.service"*) exit 0 ;;
  *pgrep*) exit 0 ;;
  *) exit 1 ;;
esac
STUB
printf '#!/usr/bin/env bash\nprintf online\n' >"$TMP/runner-green/bin/gh"; chmod +x "$TMP/runner-green/bin/"*
if PATH="$TMP/runner-green/bin:$PATH" bash "$ROOT/playbooks/runner-offline.sh" gh-runner:test-debian9-2:offline >"$TMP/runner-green/out" 2>"$TMP/runner-green/err" && grep -q '"outcome":"shipped"' "$TMP/runner-green/out"; then ok 'GREEN runner starts exact unit and proves online'; else cat "$TMP/runner-green/err" >&2; bad 'GREEN runner starts exact unit and proves online'; fi
printf 'PASS=%s FAIL=%s\n' "$pass" "$fail"
[[ "$fail" -eq 0 ]]
