Files

67 lines
2.2 KiB
JavaScript

import { assert } from 'chai'
import sinon from 'sinon'
import FacilitatorLib from '../../../src/adapters/facilitator.js'
let uut
let sandbox
describe('Facilitator', () => {
beforeEach(() => {
uut = new FacilitatorLib()
sandbox = sinon.createSandbox()
})
afterEach(() => sandbox.restore())
describe('start()', () => {
it('should throw error if EVM_PRIVATE_KEY property is not provided', async () => {
try {
await uut.start()
assert(false, 'Unexpected result')
} catch (err) {
assert.include(err.message, "Property 'EVM_PRIVATE_KEY' must be a string!")
}
})
it('should start the facilitator', async () => {
try {
uut.EVM_PRIVATE_KEY = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
const viewClientMock = {
getCode: sandbox.stub().resolves('0x1'),
readContract: sandbox.stub().resolves({}),
verifyTypedData: sandbox.stub().resolves(true),
writeContract: sandbox.stub().resolves('0xhash'),
sendTransaction: sandbox.stub().resolves('0xhash'),
waitForTransactionReceipt: sandbox.stub().resolves({})
}
sandbox.stub(uut, 'privateKeyToAccount').returns({
address: '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
privateKey: '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
})
sandbox.stub(uut, 'createWalletClient').returns({
extend: () => viewClientMock
})
const signerStub = sandbox.stub(uut, 'toFacilitatorEvmSigner').returns({ address: '0x123' })
await uut.start()
// Cover all the methods of the signer
const signerConfig = signerStub.firstCall.args[0]
await signerConfig.getCode({ address: '0x1' })
await signerConfig.readContract({ address: '0x1', abi: [], functionName: 'test' })
await signerConfig.verifyTypedData({})
await signerConfig.writeContract({ address: '0x1', abi: [], functionName: 'test' })
await signerConfig.sendTransaction({})
await signerConfig.waitForTransactionReceipt({})
} catch (err) {
assert(false, 'Unexpected result')
}
})
})
})