# Real API Smoke Testing

This smoke test hits real Convertrilo infrastructure with a real API key. It is meant for staging or a dedicated test account.

## Required Environment

```bash
export CONVERTRILO_API_URL="https://api.convertrilo.com"
export CONVERTRILO_API_KEY="cvr_..."
```

By default, the smoke test only checks `/tokens/balance` so it does not spend tokens.

## Run Balance Check

```bash
pnpm run smoke:api
```

## Run A Real Encode

This submits a small 480p H.264 on-demand encode and polls until success, failure, cancellation, or timeout.

```bash
export CONVERTRILO_SMOKE_RUN_ENCODE=true
pnpm run smoke:api
```

## Smoke Modes

The default mode is `url`: URL source in, internal CDN output out.

```bash
export CONVERTRILO_SMOKE_MODE=url
```

Custom S3 output using the user's saved S3 credential:

```bash
export CONVERTRILO_SMOKE_RUN_ENCODE=true
export CONVERTRILO_SMOKE_MODE=output-s3
export CONVERTRILO_SMOKE_OUTPUT_S3_KEY="convertrilo-smoke/output.mp4"
pnpm run smoke:api
```

S3 folder/prefix source ingest using the user's saved S3 credential:

```bash
export CONVERTRILO_SMOKE_RUN_ENCODE=true
export CONVERTRILO_SMOKE_MODE=source-s3-folder
export CONVERTRILO_SMOKE_SOURCE_S3_PREFIX="qwe/"
pnpm run smoke:api
```

Google Drive output requires an explicit Google access token. This tests the BYO-token path:

```bash
export CONVERTRILO_SMOKE_RUN_ENCODE=true
export CONVERTRILO_SMOKE_MODE=output-google-drive
export CONVERTRILO_SMOKE_GOOGLE_DRIVE_FOLDER_ID="..."
export CONVERTRILO_SMOKE_GOOGLE_DRIVE_ACCESS_TOKEN="ya29..."
pnpm run smoke:api
```

Google Drive folder source ingest also requires an explicit access token and folder IDs:

```bash
export CONVERTRILO_SMOKE_RUN_ENCODE=true
export CONVERTRILO_SMOKE_MODE=source-google-drive-folder
export CONVERTRILO_SMOKE_GOOGLE_DRIVE_FOLDER_ID="..."
export CONVERTRILO_SMOKE_GOOGLE_DRIVE_OUTPUT_FOLDER_ID="..."
export CONVERTRILO_SMOKE_GOOGLE_DRIVE_ACCESS_TOKEN="ya29..."
pnpm run smoke:api
```

If you intentionally want to test the dashboard-connected Google Drive fallback instead of BYO tokens, set:

```bash
export CONVERTRILO_SMOKE_ALLOW_DASHBOARD_GDRIVE_FALLBACK=true
```

Do not use that fallback as proof that a third-party OAuth integration works. For BYO OAuth, your test app should get a Google refresh token, refresh it with its own Google OAuth client, and pass the resulting access token as `CONVERTRILO_SMOKE_GOOGLE_DRIVE_ACCESS_TOKEN`.

For a quick manual smoke token, use Google's OAuth 2.0 Playground:

1. Open `https://developers.google.com/oauthplayground/`.
2. Select the scope `https://www.googleapis.com/auth/drive`.
3. Authorize the test Google account that can read the input folder and write to the output folder.
4. Exchange the authorization code for tokens.
5. Copy the current access token into `CONVERTRILO_SMOKE_GOOGLE_DRIVE_ACCESS_TOKEN`.

This proves Convertrilo can consume a BYO access token. Production integrations should not depend on OAuth Playground; your app should own the Google OAuth client, store the refresh token, refresh access tokens server-side, and pass fresh access tokens to Convertrilo.

Optional knobs:

```bash
export CONVERTRILO_SMOKE_SOURCE_URL="https://example.com/sample.mp4"
export CONVERTRILO_SMOKE_S3_CREDENTIAL_ID="optional-saved-credential-id"
export CONVERTRILO_SMOKE_TIMEOUT_MS=600000
export CONVERTRILO_SMOKE_POLL_INTERVAL_MS=5000
```

