258 lines
4.6 KiB
Markdown
258 lines
4.6 KiB
Markdown
# Testing Guide
|
|
|
|
## Overview
|
|
|
|
The DBIS system includes comprehensive test coverage across multiple test types:
|
|
- Unit tests
|
|
- Integration tests
|
|
- Invariant tests
|
|
- Fuzz tests
|
|
- Fork tests
|
|
|
|
## Running Tests
|
|
|
|
### All Tests
|
|
```bash
|
|
forge test
|
|
```
|
|
|
|
### Specific Test File
|
|
```bash
|
|
forge test --match-path test/vault/DBISInstitutionalVault.t.sol
|
|
```
|
|
|
|
### Verbose Output
|
|
```bash
|
|
forge test -vvv
|
|
```
|
|
|
|
### Gas Report
|
|
```bash
|
|
forge test --gas-report
|
|
```
|
|
|
|
## Test Structure
|
|
|
|
### Unit Tests
|
|
Located in `test/{contract}/`:
|
|
- `test/vault/DBISInstitutionalVault.t.sol`
|
|
- `test/router/FlashLoanRouter.t.sol`
|
|
- `test/kernel/RecursiveLeverageKernel.t.sol`
|
|
|
|
### Integration Tests
|
|
Located in `test/integration/`:
|
|
- `test/integration/FullCycle.t.sol`
|
|
- `test/integration/AmortizationInvariant.t.sol`
|
|
|
|
### Fuzz Tests
|
|
Located in `test/fuzz/`:
|
|
- `test/fuzz/KernelFuzz.t.sol`
|
|
|
|
## Test Categories
|
|
|
|
### 1. Core Functionality Tests
|
|
|
|
#### Vault Tests
|
|
```solidity
|
|
function test_RecordCollateralAdded() public {
|
|
// Test collateral recording
|
|
}
|
|
|
|
function test_RecordDebtRepaid() public {
|
|
// Test debt repayment recording
|
|
}
|
|
|
|
function test_VerifyPositionImproved() public {
|
|
// Test invariant verification
|
|
}
|
|
```
|
|
|
|
#### Kernel Tests
|
|
```solidity
|
|
function test_ExecuteAmortizingCycle() public {
|
|
// Test full amortization cycle
|
|
}
|
|
|
|
function test_ExecuteAmortizingCycleRevertsIfInvariantFails() public {
|
|
// Test invariant enforcement
|
|
}
|
|
```
|
|
|
|
### 2. Invariant Tests
|
|
|
|
Foundry invariant tests ensure invariants hold:
|
|
|
|
```solidity
|
|
function invariant_HealthFactorNeverDecreases() public {
|
|
// HF must never decrease
|
|
}
|
|
|
|
function invariant_DebtNeverIncreases() public {
|
|
// Debt must never increase
|
|
}
|
|
|
|
function invariant_CollateralNeverDecreases() public {
|
|
// Collateral must never decrease
|
|
}
|
|
```
|
|
|
|
Run invariant tests:
|
|
```bash
|
|
forge test --match-test invariant
|
|
```
|
|
|
|
### 3. Fuzz Tests
|
|
|
|
Random input testing to find edge cases:
|
|
|
|
```solidity
|
|
function testFuzz_ExecuteCycleWithRandomAmounts(
|
|
uint256 flashAmount,
|
|
uint256 yieldAmount
|
|
) public {
|
|
// Fuzz with random amounts
|
|
// Ensure invariants hold
|
|
}
|
|
```
|
|
|
|
Run fuzz tests:
|
|
```bash
|
|
forge test --match-test testFuzz
|
|
```
|
|
|
|
### 4. Fork Tests
|
|
|
|
Test against mainnet state:
|
|
|
|
```bash
|
|
forge test --fork-url $RPC_URL
|
|
```
|
|
|
|
## Test Utilities
|
|
|
|
### Helpers
|
|
|
|
Create test helpers in `test/helpers/`:
|
|
|
|
```solidity
|
|
contract TestHelpers {
|
|
function deployMockAavePool() internal returns (address) {
|
|
// Deploy mock
|
|
}
|
|
|
|
function createPosition(uint256 collateral, uint256 debt) internal {
|
|
// Setup test position
|
|
}
|
|
}
|
|
```
|
|
|
|
### Mocks
|
|
|
|
Mock external contracts:
|
|
- Mock Aave Pool
|
|
- Mock Oracle
|
|
- Mock Uniswap Router
|
|
|
|
## Test Scenarios
|
|
|
|
### Happy Path
|
|
1. Successful amortization cycle
|
|
2. Position improvement
|
|
3. Invariant preservation
|
|
|
|
### Edge Cases
|
|
1. Maximum loops reached
|
|
2. Flash loan fee exceeds profit
|
|
3. Swap slippage too high
|
|
4. Health factor at threshold
|
|
|
|
### Failure Modes
|
|
1. Policy denial
|
|
2. Invariant violation
|
|
3. Flash loan unavailable
|
|
4. Insufficient gas
|
|
|
|
### Stress Tests
|
|
1. Large positions
|
|
2. High leverage
|
|
3. Multiple concurrent cycles
|
|
4. Rapid price changes
|
|
|
|
## Coverage Goals
|
|
|
|
Target coverage:
|
|
- Core contracts: >90%
|
|
- Governance: >85%
|
|
- Utilities: >80%
|
|
- Overall: >85%
|
|
|
|
Generate coverage report:
|
|
```bash
|
|
forge coverage
|
|
```
|
|
|
|
## Continuous 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: Generate coverage
|
|
run: forge coverage
|
|
```
|
|
|
|
## Best Practices
|
|
|
|
1. **Test All Edge Cases**: Include boundary conditions
|
|
2. **Use Descriptive Names**: Clear test function names
|
|
3. **Test Invariants**: Always verify invariants
|
|
4. **Mock External Dependencies**: Use mocks for external calls
|
|
5. **Test Both Success and Failure**: Cover all paths
|
|
6. **Use Fuzz Testing**: Find unexpected edge cases
|
|
7. **Fork Tests for Integration**: Test with real protocols
|
|
8. **Gas Optimization Tests**: Ensure gas efficiency
|
|
|
|
## Debugging Tests
|
|
|
|
### Verbose Output
|
|
```bash
|
|
forge test -vvvv # Maximum verbosity
|
|
```
|
|
|
|
### Debug Specific Test
|
|
```bash
|
|
forge test --match-test test_ExecuteCycle -vvv
|
|
```
|
|
|
|
### Trace Transactions
|
|
```bash
|
|
forge test --debug test_ExecuteCycle
|
|
```
|
|
|
|
## Test Data
|
|
|
|
### Fixtures
|
|
Store test data in `test/fixtures/`:
|
|
- Sample positions
|
|
- Test transaction data
|
|
- Expected outcomes
|
|
|
|
### Constants
|
|
Define test constants:
|
|
```solidity
|
|
uint256 constant TEST_COLLATERAL = 1000e18;
|
|
uint256 constant TEST_DEBT = 800e18;
|
|
address constant TEST_ASSET = 0x...
|
|
```
|
|
|