-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2861ad2
commit ff8c644
Showing
5 changed files
with
222 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
branches: main | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
test: | ||
name: Test | ||
runs-on: ubuntu-latest-16-core | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up NodeJS | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18 | ||
cache: 'npm' | ||
|
||
- run: npm ci | ||
- run: npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { ConfigService } from '@nestjs/config' | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { Job } from 'bull' | ||
import { ethers } from 'ethers' | ||
import { EventEmitter } from 'stream' | ||
|
||
import { ExecuteDto } from './execute.dto' | ||
import { ExecutionProcessorV1 } from './execute.processor' | ||
|
||
const VALID_PRIVATE_KEY = | ||
'0xc6cbd7d76bc5baca530c875663711b947efa6a86a900a9e8645ce32e5821484e' | ||
const TOPOS_CORE_CONTRACT_ADDRESS = '0x1D7b9f9b1FF6cf0A3BEB0F84fA6F8628E540E97F' | ||
const TOPOS_SUBNET_ENDPOINT = 'topos-subnet-endpoint' | ||
|
||
const validExecuteJob: Partial<Job<ExecuteDto>> = { | ||
data: { | ||
indexOfDataInTxRaw: 4, | ||
messagingContractAddress: '', | ||
subnetId: 'id', | ||
txRaw: '', | ||
txTrieRoot: '', | ||
txTrieMerkleProof: '', | ||
}, | ||
progress: jest.fn(), | ||
} | ||
|
||
const subnetMock = { endpoint: 'endpoint' } | ||
const providerMock = Object.assign(new EventEmitter(), { | ||
getCode: jest.fn().mockResolvedValue('0x123'), | ||
}) | ||
const walletMock = {} | ||
const transactionMock = { wait: jest.fn(() => Promise.resolve({})) } | ||
const contractMock = { | ||
execute: jest.fn().mockResolvedValue(transactionMock), | ||
networkSubnetId: jest.fn().mockResolvedValue(''), | ||
subnets: jest.fn().mockResolvedValue(subnetMock), | ||
txRootToCertId: jest.fn().mockResolvedValue(''), | ||
} | ||
|
||
describe('ExecuteProcessor', () => { | ||
let app: TestingModule | ||
let executeProcessor: ExecutionProcessorV1 | ||
|
||
beforeEach(async () => { | ||
app = await Test.createTestingModule({ | ||
providers: [ExecutionProcessorV1], | ||
}) | ||
.useMocker((token) => { | ||
if (token === ConfigService) { | ||
return { | ||
get: jest.fn().mockImplementation((key: string) => { | ||
switch (key) { | ||
case 'PRIVATE_KEY': | ||
return VALID_PRIVATE_KEY | ||
case 'TOPOS_CORE_CONTRACT_ADDRESS': | ||
return TOPOS_CORE_CONTRACT_ADDRESS | ||
case 'TOPOS_SUBNET_ENDPOINT': | ||
return TOPOS_SUBNET_ENDPOINT | ||
} | ||
}), | ||
} | ||
} | ||
}) | ||
.compile() | ||
|
||
executeProcessor = app.get(ExecutionProcessorV1) | ||
}) | ||
|
||
describe('execute', () => { | ||
it('should go through if processed job is valid', async () => { | ||
const ethersProviderMock = jest | ||
.spyOn<any, any>(ethers.providers, 'WebSocketProvider') | ||
.mockReturnValue(providerMock) | ||
|
||
const ethersWalletMock = jest | ||
.spyOn<any, any>(ethers, 'Wallet') | ||
.mockReturnValue(walletMock) | ||
|
||
jest.spyOn<any, any>(ethers, 'Contract').mockReturnValue(contractMock) | ||
|
||
await executeProcessor.execute( | ||
validExecuteJob as unknown as Job<ExecuteDto> | ||
) | ||
|
||
expect(ethersProviderMock).toHaveBeenCalledWith( | ||
`ws://${TOPOS_SUBNET_ENDPOINT}/ws` | ||
) | ||
expect(ethersProviderMock).toHaveBeenCalledWith( | ||
`ws://${subnetMock.endpoint}/ws` | ||
) | ||
expect(ethersWalletMock).toHaveBeenCalledWith( | ||
VALID_PRIVATE_KEY, | ||
providerMock | ||
) | ||
|
||
expect(contractMock.txRootToCertId).toHaveBeenCalled() | ||
|
||
expect(validExecuteJob.progress).toHaveBeenCalledWith(50) | ||
|
||
expect(contractMock.execute).toHaveBeenCalledWith( | ||
validExecuteJob.data.indexOfDataInTxRaw, | ||
validExecuteJob.data.txTrieMerkleProof, | ||
validExecuteJob.data.txRaw, | ||
validExecuteJob.data.txTrieRoot, | ||
{ | ||
gasLimit: 4_000_000, | ||
} | ||
) | ||
expect(validExecuteJob.progress).toHaveBeenCalledWith(100) | ||
}) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.