Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
test: add missing unit tests (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiendan authored Jul 10, 2023
1 parent 85571bd commit 3984364
Show file tree
Hide file tree
Showing 8 changed files with 2,813 additions and 1,903 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
</p>
</div>

![example workflow](https://github.com/topos-protocol/executor-service/actions/workflows/docker_build_push.yml/badge.svg)
![example workflow](https://github.com/topos-protocol/executor-service/actions/workflows/test.yml/badge.svg)
[![](https://dcbadge.vercel.app/api/server/7HZ8F8ykBT?style=flat)](https://discord.gg/7HZ8F8ykBT)

## Prerequisites

### Redis
Expand Down
4,302 changes: 2,556 additions & 1,746 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
"@nestjs/common": "^9.4.0",
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^9.4.0",
"@nestjs/jwt": "^9.0.0",
"@nestjs/jwt": "^10.1.0",
"@nestjs/passport": "^9.0.0",
"@nestjs/platform-express": "^9.4.0",
"@nestjs/swagger": "^6.1.2",
"@topos-protocol/topos-smart-contracts": "^1.1.2",
"bcrypt": "^5.1.0",
"bull": "^4.10.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"class-validator": "^0.14.0",
"ethers": "^5.7.1",
"jwks-rsa": "^2.1.5",
"passport": "^0.6.0",
Expand All @@ -50,11 +50,11 @@
"devDependencies": {
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.1.6",
"@nestjs/testing": "^9.4.0",
"@types/bcrypt": "^5.0.0",
"@types/bull": "^4.10.0",
"@types/express": "^4.17.13",
"@types/jest": "^28.1.4",
"@types/jest": "^27.5.2",
"@types/node": "^18.0.3",
"@types/passport-jwt": "^3.0.7",
"@types/passport-local": "^1.0.34",
Expand All @@ -64,11 +64,11 @@
"eslint": "^8.19.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^28.1.2",
"jest": "^27.0.3",
"prettier": "^2.7.1",
"source-map-support": "^0.5.21",
"supertest": "^6.2.4",
"ts-jest": "^28.0.5",
"ts-jest": "^27.1.5",
"ts-loader": "^9.3.1",
"ts-node": "^10.8.2",
"tsconfig-paths": "^4.0.0",
Expand Down
37 changes: 11 additions & 26 deletions src/execute/execute.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { ExecuteControllerV1 } from './execute.controller'
import { ExecuteDto } from './execute.dto'
import { ExecuteServiceV1 } from './execute.service'

const validExecuteDto: ExecuteDto = {
indexOfDataInTxRaw: 4,
messagingContractAddress: '',
subnetId: '',
txRaw: '',
txTrieRoot: '',
txTrieMerkleProof: '',
}

describe('ExecuteController', () => {
let app: TestingModule
let executeController: ExecuteControllerV1
Expand Down Expand Up @@ -33,35 +42,11 @@ describe('ExecuteController', () => {

describe('execute', () => {
it('should complete', async () => {
const executeDto: ExecuteDto = {
certId: '',
crossSubnetMessage: {
args: [],
contractAddress: '',
id: '',
method: '',
receivingSubnetEndpoint: '',
},
inclusionProof: new Uint8Array(),
}

expect(await executeController.executeV1(executeDto)).toEqual({})
expect(await executeController.executeV1(validExecuteDto)).toEqual({})
})

it('should call executeService.execute', () => {
const executeDto: ExecuteDto = {
certId: '',
crossSubnetMessage: {
args: [],
contractAddress: '',
id: '',
method: '',
receivingSubnetEndpoint: '',
},
inclusionProof: new Uint8Array(),
}

executeController.executeV1(executeDto)
executeController.executeV1(validExecuteDto)
expect(executeService.execute).toHaveBeenCalled()
})
})
Expand Down
112 changes: 112 additions & 0 deletions src/execute/execute.processor.spec.ts
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)
})
})
})
Loading

0 comments on commit 3984364

Please sign in to comment.