|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Integration test for HTTP error codes from the /bundle/ endpoint. |
| 4 | +# |
| 5 | +# Prerequisites: |
| 6 | +# - Redis running on localhost:6379 |
| 7 | +# - Node.js with --openssl-legacy-provider (webpack 4 + Node 22) |
| 8 | +# |
| 9 | +# Usage: ./scripts/test-error-codes.sh |
| 10 | + |
| 11 | +set -euo pipefail |
| 12 | + |
| 13 | +PORT=3099 |
| 14 | +REDIS_DB=2 |
| 15 | +REDIS_URL="redis://localhost:6379/$REDIS_DB" |
| 16 | +PASS=0 |
| 17 | +FAIL=0 |
| 18 | + |
| 19 | +cleanup() { |
| 20 | + if [ -n "${SERVER_PID:-}" ]; then |
| 21 | + kill "$SERVER_PID" 2>/dev/null || true |
| 22 | + wait "$SERVER_PID" 2>/dev/null || true |
| 23 | + fi |
| 24 | + redis-cli -n "$REDIS_DB" FLUSHDB > /dev/null 2>&1 || true |
| 25 | +} |
| 26 | +trap cleanup EXIT |
| 27 | + |
| 28 | +request() { |
| 29 | + local url="http://localhost:$PORT$1" |
| 30 | + local tmpfile |
| 31 | + tmpfile=$(mktemp) |
| 32 | + local status |
| 33 | + status=$(curl -s --max-time "${2:-30}" -o "$tmpfile" -w "%{http_code}" "$url") |
| 34 | + local body |
| 35 | + body=$(cat "$tmpfile") |
| 36 | + rm -f "$tmpfile" |
| 37 | + echo "$status" |
| 38 | + echo "$body" |
| 39 | +} |
| 40 | + |
| 41 | +assert_status() { |
| 42 | + local description="$1" expected="$2" actual="$3" body="$4" |
| 43 | + if [ "$actual" = "$expected" ]; then |
| 44 | + echo " PASS: $description (HTTP $actual)" |
| 45 | + PASS=$((PASS + 1)) |
| 46 | + else |
| 47 | + echo " FAIL: $description — expected HTTP $expected, got HTTP $actual" |
| 48 | + echo " Body: $body" |
| 49 | + FAIL=$((FAIL + 1)) |
| 50 | + fi |
| 51 | +} |
| 52 | + |
| 53 | +assert_contains() { |
| 54 | + local description="$1" expected="$2" actual="$3" |
| 55 | + if echo "$actual" | grep -qF "$expected"; then |
| 56 | + echo " PASS: $description" |
| 57 | + PASS=$((PASS + 1)) |
| 58 | + else |
| 59 | + echo " FAIL: $description — expected body to contain '$expected'" |
| 60 | + echo " Body: $actual" |
| 61 | + FAIL=$((FAIL + 1)) |
| 62 | + fi |
| 63 | +} |
| 64 | + |
| 65 | +wait_for_redis_type() { |
| 66 | + local key="$1" expected_type="$2" timeout_secs="${3:-120}" |
| 67 | + for i in $(seq 1 "$timeout_secs"); do |
| 68 | + TYPE=$(redis-cli -n "$REDIS_DB" HGET "$key" type 2>/dev/null) |
| 69 | + if [ "$TYPE" = "$expected_type" ]; then |
| 70 | + return 0 |
| 71 | + fi |
| 72 | + sleep 1 |
| 73 | + done |
| 74 | + echo " Timed out waiting for Redis key $key to have type=$expected_type (got $TYPE)" |
| 75 | + return 1 |
| 76 | +} |
| 77 | + |
| 78 | +echo "Starting snackager on port $PORT..." |
| 79 | +redis-cli -n "$REDIS_DB" FLUSHDB > /dev/null |
| 80 | + |
| 81 | +NODE_OPTIONS=--openssl-legacy-provider \ |
| 82 | +DISABLE_INSTRUMENTATION=1 \ |
| 83 | +REDIS_URL="$REDIS_URL" \ |
| 84 | +IMPORT_SERVER_URL="http://localhost:$PORT" \ |
| 85 | +AWS_ACCESS_KEY_ID=test \ |
| 86 | +AWS_SECRET_ACCESS_KEY=test \ |
| 87 | +S3_BUCKET=test \ |
| 88 | +IMPORTS_S3_BUCKET=test \ |
| 89 | +S3_REGION=us-west-1 \ |
| 90 | +CLOUDFRONT_URL="http://localhost:$PORT/serve" \ |
| 91 | +API_SERVER_URL=https://staging.exp.host \ |
| 92 | +PORT="$PORT" \ |
| 93 | +npx ts-node --require tsconfig-paths/register src/index.ts > /tmp/snackager-test.log 2>&1 & |
| 94 | +SERVER_PID=$! |
| 95 | + |
| 96 | +for i in $(seq 1 30); do |
| 97 | + if curl -s "http://localhost:$PORT/status" > /dev/null 2>&1; then |
| 98 | + break |
| 99 | + fi |
| 100 | + sleep 1 |
| 101 | +done |
| 102 | + |
| 103 | +if ! curl -s "http://localhost:$PORT/status" > /dev/null 2>&1; then |
| 104 | + echo "Server failed to start" |
| 105 | + tail -20 /tmp/snackager-test.log |
| 106 | + exit 1 |
| 107 | +fi |
| 108 | +echo "Server ready (PID $SERVER_PID)" |
| 109 | +echo |
| 110 | + |
| 111 | +# ── 404: Package not found ─────────────────────────────────────────── |
| 112 | +# PackageNotFoundError is thrown from fetchMetadata before fetchBundle |
| 113 | +# is called, so the 404 is returned synchronously on the first request. |
| 114 | +# The registry 404 is never cached in Redis, so repeated requests also |
| 115 | +# hit the registry and return 404. |
| 116 | +echo "── 404: Package not found ──" |
| 117 | + |
| 118 | +echo " Cold fetch:" |
| 119 | +RESULT=$(request "/bundle/this-package-does-not-exist-xyz@1.0.0?platforms=ios") |
| 120 | +STATUS=$(echo "$RESULT" | head -1) |
| 121 | +BODY=$(echo "$RESULT" | tail -n +2) |
| 122 | +assert_status "nonexistent package returns 404" 404 "$STATUS" "$BODY" |
| 123 | +assert_contains "body explains the package was not found" "not found in the registry" "$BODY" |
| 124 | +assert_contains "body suggests checking the name and version" "Verify the package name and version" "$BODY" |
| 125 | + |
| 126 | +echo " Repeated fetch (registry 404 is not cached):" |
| 127 | +RESULT=$(request "/bundle/this-package-does-not-exist-xyz@1.0.0?platforms=ios") |
| 128 | +STATUS=$(echo "$RESULT" | head -1) |
| 129 | +BODY=$(echo "$RESULT" | tail -n +2) |
| 130 | +assert_status "repeated request also returns 404" 404 "$STATUS" "$BODY" |
| 131 | + |
| 132 | +echo |
| 133 | + |
| 134 | +echo " Scoped package:" |
| 135 | +RESULT=$(request "/bundle/@example/nonexistent@1.0.0?platforms=ios") |
| 136 | +STATUS=$(echo "$RESULT" | head -1) |
| 137 | +BODY=$(echo "$RESULT" | tail -n +2) |
| 138 | +assert_status "nonexistent scoped package returns 404" 404 "$STATUS" "$BODY" |
| 139 | + |
| 140 | +echo |
| 141 | + |
| 142 | +# ── 422: Unbundleable package ──────────────────────────────────────── |
| 143 | +# UnbundleablePackageError is thrown inside fetchBundle's try block, |
| 144 | +# which catches the error, caches it in Redis (with errorName), and |
| 145 | +# returns {pending: true}. The 422 is only returned on subsequent |
| 146 | +# requests when the cached error is re-thrown. |
| 147 | +echo "── 422: Unbundleable package ──" |
| 148 | + |
| 149 | +# bcrypt imports Node.js's crypto module as a dependency; the bundler |
| 150 | +# installs it but still can't resolve it, triggering |
| 151 | +# UnbundleablePackageError ("Cannot resolve module crypto after |
| 152 | +# installing it as a dependency"). |
| 153 | +echo " Cold fetch (bundling starts in background):" |
| 154 | +RESULT=$(request "/bundle/bcrypt@5.1.1?platforms=ios" 120) |
| 155 | +STATUS=$(echo "$RESULT" | head -1) |
| 156 | +BODY=$(echo "$RESULT" | tail -n +2) |
| 157 | +assert_status "first request returns 200 pending" 200 "$STATUS" "$BODY" |
| 158 | +assert_contains "response is pending" '"pending":true' "$BODY" |
| 159 | + |
| 160 | +echo " Waiting for bundling to fail..." |
| 161 | +wait_for_redis_type "snackager/buildStatus/1/bcrypt@5.1.1-ios" "error" |
| 162 | + |
| 163 | +echo " Redis cache:" |
| 164 | +ERROR_NAME=$(redis-cli -n "$REDIS_DB" HGET "snackager/buildStatus/1/bcrypt@5.1.1-ios" errorName 2>/dev/null) |
| 165 | +assert_status "errorName preserved in Redis" "UnbundleablePackageError" "$ERROR_NAME" "" |
| 166 | + |
| 167 | +echo " Cached fetch (error re-thrown from Redis):" |
| 168 | +RESULT=$(request "/bundle/bcrypt@5.1.1?platforms=ios") |
| 169 | +STATUS=$(echo "$RESULT" | head -1) |
| 170 | +BODY=$(echo "$RESULT" | tail -n +2) |
| 171 | +assert_status "unbundleable package returns 422" 422 "$STATUS" "$BODY" |
| 172 | +assert_contains "body includes the bundler error" "Cannot resolve module crypto" "$BODY" |
| 173 | +assert_contains "body explains the limitation" "cannot be bundled for the Snack runtime" "$BODY" |
| 174 | + |
| 175 | +echo |
| 176 | + |
| 177 | +# ── 400: Core module ──────────────────────────────────────────────── |
| 178 | +# Core modules are rejected by parseRequest before servePackage is |
| 179 | +# called. This is existing behavior. |
| 180 | +echo "── 400: Core module (existing behavior) ──" |
| 181 | + |
| 182 | +RESULT=$(request "/bundle/react-native@0.73.0?platforms=ios" 15) |
| 183 | +STATUS=$(echo "$RESULT" | head -1) |
| 184 | +BODY=$(echo "$RESULT" | tail -n +2) |
| 185 | +assert_status "core module returns 400" 400 "$STATUS" "$BODY" |
| 186 | + |
| 187 | +echo |
| 188 | + |
| 189 | +# ── Summary ────────────────────────────────────────────────────────── |
| 190 | +echo "── Results: $PASS passed, $FAIL failed ──" |
| 191 | +if [ "$FAIL" -gt 0 ]; then |
| 192 | + exit 1 |
| 193 | +fi |
0 commit comments