# Heavy Rotation DynamoDB Test

This test fetches refresh tokens from DynamoDB (production presave tasks) and analyzes Spotify Heavy Rotation data for up to 50 fans who pre-saved a specific album.

## Setup

### 1. Configure AWS Profile

Follow the team's standard AWS access process to create a `songwhip` profile:

```bash
# Set profile name
export PROFILE_NAME=songwhip

# Create the profile (assumes you already have 'prod' profile configured)
aws configure set source_profile prod --profile $PROFILE_NAME

# Configure role session name (required for auditing)
export AWS_USERNAME=$(aws sts get-caller-identity --query 'Arn' --output text | cut -d '/' -f2)
aws configure set role_session_name $AWS_USERNAME --profile $PROFILE_NAME

# Configure the role ARN
export ROLE_ARN=arn:aws:iam::926734670777:role/songwhip-role
aws configure set role_arn ${ROLE_ARN} --profile $PROFILE_NAME
```

Your `~/.aws/config` should now have:
```ini
[profile songwhip]
source_profile = prod
role_session_name = <your_aws_username>
role_arn = arn:aws:iam::926734670777:role/songwhip-role
```

### 2. Install Dependencies

```bash
npm install
```

### 3. Configure Environment

Create a `.env` file (use `.env.example` as a template):

```bash
cp .env.example .env
```

Edit `.env` with your values:
```
SPOTIFY_CLIENT_ID=your_client_id_here
SPOTIFY_CLIENT_SECRET=your_client_secret_here
AWS_PROFILE=default
ALBUMID=6644715
```

**Note:** `AWS_PROFILE=default` is used because `awsume -o default` exports credentials to the default profile.

## Running the Test

```bash
# 1. First, assume your prod profile with MFA
awsume prod

# 2. Then assume the songwhip role and export to default profile
awsume songwhip -o default

# 3. Verify you're in the correct role (should show songwhip-role in account 926734670777)
aws sts get-caller-identity

# 4. Run the test
npm test
```

**Important:** The test uses `AWS_PROFILE=default` which reads credentials that `awsume` exports. Make sure to use the `-o default` flag when running `awsume songwhip`.

## What It Does

1. **Queries DynamoDB** for the specified album ID
   - Table: `songwhip-release-tasks-production`
   - Partition key: `group:album{ALBUMID}`
   - Sort key begins with: `task:spotify-presave`
   - Fetches up to 50 refresh tokens

2. **For each fan**, fetches Heavy Rotation data across 3 time ranges:
   - Last 4 Weeks (short_term)
   - Last 6 Months (medium_term)
   - All Time (long_term)

3. **Displays summary statistics**:
   - Total fans tested
   - Success/failure counts
   - Average tracks and artists per time range
   - Total execution time

## Files

- **`dynamodb-client.ts`** - DynamoDB query logic with AWS role assumption
- **`test.ts`** - Main test runner
- **`config.ts`** - Spotify API type definitions (copied from api-test)
- **`auth.ts`** - Spotify OAuth token management (copied from api-test)
- **`spotify-client.ts`** - Axios wrapper with auto token refresh (copied from api-test)
- **`fetch-heavy-rotation.ts`** - Heavy Rotation data fetcher (copied from api-test)

## Notes

- **IAM Group Requirement:** You must be in the `songwhip` IAM group to assume the `songwhip-role`
- **Role Assumption:** Use `awsume prod` then `awsume songwhip -o default` to get credentials with MFA
- **Credentials Lifetime:** Role credentials expire after 1 hour; re-run `awsume` commands to refresh
- **Rate Limiting:** The test includes a 100ms delay between fans to avoid Spotify API rate limits
- **Error Handling:** Failed token refreshes are logged but don't stop the test from continuing
- **Isolation:** The working `api-test` folder remains completely untouched

## Troubleshooting

**"AccessDenied: User is not authorized to perform: sts:AssumeRole"**
- Ensure you're in the `songwhip` IAM group (check with: `aws iam list-groups-for-user --user-name <username> --profile prod`)
- Make sure you ran `awsume prod` first to get MFA-authenticated session
- Verify the songwhip profile exists in `~/.aws/config`

**"No refresh tokens found"**
- Check that the ALBUMID in `.env` is correct
- Verify that presave tasks exist for that album in DynamoDB
- Ensure you have read permissions on the `songwhip-release-tasks-production` table
