npm package: @pioneer-platform/e2e-endpoint-portfolio
Description: Advanced portfolio endpoint performance testing
Latest version: 1.0.0

# Portfolio Endpoint Testing Suite

Advanced testing suite for the `/api/v1/portfolio` endpoint with performance benchmarking and server log diagnostics.

## Features

- ✅ Quick failure diagnosis with immediate server log access
- ✅ Cache performance testing (cold vs cached requests)
- ✅ Response structure validation
- ✅ Multi-pubkey testing
- ✅ Server health checking
- ✅ Real-time performance metrics

## Usage

```bash
# Run the test suite
bun run dev

# Run with custom API URL
API_URL=http://localhost:9001 bun run dev

# Run with real pubkeys (recommended)
ETH_PUBKEY=0x... bun run dev
```

## Test Structure

### 1. Server Health Check
Validates server is running and responsive before executing tests.

### 2. Response Structure Validation
Checks that the portfolio endpoint returns:
- `portfolio` array field
- `totalValueUsd` field
- Proper HTTP status codes

### 3. Cache Performance Test
Measures performance across 3 requests:
- **Cold start**: First request (no cache)
- **Cached #1**: Second request (should be cached)
- **Cached #2**: Third request (validate cache consistency)

Expected results:
- Cold start: 5-15 seconds (blockchain sync)
- Cached: <100ms (Redis cache)
- Speedup: 50-150x

### 4. Multi-Pubkey Test
Tests the endpoint with multiple pubkeys simultaneously.

## Current Test Results

### ❌ Issue Found: Validation Error

**Status**: 400 Bad Request
**Error**: `{"message":"Something went wrong","status":400}`

**Server Logs Show**:
- Error handler at `app.ts:131` is catching the error
- Query params are empty (expected for POST)
- Actual validation error message is not being logged

**Root Cause**:
The error handler is swallowing the actual validation error. Need to improve error logging in `app.ts` to show the actual validation failure reason.

**Next Steps**:
1. ✅ Update error handler to log actual error details
2. ⏳ Use real pubkeys instead of fake test data
3. ⏳ Verify portfolio data structure with real blockchain data

## Log Files

- Server logs: `/Users/highlander/WebstormProjects/keepkey-stack/projects/pioneer/logs/server.log`
- Discovery logs: `/Users/highlander/WebstormProjects/keepkey-stack/projects/pioneer/logs/discovery.log`

## Environment Variables

```bash
# API server URL
API_URL=http://127.0.0.1:9001

# Test pubkeys (use real pubkeys for actual testing)
ETH_PUBKEY=0x...
BTC_PUBKEY=bc1q...
```

## Adding More Test Cases

To add new chain tests, update `src/index.ts`:

```typescript
const TEST_PUBKEYS = {
    ethereum: {
        pubkey: process.env.ETH_PUBKEY || '0x...',
        caip: 'eip155:1:' + (process.env.ETH_PUBKEY || '0x...'),
        name: 'Ethereum Mainnet'
    },
    bitcoin: {
        pubkey: process.env.BTC_PUBKEY || 'bc1q...',
        caip: 'bip122:000000000019d6689c085ae165831e93:' + (process.env.BTC_PUBKEY || 'bc1q...'),
        name: 'Bitcoin Mainnet'
    }
};
```

## Performance Benchmarks

Target performance metrics:

| Metric | Target | Current |
|--------|--------|---------|
| Cold start | <15s | N/A (validation error) |
| Cached request | <100ms | 1ms (cached error) |
| Cache speedup | >50x | N/A |
| Response time (P95) | <200ms | N/A |

## Troubleshooting

### Server returns 400 errors
1. Check server logs for actual validation error
2. Verify pubkey format matches expected CAIP format
3. Check that portfolio controller is receiving the request body
4. Validate request payload structure

### Cache not working
1. Check Redis is running: `redis-cli ping`
2. Verify cache TTL configuration
3. Check cache key generation logic

### Slow performance
1. Check network latency to blockchain nodes
2. Verify database indexes
3. Check Redis cache hit rate
4. Review query optimization