"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const sinon_1 = __importDefault(require("sinon"));
const modules_1 = require("../../src/modules");
const chain_state_1 = require("../../src/providers/chain-state");
const libs_1 = require("../../src/providers/libs");
const config_1 = require("../../src/services/config");
const p2p_1 = require("../../src/services/p2p");
const verification_1 = require("../../src/services/verification");
const unit_1 = require("../helpers/unit");
describe('Modules', function () {
    before(unit_1.unitBeforeHelper);
    after(unit_1.unitAfterHelper);
    it('should load configured modules correctly', () => {
        const sandbox = sinon_1.default.createSandbox();
        sandbox.stub(config_1.Config, 'get').returns(mockConfig);
        validateModules();
        sandbox.restore();
    });
    it('should try to load custom module', () => {
        const sandbox = sinon_1.default.createSandbox();
        const mockConfigCopy = JSON.parse(JSON.stringify(mockConfig));
        mockConfigCopy.chains.BTC.testnet.module = './bitcoin-custom';
        sandbox.stub(config_1.Config, 'get').returns(mockConfigCopy);
        try {
            modules_1.Modules.loadConfigured();
            throw new Error('it should have thrown due to a non-existing custom module');
        }
        catch (e) {
            (0, chai_1.expect)(e.message).to.include('Cannot find module \'./bitcoin-custom\'');
        }
        sandbox.restore();
    });
    it('should have services registered after loading modules', () => {
        const chainsNetworks = config_1.Config.chainNetworks();
        for (const { chain, network } of chainsNetworks) {
            const service = chain_state_1.ChainStateProvider.get({ chain, network });
            (0, chai_1.expect)(service).to.exist;
        }
    });
    it('should have libraries registered', () => {
        const chains = ['BTC', 'BCH'];
        for (const chain of chains) {
            const service = libs_1.Libs.get(chain);
            (0, chai_1.expect)(service).to.exist;
        }
    });
    it('should have p2p services registered', () => {
        const chains = [['BTC', 'regtest'], ['BCH', 'regtest']];
        for (const [chain, network] of chains) {
            const service = p2p_1.P2P.get(chain, network);
            (0, chai_1.expect)(service).to.exist;
        }
    });
    it('should have verification services registered', () => {
        const chains = [['BTC', 'regtest'], ['BCH', 'regtest']];
        for (const [chain, network] of chains) {
            const service = verification_1.Verification.get(chain, network);
            (0, chai_1.expect)(service).to.exist;
        }
    });
});
const mockConfig = {
    chains: {
        BTC: {
            testnet: {
                chainSource: 'p2p',
                trustedPeers: [
                    {
                        host: '127.0.0.1',
                        port: 18333
                    }
                ],
                rpc: {
                    host: '127.0.0.1',
                    port: 18332,
                    username: 'bitpaytest',
                    password: 'local321'
                }
            },
        },
        ETH: {
            dev: {
                trustedPeers: [
                    {
                        host: '127.0.0.1',
                        port: 8545
                    }
                ],
                chainSource: 'p2p',
                provider: {
                    protocol: 'http',
                    host: '127.0.0.1',
                    port: 8545,
                    chain: 'ETH'
                }
            }
        }
    }
};
const validateModules = () => {
    modules_1.Modules.internalServices = []; // Remove all loaded modules from internalServices array for a fresh load
    modules_1.Modules.loadConfigured(); // Re-load modules with stubbed Config.get()
    (0, chai_1.expect)(modules_1.Modules.internalServices.length).to.equal(2);
    (0, chai_1.expect)(modules_1.Modules.internalServices[0].constructor.name).to.equal('BitcoinModule');
    (0, chai_1.expect)(modules_1.Modules.internalServices[1].constructor.name).to.equal('ETHModule');
};
//# sourceMappingURL=modules.test.js.map