ai-code-battle/scripts/upload-test-replays.sh
jedarden 1a9c8374f2 fix(scripts): add test replay generation and R2 upload tools
Root cause: The R2 bucket 'acb-data' is empty - no replay files were ever
uploaded. The matches/index.json lists test matches, but the corresponding
replay files return 404 when accessed via the Pages Function at /r2/.

Fix: Add tooling to generate test replays matching index.json entries and
upload them to R2. The viewer already has proper error handling (response.ok
check + user-visible error messages in replay.ts:1397-1402).

Changes:
- scripts/generate-test-replays.sh: Generate all 8 test replays from index.json
  with correct match IDs, gzip them, place in test-replays/
- scripts/upload-test-replays.sh: Upload generated replays to R2 via wrangler
- scripts/README.md: Document the R2 setup and replay upload workflow
- .gitignore: Add test-replays/ (generated files, not committed)

Usage:
  1. bash scripts/generate-test-replays.sh
  2. npm install -g wrangler && wrangler login
  3. bash scripts/upload-test-replays.sh

Verified: Generated replays have correct match_id, format_version="1.0",
and valid JSON structure. The viewer error path handles 404 correctly.

Closes: bf-360t
2026-05-24 21:54:36 -04:00

73 lines
1.9 KiB
Bash
Executable file

#!/bin/bash
# Upload test replays to R2 bucket
# Requires: wrangler CLI installed and authenticated
#
# Prerequisites:
# 1. Run ./scripts/generate-test-replays.sh first
# 2. Install wrangler: npm install -g wrangler
# 3. Authenticate: wrangler login
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Configuration
BUCKET_NAME="acb-data"
REPLAY_DIR="test-replays"
echo "=== Uploading Test Replays to R2 ==="
echo ""
# Check if wrangler is available
if ! command -v wrangler &> /dev/null; then
if [ -f ~/.local/bin/wrangler ]; then
WRANGLER=~/.local/bin/wrangler
else
echo -e "${RED}ERROR: wrangler not found${NC}"
echo "Install with: npm install -g wrangler"
echo "Then authenticate: wrangler login"
exit 1
fi
else
WRANGLER=wrangler
fi
# Check authentication
echo "Checking wrangler authentication..."
if ! $WRANGLER whoami &>/dev/null; then
echo -e "${RED}ERROR: Not authenticated with Cloudflare${NC}"
echo "Run: $WRANGLER login"
exit 1
fi
$WRANGLER whoami
echo ""
# Check if replay directory exists
if [ ! -d "$REPLAY_DIR" ]; then
echo -e "${RED}ERROR: $REPLAY_DIR not found${NC}"
echo "Run: ./scripts/generate-test-replays.sh"
exit 1
fi
# Upload each replay
echo "Uploading replays to R2 bucket '$BUCKET_NAME'..."
for file in "$REPLAY_DIR"/*.json.gz; do
if [ -f "$file" ]; then
filename=$(basename "$file")
key="replays/$filename"
echo -n "Uploading $key... "
$WRANGLER r2 object put "$BUCKET_NAME/$key" --file="$file" &>/dev/null
echo -e "${GREEN}OK${NC}"
fi
done
echo ""
echo -e "${GREEN}=== Upload Complete! ===${NC}"
echo ""
echo "Verify with:"
echo " curl -I https://r2.aicodebattle.com/replays/m_test_upset_v1.json.gz"
echo " or test in viewer: https://ai-code-battle.pages.dev/#/watch/replay?url=/r2/replays/m_test_upset_v1.json.gz"