# Trigger Spatial Audio Transcoding lambda

Lambda function for triggering spatial audio transcoding. This function triggers audio transcoding jobs through the ows-transcoding service.

## Setup

1. **Install Docker** - See [Docker installation guide](https://docs.docker.com/get-docker/)
2. **AWS Credentials** - Set up AWS credentials with access to QA resources. See [internal AWS access guide](https://wiki.sonymusicdigital.com/display/PDE/AWS+Access) for instructions.

## Local Development

### Running the Function Locally

Start the lambda function in local emulation mode:

```sh
docker compose up --build -d dev
```

Invoke the function with a test event:

```sh
curl --request POST \
  --url http://localhost:9000/2015-03-31/functions/function/invocations \
  --header 'Content-Type: application/json' \
  --data '{
    "bucket": "test-bucket",
    "key": "audio/test.wav"
  }'
```

Expected result:
- `HTTP/1.1 200` with payload containing `{"is_encoded": true}`

### Integration-like Local Verification (QA-backed)

To hit `ows-transcoding` from local, provide runtime vars:

```sh
export ENVIRONMENT=qa
```

The function calls `ows-transcoding` through `owsrequest`, so your AWS identity must also be allowed to write to the `qa-ows-request-authorization` DynamoDB table.

Restart the lambda after env changes:

```sh
docker compose restart dev
```

If quoting causes JSON parse errors, use a file instead:

```sh
cat > /tmp/spatial-event.json <<'JSON'
{
  "bucket": "test-bucket",
  "key": "audio/test.wav"
}
JSON

curl --request POST \
  --url http://localhost:9000/2015-03-31/functions/function/invocations \
  --header 'Content-Type: application/json' \
  --data-binary @/tmp/spatial-event.json
```

Expected behavior:
- Transport success returns `HTTP/1.1 200` from the lambda runtime.
- If the payload body contains `errorMessage`, it is an application/config/permission failure (check `docker compose logs dev`).

### Load Changes Quickly

Like `audio_validation`, `src` is mounted into the running container for local development.
After code edits:

```sh
docker compose restart dev
```

Stop the function:

```sh
docker compose down
```

### Linting and Testing

Run all lints, type checks, and tests:

```sh
docker compose run --rm --build lint-and-test
```

Skip specific checks:

```sh
# Skip linting only
docker compose run --rm -e SKIP_LINT=1 lint-and-test

# Skip type checks only
docker compose run --rm -e SKIP_TYPE_CHECKS=1 lint-and-test

# Skip tests only
docker compose run --rm -e SKIP_TESTS=1 lint-and-test
```

### Formatting

Auto-format code with ruff:

```sh
docker compose run --rm --build format
```

### Updating Dependencies

Update the poetry.lock file:

```sh
docker compose run --rm --build update-lockfile
```

Update a specific package:

```sh
docker compose run --rm --build update-lockfile boto3
```

## Environment Variables

- `ENVIRONMENT` - Deployment environment (qa, prod)
- `SENTRY_DSN` - Sentry error tracking DSN (optional)
- `LOGGING_LEVEL` - Python logging level (default: INFO)
- AWS credentials used locally must allow `dynamodb:PutItem` on `qa-ows-request-authorization` for `owsrequest`

## CI/CD

This function is automatically built, tested, and deployed via Jenkins. See the parent repository's Jenkinsfile for the full pipeline.
