import { encodeErrorResult } from 'viem' import { afterEach, describe, expect, it } from 'vitest' import { createMemoryClient } from '../../../packages/memory-client/src/createMemoryClient.js' import { AdvancedContract, BlockReader, ErrorContract, getAlchemyUrl, normalizeRpcUrl, SimpleContract, TestERC20, TestERC721, transports, } from './index.js' const address = '0x0000000000000000000000000000000000000042' const clients: ReturnType[] = [] afterEach(async () => { await Promise.all(clients.splice(0).map((client) => client.tevmClose())) }) describe('@tevm/test-utils fixtures', () => { it('ships usable ABI and bytecode for every exported contract fixture', () => { for (const contract of [AdvancedContract, BlockReader, ErrorContract, SimpleContract, TestERC20, TestERC721]) { expect(contract.humanReadableAbi.length).toBeGreaterThan(0) expect(contract.bytecode).toMatch(/^0x[0-9a-f]+$/) expect(contract.deployedBytecode).toMatch(/^0x[0-9a-f]+$/) } }) it('executes the SimpleContract fixture in a real in-process EVM', async () => { const client = createMemoryClient() clients.push(client) await client.tevmReady() await client.tevmSetAccount({ address, deployedBytecode: SimpleContract.deployedBytecode }) const contract = SimpleContract.withAddress(address) await client.tevmContract({ ...contract.write.set(42n), addToBlockchain: true }) const result = await client.tevmContract(contract.read.get()) expect(result.data).toBe(42n) }) it('preserves revert information from the ErrorContract fixture', async () => { const client = createMemoryClient() clients.push(client) await client.tevmReady() await client.tevmSetAccount({ address, deployedBytecode: ErrorContract.deployedBytecode }) await expect( client.tevmContract(ErrorContract.withAddress(address).write.revertWithStringError()), ).rejects.toMatchObject({ code: 3, data: encodeErrorResult({ abi: [{ type: 'error', name: 'Error', inputs: [{ type: 'string' }] }], errorName: 'Error', args: ['This is a string error message'], }), }) }) it('uses the exported Optimism transport against a pinned real block', async () => { const block = await transports.optimism.request({ method: 'eth_getBlockByNumber', params: ['0x7bfa480', false], }) expect((block as { hash: string }).hash).toBe('0xaf131f54209291613f0b74e61903405ea84bf30368ea5c6cf787992351ad843d') }, 30_000) it('builds an Alchemy URL with an explicit key', () => { expect(getAlchemyUrl('optimism', 'test-key')).toBe('https://opt-mainnet.g.alchemy.com/v2/test-key') expect(getAlchemyUrl('mainnet', 'test-key')).toBe('https://eth-mainnet.g.alchemy.com/v2/test-key') }) it('rewrites the retired alchemyapi.io host and leaves every other RPC URL alone', () => { expect(normalizeRpcUrl('https://eth-mainnet.alchemyapi.io/v2/key')).toBe('https://eth-mainnet.g.alchemy.com/v2/key') expect(normalizeRpcUrl('https://opt-mainnet.alchemyapi.io/v2/key')).toBe('https://opt-mainnet.g.alchemy.com/v2/key') expect(normalizeRpcUrl('https://eth-mainnet.g.alchemy.com/v2/key')).toBe('https://eth-mainnet.g.alchemy.com/v2/key') expect(normalizeRpcUrl('https://mainnet.optimism.io')).toBe('https://mainnet.optimism.io') expect(normalizeRpcUrl('https://not-alchemyapi.io.example.com/v2')).toBe('https://not-alchemyapi.io.example.com/v2') }) })