From acad2e4137465792895b8436fa2936b32c77fb79 Mon Sep 17 00:00:00 2001 From: brusher_ru Date: Tue, 16 Jan 2024 19:56:47 +0700 Subject: [PATCH] test: remove over-mocked tests --- tests/fixtures/config.ts | 231 -------------------- tests/nodeConfig.spec.ts | 455 --------------------------------------- 2 files changed, 686 deletions(-) delete mode 100644 tests/fixtures/config.ts delete mode 100644 tests/nodeConfig.spec.ts diff --git a/tests/fixtures/config.ts b/tests/fixtures/config.ts deleted file mode 100644 index ae3df2ffc..000000000 --- a/tests/fixtures/config.ts +++ /dev/null @@ -1,231 +0,0 @@ -export const defaultDiscoveryConfig = { - api: { - 'grpc-public-services': [ - 'debug', - 'global', - 'mesh', - 'node', - 'transaction', - 'activation', - ], - 'grpc-public-listener': '0.0.0.0:9092', - 'grpc-private-services': ['smesher', 'admin'], - 'grpc-private-listener': '0.0.0.0:9093', - 'grpc-json-listener': '0.0.0.0:9094', - }, - preset: 'testnet', - p2p: { - 'disable-reuseport': true, - bootnodes: [], - }, - main: { - 'layer-duration': '5m', - 'layers-per-epoch': 576, - 'tick-size': 1036800, - 'block-gas-limit': 1000000, - 'eligibility-confidence-param': 100, - 'poet-server': [], - }, - genesis: { - 'genesis-time': '2023-05-31T20:00:00.498Z', - 'genesis-extra-data': 'testnet-05', - }, - poet: { - 'phase-shift': '12h', - 'cycle-gap': '12h', - 'grace-period': '1h', - 'retry-delay': '10s', - }, - logging: { - nipostBuilder: 'debug', - post: 'debug', - }, - post: { - 'post-labels-per-unit': 536870912, - 'post-max-numunits': 1000, - 'post-min-numunits': 4, - 'post-k1': 26, - 'post-k2': 37, - 'post-k3': 37, - 'post-k2pow-difficulty': 89157696150400, - 'post-pow-difficulty': - '001bf647612f3696000000000000000000000000000000000000000000000000', - }, - hare: { - 'hare-wakeup-delta': '25s', - 'hare-round-duration': '25s', - 'hare-limit-iterations': 4, - 'hare-committee-size': 200, - }, - tortoise: { - 'tortoise-zdist': 2, - 'tortoise-hdist': 2, - 'tortoise-window-size': 10000, - 'tortoise-delay-layers': 100, - }, - beacon: { - 'beacon-grace-period-duration': '10m', - 'beacon-proposal-duration': '4m', - 'beacon-first-voting-round-duration': '30m', - 'beacon-rounds-number': 200, - 'beacon-voting-round-duration': '4m', - 'beacon-weak-coin-round-duration': '4m', - }, - bootstrap: { - 'bootstrap-url': 'https://bootstrap.spacemesh.network/testnet-05', - }, - recovery: { - 'recovery-uri': - 'https://recovery.spacemesh.network/testnet-05/snapshot-6264', - 'recovery-layer': 6270, - 'preserve-own-atx': false, - }, -}; - -export const defaultDiscoveryConfigDevnet = { - ...defaultDiscoveryConfig, - genesis: { - 'genesis-time': '2023-05-29T15:00:00.498Z', - 'genesis-extra-data': 'devnet-401-short', - }, -}; - -export const defaultNodeConfig = { - smeshing: { - 'smeshing-opts': { - 'smeshing-opts-datadir': '/Users/dir/post/7f8f332c', - }, - }, - api: { - 'grpc-public-services': [ - 'debug', - 'global', - 'mesh', - 'node', - 'transaction', - 'activation', - ], - 'grpc-public-listener': '0.0.0.0:9092', - 'grpc-private-services': ['smesher', 'admin'], - 'grpc-private-listener': '0.0.0.0:9093', - 'grpc-json-listener': '0.0.0.0:9094', - }, - preset: 'testnet', - p2p: { - 'disable-reuseport': true, - bootnodes: [ - '/dns4/testnet-05-bootnode-0.spacemesh.network/tcp/5000/p2p/12D3KooWQMAAL9nkgXJgTM2psJLMbWRgZLjAxJozx7dNkKYczs2V', - '/dns4/testnet-05-bootnode-1.spacemesh.network/tcp/5000/p2p/12D3KooWF2bhnqsnu2UjxJGZs8KCbzvd91vnud29KaG9rzTNFH78', - '/dns4/testnet-05-bootnode-2.spacemesh.network/tcp/5000/p2p/12D3KooWQKosE9LZraMfFPg9QRYyKwxT4ipsar37oT6wRy8WqWos', - '/dns4/testnet-05-bootnode-3.spacemesh.network/tcp/5000/p2p/12D3KooWKMQ5j15x2gzfXfX7uisss7uwkPnqJyoHcfyiYfHgSo3F', - '/dns4/testnet-05-bootnode-4.spacemesh.network/tcp/5000/p2p/12D3KooWE1YEa3roqamhPt3CNiVRBzS7Lbv8HRbByE7fpSyLAZis', - '/dns4/testnet-05-bootnode-5.spacemesh.network/tcp/5000/p2p/12D3KooWFCqyEner8hSnBmyQCkcxkuHjwY5BhHTaRYmGuNC1SQET', - '/dns4/testnet-05-bootnode-6.spacemesh.network/tcp/5000/p2p/12D3KooWA96fvi2pQaQHpS2cpahDJCyQSsBW6A4KwHCPXsDVuiEJ', - '/dns4/testnet-05-bootnode-7.spacemesh.network/tcp/5000/p2p/12D3KooWRvAVZrK3EURQBh1wdMPRDgjtowU84div3YxWYKuGDaFU', - '/dns4/testnet-05-bootnode-8.spacemesh.network/tcp/5000/p2p/12D3KooWL3VGmZ4xgT1J5KnVJz7b6wxAUUFAVqNNRURKFfWNXLE4', - '/dns4/testnet-05-bootnode-9.spacemesh.network/tcp/5000/p2p/12D3KooWCfadA8PUva7eRn1GzdLhnfELRLu81s1na6JbdYivwBgs', - ], - }, - main: { - 'layer-duration': '5m', - 'layers-per-epoch': 576, - 'tick-size': 1036800, - 'block-gas-limit': 1000000, - 'eligibility-confidence-param': 100, - 'poet-server': [ - 'https://testnet-05-poet-0.spacemesh.network', - 'https://testnet-05-poet-1.spacemesh.network', - 'https://testnet-05-poet-2.spacemesh.network', - 'https://poet-11.spacemesh.network', - ], - }, - genesis: { - 'genesis-time': '2023-05-31T20:00:00.498Z', - 'genesis-extra-data': 'testnet-05', - }, - poet: { - 'phase-shift': '12h', - 'cycle-gap': '12h', - 'grace-period': '1h', - 'retry-delay': '10s', - }, - logging: { - nipostBuilder: 'debug', - post: 'debug', - }, - post: { - 'post-labels-per-unit': 536870912, - 'post-max-numunits': 1000, - 'post-min-numunits': 4, - 'post-k1': 26, - 'post-k2': 37, - 'post-k3': 37, - 'post-k2pow-difficulty': 89157696150400, - 'post-pow-difficulty': - '001bf647612f3696000000000000000000000000000000000000000000000000', - }, - hare: { - 'hare-wakeup-delta': '25s', - 'hare-round-duration': '25s', - 'hare-limit-iterations': 4, - 'hare-committee-size': 200, - }, - tortoise: { - 'tortoise-zdist': 2, - 'tortoise-hdist': 2, - 'tortoise-window-size': 10000, - 'tortoise-delay-layers': 100, - }, - beacon: { - 'beacon-grace-period-duration': '10m', - 'beacon-proposal-duration': '4m', - 'beacon-first-voting-round-duration': '30m', - 'beacon-rounds-number': 200, - 'beacon-voting-round-duration': '4m', - 'beacon-weak-coin-round-duration': '4m', - }, - bootstrap: { - 'bootstrap-url': 'https://bootstrap.spacemesh.network/testnet-05', - }, - recovery: { - 'recovery-uri': - 'https://recovery.spacemesh.network/testnet-05/snapshot-6264', - 'recovery-layer': 6270, - 'preserve-own-atx': false, - }, -}; - -export const customConfigWithNoSpecifiedLogging = { - logging: { - nipostBuilder: 'info', - }, -}; - -export const smeshingOptsFromStoreService = { - smeshing: { - 'smeshing-opts': { - 'smeshing-opts-datadir': '/Users/max/post/data', - 'smeshing-opts-maxfilesize': 2147483648, - 'smeshing-opts-numunits': 4, - 'smeshing-opts-provider': 0, - 'smeshing-opts-compute-batch-size': 1048576, - }, - 'smeshing-coinbase': 'stest1qqqqqqpx76g4dg297uqu6jaxaz0d4yfxjy5ufacpztqwf', - 'smeshing-proving-opts': { - 'smeshing-opts-proving-nonces': 288, - 'smeshing-opts-proving-threads': 9, - }, - 'smeshing-start': true, - }, -}; - -export const defaultSmeshingOpts = { - 'smeshing-opts': { - 'smeshing-opts-datadir': '/Users/max/post/data', - }, -}; - -export const defaultNodeConfigWithInitSmeshing = { - ...defaultNodeConfig, - smeshing: smeshingOptsFromStoreService, -}; diff --git a/tests/nodeConfig.spec.ts b/tests/nodeConfig.spec.ts deleted file mode 100644 index d2c643946..000000000 --- a/tests/nodeConfig.spec.ts +++ /dev/null @@ -1,455 +0,0 @@ -/* eslint-disable global-require */ -import { generateGenesisIDFromConfig } from '../desktop/main/Networks'; -import { NodeConfig } from '../shared/types'; -import { - customConfigWithNoSpecifiedLogging, - defaultDiscoveryConfig, - defaultDiscoveryConfigDevnet, - defaultNodeConfigWithInitSmeshing, - smeshingOptsFromStoreService, -} from './fixtures/config'; - -jest.mock('electron-fetch', () => - jest.fn().mockResolvedValue({ - text: () => JSON.stringify(defaultDiscoveryConfig), - }) -); -describe('NodeConfig.ts', () => { - beforeEach(() => { - jest.mock('fs/promises'); - jest.mock('fs'); - jest.mock('electron-store'); - jest.mock('electron-log', () => ({ - transports: { - file: {}, - console: {}, - }, - })); - }); - - afterEach(() => { - jest.resetModules(); - jest.restoreAllMocks(); - }); - - describe('download node config', () => { - it('download fresh node config and create custom config', async () => { - jest.mock('electron-fetch', () => - jest.fn().mockResolvedValue({ - text: () => JSON.stringify(defaultDiscoveryConfig), - }) - ); - - // mock StoreService , nothing to migrate - jest.mock( - '../desktop/storeService', - jest.fn().mockImplementation(() => ({ - get: jest.fn().mockImplementation(() => undefined), - remove: jest.fn().mockImplementation(() => {}), - })) - ); - - const fsPromise = require('fs/promises'); - const { downloadNodeConfig } = require('../desktop/main/NodeConfig'); - - const resultConfig = ( - await downloadNodeConfig('https://testnet-5.spacemesh.io') - ).mergedConfig; - - const smeshingOptsResult = { - smeshing: { - 'smeshing-opts': { - 'smeshing-opts-datadir': - resultConfig.smeshing['smeshing-opts']['smeshing-opts-datadir'], - }, - }, - }; - - // check config after download - expect(resultConfig).toEqual({ - ...defaultDiscoveryConfig, - ...smeshingOptsResult, - }); - - // write opts for custom config - expect(fsPromise.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.7f8f332c.json'), - JSON.stringify(smeshingOptsResult, null, 2), - { encoding: 'utf8' } - ); - - // write merged config, custom config + discovery config - expect(fsPromise.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.json'), - JSON.stringify({ - ...defaultDiscoveryConfig, - ...smeshingOptsResult, - }), - { encoding: 'utf8' } - ); - }); - - it('download fresh node config and migrate Store service', async () => { - jest.mock('electron-fetch', () => - jest.fn().mockResolvedValue({ - text: () => JSON.stringify(defaultDiscoveryConfig), - }) - ); - // mock StoreService , to check migration - jest.mock( - '../desktop/storeService', - jest.fn().mockImplementation(() => ({ - get: jest - .fn() - .mockImplementation(() => smeshingOptsFromStoreService.smeshing), - remove: jest.fn().mockImplementation(() => {}), - })) - ); - - const fsPromise = require('fs/promises'); - const { downloadNodeConfig } = require('../desktop/main/NodeConfig'); - - const resultConfig = ( - await downloadNodeConfig('https://testnet-5.spacemesh.io') - ).mergedConfig; - - // check config after download - expect(resultConfig).toEqual({ - ...defaultDiscoveryConfig, - ...smeshingOptsFromStoreService, - }); - - // write opts for custom config - expect(fsPromise.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.7f8f332c.json'), - JSON.stringify(smeshingOptsFromStoreService, null, 2), - { encoding: 'utf8' } - ); - - // write merged config, custom config + discovery config - expect(fsPromise.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.json'), - JSON.stringify({ - ...defaultDiscoveryConfig, - ...smeshingOptsFromStoreService, - }), - { encoding: 'utf8' } - ); - }); - - it('download config for testnet-5 and download config for testnet-6 should rewrite config file', async () => { - // mock discovery config - jest.mock('electron-fetch', () => - jest - .fn() - .mockResolvedValueOnce({ - text: () => JSON.stringify(defaultDiscoveryConfig), - }) - .mockResolvedValueOnce({ - text: () => JSON.stringify(defaultDiscoveryConfigDevnet), - }) - ); - - // mock StoreService - jest.mock( - '../desktop/storeService', - jest.fn().mockImplementation(() => ({ - get: jest.fn().mockImplementation(() => undefined), - remove: jest.fn().mockImplementation(() => {}), - })) - ); - - const fsPromise = require('fs/promises'); - const { downloadNodeConfig } = require('../desktop/main/NodeConfig'); - - const resultConfigTestNet = ( - await downloadNodeConfig('https://testnet-5.spacemesh.io') - ).mergedConfig; - - const smeshingOptsResultTestNet = { - smeshing: { - 'smeshing-opts': { - 'smeshing-opts-datadir': - resultConfigTestNet.smeshing['smeshing-opts'][ - 'smeshing-opts-datadir' - ], - }, - }, - }; - - // check config after download - expect(resultConfigTestNet).toEqual({ - ...defaultDiscoveryConfig, - ...smeshingOptsResultTestNet, - }); - - const resultConfigDevnet = ( - await downloadNodeConfig('https://devent-000.spacemesh.io') - ).mergedConfig; - - const smeshingOptsResultDevnet = { - smeshing: { - 'smeshing-opts': { - 'smeshing-opts-datadir': - resultConfigDevnet.smeshing['smeshing-opts'][ - 'smeshing-opts-datadir' - ], - }, - }, - }; - - // check config after download - expect(resultConfigDevnet).toEqual({ - ...defaultDiscoveryConfigDevnet, - ...smeshingOptsResultDevnet, - }); - - // check datadir not equal for 2 configs and check gensisID is different - expect( - resultConfigTestNet.smeshing['smeshing-opts']['smeshing-opts-datadir'] - ).not.toEqual( - resultConfigDevnet.smeshing['smeshing-opts']['smeshing-opts-datadir'] - ); - - // write opts for custom config - expect(fsPromise.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.7f8f332c.json'), - JSON.stringify(smeshingOptsResultTestNet, null, 2), - { encoding: 'utf8' } - ); - - // write merged config, custom config + discovery config for testnet-5 - expect(fsPromise.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.json'), - JSON.stringify({ - ...defaultDiscoveryConfig, - ...smeshingOptsResultTestNet, - }), - { encoding: 'utf8' } - ); - - expect(fsPromise.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.1ee20fed.json'), - JSON.stringify(smeshingOptsResultDevnet, null, 2), - { encoding: 'utf8' } - ); - - // write merged config, custom config + discovery config for standalone_network - expect(fsPromise.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.json'), - JSON.stringify({ - ...defaultDiscoveryConfigDevnet, - ...smeshingOptsResultDevnet, - }), - { encoding: 'utf8' } - ); - }); - }); - - describe('update smeshing opts for node config', () => { - it('expect to delete smeshing opts, update custom config', async () => { - const fsPromises = require('fs/promises'); - const nodeConfigHelpers = require('../desktop/main/NodeConfig'); - - // eslint-disable-next-line @typescript-eslint/no-unused-vars,import/no-named-as-default-member - nodeConfigHelpers.loadCustomNodeConfig = jest - .fn() - .mockImplementation(() => smeshingOptsFromStoreService); - // eslint-disable-next-line @typescript-eslint/no-unused-vars,import/no-named-as-default-member - nodeConfigHelpers.loadNodeConfig = jest - .fn() - .mockImplementation(() => defaultNodeConfigWithInitSmeshing); - - // eslint-disable-next-line import/no-named-as-default-member - const resultConfigTestNet = await nodeConfigHelpers.updateSmeshingOpts( - generateGenesisIDFromConfig( - (defaultNodeConfigWithInitSmeshing as unknown) as NodeConfig - ), - {} - ); - - const smeshingAfterReset = { - smeshing: { - 'smeshing-opts': { - 'smeshing-opts-datadir': - resultConfigTestNet.smeshing['smeshing-opts'][ - 'smeshing-opts-datadir' - ], - }, - }, - }; - - expect(resultConfigTestNet).toEqual({ - ...defaultNodeConfigWithInitSmeshing, - ...smeshingAfterReset, - }); - - // write opts for custom config - expect(fsPromises.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.7f8f332c.json'), - JSON.stringify(smeshingAfterReset, null, 2), - { encoding: 'utf8' } - ); - - // write merged config, custom config + discovery config - expect(fsPromises.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.json'), - JSON.stringify({ - ...defaultNodeConfigWithInitSmeshing, - ...smeshingAfterReset, - }), - { encoding: 'utf8' } - ); - }); - - it('expect to stop smeshing, on propriate input for updateSmeshingOpts', async () => { - const fsPromises = require('fs/promises'); - const nodeConfigHelpers = require('../desktop/main/NodeConfig'); - - // eslint-disable-next-line @typescript-eslint/no-unused-vars,import/no-named-as-default-member - nodeConfigHelpers.loadCustomNodeConfig = jest - .fn() - .mockImplementation(() => smeshingOptsFromStoreService); - // eslint-disable-next-line @typescript-eslint/no-unused-vars,import/no-named-as-default-member - nodeConfigHelpers.loadNodeConfig = jest - .fn() - .mockImplementation(() => defaultNodeConfigWithInitSmeshing); - - // eslint-disable-next-line import/no-named-as-default-member - const resultConfigTestNet = await nodeConfigHelpers.updateSmeshingOpts( - generateGenesisIDFromConfig( - (defaultNodeConfigWithInitSmeshing as unknown) as NodeConfig - ), - { 'smeshing-start': false } - ); - - const smeshingOptsAfterReset = { - smeshing: { - ...smeshingOptsFromStoreService.smeshing, - }, - }; - - expect(resultConfigTestNet).toEqual({ - ...defaultNodeConfigWithInitSmeshing, - ...smeshingOptsAfterReset, - }); - - // write opts for custom config - expect(fsPromises.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.7f8f332c.json'), - JSON.stringify(smeshingOptsAfterReset, null, 2), - { encoding: 'utf8' } - ); - - // write merged config, custom config + discovery config - expect(fsPromises.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.json'), - JSON.stringify({ - ...defaultNodeConfigWithInitSmeshing, - ...smeshingOptsAfterReset, - }), - { encoding: 'utf8' } - ); - }); - }); - - describe('merge node config and custom config', () => { - it('expect to rewrite whole section if 1 property defined in custom config for smeshing update', async () => { - const fsPromises = require('fs/promises'); - const nodeConfigHelpers = require('../desktop/main/NodeConfig'); - const customConfigWithInitedSmeshingOptsAndRewriteLogging = { - ...smeshingOptsFromStoreService, - ...customConfigWithNoSpecifiedLogging, - }; - // eslint-disable-next-line @typescript-eslint/no-unused-vars,import/no-named-as-default-member - nodeConfigHelpers.loadCustomNodeConfig = jest - .fn() - .mockImplementation( - () => customConfigWithInitedSmeshingOptsAndRewriteLogging - ); - // eslint-disable-next-line @typescript-eslint/no-unused-vars,import/no-named-as-default-member - nodeConfigHelpers.loadNodeConfig = jest - .fn() - .mockImplementation(() => defaultNodeConfigWithInitSmeshing); - - // eslint-disable-next-line import/no-named-as-default-member - const resultConfigTestNet = await nodeConfigHelpers.updateSmeshingOpts( - generateGenesisIDFromConfig( - (defaultNodeConfigWithInitSmeshing as unknown) as NodeConfig - ), - { 'smeshing-start': false } - ); - - const smeshingOptsAfterReset = { - smeshing: { - ...customConfigWithInitedSmeshingOptsAndRewriteLogging.smeshing, - }, - logging: customConfigWithInitedSmeshingOptsAndRewriteLogging.logging, - }; - - expect(resultConfigTestNet).toEqual({ - ...defaultNodeConfigWithInitSmeshing, - ...smeshingOptsAfterReset, - }); - - // write opts for custom config - expect(fsPromises.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.7f8f332c.json'), - JSON.stringify(smeshingOptsAfterReset, null, 2), - { encoding: 'utf8' } - ); - - // write merged config, custom config + discovery config - expect(fsPromises.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.json'), - JSON.stringify({ - ...defaultNodeConfigWithInitSmeshing, - ...smeshingOptsAfterReset, - }), - { encoding: 'utf8' } - ); - }); - - it('expect to rewrite whole section for node-config if only 1 property defined in custom config', async () => { - // mock discovery config - jest.mock('electron-fetch', () => - jest.fn().mockResolvedValue({ - text: () => JSON.stringify(defaultDiscoveryConfig), - }) - ); - - const fs = require('fs'); - const fsPromise = require('fs/promises'); - const nodeConfigHelpers = require('../desktop/main/NodeConfig'); - - fs.existsSync = jest.fn().mockImplementation(() => true); - - // eslint-disable-next-line @typescript-eslint/no-unused-vars,import/no-named-as-default-member - nodeConfigHelpers.loadCustomNodeConfig = jest - .fn() - .mockImplementation(() => customConfigWithNoSpecifiedLogging); - - // eslint-disable-next-line import/no-named-as-default-member - const resultConfig = ( - await nodeConfigHelpers.downloadNodeConfig( - 'https://testnet-5.spacemesh.io' - ) - ).mergedConfig; - - // check config after download - expect(resultConfig).toEqual({ - ...defaultDiscoveryConfig, - ...customConfigWithNoSpecifiedLogging, - }); - // write merged config - expect(fsPromise.writeFile).toHaveBeenCalledWith( - expect.stringContaining('Electron/node-config.json'), - JSON.stringify({ - ...defaultDiscoveryConfig, - ...customConfigWithNoSpecifiedLogging, - }), - { encoding: 'utf8' } - ); - }); - }); -});