Generate a comprehensive Foundry test suite with unit, fuzz, and invariant tests for a Solidity contract.
## CONTEXT A team using Foundry in 2026 wants thorough test coverage before audit. They need unit tests, fuzz tests, and stateful invariant tests, plus fork tests against mainnet state for integrations. Solidity 0.8.28+ and forge-std are available. ## ROLE Act as a smart contract test engineer who treats tests as executable specifications and aims for invariant coverage of every protocol guarantee, not just line coverage. ## RESPONSE GUIDELINES - Provide runnable Foundry test code using forge-std conventions. - Cover happy paths, revert paths, and boundary conditions. - Annotate each test with the property it verifies. - Recommend coverage targets and how to measure them. ## TASK CRITERIA ### Unit Tests - Write setUp with realistic fixtures and actors. - Test each public/external function for success and every revert reason. - Use vm.expectRevert with specific custom errors. - Assert state changes, balances, and emitted events. ### Fuzz Tests - Identify numeric inputs worth fuzzing and bound them with vm.assume/bound. - Verify monotonic and conservation properties hold across random inputs. - Catch precision and rounding edge cases through randomized amounts. - Use a sufficient run count for confidence. ### Invariant Tests - Define a handler contract that exercises realistic call sequences. - State invariants such as total supply equals sum of balances. - Configure targetContracts and targetSelectors correctly. - Ensure ghost variables track expected accounting. ### Fork & Integration Tests - Fork mainnet/L2 at a pinned block for deterministic runs. - Test against real tokens, oracles, and AMMs the protocol depends on. - Simulate adversarial actors (flash loans, donations). - Assert behavior when an external dependency reverts or returns extremes. ### Coverage & CI - Recommend forge coverage thresholds per module. - Suggest gas snapshot assertions to catch regressions. - Outline a CI workflow that runs tests, coverage, and slither. - Flag any code paths that remain hard to cover and why. ## ASK THE USER FOR - The contract(s) under test and their interfaces. - The protocol invariants in plain language. - External integrations to fork against and a block to pin. - Their target coverage level and CI environment.
Or press ⌘C to copy