import { createContract, createTevmNode, definePrecompile, http, parseAbi } from 'tevm' import { describe, expect, it } from 'vitest' describe('Create Tevm Node', () => { describe('Basic Node Creation', () => { it('should create a basic node instance', async () => { const node = createTevmNode() await node.ready() expect(node).toBeDefined() }) }) describe('Fork Configuration', () => { it('should create a forked node', async () => { const rpcUrl = process.env.MAINNET_RPC_URL if (!rpcUrl) { expect(rpcUrl).toBeUndefined() return } const node = createTevmNode({ fork: { transport: http(rpcUrl)({}), blockTag: 17_000_000n, }, }) await node.ready() expect(node).toBeDefined() }) }) describe('Mining Configuration', () => { it('should configure auto mining', async () => { const node = createTevmNode({ miningConfig: { type: 'auto', // Mine a block for every transaction }, }) await node.ready() expect(node.miningConfig.type).toBe('auto') }) it('should configure interval mining', async () => { const node = createTevmNode({ miningConfig: { type: 'interval', blockTime: 12, // Mine every 12 seconds }, }) await node.ready() expect(node.miningConfig.type).toBe('interval') expect(node.miningConfig.type === 'interval' && node.miningConfig.blockTime).toBe(12) }) }) describe('Custom Precompiles', () => { it('should add custom precompiles', async () => { const myPrecompile = definePrecompile({ contract: createContract({ abi: parseAbi([ 'function add(uint256 a, uint256 b) returns (uint256)', 'function subtract(uint256 a, uint256 b) returns (uint256)', ]), address: '0x0000000000000000000000000000000000000100', }), call: async ({ data, gasLimit }) => { console.log(data, gasLimit) return { returnValue: new Uint8Array([0x01]), executionGasUsed: 200n, } }, }) const node = createTevmNode({ customPrecompiles: [myPrecompile.precompile()], }) await node.ready() expect(node).toBeDefined() }) }) describe('Performance Profiling', () => { it('should enable profiler', async () => { const node = createTevmNode({ profiler: true, }) await node.ready() const vm = await node.getVm() const logs = vm.evm.getPerformanceLogs() expect(logs).toBeDefined() }) }) describe('Example Configurations', () => { it('should create local development node', async () => { const devNode = createTevmNode({ miningConfig: { type: 'auto' }, loggingLevel: 'debug', allowUnlimitedContractSize: true, }) await devNode.ready() expect(devNode).toBeDefined() }) it('should create production forked node', async () => { const rpcUrl = process.env.MAINNET_RPC_URL if (!rpcUrl) { expect(rpcUrl).toBeUndefined() return } const prodNode = createTevmNode({ fork: { transport: http(rpcUrl)({}), blockTag: 'latest', }, miningConfig: { type: 'interval', blockTime: 12 }, loggingLevel: 'error', }) await prodNode.ready() expect(prodNode).toBeDefined() }) it('should create testing node', async () => { const testNode = createTevmNode({ miningConfig: { type: 'auto' }, profiler: true, }) await testNode.ready() expect(testNode).toBeDefined() }) }) describe('Error Handling', () => { it('should handle initialization errors', async () => { try { const node = createTevmNode() await node.ready() } catch (error) { console.error('Node initialization failed:', error) } }) }) })