#!/usr/bin/env bash
# Layer 12: Content-Addressed Transfer (HyperOverlay)
# Tests: Multiple HyperswarmMultiplayerPeer instances in a single Godot process.
# Matches JS Hyperswarm test pattern: all peers in one process, shared testnet.
set -euo pipefail
cd "$(dirname "$0")"
echo "=== Layer 12: Content-Addressed Transfer (HyperOverlay) ==="
rm -f /tmp/layer12_status.txt
# ---- Start testnet ----
echo "[orchestrator] starting DHT testnet..."
TESTNET_PID=""
cleanup() {
[ -n "$TESTNET_PID" ] && kill "$TESTNET_PID" 2>/dev/null || true
rm -f /tmp/layer_bootstrap.txt /tmp/layer12_status.txt
}
trap cleanup EXIT
node layer2_testnet.js &
TESTNET_PID=$!
# Wait for bootstrap
for i in $(seq 1 30); do
[ -f /tmp/layer_bootstrap.txt ] && break
sleep 0.5
done
if [ ! -f /tmp/layer_bootstrap.txt ]; then
echo "FAIL: testnet did not write bootstrap within 15s"
echo "=== Layer 12: FAILED ==="
exit 1
fi
BOOTSTRAP=$(cat /tmp/layer_bootstrap.txt)
echo "[orchestrator] testnet ready: $BOOTSTRAP"
# ---- Copy test script to project dir ----
cp layer12_overlay.gd project/layer12_overlay.gd 2>/dev/null || true
# ---- Run in-process tests ----
echo "[orchestrator] running in-process tests (single Godot process)..."
OUTPUT=$(timeout -k 10 120 godot4 --headless --path project --script res://layer12_overlay.gd 2>&1 || true)
echo "$OUTPUT"
# ---- Check result ----
STATUS=$(cat /tmp/layer12_status.txt 2>/dev/null || echo "UNKNOWN")
if [ "$STATUS" = "PASS" ]; then
echo "=== Layer 12: PASS ==="
exit 0
elif echo "$STATUS" | grep -q "^FAIL"; then
echo "=== Layer 12: FAILED ($STATUS) ==="
exit 1
else
echo "=== Layer 12: UNKNOWN ($STATUS) ==="
exit 1
fi