Changeset View
Changeset View
Standalone View
Standalone View
apps/alias-server/test/chronikWsHandlerTests.js
Show All 13 Lines | |||||
const { mockBlock } = require('./mocks/chronikResponses'); | const { mockBlock } = require('./mocks/chronikResponses'); | ||||
const mockSecrets = require('../secrets.sample'); | const mockSecrets = require('../secrets.sample'); | ||||
const MockAdapter = require('axios-mock-adapter'); | const MockAdapter = require('axios-mock-adapter'); | ||||
const axios = require('axios'); | const axios = require('axios'); | ||||
// Mock mongodb | // Mock mongodb | ||||
const { initializeDb } = require('../src/db'); | const { initializeDb } = require('../src/db'); | ||||
const { MongoClient } = require('mongodb'); | const { MongoClient } = require('mongodb'); | ||||
const { MongoMemoryServer } = require('mongodb-memory-server'); | const { MongoMemoryServer } = require('mongodb-memory-server'); | ||||
const { testAddressAliases } = require('./mocks/aliasMocks'); | const { generated } = require('./mocks/aliasMocks'); | ||||
describe('alias-server chronikWsHandler.js', async function () { | describe('alias-server chronikWsHandler.js', async function () { | ||||
let mongoServer, testMongoClient; | let mongoServer, testMongoClient; | ||||
before(async () => { | before(async () => { | ||||
// Start mongo memory server before running this suite of unit tests | // Start mongo memory server before running this suite of unit tests | ||||
mongoServer = await MongoMemoryServer.create(); | mongoServer = await MongoMemoryServer.create(); | ||||
const mongoUri = mongoServer.getUri(); | const mongoUri = mongoServer.getUri(); | ||||
testMongoClient = new MongoClient(mongoUri); | testMongoClient = new MongoClient(mongoUri); | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | it('parseWebsocketMessage correctly processes a chronik websocket BlockConnected message if block is avalanche finalized', async function () { | ||||
// Add tx history to mockedChronik | // Add tx history to mockedChronik | ||||
// Set the script | // Set the script | ||||
const { type, hash } = cashaddr.decode( | const { type, hash } = cashaddr.decode( | ||||
config.aliasConstants.registrationAddress, | config.aliasConstants.registrationAddress, | ||||
true, | true, | ||||
); | ); | ||||
mockedChronik.setScript(type, hash); | mockedChronik.setScript(type, hash); | ||||
// Set the mock tx history | // Set the mock tx history | ||||
mockedChronik.setTxHistory(testAddressAliases.txHistory); | mockedChronik.setTxHistory(generated.txHistory); | ||||
// Mock avalanche RPC call | // Mock avalanche RPC call | ||||
// onNoMatch: 'throwException' helps to debug if mock is not being used | // onNoMatch: 'throwException' helps to debug if mock is not being used | ||||
const mock = new MockAdapter(axios, { onNoMatch: 'throwException' }); | const mock = new MockAdapter(axios, { onNoMatch: 'throwException' }); | ||||
// Mock response for rpc return of true for isfinalblock method | // Mock response for rpc return of true for isfinalblock method | ||||
mock.onPost().reply(200, { | mock.onPost().reply(200, { | ||||
result: true, | result: true, | ||||
error: null, | error: null, | ||||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | it('If parseWebsocketMessage is called before a previous call to handleBlockConnected has completed, the next call to handleBlockConnected will not enter until the first is completed', async function () { | ||||
// Add tx history to mockedChronik | // Add tx history to mockedChronik | ||||
// Set the script | // Set the script | ||||
const { type, hash } = cashaddr.decode( | const { type, hash } = cashaddr.decode( | ||||
config.aliasConstants.registrationAddress, | config.aliasConstants.registrationAddress, | ||||
true, | true, | ||||
); | ); | ||||
mockedChronik.setScript(type, hash); | mockedChronik.setScript(type, hash); | ||||
// Set the mock tx history | // Set the mock tx history | ||||
mockedChronik.setTxHistory(testAddressAliases.txHistory); | mockedChronik.setTxHistory(generated.txHistory); | ||||
// Mock avalanche RPC call | // Mock avalanche RPC call | ||||
// onNoMatch: 'throwException' helps to debug if mock is not being used | // onNoMatch: 'throwException' helps to debug if mock is not being used | ||||
const mock = new MockAdapter(axios, { onNoMatch: 'throwException' }); | const mock = new MockAdapter(axios, { onNoMatch: 'throwException' }); | ||||
// Mock response for rpc return of true for isfinalblock method | // Mock response for rpc return of true for isfinalblock method | ||||
mock.onPost().reply(200, { | mock.onPost().reply(200, { | ||||
result: true, | result: true, | ||||
error: null, | error: null, | ||||
Show All 19 Lines | it('If parseWebsocketMessage is called before a previous call to handleBlockConnected has completed, the next call to handleBlockConnected will not enter until the first is completed', async function () { | ||||
output: nextMockBlock, | output: nextMockBlock, | ||||
}); | }); | ||||
// Add tx history to nextMockedChronik | // Add tx history to nextMockedChronik | ||||
// Set the script | // Set the script | ||||
nextMockedChronik.setScript(type, hash); | nextMockedChronik.setScript(type, hash); | ||||
// Set the mock tx history | // Set the mock tx history | ||||
// For now, assume it's the same as before, i.e. no new txs found | // For now, assume it's the same as before, i.e. no new txs found | ||||
nextMockedChronik.setTxHistory(testAddressAliases.txHistory); | nextMockedChronik.setTxHistory(generated.txHistory); | ||||
const firstCallPromise = parseWebsocketMessage( | const firstCallPromise = parseWebsocketMessage( | ||||
mockedChronik, | mockedChronik, | ||||
db, | db, | ||||
telegramBot, | telegramBot, | ||||
channelId, | channelId, | ||||
avalancheRpc, | avalancheRpc, | ||||
wsMsg, | wsMsg, | ||||
Show All 22 Lines |