joeylib2/scripts/verify-web-save.sh

51 lines
2.3 KiB
Bash
Executable file

#!/usr/bin/env bash
# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
#
# verify-web-save.sh - Save gate for the web port: a save outlives the page,
# and each page keeps its own.
#
# A web program's SAVES/ is kept in the browser's IndexedDB (the SDL3
# backend mounts IDBFS there with autoPersist, so each file is written back
# as it is closed, and again when the page is left), in a folder named after
# the page: SAVE.html's is /SAVE/SAVES, a store of its own on a site every
# page shares. SAVE (examples/save) counts its launches in SAVES/runs.dat.
# Its folder is served with a second copy of its page, OTHER.html, and in
# one headless Chromium OTHER.html is opened first (its own count, 1), then
# SAVE.html, given time to write, opened again (reloaded), and given time
# again: the count SAVE.html then holds must be 2 -- the first launch's 1
# came back from IndexedDB and the second launch added one; a store shared
# with OTHER.html would say 3. Each wait covers a cold browser loading the
# program (4 s once fell short).
#
# Usage: scripts/verify-web-save.sh
# Requires: Chromium, emsdk's node; build/web/bin/SAVE (make web).
set -euo pipefail
repo=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
source "$repo/scripts/lib/joeyEnv.sh"
source "$repo/scripts/lib/webGate.sh"
webNeed
dir=$repo/build/web/bin/SAVE
[ -f "$dir/SAVE.html" ] || { echo "verify-web-save: build SAVE first (make web)" >&2; exit 2; }
work=$(mktemp -d -t joeylib-web-save.XXXXXX)
cleanup() {
webStop
rm -rf "$work"
}
trap cleanup EXIT
cp -r "$dir" "$work/site"
cp "$work/site/SAVE.html" "$work/site/OTHER.html"
webServe "$work/site"
webDrive "OTHER.html" --step wait:8 --step open:SAVE.html --step wait:8 --step reload --step wait:8 --file "/SAVE/SAVES/runs.dat:$work/runs.dat"
count=$(python3 -c "import struct, sys; print(struct.unpack('<H', open(sys.argv[1], 'rb').read(2))[0])" "$work/runs.dat")
if [ "$count" = 2 ]; then
echo "verify-web-save: PASS (SAVE.html's second launch counted 2 in /SAVE/SAVES: its first launch's save came back from IndexedDB, and OTHER.html's launch on the same site was not counted)"
else
echo "verify-web-save: FAIL - SAVE.html's second launch counted $count, not 2 (3: its store is shared with OTHER.html)" >&2
exit 1
fi