Skip to content

Commit

Permalink
🚧 More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
William Cory authored and William Cory committed Jun 27, 2024
1 parent 93d0705 commit 2c8b698
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
95 changes: 95 additions & 0 deletions packages/procedures/src/eth/ethSignTransactionProcedure.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { describe, expect, it, beforeEach } from 'bun:test'
import { createBaseClient, type BaseClient } from '@tevm/base-client'
import { ethSignTransactionProcedure } from './ethSignTransactionProcedure.js'
import type { EthSignTransactionJsonRpcRequest } from './EthJsonRpcRequest.js'
import { testAccounts } from '@tevm/utils'

let client: BaseClient

beforeEach(() => {
client = createBaseClient()
})

describe('ethSignTransactionProcedure', () => {
it('should sign a transaction successfully', async () => {
const request: EthSignTransactionJsonRpcRequest = {
jsonrpc: '2.0',
method: 'eth_signTransaction',
id: 1,
params: [
{
from: testAccounts[0].address,
to: `0x${'69'.repeat(20)}`,
value: '0x1a4',
gas: '0x5208',
gasPrice: '0x3b9aca00',
nonce: '0x0',
data: '0x',
},
],
}

const response = await ethSignTransactionProcedure({
getAccounts: async () => testAccounts,
})(request)
expect(response.error).toBeUndefined()
expect(response.result).toBeDefined()
expect(response.method).toBe('eth_signTransaction')
expect(response.id).toBe(request.id as any)
expect(response.result).toMatchSnapshot()
})

it('should handle requests without an id', async () => {
const request: EthSignTransactionJsonRpcRequest = {
jsonrpc: '2.0',
method: 'eth_signTransaction',
params: [
{
from: testAccounts[0].address,
to: `0x${'69'.repeat(20)}`,
value: '0x1a4',
gas: '0x5208',
gasPrice: '0x3b9aca00',
nonce: '0x0',
data: '0x',
},
],
}

const response = await ethSignTransactionProcedure({
getAccounts: async () => testAccounts,
})(request)
expect(response.error).toBeUndefined()
expect(response.result).toBeDefined()
expect(response.method).toBe('eth_signTransaction')
expect(response.id).toBeUndefined()
expect(response.result).toMatchSnapshot()
})

it('should handle errors from ethSignTransactionHandler', async () => {
const request: EthSignTransactionJsonRpcRequest = {
jsonrpc: '2.0',
method: 'eth_signTransaction',
id: 1,
params: [
{
from: `0x${'00'.repeat(20)}`,
to: `0x${'69'.repeat(20)}`,
value: '0x1a4',
gas: '0x5208',
gasPrice: '0x3b9aca00',
nonce: '0x0',
data: '0x',
},
],
}

const response = await ethSignTransactionProcedure({
getAccounts: async () => testAccounts,
})(request)
expect(response.method).toBe('eth_signTransaction')
expect(response.id).toBe(request.id as any)
expect(response.error).toBeDefined()
expect(response.error).toMatchSnapshot()
})
})
72 changes: 72 additions & 0 deletions packages/procedures/src/eth/ethUninstallFilterProcedure.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, expect, it, beforeEach } from 'bun:test'
import { createBaseClient, type BaseClient } from '@tevm/base-client'
import { ethUninstallFilterJsonRpcProcedure } from './ethUninstallFilterProcedure.js'
import type { EthUninstallFilterJsonRpcRequest } from './EthJsonRpcRequest.js'
import { type Hex } from '@tevm/utils'

let client: BaseClient
const filterId: Hex = '0x1'

beforeEach(() => {
client = createBaseClient()

client.getFilters().set(filterId, {
id: filterId,
type: 'Log',
created: Date.now(),
logs: [],
tx: [],
blocks: [],
installed: {},
err: undefined,
registeredListeners: [() => {}],
})
})

describe('ethUninstallFilterJsonRpcProcedure', () => {
it('should uninstall an existing filter and return true', async () => {
const request: EthUninstallFilterJsonRpcRequest = {
jsonrpc: '2.0',
method: 'eth_uninstallFilter',
id: 1,
params: [filterId],
}

const response = await ethUninstallFilterJsonRpcProcedure(client)(request)
expect(response.error).toBeUndefined()
expect(response.result).toBe(true)
expect(response.method).toBe('eth_uninstallFilter')
expect(response.id).toBe(request.id as any)
expect(client.getFilters().has(filterId)).toBe(false)
})

it('should return false for non-existent filter', async () => {
const nonExistentFilterId: Hex = '0x2'
const request: EthUninstallFilterJsonRpcRequest = {
jsonrpc: '2.0',
method: 'eth_uninstallFilter',
id: 1,
params: [nonExistentFilterId],
}

const response = await ethUninstallFilterJsonRpcProcedure(client)(request)
expect(response.error).toBeUndefined()
expect(response.result).toBe(false)
expect(response.method).toBe('eth_uninstallFilter')
expect(response.id).toBe(request.id as any)
})
it('should handle requests without an id', async () => {
const request: EthUninstallFilterJsonRpcRequest = {
jsonrpc: '2.0',
method: 'eth_uninstallFilter',
params: [filterId],
}

const response = await ethUninstallFilterJsonRpcProcedure(client)(request)
expect(response.error).toBeUndefined()
expect(response.result).toBe(true)
expect(response.method).toBe('eth_uninstallFilter')
expect(response.id).toBeUndefined()
expect(client.getFilters().has(filterId)).toBe(false)
})
})

0 comments on commit 2c8b698

Please sign in to comment.