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
| Mode | Command | Environment | Method |
|---|---|---|---|
| Local | pnpm test:e2e | Local Supabase | Cloudflare Workers pool |
| Sandbox | pnpm test:sandbox | Hosted Supabase | Real 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
| User | Password | Plan | |
|---|---|---|---|
| Free | test-free@example.com | TestPassword123! | free |
| Pro | test-pro@example.com | TestPassword123! | pro |
| Agency | test-agency@example.com | TestPassword123! | agency |
API Keys
For API key authentication tests:
| Plan | Key |
|---|---|
| Pro | faiht_sk_test_pro_abcdef123456 |
| Agency | faiht_sk_test_agency_xyz789def |
Coverage Targets
- Statement Coverage: 90-100%
- Branch Coverage: 90-100%
- All 10 API endpoints tested
Endpoints Covered
| Endpoint | Auth | Validation | Edge Cases |
|---|---|---|---|
GET /v1/trends | ✅ | ✅ | ✅ |
GET /v1/hashtags | ✅ | ✅ | ✅ |
POST /v1/generations | ✅ | ✅ | ✅ |
POST /v1/contact | ✅ | ✅ | ✅ |
GET /v1/user/profile | ✅ | N/A | ✅ |
GET /v1/user/subscription | ✅ | N/A | ✅ |
GET /v1/user/quota | ✅ | N/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_URLSANDBOX_SUPABASE_ANON_KEYSANDBOX_SUPABASE_SERVICE_ROLE_KEY