# Pre-Deploy Validation

## Contract

Running `npm run check` guarantees:

✅ **Build succeeds** - TypeScript compilation passes with no errors  
✅ **Security checks pass** - Schema, RLS, secrets, guards, tenant isolation verified  
✅ **Lifecycle checks pass** - Trade executor, lifecycle, order sync, persistence, failure injection, WebSocket contract verified

❌ **Does NOT guarantee**:
- Unit test coverage (no unit tests configured)
- Runtime behavior
- Database connectivity
- External API availability

---

## Pre-Deploy Command

```bash
npm run check
```

**What it runs**:
1. `npm run build` - TypeScript compilation
2. `npm run lint` - Security checks (schema, RLS, secrets, guards, tenant)
3. `npm run format` - Lifecycle checks (executor, sync, persistence, injection, WebSocket)

---

## Individual Check Commands

### Build Check
```bash
npm run build
```
**Pass criteria**: TypeScript compiles with 0 errors  
**Fail criteria**: Any TypeScript error

### Security Checks (Lint)
```bash
npm run lint
```
**Runs**:
- `check:schema-contract` - Database schema matches code expectations
- `check:rls-policies` - Row-level security policies are correct
- `check:secret-hygiene` - No secrets in code, env vars configured
- `check:security-guards` - Auth guards in place
- `check:tenant-isolation` - Multi-tenant data isolation verified

**Pass criteria**: All 5 checks pass  
**Fail criteria**: Any check fails

### Lifecycle Checks (Format)
```bash
npm run format
```
**Runs**:
- `check:trade-executor-lifecycle` - Trade execution flow works
- `check:lifecycle-regressions` - No regressions in trade lifecycle
- `check:order-sync-regressions` - Order status sync works
- `check:supabase-order-persistence-regressions` - Orders persist correctly
- `check:failure-injection` - System handles failures gracefully
- `check:websocket-contract` - WebSocket events match contract

**Pass criteria**: All 6 checks pass  
**Fail criteria**: Any check fails

---

## Pass/Fail Rules

### ✅ SAFE TO DEPLOY
All of the following must be true:
- `npm run build` exits with code 0
- `npm run lint` exits with code 0
- `npm run format` exits with code 0

### ❌ DO NOT DEPLOY
If any of the following are true:
- `npm run build` fails (TypeScript errors)
- `npm run lint` fails (security check failed)
- `npm run format` fails (lifecycle check failed)

---

## Current Issues (Must Fix Before Deploy)

⚠️ **Build is currently failing**:
```
src/scripts/verifyWebsocketContract.ts:83:5 - error TS2741: 
Property 'health' is missing in type 'BotState'
```

**Action required**: Fix TypeScript error in `verifyWebsocketContract.ts` before deploying

---

## Quick Reference

| Command | Purpose | Time | Critical |
|---------|---------|------|----------|
| `npm run build` | Compile TypeScript | ~10s | YES |
| `npm run lint` | Security checks | ~30s | YES |
| `npm run format` | Lifecycle checks | ~45s | YES |
| `npm run check` | All checks | ~90s | YES |

---

## Troubleshooting

### Build fails
- Check TypeScript errors in output
- Fix type errors in code
- Ensure all imports are correct

### Lint fails
- Check which security check failed
- Review error output
- Fix schema/RLS/secret/guard/tenant issue

### Format fails
- Check which lifecycle check failed
- Review error output
- Fix trade executor/sync/persistence issue

---

## Notes

- **No unit tests**: This project uses integration-style checks instead of unit tests
- **Checks are mandatory**: All checks must pass before deploy
- **No shortcuts**: Do not skip checks or deploy with failures
- **Independent**: This project's checks are independent of the frontend

---

## Related

- Frontend pre-deploy: `../bytelyst-trading-dashboard-web/docs/pre-deploy.md`
- Deployment script: `deploy.ps1`
