#!/usr/bin/env bash
set -Eeuo pipefail
LIB="${ROUTER_STEP_WORKFLOW_LIB_UNDER_TEST:-/opt/router-ops/lib/router-step-workflow.sh}"
source "$LIB"
WORK="$(mktemp -d /tmp/router-step-workflow-selftest.XXXXXX)"
trap 'rm -rf "$WORK"' EXIT

router_step_workflow_init "$WORK/current.env" "$WORK/events.tsv" transfer core publication http
[[ "$(router_step_first_incomplete)" == transfer ]]
router_step_phase_begin transfer
router_step_phase_complete transfer
[[ "$(router_step_first_incomplete)" == core ]]
router_step_phase_begin core
router_step_phase_complete core
router_step_phase_skip publication synthetic_salvage
[[ "$(router_step_first_incomplete)" == http ]]
router_step_phase_begin http
router_step_phase_complete http
router_step_workflow_mark_complete
router_step_workflow_is_complete

set +e
late="$(router_step_error_classification STOP_SYNTHETIC complete 999 1 'false command --arg value' VM101 true not_allowed none inspect_log)"
late_rc=$?
set -e
[[ "$late_rc" -eq 0 ]]
grep -qx RESULT=PASS_WORKFLOW_ALREADY_COMPLETE <<<"$late"
grep -qx LATE_ERROR_AFTER_COMPLETE=true <<<"$late"
grep -q '^LATE_ERROR_COMMAND=' <<<"$late"

router_step_workflow_init "$WORK/current2.env" "$WORK/events2.tsv" preflight apply
set +e
stop="$(router_step_error_classification STOP_TEST apply 77 23 'cat /vm101/path' VM101 false completed /tmp/latest.zip inspect_remote_ack)"
stop_rc=$?
set -e
[[ "$stop_rc" -eq 1 ]]
grep -qx STOP_MACHINE_OWNER=VM101 <<<"$stop"
grep -q '^STOP_COMMAND=' <<<"$stop"
grep -qx CORE_CHANGE_COMPLETE=false <<<"$stop"
grep -qx ROLLBACK_STATUS=completed <<<"$stop"
grep -qx LATEST_ARCHIVE=/tmp/latest.zip <<<"$stop"
[[ "$(wc -l < "$WORK/events.tsv")" -ge 8 ]]
echo RESULT=PASS_ROUTER_STEP_WORKFLOW_SELFTEST

# Incoming retention/hygiene regression.
HBASE="$WORK/hygiene-base"
HIN="$WORK/hygiene-incoming"
mkdir -p "$HBASE/public/r/token/latest/sample" "$HIN"
printf 'token\n' > "$HBASE/public/.router-public-token"
printf 'current\n' > "$HBASE/public/r/token/latest/sample/report.txt"

python3 - "$HIN/STEP_SELFTEST.zip" "$HIN/WG_PAID_MGTS_LATEST_19990101-000000.zip" <<'PYZIP'
import sys,zipfile
for path,name in [(sys.argv[1],'bundle.txt'),(sys.argv[2],'latest.txt')]:
    with zipfile.ZipFile(path,'w') as z: z.writestr(name,'ok\n')
PYZIP
bundle_sha="$(sha256sum "$HIN/STEP_SELFTEST.zip" | awk '{print $1}')"
router_step_retain_bundle "$HIN/STEP_SELFTEST.zip" "$bundle_sha" "$HBASE" "$HIN" > "$WORK/retain-bundle.log"
grep -qx RESULT=PASS_ROUTER_STEP_RETAIN_BUNDLE "$WORK/retain-bundle.log"
[[ ! -e "$HIN/STEP_SELFTEST.zip" ]]
[[ -f "$HBASE/state/retained-step-zips/STEP_SELFTEST/${bundle_sha}.zip" ]]

mkdir -p "$HIN/.unpack-STEP_SELFTEST-123"
router_step_cleanup_stale_unpacks "$HBASE" "$HIN" > "$WORK/cleanup-unpack.log"
grep -qx STALE_UNPACK_REMOVED_COUNT=1 "$WORK/cleanup-unpack.log"
[[ ! -e "$HIN/.unpack-STEP_SELFTEST-123" ]]

router_step_archive_latest_python "$HBASE" "$HIN" 20000101-000000 > "$WORK/hygiene-latest.log"
grep -qx RESULT=PASS_ROUTER_STEP_ARCHIVE_LATEST "$WORK/hygiene-latest.log"
grep -qx PREVIOUS_LATEST_RETAINED_COUNT=1 "$WORK/hygiene-latest.log"
grep -qx INCOMING_LATEST_COUNT=1 "$WORK/hygiene-latest.log"
[[ -f "$HIN/WG_PAID_MGTS_LATEST_20000101-000000.zip" ]]
old_sha="$(find "$HBASE/state/retained-latest" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | head -n1)"
[[ "$old_sha" =~ ^[0-9a-f]{64}$ ]]
[[ -f "$HBASE/state/retained-latest/$old_sha/WG_PAID_MGTS_LATEST_19990101-000000.zip" ]]
echo RESULT=PASS_ROUTER_INCOMING_RETENTION_HYGIENE_SELFTEST
