Merge pull request #2019 from knorrium/knorrium/backend_unit_tests
Start adding unit tests to the backend
This commit is contained in:
		
						commit
						927a54e4d9
					
				
							
								
								
									
										6
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							@ -42,8 +42,10 @@ jobs:
 | 
			
		||||
        run: npm run lint
 | 
			
		||||
        working-directory: ${{ matrix.node }}/${{ matrix.flavor }}/backend
 | 
			
		||||
 | 
			
		||||
      #  - name: Test
 | 
			
		||||
      #    run: npm run test
 | 
			
		||||
      - name: Unit Tests
 | 
			
		||||
        if: ${{ matrix.flavor == 'dev'}}
 | 
			
		||||
        run: npm run test
 | 
			
		||||
        working-directory: ${{ matrix.node }}/${{ matrix.flavor }}/backend
 | 
			
		||||
 | 
			
		||||
      - name: Build
 | 
			
		||||
        run: npm run build
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										3
									
								
								backend/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								backend/.gitignore
									
									
									
									
										vendored
									
									
								
							@ -1,7 +1,8 @@
 | 
			
		||||
# See http://help.github.com/ignore-files/ for more about ignoring files.
 | 
			
		||||
 | 
			
		||||
# production config and external assets
 | 
			
		||||
 | 
			
		||||
!mempool-config.template.json
 | 
			
		||||
!mempool-config.sample.json
 | 
			
		||||
mempool-config.json
 | 
			
		||||
pools.json
 | 
			
		||||
icons.json
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										17
									
								
								backend/jest.config.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								backend/jest.config.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,17 @@
 | 
			
		||||
import type { Config } from "@jest/types"
 | 
			
		||||
 | 
			
		||||
