# How AI Agents Use Parallel for Testing

## The Problem

Agents need to test code that uses databases, caches, and message queues. Current options:
- Mock everything (unreliable, doesn't catch real bugs)
- Use real services (slow, expensive, side effects)
- Skip testing (dangerous)

## The Solution

Parallel gives agents a lightweight gym to practice against real protocols.

## Agent Experience

### Option 1: SDK (Recommended)

```typescript
import { createTestContext } from "@parlel/parlel-pool/agent";

// One line - spins up Redis + Postgres
const ctx = await createTestContext();

// Use Redis
await ctx.redis.set("session:123", '{"user":"alice"}');
const session = await ctx.redis.get("session:123");

// Use Postgres
await ctx.postgres.execute("CREATE TABLE users (id SERIAL PRIMARY KEY, email VARCHAR(255))");
await ctx.postgres.execute("INSERT INTO users (email) VALUES ('alice@test.com')");
const users = await ctx.postgres.query("SELECT * FROM users");

// Cleanup - everything disappears
await ctx.cleanup();
```

### Option 2: CLI

```bash
# Start services
parallel up redis postgres

# Test your code
npm test

# Cleanup
parallel down
```

### Option 3: Docker

```bash
# Run all services in one container
docker run -p 3100:3100 -p 16379:6379 -p 15432:5432 parallel:latest

# Test your code
npm test

# Stop container
docker stop parallel
```

## What Agents Get

| Feature | Benefit |
|---------|---------|
| Real protocols | redis-cli, psql work |
| Zero side effects | Ephemeral state |
| Fast startup | <1 second |
| Small footprint | ~220KB total |
| Auto cleanup | No leftover data |

## Example: Agent Testing a User Service

```typescript
import { createTestContext } from "@parlel/parlel-pool/agent";
import { UserService } from "./user-service";

describe("UserService", () => {
  let ctx;
  let service;

  beforeAll(async () => {
    ctx = await createTestContext();
    service = new UserService(ctx.postgres, ctx.redis);
  });

  afterAll(async () => {
    await ctx.cleanup();
  });

  it("should create user", async () => {
    const user = await service.createUser("alice@test.com", "Alice");
    expect(user.id).toBeDefined();
    expect(user.email).toBe("alice@test.com");
  });

  it("should cache user in Redis", async () => {
    const user = await service.createUser("bob@test.com", "Bob");
    const cached = await ctx.redis.get(`user:${user.id}`);
    expect(cached).toContain("Bob");
  });

  it("should handle duplicate email", async () => {
    await service.createUser("dup@test.com", "First");
    await expect(service.createUser("dup@test.com", "Second"))
      .rejects.toThrow("already exists");
  });
});
```

## Architecture

```
┌─────────────────────────────────────────┐
│           Agent Test Suite              │
├─────────────────────────────────────────┤
│         @parlel/parlel-pool           │
│              (SDK)                      │
├─────────────────────────────────────────┤
│     Redis    │   Postgres   │   Kafka   │
│   (50KB)     │    (80KB)    │   (90KB)  │
│   RESP       │   Wire       │   Binary  │
│   Protocol   │   Protocol   │   Protocol│
└─────────────────────────────────────────┘
```

## Why This Works

1. **Real protocols** - Not mocks. Actual Redis RESP, Postgres wire protocol.
2. **Ephemeral** - State disappears when tests end.
3. **Fast** - Starts in <1 second.
4. **Small** - ~220KB vs ~880MB for real services.
5. **Composable** - Use only what you need.