Use a dedicated test API key with the scopes `jobs:create`, `jobs:read`, and
`jobs:cancel`. Add `credentials:manage` only when the smoke test reads a saved
S3 credential secret via `CONVERTRILO_SMOKE_S3_CREDENTIAL_ID`.

## Release Smoke Matrix

Run this matrix after backend/API deploys that touch jobs, storage integrations, workers, or webhooks.

Start with common environment:

```bash
export CONVERTRILO_API_URL="https://api.convertrilo.com"
export CONVERTRILO_API_KEY="cvr_..."
export CONVERTRILO_SMOKE_RUN_ENCODE=true
```

URL source to Convertrilo CDN:

```bash
export CONVERTRILO_SMOKE_MODE=url
pnpm run smoke:api
```

URL source to customer S3-compatible output:

```bash
export CONVERTRILO_SMOKE_MODE=output-s3
export CONVERTRILO_SMOKE_OUTPUT_S3_KEY="convertrilo-smoke/output-$(date +%s).mp4"
pnpm run smoke:api
```

S3 folder source to customer S3-compatible output:

```bash
export CONVERTRILO_SMOKE_MODE=source-s3-folder
export CONVERTRILO_SMOKE_SOURCE_S3_PREFIX="test/"
export CONVERTRILO_SMOKE_OUTPUT_S3_PREFIX="convertrilo-smoke/folder-output-$(date +%s)/"
export CONVERTRILO_SMOKE_MAX_FOLDER_JOBS=1
pnpm run smoke:api
```

URL source to Google Drive output with BYO access token:

```bash
export CONVERTRILO_SMOKE_MODE=output-google-drive
export CONVERTRILO_SMOKE_GOOGLE_DRIVE_OUTPUT_FOLDER_ID="..."
export CONVERTRILO_SMOKE_GOOGLE_DRIVE_ACCESS_TOKEN="ya29..."
pnpm run smoke:api
```

Google Drive folder source to Google Drive output with BYO access token:

```bash
export CONVERTRILO_SMOKE_MODE=source-google-drive-folder
export CONVERTRILO_SMOKE_GOOGLE_DRIVE_FOLDER_ID="..."
export CONVERTRILO_SMOKE_GOOGLE_DRIVE_OUTPUT_FOLDER_ID="..."
export CONVERTRILO_SMOKE_GOOGLE_DRIVE_ACCESS_TOKEN="ya29..."
export CONVERTRILO_SMOKE_MAX_FOLDER_JOBS=1
pnpm run smoke:api
```

Managed webhook delivery with a real terminal `job.completed` event:

```bash
export CONVERTRILO_SMOKE_WEBHOOK_URL="https://api.convertrilo.com/smoke/webhooks/convertrilo/$SMOKE_WEBHOOK_RECEIVER_TOKEN"
export CONVERTRILO_SMOKE_RUN_ENCODE=true
pnpm run smoke:webhooks
```

The owned smoke receiver is enabled only when `SMOKE_WEBHOOK_RECEIVER_TOKEN` is configured on the API server. It returns 204 for matching tokens, logs minimal delivery metadata, and stores no webhook body. If it is not configured, use a receiver you control or an intentionally disposable test receiver for smoke testing only.

## Run Webhook Delivery History Smoke

This creates a temporary managed webhook, sends a `webhook.test` event, verifies
`GET /webhooks/{id}/deliveries`, and deletes the temporary webhook.

```bash
export CONVERTRILO_API_URL="https://api.convertrilo.com"
export CONVERTRILO_API_KEY="cvr_..."
pnpm run smoke:webhooks
```

By default it uses `https://httpbingo.org/status/204` as the receiver. To use your
own receiver:

```bash
export CONVERTRILO_SMOKE_WEBHOOK_URL="https://your-app.com/webhooks/convertrilo"
pnpm run smoke:webhooks
```

To also submit a real encode and verify a terminal job webhook delivery:

```bash
export CONVERTRILO_SMOKE_RUN_ENCODE=true
pnpm run smoke:webhooks
```

The API key must be able to manage webhooks and create/read jobs for the encode mode.