const config: Config.InitialOptions = {
 | 
			
		||||
  preset: "ts-jest",
 | 
			
		||||
  testEnvironment: "node",
 | 
			
		||||
  verbose: true,
 | 
			
		||||
  automock: false,
 | 
			
		||||
  collectCoverage: true,
 | 
			
		||||
  collectCoverageFrom: ["./src/**/**.ts"],
 | 
			
		||||
  coverageProvider: "v8",
 | 
			
		||||
  coverageThreshold: {
 | 
			
		||||
    global: {
 | 
			
		||||
      lines: 1
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
export default config;
 | 
			
		||||
							
								
								
									
										5892
									
								
								backend/package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										5892
									
								
								backend/package-lock.json
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@ -21,16 +21,17 @@
 | 
			
		||||
  ],
 | 
			
		||||
  "main": "index.ts",
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "tsc": "./node_modules/typescript/bin/tsc",
 | 
			
		||||
    "tsc": "./node_modules/typescript/bin/tsc -p tsconfig.build.json",
 | 
			
		||||
    "build": "npm run tsc",
 | 
			
		||||
    "start": "node --max-old-space-size=2048 dist/index.js",
 | 
			
		||||
    "start-production": "node --max-old-space-size=4096 dist/index.js",
 | 
			
		||||
    "test": "echo \"Error: no test specified\" && exit 1",
 | 
			
		||||
    "test": "./node_modules/.bin/jest --coverage",
 | 
			
		||||
    "lint": "./node_modules/.bin/eslint . --ext .ts",
 | 
			
		||||
    "lint:fix": "./node_modules/.bin/eslint . --ext .ts --fix",
 | 
			
		||||
    "prettier": "./node_modules/.bin/prettier --write \"src/**/*.{js,ts}\""
 | 
			
		||||
  },
 | 
			
		||||
  "dependencies": {
 | 
			
		||||
    "@babel/core": "^7.18.6",
 | 
			
		||||
    "@mempool/electrum-client": "^1.1.7",
 | 
			
		||||
    "@types/node": "^16.11.41",
 | 
			
		||||
    "axios": "~0.27.2",
 | 
			
		||||
@ -46,14 +47,20 @@
 | 
			
		||||
    "ws": "~8.8.0"
 | 
			
		||||
  },
 | 
			
		||||
  "devDependencies": {
 | 
			
		||||
    "@babel/core": "^7.18.6",
 | 
			
		||||
    "@babel/code-frame": "^7.18.6",
 | 
			
		||||
    "@types/compression": "^1.7.2",
 | 
			
		||||
    "@types/crypto-js": "^4.1.1",
 | 
			
		||||
    "@types/express": "^4.17.13",
 | 
			
		||||
    "@types/jest": "^28.1.4",
 | 
			
		||||
    "@types/ws": "~8.5.3",
 | 
			
		||||
    "@typescript-eslint/eslint-plugin": "^5.30.5",
 | 
			
		||||
    "@typescript-eslint/parser": "^5.30.5",
 | 
			
		||||
    "eslint": "^8.19.0",
 | 
			
		||||
    "eslint-config-prettier": "^8.5.0",
 | 
			
		||||
    "prettier": "^2.7.1"
 | 
			
		||||
    "jest": "^28.1.2",
 | 
			
		||||
    "prettier": "^2.7.1",
 | 
			
		||||
    "ts-jest": "^28.0.5",
 | 
			
		||||
    "ts-node": "^10.8.2"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										92
									
								
								backend/src/__fixtures__/mempool-config.template.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								backend/src/__fixtures__/mempool-config.template.json
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,92 @@
 | 
			
		||||
{
 | 
			
		||||
  "MEMPOOL": {
 | 
			
		||||
    "NETWORK": "__MEMPOOL_NETWORK__",
 | 
			
		||||
    "BACKEND": "__MEMPOOL_BACKEND__",
 | 
			
		||||
    "BLOCKS_SUMMARIES_INDEXING": true,
 | 
			
		||||
    "HTTP_PORT": 1,
 | 
			
		||||
    "SPAWN_CLUSTER_PROCS": 2,
 | 
			
		||||
    "API_URL_PREFIX": "__MEMPOOL_API_URL_PREFIX__",
 | 
			
		||||
    "AUTOMATIC_BLOCK_REINDEXING": true,
 | 
			
		||||
    "POLL_RATE_MS": 3,
 | 
			
		||||
    "CACHE_DIR": "__MEMPOOL_CACHE_DIR__",
 | 
			
		||||
    "CLEAR_PROTECTION_MINUTES": 4,
 | 
			
		||||
    "RECOMMENDED_FEE_PERCENTILE": 5,
 | 
			
		||||
    "BLOCK_WEIGHT_UNITS": 6,
 | 
			
		||||
    "INITIAL_BLOCKS_AMOUNT": 7,
 | 
			
		||||
    "MEMPOOL_BLOCKS_AMOUNT": 8,
 | 
			
		||||
    "PRICE_FEED_UPDATE_INTERVAL": 9,
 | 
			
		||||
    "USE_SECOND_NODE_FOR_MINFEE": 10,
 | 
			
		||||
    "EXTERNAL_ASSETS": 11,
 | 
			
		||||
    "EXTERNAL_MAX_RETRY": 12,
 | 
			
		||||
    "EXTERNAL_RETRY_INTERVAL": 13,
 | 
			
		||||
    "USER_AGENT": "__MEMPOOL_USER_AGENT__",
 | 
			
		||||
    "STDOUT_LOG_MIN_PRIORITY": "__MEMPOOL_STDOUT_LOG_MIN_PRIORITY__",
 | 
			
		||||
    "INDEXING_BLOCKS_AMOUNT": 14,
 | 
			
		||||
    "POOLS_JSON_TREE_URL": "__POOLS_JSON_TREE_URL__",
 | 
			
		||||
    "POOLS_JSON_URL": "__POOLS_JSON_URL__"
 | 
			
		||||
  },
 | 
			
		||||
  "CORE_RPC": {
 | 
			
		||||
    "HOST": "__CORE_RPC_HOST__",
 | 
			
		||||
    "PORT": 15,
 | 
			
		||||
    "USERNAME": "__CORE_RPC_USERNAME__",
 | 
			
		||||
    "PASSWORD": "__CORE_RPC_PASSWORD__"
 | 
			
		||||
  },
 | 
			
		||||
  "ELECTRUM": {
 | 
			
		||||
    "HOST": "__ELECTRUM_HOST__",
 | 
			
		||||
    "PORT": 16,
 | 
			
		||||
    "TLS_ENABLED": true
 | 
			
		||||
  },
 | 
			
		||||
  "ESPLORA": {
 | 
			
		||||
    "REST_API_URL": "__ESPLORA_REST_API_URL__"
 | 
			
		||||
  },
 | 
			
		||||
  "SECOND_CORE_RPC": {
 | 
			
		||||
    "HOST": "__SECOND_CORE_RPC_HOST__",
 | 
			
		||||
    "PORT": 17,
 | 
			
		||||
    "USERNAME": "__SECOND_CORE_RPC_USERNAME__",
 | 
			
		||||
    "PASSWORD": "__SECOND_CORE_RPC_PASSWORD__"
 | 
			
		||||
  },
 | 
			
		||||
  "DATABASE": {
 | 
			
		||||
    "ENABLED": false,
 | 
			
		||||
    "HOST": "__DATABASE_HOST__",
 | 
			
		||||
    "SOCKET": "__DATABASE_SOCKET__",
 | 
			
		||||
    "PORT": 18,
 | 
			
		||||
    "DATABASE": "__DATABASE_DATABASE__",
 | 
			
		||||
    "USERNAME": "__DATABASE_USERNAME__",
 | 
			
		||||
    "PASSWORD": "__DATABASE_PASSWORD__"
 | 
			
		||||
  },
 | 
			
		||||
  "SYSLOG": {
 | 
			
		||||
    "ENABLED": false,
 | 
			
		||||
    "HOST": "__SYSLOG_HOST__",
 | 
			
		||||
    "PORT": 19,
 | 
			
		||||
    "MIN_PRIORITY": "__SYSLOG_MIN_PRIORITY__",
 | 
			
		||||
    "FACILITY": "__SYSLOG_FACILITY__"
 | 
			
		||||
  },
 | 
			
		||||
  "STATISTICS": {
 | 
			
		||||
    "ENABLED": false,
 | 
			
		||||
    "TX_PER_SECOND_SAMPLE_PERIOD": 20
 | 
			
		||||
  },
 | 
			
		||||
  "BISQ": {
 | 
			
		||||
    "ENABLED": true,
 | 
			
		||||
    "DATA_PATH": "__BISQ_DATA_PATH__"
 | 
			
		||||
  },
 | 
			
		||||
  "SOCKS5PROXY": {
 | 
			
		||||
    "ENABLED": true,
 | 
			
		||||
    "USE_ONION": true,
 | 
			
		||||
    "HOST": "__SOCKS5PROXY_HOST__",
 | 
			
		||||
    "PORT": "__SOCKS5PROXY_PORT__",
 | 
			
		||||
    "USERNAME": "__SOCKS5PROXY_USERNAME__",
 | 
			
		||||
    "PASSWORD": "__SOCKS5PROXY_PASSWORD__"
 | 
			
		||||
  },
 | 
			
		||||
  "PRICE_DATA_SERVER": {
 | 
			
		||||
    "TOR_URL": "__PRICE_DATA_SERVER_TOR_URL__",
 | 
			
		||||
    "CLEARNET_URL": "__PRICE_DATA_SERVER_CLEARNET_URL__"
 | 
			
		||||
  },
 | 
			
		||||
  "EXTERNAL_DATA_SERVER": {
 | 
			
		||||
    "MEMPOOL_API": "__EXTERNAL_DATA_SERVER_MEMPOOL_API__",
 | 
			
		||||
    "MEMPOOL_ONION": "__EXTERNAL_DATA_SERVER_MEMPOOL_ONION__",
 | 
			
		||||
    "LIQUID_API": "__EXTERNAL_DATA_SERVER_LIQUID_API__",
 | 
			
		||||
    "LIQUID_ONION": "__EXTERNAL_DATA_SERVER_LIQUID_ONION__",
 | 
			
		||||
    "BISQ_URL": "__EXTERNAL_DATA_SERVER_BISQ_URL__",
 | 
			
		||||
    "BISQ_ONION": "__EXTERNAL_DATA_SERVER_BISQ_ONION__"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										140
									
								
								backend/src/__tests__/config.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										140
									
								
								backend/src/__tests__/config.test.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,140 @@
 | 
			
		||||
import * as fs from 'fs';
 | 
			
		||||
 | 
			
		||||
describe('Mempool Backend Config', () => {
 | 
			
		||||
  beforeEach(() => {
 | 
			
		||||
    jest.resetAllMocks();
 | 
			
		||||
    jest.resetModules();
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  test('should return defaults when no file is present', () => {
 | 
			
		||||
    jest.isolateModules(() => {
 | 
			
		||||
      jest.mock('../../mempool-config.json', () => ({}), { virtual: true });
 | 
			
		||||
 | 
			
		||||
      const config = jest.requireActual('../config').default;
 | 
			
		||||
 | 
			
		||||
      expect(config.MEMPOOL).toStrictEqual({
 | 
			
		||||
        NETWORK: 'mainnet',
 | 
			
		||||
        BACKEND: 'none',
 | 
			
		||||
        BLOCKS_SUMMARIES_INDEXING: false,
 | 
			
		||||
        HTTP_PORT: 8999,
 | 
			
		||||
        SPAWN_CLUSTER_PROCS: 0,
 | 
			
		||||
        API_URL_PREFIX: '/api/v1/',
 | 
			
		||||
        AUTOMATIC_BLOCK_REINDEXING: false,
 | 
			
		||||
        POLL_RATE_MS: 2000,
 | 
			
		||||
        CACHE_DIR: './cache',
 | 
			
		||||
        CLEAR_PROTECTION_MINUTES: 20,
 | 
			
		||||
        RECOMMENDED_FEE_PERCENTILE: 50,
 | 
			
		||||
        BLOCK_WEIGHT_UNITS: 4000000,
 | 
			
		||||
        INITIAL_BLOCKS_AMOUNT: 8,
 | 
			
		||||
        MEMPOOL_BLOCKS_AMOUNT: 8,
 | 
			
		||||
        INDEXING_BLOCKS_AMOUNT: 11000,
 | 
			
		||||
        PRICE_FEED_UPDATE_INTERVAL: 600,
 | 
			
		||||
        USE_SECOND_NODE_FOR_MINFEE: false,
 | 
			
		||||
        EXTERNAL_ASSETS: [],
 | 
			
		||||
        EXTERNAL_MAX_RETRY: 1,
 | 
			
		||||
        EXTERNAL_RETRY_INTERVAL: 0,
 | 
			
		||||
        USER_AGENT: 'mempool',
 | 
			
		||||
        STDOUT_LOG_MIN_PRIORITY: 'debug',
 | 
			
		||||
        POOLS_JSON_TREE_URL: 'https://api.github.com/repos/mempool/mining-pools/git/trees/master',
 | 
			
		||||
        POOLS_JSON_URL: 'https://raw.githubusercontent.com/mempool/mining-pools/master/pools.json'
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      expect(config.ELECTRUM).toStrictEqual({ HOST: '127.0.0.1', PORT: 3306, TLS_ENABLED: true });
 | 
			
		||||
 | 
			
		||||
      expect(config.ESPLORA).toStrictEqual({ REST_API_URL: 'http://127.0.0.1:3000' });
 | 
			
		||||
 | 
			
		||||
      expect(config.CORE_RPC).toStrictEqual({
 | 
			
		||||
        HOST: '127.0.0.1',
 | 
			
		||||
        PORT: 8332,
 | 
			
		||||
        USERNAME: 'mempool',
 | 
			
		||||
        PASSWORD: 'mempool'
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      expect(config.SECOND_CORE_RPC).toStrictEqual({
 | 
			
		||||
        HOST: '127.0.0.1',
 | 
			
		||||
        PORT: 8332,
 | 
			
		||||
        USERNAME: 'mempool',
 | 
			
		||||
        PASSWORD: 'mempool'
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      expect(config.DATABASE).toStrictEqual({
 | 
			
		||||
        ENABLED: true,
 | 
			
		||||
        HOST: '127.0.0.1',
 | 
			
		||||
        SOCKET: '',
 | 
			
		||||
        PORT: 3306,
 | 
			
		||||
        DATABASE: 'mempool',
 | 
			
		||||
        USERNAME: 'mempool',
 | 
			
		||||
        PASSWORD: 'mempool'
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      expect(config.SYSLOG).toStrictEqual({
 | 
			
		||||
        ENABLED: true,
 | 
			
		||||
        HOST: '127.0.0.1',
 | 
			
		||||
        PORT: 514,
 | 
			
		||||
        MIN_PRIORITY: 'info',
 | 
			
		||||
        FACILITY: 'local7'
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      expect(config.STATISTICS).toStrictEqual({ ENABLED: true, TX_PER_SECOND_SAMPLE_PERIOD: 150 });
 | 
			
		||||
 | 
			
		||||
      expect(config.BISQ).toStrictEqual({ ENABLED: false, DATA_PATH: '/bisq/statsnode-data/btc_mainnet/db' });
 | 
			
		||||
 | 
			
		||||
      expect(config.SOCKS5PROXY).toStrictEqual({
 | 
			
		||||
        ENABLED: false,
 | 
			
		||||
        USE_ONION: true,
 | 
			
		||||
        HOST: '127.0.0.1',
 | 
			
		||||
        PORT: 9050,
 | 
			
		||||
        USERNAME: '',
 | 
			
		||||
        PASSWORD: ''
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      expect(config.PRICE_DATA_SERVER).toStrictEqual({
 | 
			
		||||
        TOR_URL: 'http://wizpriceje6q5tdrxkyiazsgu7irquiqjy2dptezqhrtu7l2qelqktid.onion/getAllMarketPrices',
 | 
			
		||||
        CLEARNET_URL: 'https://price.bisq.wiz.biz/getAllMarketPrices'
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      expect(config.EXTERNAL_DATA_SERVER).toStrictEqual({
 | 
			
		||||
        MEMPOOL_API: 'https://mempool.space/api/v1',
 | 
			
		||||
        MEMPOOL_ONION: 'http://mempoolhqx4isw62xs7abwphsq7ldayuidyx2v2oethdhhj6mlo2r6ad.onion/api/v1',
 | 
			
		||||
        LIQUID_API: 'https://liquid.network/api/v1',
 | 
			
		||||
        LIQUID_ONION: 'http://liquidmom47f6s3m53ebfxn47p76a6tlnxib3wp6deux7wuzotdr6cyd.onion/api/v1',
 | 
			
		||||
        BISQ_URL: 'https://bisq.markets/api',
 | 
			
		||||
        BISQ_ONION: 'http://bisqmktse2cabavbr2xjq7xw3h6g5ottemo5rolfcwt6aly6tp5fdryd.onion/api'
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  test('should override the default values with the passed values', () => {
 | 
			
		||||
    jest.isolateModules(() => {
 | 
			
		||||
      const fixture = JSON.parse(fs.readFileSync(`${__dirname}/../__fixtures__/mempool-config.template.json`, 'utf8'));
 | 
			
		||||
      jest.mock('../../mempool-config.json', () => (fixture), { virtual: true });
 | 
			
		||||
 | 
			
		||||
      const config = jest.requireActual('../config').default;
 | 
			
		||||
 | 
			
		||||
      expect(config.MEMPOOL).toStrictEqual(fixture.MEMPOOL);
 | 
			
		||||
 | 
			
		||||
      expect(config.ELECTRUM).toStrictEqual(fixture.ELECTRUM);
 | 
			
		||||
 | 
			
		||||
      expect(config.ESPLORA).toStrictEqual(fixture.ESPLORA);
 | 
			
		||||
 | 
			
		||||
      expect(config.CORE_RPC).toStrictEqual(fixture.CORE_RPC);
 | 
			
		||||
 | 
			
		||||
      expect(config.SECOND_CORE_RPC).toStrictEqual(fixture.SECOND_CORE_RPC);
 | 
			
		||||
 | 
			
		||||
      expect(config.DATABASE).toStrictEqual(fixture.DATABASE);
 | 
			
		||||
 | 
			
		||||
      expect(config.SYSLOG).toStrictEqual(fixture.SYSLOG);
 | 
			
		||||
 | 
			
		||||
      expect(config.STATISTICS).toStrictEqual(fixture.STATISTICS);
 | 
			
		||||
 | 
			
		||||
      expect(config.BISQ).toStrictEqual(fixture.BISQ);
 | 
			
		||||
 | 
			
		||||
      expect(config.SOCKS5PROXY).toStrictEqual(fixture.SOCKS5PROXY);
 | 
			
		||||
 | 
			
		||||
      expect(config.PRICE_DATA_SERVER).toStrictEqual(fixture.PRICE_DATA_SERVER);
 | 
			
		||||
 | 
			
		||||
      expect(config.EXTERNAL_DATA_SERVER).toStrictEqual(fixture.EXTERNAL_DATA_SERVER);
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
});
 | 
			
		||||
							
								
								
									
										8
									
								
								backend/tsconfig.build.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								backend/tsconfig.build.json
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,8 @@
 | 
			
		||||
{
 | 
			
		||||
  "extends": "./tsconfig",
 | 
			
		||||
  "exclude": ["**/*.test.*", "**/__mocks__/*", "**/__tests__/*"],
 | 
			
		||||
  "compilerOptions": {
 | 
			
		||||
    "types": ["node"]
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -1,8 +1,8 @@
 | 
			
		||||
{
 | 
			
		||||
  "compilerOptions": {
 | 
			
		||||
    "types": ["node"],
 | 
			
		||||
    "module": "commonjs",
 | 
			
		||||
    "target": "esnext",
 | 
			
		||||
    "types": ["node", "jest"],
 | 
			
		||||
    "lib": ["es2019", "dom"],
 | 
			
		||||
    "strict": true,
 | 
			
		||||
    "noImplicitAny": false,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user