Skip to main content

API Testing

Comprehensive automated testing for the API Gateway (apps/api) with dual-environment support.

Test Architecture

apps/api/test/
├── integration/ # Local Cloudflare Workers tests
│ ├── auth.spec.ts # Bearer/API key authentication
│ ├── rate-limiting.spec.ts
│ ├── generations.spec.ts
│ ├── api-keys.spec.ts
│ ├── proxy.spec.ts # Public routes
│ └── user.spec.ts # User profile/subscription/quota
├── sandbox/ # Deployed sandbox HTTP tests
│ ├── utils.ts
│ └── sandbox.spec.ts
└── utils.ts # Shared test utilities

Test Modes

ModeCommandEnvironmentMethod
Localpnpm test:e2eLocal SupabaseCloudflare Workers pool
Sandboxpnpm test:sandboxHosted SupabaseReal HTTP fetch

Running Local Tests

Local tests use @cloudflare/vitest-pool-workers to run the Worker in-process.

# 1. Start local Supabase
cd services/supabase && npx supabase start

# 2. Seed local test data
cd services/supabase && npx tsx scripts/seed-auth.ts

# 3. Run integration tests
cd apps/api && pnpm test:e2e

Running Sandbox Tests

Sandbox tests make real HTTP requests to the deployed API Gateway.

Prerequisites

Set environment variables:

export SANDBOX_SUPABASE_URL="https://wyjzdyfxczuixkukurdg.supabase.co"
export SANDBOX_SUPABASE_ANON_KEY="your-anon-key"
export SANDBOX_SUPABASE_SERVICE_ROLE_KEY="your-service-role-key"

Seeding (Idempotent)

The sandbox seed script is safe to run multiple times:

cd apps/api && pnpm test:sandbox:seed

Running Tests

cd apps/api && pnpm test:sandbox

Test Credentials

UserEmailPasswordPlan
Freetest-free@example.comTestPassword123!free
Protest-pro@example.comTestPassword123!pro
Agencytest-agency@example.comTestPassword123!agency

API Keys

For API key authentication tests:

PlanKey
Profaiht_sk_test_pro_abcdef123456
Agencyfaiht_sk_test_agency_xyz789def

Coverage Targets

  • Statement Coverage: 90-100%
  • Branch Coverage: 90-100%
  • All 10 API endpoints tested

Endpoints Covered

EndpointAuthValidationEdge Cases
GET /v1/trends
GET /v1/hashtags
POST /v1/generations
POST /v1/contact
GET /v1/user/profileN/A
GET /v1/user/subscriptionN/A
GET /v1/user/quotaN/A
POST /v1/api-keys
DELETE /v1/api-keys/:id

CI/CD Integration

Tests are integrated into the CI/CD pipeline:

# In .github/workflows/ci-cd.yml
- name: Run API Tests
run: pnpm --filter api test:e2e

For sandbox testing in CI, set the following secrets:

  • SANDBOX_SUPABASE_URL
  • SANDBOX_SUPABASE_ANON_KEY
  • SANDBOX_SUPABASE_SERVICE_ROLE_KEY