# Testing Guide

**Last Updated**: 2025-01-27  
**Status**: Active

This guide explains the testing infrastructure and how to run tests for the DeFi Oracle Meta Mainnet project.

## Table of Contents

- [Overview](#overview)
- [Test Structure](#test-structure)
- [Running Tests](#running-tests)
- [Writing Tests](#writing-tests)
- [Test Types](#test-types)
- [CI/CD Integration](#cicd-integration)

## Overview

The project includes a comprehensive testing infrastructure with multiple layers:

- **Unit Tests** - Contract-level tests using Foundry
- **Integration Tests** - Cross-contract and service integration tests
- **End-to-End Tests** - Full system tests
- **Load Tests** - Performance and stress tests
- **Security Tests** - Vulnerability scanning

## Test Structure

### Contract Tests

Located in `test/` directory:

```
test/
├── Aggregator.t.sol          # Oracle aggregator tests
├── AggregatorFuzz.t.sol      # Fuzz tests for aggregator
├── WETH.t.sol                # WETH9 tests
├── WETH10.t.sol              # WETH10 tests
├── Multicall.t.sol          # Multicall tests
├── CCIPWETH9Bridge.t.sol    # CCIP WETH9 bridge tests
├── CCIPWETH10Bridge.t.sol   # CCIP WETH10 bridge tests
├── TwoWayBridge.t.sol       # Two-way bridge tests
├── ccip/                     # CCIP integration tests
└── e2e/                      # End-to-end tests
```

### Integration Tests

Located in `tests/` directory:

```
tests/
├── e2e/                      # End-to-end test suites
├── health-check.sh           # Health check script
├── load-test.sh              # General load test
├── load-test-rpc.sh          # RPC load test
├── load-test-oracle.sh       # Oracle load test
├── load-test-ccip.sh         # CCIP load test
├── load-test-production.sh   # Production load test
├── metamask-integration.test.ts  # MetaMask integration tests
└── playwright.config.ts      # Playwright configuration
```

## Running Tests

### Contract Tests (Foundry)

```bash
# Run all contract tests
forge test

# Run specific test file
forge test --match-path test/WETH.t.sol

# Run with verbosity
forge test -vvv

# Run with gas reporting
forge test --gas-report

# Run fuzz tests
forge test --match-contract Fuzz
```

### Integration Tests

```bash
# Run health check
./tests/health-check.sh

# Run load tests
./tests/load-test.sh

# Run RPC load test
./tests/load-test-rpc.sh

# Run oracle load test
./tests/load-test-oracle.sh

# Run CCIP load test
./tests/load-test-ccip.sh
```

### End-to-End Tests

```bash
# Run E2E tests
cd tests/e2e
npm test

# Run with Playwright
npx playwright test
```

### All Tests

```bash
# Run all tests using Makefile
make test

# Or run tests in parallel
make test-parallel
```

## Writing Tests

### Foundry Test Structure

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import {Test, console} from "forge-std/Test.sol";
import {MyContract} from "../contracts/MyContract.sol";

contract MyContractTest is Test {
    MyContract public contract;
    address public deployer;

    function setUp() public {
        deployer = address(this);
        contract = new MyContract();
    }

    function testFunction() public {
        // Arrange
        uint256 expected = 100;
        
        // Act
        uint256 result = contract.function();
        
        // Assert
        assertEq(result, expected);
    }

    function testFuzz(uint256 input) public {
        // Fuzz test
        uint256 result = contract.function(input);
        assertTrue(result >= 0);
    }
}
```

### Integration Test Structure

```bash
#!/bin/bash
# Integration test example

set -e

# Setup
RPC_URL="${RPC_URL:-http://localhost:8545}"

# Test
echo "Testing RPC endpoint..."
response=$(curl -X POST "$RPC_URL" \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}')

# Verify
if echo "$response" | grep -q "result"; then
    echo "✅ Test passed"
    exit 0
else
    echo "❌ Test failed"
    exit 1
fi
```

## Test Types

### Unit Tests

**Purpose**: Test individual contract functions

**Location**: `test/*.t.sol`

**Example**:
```solidity
function testDeposit() public {
    weth.deposit{value: 1 ether}();
    assertEq(weth.balanceOf(address(this)), 1 ether);
}
```

### Integration Tests

**Purpose**: Test interactions between contracts

**Location**: `test/ccip/`, `test/e2e/`

**Example**:
```solidity
function testCCIPBridge() public {
    // Deploy bridge
    // Send message
    // Verify on destination
}
```

### End-to-End Tests

**Purpose**: Test full system workflows

**Location**: `tests/e2e/`

**Example**:
- Deploy contracts
- Configure oracle
- Send transactions
- Verify results

### Load Tests

**Purpose**: Test system under load

**Location**: `tests/load-test*.sh`

**Example**:
```bash
# Test RPC under load
./tests/load-test-rpc.sh --requests 1000 --concurrent 10
```

### Fuzz Tests

**Purpose**: Find edge cases through random input

**Location**: `test/*Fuzz.t.sol`

**Example**:
```solidity
function testFuzzTransfer(uint256 amount) public {
    amount = bound(amount, 1, type(uint256).max);
    // Test with random amounts
}
```

## CI/CD Integration

### GitHub Actions Example

```yaml
name: Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Foundry
        uses: foundry-rs/foundry-toolchain@v1
      - name: Run tests
        run: forge test
      - name: Run integration tests
        run: ./tests/health-check.sh
```

## Test Best Practices

1. **Test Coverage**
   - Aim for >80% coverage
   - Test happy paths
   - Test error cases
   - Test edge cases

2. **Test Organization**
   - One test file per contract
   - Group related tests
   - Use descriptive names
   - Document complex tests

3. **Test Data**
   - Use fixtures for complex data
   - Use fuzzing for edge cases
   - Test with realistic data
   - Test boundary conditions

4. **Test Maintenance**
   - Keep tests up to date
   - Fix flaky tests
   - Remove obsolete tests
   - Review test failures

## Related Documentation

- [E2E Testing Report (Archived)](../archive/status-reports/docs-root-legacy/E2E_TESTING_REPORT.md)
- [Security Scanning Guide](../security/SECURITY_SCANNING_GUIDE.md)
- [Deployment Guide](../deployment/DEPLOYMENT.md)

---

**Last Updated**: 2025-01-27
