Overview
MetaVault AI includes comprehensive testing for smart contracts and provides guidelines for testing AI agents and frontend functionality.Testing ensures the vault operates correctly and safely manages user funds. Always run tests before deploying to production.
Test Structure
The project includes tests at different levels:Contract Tests
Unit and integration tests for smart contracts using Hardhat and Chai
Agent Testing
Manual and automated testing of AI agent behaviors
Integration Tests
End-to-end tests covering the full stack
Smart Contract Testing
Running Contract Tests
Navigate to the contracts package and run the test suite:Test Configuration
The Hardhat configuration includes:hardhat.config.ts
Tests run on a local Hardhat network that can fork Sepolia testnet for realistic testing conditions.
Writing Contract Tests
Contract tests are located inpackages/contracts/test/ and use the following structure:
Test Coverage
Generate a coverage report:- Line coverage
- Branch coverage
- Function coverage
- Statement coverage
Testing with Mock Contracts
MetaVault AI uses mock contracts to simulate DeFi protocols:Deploy Mock Contracts
Deploy the complete mock environment:This deploys:
- Mock LINK and WETH tokens
- Mock Aave Pool (simulates lending)
- Mock Swap Router (simulates Uniswap)
- MetaVault and strategies
Run Testing Script
Use the testing script to simulate various scenarios:This script can:
- Simulate deposits and withdrawals
- Test strategy allocations
- Trigger yield accrual
- Test emergency scenarios
AI Agent Testing
Test the AI agents to ensure they manage the vault correctly:Manual Agent Testing
Test Strategy Sentinel
The Strategy Sentinel Agent monitors vault health. Test by:
- Checking initial strategy state
- Simulating market volatility (adjust mock prices)
- Observing agent responses
- Verifying rebalancing actions
Test Chat Agent
Test the user-facing chat agent:Test various queries:
- Balance inquiries
- Deposit/withdrawal requests
- Strategy information
- Market conditions
Automated Agent Testing
Test automation capabilities:- Run every 5 minutes (configurable)
- Monitor strategy health
- Check liquidation risks
- Perform rebalancing if needed
- Harvest and compound yields
Frontend Testing
Manual Frontend Testing
Start Development Server
Test Wallet Connection
- Connect MetaMask to localhost network
- Import test account from Hardhat
- Connect wallet through UI
- Verify account displays correctly
Test Core Functions
Test the main user flows:Deposit Flow:
- Navigate to deposit interface
- Enter amount to deposit
- Approve token spending (if needed)
- Confirm deposit transaction
- Verify balance updates
- Navigate to withdrawal interface
- Enter amount to withdraw
- Confirm withdrawal transaction
- Verify balance updates
- Open chat widget
- Send test messages
- Verify AI responses
- Test different query types
Linting and Type Checking
Run code quality checks:Integration Testing
Test the complete system working together:End-to-End Test Scenario
User Deposit
- Connect wallet to frontend
- Approve LINK token spending
- Deposit 1000 LINK tokens
- Verify:
- Transaction confirms
- Vault balance increases
- User receives vault shares
- Frontend updates balance
Strategy Allocation
- Check initial strategy allocations
- Verify funds distributed correctly
- Monitor agent logs for allocation decisions
- Confirm strategies receive funds
Yield Generation
- Wait for yield to accrue (or simulate)
- Check updated vault value
- Verify yield calculation
- Test harvest functionality
Agent Actions
- Trigger agent automation
- Monitor rebalancing decisions
- Verify risk management
- Check strategy adjustments
Performance Testing
Test system performance and gas usage:Gas Usage Analysis
- Gas per function call
- Average gas costs
- Deployment costs
Load Testing Agents
Test agent performance under load:- Response times
- Memory usage
- CPU utilization
- Error rates
Debugging Tests
Hardhat Console
Debug contracts interactively:Agent Debug Logs
Enable detailed logging:- Tool invocations
- Agent decisions
- State changes
- Error traces
Frontend DevTools
Use browser developer tools:- Console for errors and warnings
- Network tab for API calls
- React DevTools for component state
- MetaMask for transaction details
Test Checklist
Before deploying or major changes:Contract Tests
Contract Tests
- All unit tests pass
- Coverage above 80%
- Gas usage optimized
- Edge cases covered
- Mock contracts work correctly
Agent Tests
Agent Tests
- Strategy Sentinel monitors correctly
- Chat Agent responds appropriately
- Yield Simulator generates accurate projections
- Automation runs without errors
- Error handling works
Frontend Tests
Frontend Tests
- Wallet connection works
- Deposits process correctly
- Withdrawals work as expected
- UI updates reflect blockchain state
- Chat interface functions
- Mobile responsive
Integration Tests
Integration Tests
- Full deposit-to-withdrawal flow works
- Agents interact with contracts correctly
- Frontend displays accurate data
- Error states handled gracefully
- Multi-user scenarios work
Continuous Integration
Set up CI/CD for automated testing:.github/workflows/test.yml
Common Issues
Tests failing after contract changes
Tests failing after contract changes
Recompile contracts and regenerate types:
Agent tests timing out
Agent tests timing out
Ensure:
- Contracts are deployed
- RPC URL is correct
- OpenRouter API key is valid
- Increase timeout in test config
Frontend tests fail on CI
Frontend tests fail on CI
Common causes:
- Missing environment variables
- Node version mismatch
- Cache issues
Gas usage too high
Gas usage too high
Optimize contracts:
- Use
memoryinstead ofstoragewhere possible - Batch operations
- Optimize loops
- Use events instead of storage for historical data
Best Practices
Test Isolation
Each test should be independent and not rely on previous test state
Clear Assertions
Use descriptive test names and clear assertion messages
Test Edge Cases
Test boundary conditions, zero values, and error states
Mock External Calls
Use mocks for external protocols to ensure consistent test results
Next Steps
Architecture
Understand the system architecture
Smart Contracts
Learn about the smart contract design