Skip to content

Merge pull request #339 from lane711/smoke-test-form-create #103

Merge pull request #339 from lane711/smoke-test-form-create

Merge pull request #339 from lane711/smoke-test-form-create #103

Workflow file for this run

name: PR Tests
on:
pull_request:
branches:
- main
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
# TODO: Re-enable type checking after fixing remaining TypeScript errors
# Tracked in follow-up issue
# - name: Type check
# run: npm run type-check
- name: Run unit tests
run: npm test
- name: Build core package
run: npm run build:core
- name: Create fresh D1 database for PR
id: create-db
run: |
cd my-sonicjs-app
BRANCH_NAME="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
# Limit to 43 chars to allow for "sonicjs-pr-" prefix (11 chars) = 54 total
# Remove trailing hyphens to avoid invalid worker names
SAFE_BRANCH=$(echo "$BRANCH_NAME" | sed 's/[^a-zA-Z0-9-]/-/g' | cut -c1-43 | sed 's/-*$//')
DB_NAME="sonicjs-pr-${SAFE_BRANCH}"
echo "Creating fresh D1 database: $DB_NAME"
# Check if database already exists
EXISTING_DB=$(npx wrangler d1 list --json | jq -r ".[] | select(.name == \"$DB_NAME\") | .uuid" 2>/dev/null || echo "")
if [ -n "$EXISTING_DB" ]; then
echo "Database $DB_NAME already exists with ID: $EXISTING_DB"
DB_ID="$EXISTING_DB"
else
# Create new database
CREATE_OUTPUT=$(npx wrangler d1 create "$DB_NAME" 2>&1)
echo "$CREATE_OUTPUT"
# Extract database ID from creation output
DB_ID=$(echo "$CREATE_OUTPUT" | grep -oP 'database_id\s*=\s*"\K[^"]+' || echo "")
if [ -z "$DB_ID" ]; then
# Try alternative extraction method
DB_ID=$(npx wrangler d1 list --json | jq -r ".[] | select(.name == \"$DB_NAME\") | .uuid")
fi
fi
if [ -z "$DB_ID" ]; then
echo "Error: Failed to get database ID"
exit 1
fi
echo "Database ID: $DB_ID"
echo "db_id=$DB_ID" >> $GITHUB_OUTPUT
echo "db_name=$DB_NAME" >> $GITHUB_OUTPUT
# Update wrangler.toml with the new database ID
sed -i "s/database_id = \"[^\"]*\"/database_id = \"$DB_ID\"/" wrangler.toml
sed -i "s/database_name = \"[^\"]*\"/database_name = \"$DB_NAME\"/" wrangler.toml
echo "Updated wrangler.toml with new database"
grep -A2 "d1_databases" wrangler.toml
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
- name: Run D1 migrations
run: |
cd my-sonicjs-app
echo "Applying migrations to database: ${{ steps.create-db.outputs.db_name }}"
npx wrangler d1 migrations apply ${{ steps.create-db.outputs.db_name }} --remote
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
- name: Deploy to Cloudflare Workers Preview
id: deploy
run: |
cd my-sonicjs-app
# Deploy to preview with unique name based on PR/branch
# Cloudflare Workers has a 54 character limit for script names with previews
# Prefix "sonicjs-pr-" is 11 chars, so max branch name is 43 chars
BRANCH_NAME="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
# Limit to 43 chars to allow for "sonicjs-pr-" prefix (11 chars) = 54 total
# Remove trailing hyphens to avoid invalid worker names
SAFE_BRANCH=$(echo "$BRANCH_NAME" | sed 's/[^a-zA-Z0-9-]/-/g' | cut -c1-43 | sed 's/-*$//')
# Deploy and capture the URL (don't exit on error)
set +e
echo "Deploying to preview environment: sonicjs-pr-${SAFE_BRANCH}"
DEPLOY_OUTPUT=$(npx wrangler deploy --name "sonicjs-pr-${SAFE_BRANCH}" 2>&1)
DEPLOY_EXIT_CODE=$?
set -e
echo "=== Wrangler Deploy Output ==="
echo "$DEPLOY_OUTPUT"
echo "=== End Deploy Output ==="
if [ $DEPLOY_EXIT_CODE -ne 0 ]; then
echo "Error: Wrangler deploy failed with exit code $DEPLOY_EXIT_CODE"
exit 1
fi
# Extract the URL from wrangler output
PREVIEW_URL=$(echo "$DEPLOY_OUTPUT" | grep -oP 'https://[^\s]+\.workers\.dev' | head -1)
if [ -z "$PREVIEW_URL" ]; then
echo "Failed to extract preview URL from wrangler output"
exit 1
fi
echo "Preview URL: $PREVIEW_URL"
echo "preview_url=$PREVIEW_URL" >> $GITHUB_OUTPUT
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
- name: Wait for preview deployment
run: |
echo "Waiting for preview to be ready..."
for i in {1..30}; do
if curl -s -o /dev/null -w "%{http_code}" "${{ steps.deploy.outputs.preview_url }}" | grep -q "200\|301\|302"; then
echo "Preview is ready!"
exit 0
fi
echo "Attempt $i/30: Preview not ready yet, waiting..."
sleep 10
done
echo "Preview failed to become ready"
exit 1
- name: Install Playwright browsers
run: npx playwright install --with-deps chromium
- name: Run E2E tests against preview
run: npm run e2e
env:
CI: true
BASE_URL: ${{ steps.deploy.outputs.preview_url }}
- name: Upload test results
if: always()
uses: actions/upload-artifact@v4
with:
name: playwright-report
path: playwright-report/
retention-days: 7
- name: Upload test videos
if: failure()
uses: actions/upload-artifact@v4
with:
name: test-videos
path: test-results/
retention-days: 7