mecha10-core 4.2.2

Mecha10 core runtime and foundational types
Documentation
# Integration Tests for Mecha10 Core

This directory contains integration tests for the `mecha10-core` package that require external dependencies like Redis.

## Prerequisites

Before running the integration tests, ensure you have:

1. **Redis Server** running on `localhost:6379`
   ```bash
   # Install Redis (macOS)
   brew install redis

   # Install Redis (Ubuntu/Debian)
   sudo apt-get install redis-server

   # Start Redis server
   redis-server
   ```

2. **Rust toolchain** installed
   ```bash
   curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
   ```

## Running Tests

### Run All Integration Tests

```bash
# From the repository root
cargo test --package mecha10-core --test aggregated_topics_integration -- --ignored

# Or from this directory
cd packages/core
cargo test --test aggregated_topics_integration -- --ignored
```

**Note**: The `--ignored` flag is required because these tests are marked with `#[ignore]` since they require Redis to be running.

### Run Specific Test

```bash
cargo test --package mecha10-core --test aggregated_topics_integration test_discover_topics_basic -- --ignored --nocapture
```

The `--nocapture` flag shows `println!` output from tests, which is useful for debugging.

### Run Without Redis

To verify the tests compile without running them:

```bash
cargo test --package mecha10-core --test aggregated_topics_integration --no-run
```

## Test Coverage

### `aggregated_topics_integration.rs`

Comprehensive integration tests for the Aggregated Topics feature:

1. **`test_discover_topics_basic`**
   - Tests basic topic discovery with single wildcard (`*`)
   - Verifies all published topics are discovered
   - Pattern: `/sensor/camera/*`

2. **`test_discover_topics_with_wildcard`**
   - Tests multi-segment wildcard (`**`)
   - Verifies hierarchical topic matching
   - Patterns: `/sensor/camera/*`, `/sensor/**`

3. **`test_subscribe_aggregated_basic`**
   - Tests basic aggregated subscription
   - Verifies `AggregatedReceiver` metadata (pattern, topic_count)
   - Verifies message reception

4. **`test_subscribe_aggregated_message_delivery`**
   - Tests end-to-end message delivery
   - Multiple publishers to multiple topics
   - Verifies correct message counts and topic distribution

5. **`test_subscribe_aggregated_multi_segment_wildcard`**
   - Tests `**` wildcard for cross-hierarchy matching
   - Verifies discovery across different sensor types

6. **`test_subscribe_aggregated_no_topics_error`**
   - Tests error handling when pattern matches no topics
   - Verifies appropriate error message

7. **`test_multiple_aggregated_receivers`**
   - Tests multiple consumers on same pattern
   - Verifies consumer group isolation (each receiver gets messages)

8. **`test_pattern_matching_single_wildcard`**
   - Tests single wildcard behavior with nested paths
   - Documents Redis KEYS pattern matching behavior

9. **`test_concurrent_publishers_and_subscribers`**
   - Stress test with concurrent publishers
   - Verifies message delivery under load
   - Tests for race conditions

## Troubleshooting

### Redis Connection Errors

If tests fail with connection errors:

```
Error: MessagingError { message: "Failed to connect to Redis", ... }
```

**Solutions**:
1. Ensure Redis is running: `redis-cli ping` (should return `PONG`)
2. Check Redis is on default port: `redis-cli -h localhost -p 6379 ping`
3. Restart Redis: `redis-server --daemonize yes`

### Test Timeouts

If tests timeout waiting for messages:

```
Timeout after receiving X/Y messages
```

**Solutions**:
1. Check Redis memory: `redis-cli info memory`
2. Clear Redis data: `redis-cli FLUSHALL`
3. Increase test timeout durations in test code

### Consumer Group Issues

If tests fail with consumer group errors:

```
BUSYGROUP Consumer Group name already exists
```

**Solutions**:
1. Clear Redis data: `redis-cli FLUSHALL`
2. Delete specific consumer groups:
   ```bash
   redis-cli XGROUP DESTROY mecha10:topic:/sensor/camera/rgb test_node
   ```

### Test Interference

If tests interfere with each other:

**Solutions**:
1. Run tests sequentially: `cargo test -- --test-threads=1 --ignored`
2. Use unique topic names per test
3. Clear Redis between test runs

## Adding New Tests

When adding new integration tests:

1. **Mark with `#[ignore]`** - Requires explicit opt-in
   ```rust
   #[tokio::test]
   #[ignore] // Requires Redis to be running
   async fn test_my_feature() -> Result<()> {
       // Test code
   }
   ```

2. **Use `setup()` helper** - Initializes logging and context
   ```rust
   let ctx = setup().await?;
   ```

3. **Add timeouts** - Prevent hanging tests
   ```rust
   use tokio::time::{timeout, Duration};

   match timeout(Duration::from_secs(2), agg_rx.recv()).await {
       Ok(Some((topic, msg))) => { /* ... */ }
       Ok(None) => { /* Stream ended */ }
       Err(_) => { /* Timeout */ }
   }
   ```

4. **Clean up topics** - Use unique topic names or clear Redis
   ```rust
   let unique_topic = format!("/test/{}/camera", test_name);
   ```

5. **Document test purpose** - Add clear comments

## CI/CD Integration

For continuous integration, use Docker Compose to run Redis:

```yaml
# docker-compose.test.yml
version: '3.8'
services:
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
```

Run tests in CI:

```bash
# Start Redis
docker-compose -f docker-compose.test.yml up -d

# Run tests
cargo test --package mecha10-core --test aggregated_topics_integration -- --ignored

# Cleanup
docker-compose -f docker-compose.test.yml down
```

## Performance Testing

To measure performance with different topic counts:

```bash
# Run with verbose output
RUST_LOG=debug cargo test --package mecha10-core --test aggregated_topics_integration test_concurrent_publishers_and_subscribers -- --ignored --nocapture

# Profile with flamegraph (requires cargo-flamegraph)
cargo flamegraph --test aggregated_topics_integration -- test_concurrent_publishers_and_subscribers --ignored
```

## Future Tests

Planned test coverage additions:

- [ ] Performance benchmarks (1, 5, 20, 100 topics) - See Phase 1, Day 5
- [ ] Dynamic topic discovery (topics added at runtime)
- [ ] Topic removal handling
- [ ] Pattern edge cases (empty patterns, invalid patterns)
- [ ] Large message payloads (1MB+)
- [ ] Network partition simulation
- [ ] Redis failover scenarios