#!/bin/sh
# Runs a test on every engine in engines.json, in the kit's container: the published builds,
# checked against their SHA-256, on a virtual display. Results land in results/<test>-<UTC time>/.
#
# ./verify.sh stability
set -eu
TEST=${1:?usage: verify.sh <test> (tests: $(ls tests | tr '\n' ' '))}
KIT=$(cd "$(dirname "$0")" && pwd)
OUT="$KIT/results/$TEST-$(date -u +%Y%m%dT%H%MZ)"
# A test can pin its platform (tests/<test>/platform); otherwise it runs on this machine's own, so no
# emulator stands between the engine and its frames. Its engines are tests/<test>/engines-<arch>.json,
# tests/<test>/engines.json or engines.json, the first there is.
case "$(uname -m)" in
x86_64 | amd64) NATIVE=linux/amd64 ;;
arm64 | aarch64) NATIVE=linux/arm64 ;;
*) NATIVE="linux/$(uname -m)" ;;
esac
PLATFORM=$(cat "$KIT/tests/$TEST/platform" 2>/dev/null || echo "$NATIVE")
ENGINES=tests/$TEST/engines-${PLATFORM#linux/}.json
[ -f "$KIT/$ENGINES" ] || ENGINES=tests/$TEST/engines.json
[ -f "$KIT/$ENGINES" ] || ENGINES=engines.json
mkdir -p "$OUT" "$KIT/.engines" "$KIT/.build"
docker build --platform "$PLATFORM" -q -t "smy-verification:${PLATFORM#linux/}" "$KIT/docker" >/dev/null
docker run --rm --platform "$PLATFORM" -v "$KIT:/kit:ro" -v "$KIT/.engines:/engines" -v "$KIT/.build:/build:ro" -v "$OUT:/out" \
"smy-verification:${PLATFORM#linux/}" sh -c "
set -e
ARGS=\$(python3 /kit/tools/fetch_engines.py /kit/$ENGINES /engines | sed 's/^/--engine /')
python3 /kit/tests/$TEST/run.py \$ARGS --out /out
"
echo "results: $OUT"