"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const ts_mockito_1 = require("ts-mockito");
const rest_service_1 = require("./api/rest-service");
const client_1 = require("./client");
const state_1 = require("./services/state");
const membernode_client_1 = require("./socket/membernode/membernode-client");
const walletnode_1 = require("./socket/walletnode");
const api_1 = require("./api");
const mockStateService = ts_mockito_1.mock(state_1.StateService);
const mockWalletNodeClient = ts_mockito_1.mock(walletnode_1.WalletNodeClient);
const mockMembernodeClient = ts_mockito_1.mock(membernode_client_1.MemberNodeClient);
const mockRestService = ts_mockito_1.mock(rest_service_1.RestService);
const mockJwtService = ts_mockito_1.mock(api_1.SetlJWT);
let mockConnectionService;
let stateService;
describe('client', () => {
    let sut;
    beforeEach(() => {
        ts_mockito_1.reset(mockStateService);
        ts_mockito_1.reset(mockWalletNodeClient);
        ts_mockito_1.reset(mockMembernodeClient);
        ts_mockito_1.reset(mockRestService);
        ts_mockito_1.reset(mockJwtService);
        stateService = new state_1.StateService();
        mockConnectionService = {
            events$: new rxjs_1.Subject(),
            walletSocket: ts_mockito_1.instance(mockWalletNodeClient),
            memberSocket: ts_mockito_1.instance(mockMembernodeClient),
        };
        sut = new client_1.SetlClient(stateService, mockConnectionService, ts_mockito_1.instance(mockRestService), ts_mockito_1.instance(mockJwtService), 'localhost');
    });
    describe('transport', () => {
        it('calls the socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            yield sut.block();
            ts_mockito_1.verify(mockWalletNodeClient.block()).once();
        }));
        it('calls rpc when switched', () => __awaiter(void 0, void 0, void 0, function* () {
            sut.switchToRpc();
            yield sut.block();
            ts_mockito_1.verify(mockRestService.block()).once();
        }));
        it('calls the socket when switched', () => __awaiter(void 0, void 0, void 0, function* () {
            sut.switchToRpc();
            sut.switchToSocket();
            yield sut.block();
            ts_mockito_1.verify(mockWalletNodeClient.block()).once();
        }));
    });
    describe('login', () => {
        it('calls the rest service', () => __awaiter(void 0, void 0, void 0, function* () {
            yield sut.login({ username: 'stuart', password: 'letmein' });
            ts_mockito_1.verify(mockRestService.getToken(ts_mockito_1.deepEqual({ username: 'stuart', password: 'letmein' }))).once();
        }));
    });
    describe('me', () => {
        it('calls the member socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            yield sut.me();
            ts_mockito_1.verify(mockMembernodeClient.me()).once();
        }));
        it('calls the rest service when RPC', () => __awaiter(void 0, void 0, void 0, function* () {
            sut.switchToRpc();
            yield sut.me();
            ts_mockito_1.verify(mockRestService.me()).once();
        }));
    });
    describe('wallets', () => {
        it('calls the member socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { Data: [{ walletID: 1 }, { walletID: 2 }] };
            ts_mockito_1.when(mockMembernodeClient.walletAccess()).thenResolve(mockResponse);
            const result = yield sut.wallets();
            ts_mockito_1.verify(mockMembernodeClient.walletAccess()).once();
            expect(result).toStrictEqual([{ walletID: 1 }, { walletID: 2 }]);
        }));
        it('calls the member socket by default + returns only single wallet', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { Data: [{ walletID: 1 }, { walletID: 2 }] };
            ts_mockito_1.when(mockMembernodeClient.walletAccess()).thenResolve(mockResponse);
            const result = yield sut.wallets({ walletId: 1 });
            ts_mockito_1.verify(mockMembernodeClient.walletAccess()).once();
            expect(result).toStrictEqual([{ walletID: 1 }]);
        }));
        it('calls rest - single wallet', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.wallet(ts_mockito_1.anything())).thenResolve('single wallet');
            sut.switchToRpc();
            const result = yield sut.wallets({ walletId: 1 });
            ts_mockito_1.verify(mockRestService.wallet(ts_mockito_1.anything())).once();
            expect(result).toBe('single wallet');
        }));
        it('calls rest - wall wallets', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.allWallets()).thenResolve('wallets');
            sut.switchToRpc();
            const result = yield sut.wallets();
            ts_mockito_1.verify(mockRestService.allWallets()).once();
            expect(result).toBe('wallets');
        }));
    });
    describe('messages', () => {
        it('calls the member socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { Data: [{ subject: 'test' }] };
            ts_mockito_1.when(mockMembernodeClient.messages(1, 2)).thenResolve(mockResponse);
            const result = yield sut.messages(1, 2);
            ts_mockito_1.verify(mockMembernodeClient.messages(1, 2)).once();
            expect(result).toStrictEqual([{ subject: 'test' }]);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            sut.switchToRpc();
            yield sut.messages(1, 2);
            ts_mockito_1.verify(mockRestService.allMessages()).once();
        }));
    });
    describe('decryptMessage', () => {
        it('calls the member socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { Data: 'decrypt' };
            ts_mockito_1.when(mockMembernodeClient.decrypt(2, 1, 'test')).thenResolve(mockResponse);
            const result = yield sut.decryptMessage({ bobWalletId: 1, aliceWalletId: 2, encryptedMessage: 'test' });
            ts_mockito_1.verify(mockMembernodeClient.decrypt(2, 1, 'test')).once();
            expect(result).toStrictEqual({ Data: 'decrypt' });
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { Data: 'decrypt' };
            ts_mockito_1.when(mockRestService.decryptMessage(ts_mockito_1.deepEqual({ bobWalletId: 1, aliceWalletId: 2, encryptedMessage: 'test' }))).thenResolve(mockResponse);
            sut.switchToRpc();
            const result = yield sut.decryptMessage({ bobWalletId: 1, aliceWalletId: 2, encryptedMessage: 'test' });
            ts_mockito_1.verify(mockRestService.decryptMessage(ts_mockito_1.deepEqual({ bobWalletId: 1, aliceWalletId: 2, encryptedMessage: 'test' }))).once();
            expect(result).toStrictEqual({ Data: 'decrypt' });
        }));
    });
    describe('encryptMessage', () => {
        it('calls the member socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { Data: 'encrypt' };
            ts_mockito_1.when(mockMembernodeClient.encrypt(2, 1, 'test')).thenResolve(mockResponse);
            const result = yield sut.encryptMessage({ bobWalletId: 1, aliceWalletId: 2, message: 'test' });
            ts_mockito_1.verify(mockMembernodeClient.encrypt(2, 1, 'test')).once();
            expect(result).toStrictEqual({ Data: 'encrypt' });
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { Data: 'encrypt' };
            ts_mockito_1.when(mockRestService.encryptMessage(ts_mockito_1.deepEqual({ bobWalletId: 1, aliceWalletId: 2, message: 'test' }))).thenResolve(mockResponse);
            sut.switchToRpc();
            const result = yield sut.encryptMessage({ bobWalletId: 1, aliceWalletId: 2, message: 'test' });
            ts_mockito_1.verify(mockRestService.encryptMessage(ts_mockito_1.deepEqual({ bobWalletId: 1, aliceWalletId: 2, message: 'test' }))).once();
            expect(result).toStrictEqual({ Data: 'encrypt' });
        }));
    });
    describe('access', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { message: { data: 'access' } };
            ts_mockito_1.when(mockRestService.access(ts_mockito_1.deepEqual({ refresh: true }))).thenResolve(mockResponse);
            const result = yield sut.access({ refresh: true });
            ts_mockito_1.verify(mockRestService.access(ts_mockito_1.deepEqual({ refresh: true }))).once();
            expect(result).toStrictEqual({ message: { data: 'access' } });
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { message: { data: 'access' } };
            ts_mockito_1.when(mockRestService.access(ts_mockito_1.deepEqual({ refresh: true }))).thenResolve(mockResponse);
            sut.switchToRpc();
            const result = yield sut.access({ refresh: true });
            ts_mockito_1.verify(mockRestService.access(ts_mockito_1.deepEqual({ refresh: true }))).once();
            expect(result).toStrictEqual({ message: { data: 'access' } });
        }));
    });
    describe('addresses', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = [{ address: 'add1', 'publicKey': 'pub1' }];
            ts_mockito_1.when(mockRestService.addresses(1)).thenResolve(mockResponse);
            const result = yield sut.addresses(1);
            ts_mockito_1.verify(mockRestService.addresses(1)).once();
            expect(result).toStrictEqual([{ address: 'add1', 'publicKey': 'pub1' }]);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = [{ address: 'add1', 'publicKey': 'pub1' }];
            ts_mockito_1.when(mockRestService.addresses(1)).thenResolve(mockResponse);
            sut.switchToRpc();
            const result = yield sut.addresses(1);
            ts_mockito_1.verify(mockRestService.addresses(1)).once();
            expect(result).toStrictEqual([{ address: 'add1', 'publicKey': 'pub1' }]);
        }));
    });
    describe('allAssetClasses', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = [{ instrument: 'GBP', namespace: 'BOE', address: 'add1', metadata: '' }];
            ts_mockito_1.when(mockRestService.allAssetClasses()).thenResolve(mockResponse);
            const result = yield sut.allAssetClasses();
            ts_mockito_1.verify(mockRestService.allAssetClasses()).once();
            expect(result).toStrictEqual([{ instrument: 'GBP', namespace: 'BOE', address: 'add1', metadata: '' }]);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = [{ instrument: 'GBP', namespace: 'BOE', address: 'add1', metadata: '' }];
            ts_mockito_1.when(mockRestService.allAssetClasses()).thenResolve(mockResponse);
            sut.switchToRpc();
            const result = yield sut.allAssetClasses();
            ts_mockito_1.verify(mockRestService.allAssetClasses()).once();
            expect(result).toStrictEqual([{ instrument: 'GBP', namespace: 'BOE', address: 'add1', metadata: '' }]);
        }));
    });
    describe('allNamespaces', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = ['BOE'];
            ts_mockito_1.when(mockRestService.allNamespaces()).thenResolve(mockResponse);
            const result = yield sut.allNamespaces();
            ts_mockito_1.verify(mockRestService.allNamespaces()).once();
            expect(result).toStrictEqual(['BOE']);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = ['BOE'];
            ts_mockito_1.when(mockRestService.allNamespaces()).thenResolve(mockResponse);
            sut.switchToRpc();
            const result = yield sut.allNamespaces();
            ts_mockito_1.verify(mockRestService.allNamespaces()).once();
            expect(result).toStrictEqual(['BOE']);
        }));
    });
    describe('assetClasses', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = [{ instrument: 'GBP', namespace: 'BOE', address: 'add1', metadata: '' }];
            ts_mockito_1.when(mockRestService.allAssetClasses()).thenResolve(mockResponse);
            const result = yield sut.allAssetClasses();
            ts_mockito_1.verify(mockRestService.allAssetClasses()).once();
            expect(result).toStrictEqual([{ instrument: 'GBP', namespace: 'BOE', address: 'add1', metadata: '' }]);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = [{ instrument: 'GBP', namespace: 'BOE', address: 'add1', metadata: '' }];
            ts_mockito_1.when(mockRestService.allAssetClasses()).thenResolve(mockResponse);
            sut.switchToRpc();
            const result = yield sut.allAssetClasses();
            ts_mockito_1.verify(mockRestService.allAssetClasses()).once();
            expect(result).toStrictEqual([{ instrument: 'GBP', namespace: 'BOE', address: 'add1', metadata: '' }]);
        }));
    });
    describe('block', () => {
        it('calls the member socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockWalletNodeClient.block()).thenResolve('block');
            const result = yield sut.block();
            ts_mockito_1.verify(mockWalletNodeClient.block()).once();
            expect(result).toBe('block');
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.block()).thenResolve('block');
            sut.switchToRpc();
            const result = yield sut.block();
            ts_mockito_1.verify(mockRestService.block()).once();
            expect(result).toBe('block');
        }));
    });
    describe('contract', () => {
        it('calls the socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { message: { data: { __address: 'con1' } } };
            ts_mockito_1.when(mockWalletNodeClient.contract(1, 'con1')).thenResolve(mockResponse);
            const result = yield sut.contract({ walletid: 1, contractid: 'con1' });
            ts_mockito_1.verify(mockWalletNodeClient.contract(1, 'con1')).once();
            expect(result).toStrictEqual(mockResponse);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.contract(ts_mockito_1.deepEqual({ walletid: 1, contractid: 'con1' }))).thenResolve('contract');
            sut.switchToRpc();
            const result = yield sut.contract({ walletid: 1, contractid: 'con1' });
            ts_mockito_1.verify(mockRestService.contract(ts_mockito_1.deepEqual({ walletid: 1, contractid: 'con1' }))).once();
            expect(result).toBe('contract');
        }));
    });
    describe('holders', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.holders(ts_mockito_1.deepEqual({ walletid: 1, namespace: 'BOE', classid: 'GBP' }))).thenResolve('holders');
            const result = yield sut.holders({ walletid: 1, namespace: 'BOE', classid: 'GBP' });
            ts_mockito_1.verify(mockRestService.holders(ts_mockito_1.deepEqual({ walletid: 1, namespace: 'BOE', classid: 'GBP' }))).once();
            expect(result).toBe('holders');
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.holders(ts_mockito_1.deepEqual({ walletid: 1, namespace: 'BOE', classid: 'GBP' }))).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.holders({ walletid: 1, namespace: 'BOE', classid: 'GBP' });
            ts_mockito_1.verify(mockRestService.holders(ts_mockito_1.deepEqual({ walletid: 1, namespace: 'BOE', classid: 'GBP' }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('holdings', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.holdings(ts_mockito_1.deepEqual({ walletId: 1 }))).thenResolve('holders');
            const result = yield sut.holdings({ walletId: 1 });
            ts_mockito_1.verify(mockRestService.holdings(ts_mockito_1.deepEqual({ walletId: 1 }))).once();
            expect(result).toBe('holders');
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.holdings(ts_mockito_1.deepEqual({ walletId: 1 }))).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.holdings({ walletId: 1 });
            ts_mockito_1.verify(mockRestService.holdings(ts_mockito_1.deepEqual({ walletId: 1 }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('holdingsDetail', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.holdingsDetail(ts_mockito_1.deepEqual({ walletId: 1 }))).thenResolve('holders');
            const result = yield sut.holdingsDetail({ walletId: 1 });
            ts_mockito_1.verify(mockRestService.holdingsDetail(ts_mockito_1.deepEqual({ walletId: 1 }))).once();
            expect(result).toBe('holders');
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.holdingsDetail(ts_mockito_1.deepEqual({ walletId: 1 }))).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.holdingsDetail({ walletId: 1 });
            ts_mockito_1.verify(mockRestService.holdingsDetail(ts_mockito_1.deepEqual({ walletId: 1 }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('holdingsDetails', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.holdingsDetails(ts_mockito_1.deepEqual({ namespace: 'BOE' }))).thenResolve('holders');
            const result = yield sut.holdingsDetails({ namespace: 'BOE' });
            ts_mockito_1.verify(mockRestService.holdingsDetails(ts_mockito_1.deepEqual({ namespace: 'BOE' }))).once();
            expect(result).toBe('holders');
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.holdingsDetails(ts_mockito_1.deepEqual({ namespace: 'BOE' }))).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.holdingsDetails({ namespace: 'BOE' });
            ts_mockito_1.verify(mockRestService.holdingsDetails(ts_mockito_1.deepEqual({ namespace: 'BOE' }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('namespace', () => {
        it('calls the socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { status: 'OK', id: 1, message: { messagetype: 'request', request: null, requestid: 1, status: 'OK', data: {} } };
            ts_mockito_1.when(mockWalletNodeClient.namespace(ts_mockito_1.deepEqual({ namespace: 'BOE' }))).thenResolve(mockResponse);
            const result = yield sut.namespace({ namespace: 'BOE' });
            ts_mockito_1.verify(mockWalletNodeClient.namespace(ts_mockito_1.deepEqual({ namespace: 'BOE' }))).once();
            expect(result).toStrictEqual(mockResponse);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.namespace(ts_mockito_1.deepEqual({ namespace: 'BOE' }))).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.namespace({ namespace: 'BOE' });
            ts_mockito_1.verify(mockRestService.namespace(ts_mockito_1.deepEqual({ namespace: 'BOE' }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('newAddress', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.newAddress(ts_mockito_1.deepEqual({ creator: 'add1', keyType: 'a', metadata: 'b', walletId: 1 }))).thenResolve('holders');
            const result = yield sut.newAddress({ creator: 'add1', keyType: 'a', metadata: 'b', walletId: 1 });
            ts_mockito_1.verify(mockRestService.newAddress(ts_mockito_1.deepEqual({ creator: 'add1', keyType: 'a', metadata: 'b', walletId: 1 }))).once();
            expect(result).toBe('holders');
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.newAddress(ts_mockito_1.deepEqual({ creator: 'add1', keyType: 'a', metadata: 'b', walletId: 1 }))).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.newAddress({ creator: 'add1', keyType: 'a', metadata: 'b', walletId: 1 });
            ts_mockito_1.verify(mockRestService.newAddress(ts_mockito_1.deepEqual({ creator: 'add1', keyType: 'a', metadata: 'b', walletId: 1 }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('ping', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.ping()).thenResolve('holders');
            const result = yield sut.ping();
            ts_mockito_1.verify(mockRestService.ping()).once();
            expect(result).toBe('holders');
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.ping()).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.ping();
            ts_mockito_1.verify(mockRestService.ping()).once();
            expect(result).toBe('holders');
        }));
    });
    describe('poaAttorneyDetails', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.poaAttorneyDetails(ts_mockito_1.deepEqual({ address: 'add1' }))).thenResolve('holders');
            const result = yield sut.poaAttorneyDetails({ address: 'add1' });
            ts_mockito_1.verify(mockRestService.poaAttorneyDetails(ts_mockito_1.deepEqual({ address: 'add1' }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('poaOwnerDetails', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.poaOwnerDetails(ts_mockito_1.deepEqual({ address: 'add1' }))).thenResolve('holders');
            const result = yield sut.poaOwnerDetails({ address: 'add1' });
            ts_mockito_1.verify(mockRestService.poaOwnerDetails(ts_mockito_1.deepEqual({ address: 'add1' }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('requestPrefix', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.requestPrefix()).thenResolve('holders');
            const result = yield sut.requestPrefix();
            ts_mockito_1.verify(mockRestService.requestPrefix()).once();
            expect(result).toBe('holders');
        }));
    });
    describe('state', () => {
        it('calls the socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { status: 'OK', id: 1, message: { messagetype: 'request', request: null, requestid: 1, status: 'OK', data: {} } };
            ts_mockito_1.when(mockWalletNodeClient.state()).thenResolve(mockResponse);
            const result = yield sut.state();
            ts_mockito_1.verify(mockWalletNodeClient.state()).once();
            expect(result).toStrictEqual(mockResponse);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.state()).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.state();
            ts_mockito_1.verify(mockRestService.state()).once();
            expect(result).toBe('holders');
        }));
    });
    describe('transferAsset', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.transferAsset(ts_mockito_1.deepEqual({ fromAddress: 'add1', toAddress: 'add2', walletId: 1, namespace: 'BOE', classId: 'GBP', amount: 100 }))).thenResolve('holders');
            const result = yield sut.transferAsset({ fromAddress: 'add1', toAddress: 'add2', walletId: 1, namespace: 'BOE', classId: 'GBP', amount: 100 });
            ts_mockito_1.verify(mockRestService.transferAsset(ts_mockito_1.deepEqual({ fromAddress: 'add1', toAddress: 'add2', walletId: 1, namespace: 'BOE', classId: 'GBP', amount: 100 }))).once();
            expect(result).toBe('holders');
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.transferAsset(ts_mockito_1.deepEqual({ fromAddress: 'add1', toAddress: 'add2', walletId: 1, namespace: 'BOE', classId: 'GBP', amount: 100 }))).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.transferAsset({ fromAddress: 'add1', toAddress: 'add2', walletId: 1, namespace: 'BOE', classId: 'GBP', amount: 100 });
            ts_mockito_1.verify(mockRestService.transferAsset(ts_mockito_1.deepEqual({ fromAddress: 'add1', toAddress: 'add2', walletId: 1, namespace: 'BOE', classId: 'GBP', amount: 100 }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('encumber', () => {
        it('calls the socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { status: 'OK', id: 1, message: { messagetype: 'request', request: null, requestid: 1, status: 'OK', data: {} } };
            ts_mockito_1.when(mockWalletNodeClient.encumberAsset(1, 'add1', '', 'test', 'BOE', 'GBP', 100, ts_mockito_1.deepEqual([]), ts_mockito_1.deepEqual([]), true, '')).thenResolve(mockResponse);
            const result = yield sut.encumber({ address: 'add1', administrators: [], beneficiaries: [], namespace: 'BOE', classId: 'GBP', amount: 100, reference: 'test', timestamp: 1234, walletId: 1 });
            ts_mockito_1.verify(mockWalletNodeClient.encumberAsset(1, 'add1', '', 'test', 'BOE', 'GBP', 100, ts_mockito_1.deepEqual([]), ts_mockito_1.deepEqual([]), true, '')).once();
            expect(result).toStrictEqual(mockResponse);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.encumber(ts_mockito_1.deepEqual({ address: 'add1', administrators: [], beneficiaries: [], namespace: 'BOE', classId: 'GBP', amount: 100, reference: 'test', timestamp: 1234, walletId: 1 }))).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.encumber({ address: 'add1', administrators: [], beneficiaries: [], namespace: 'BOE', classId: 'GBP', amount: 100, reference: 'test', timestamp: 1234, walletId: 1 });
            ts_mockito_1.verify(mockRestService.encumber(ts_mockito_1.deepEqual({ address: 'add1', administrators: [], beneficiaries: [], namespace: 'BOE', classId: 'GBP', amount: 100, reference: 'test', timestamp: 1234, walletId: 1 }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('exerciseEncumbrance', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.exerciseEncumbrance(ts_mockito_1.deepEqual({ address: 'add1', amount: 100, namespace: 'BOE', classId: 'GBP' }))).thenResolve('holders');
            const result = yield sut.exerciseEncumbrance({ address: 'add1', amount: 100, namespace: 'BOE', classId: 'GBP' });
            ts_mockito_1.verify(mockRestService.exerciseEncumbrance(ts_mockito_1.deepEqual({ address: 'add1', amount: 100, namespace: 'BOE', classId: 'GBP' }))).once();
            expect(result).toBe('holders');
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.exerciseEncumbrance(ts_mockito_1.deepEqual({ address: 'add1', amount: 100, namespace: 'BOE', classId: 'GBP' }))).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.exerciseEncumbrance({ address: 'add1', amount: 100, namespace: 'BOE', classId: 'GBP' });
            ts_mockito_1.verify(mockRestService.exerciseEncumbrance(ts_mockito_1.deepEqual({ address: 'add1', amount: 100, namespace: 'BOE', classId: 'GBP' }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('issueAsset', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.issueAsset(ts_mockito_1.deepEqual({ walletId: 1, fromAddress: 'add1', toAddress: 'add2', namespace: 'BOE', classId: 'GBP', amount: 100 }))).thenResolve('holders');
            const result = yield sut.issueAsset({ walletId: 1, fromAddress: 'add1', toAddress: 'add2', namespace: 'BOE', classId: 'GBP', amount: 100 });
            ts_mockito_1.verify(mockRestService.issueAsset(ts_mockito_1.deepEqual({ walletId: 1, fromAddress: 'add1', toAddress: 'add2', namespace: 'BOE', classId: 'GBP', amount: 100 }))).once();
            expect(result).toBe('holders');
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.issueAsset(ts_mockito_1.deepEqual({ walletId: 1, fromAddress: 'add1', toAddress: 'add2', namespace: 'BOE', classId: 'GBP', amount: 100 }))).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.issueAsset({ walletId: 1, fromAddress: 'add1', toAddress: 'add2', namespace: 'BOE', classId: 'GBP', amount: 100 });
            ts_mockito_1.verify(mockRestService.issueAsset(ts_mockito_1.deepEqual({ walletId: 1, fromAddress: 'add1', toAddress: 'add2', namespace: 'BOE', classId: 'GBP', amount: 100 }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('issuerTransfer', () => {
        it('calls the socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { status: 'OK', id: 1, message: { messagetype: 'request', request: null, requestid: 1, status: 'OK', data: {} } };
            ts_mockito_1.when(mockWalletNodeClient.issuerTransferPayload(ts_mockito_1.anything())).thenResolve(mockResponse);
            const result = yield sut.issuerTransfer({ fromAddress: 'add1', toAddress: 'add2', namespace: 'BOE', classId: 'GBP', amount: 100 });
            ts_mockito_1.verify(mockWalletNodeClient.issuerTransferPayload(ts_mockito_1.deepEqual({ fromAddress: 'add1', toAddress: 'add2', namespace: 'BOE', classId: 'GBP', amount: 100 }))).once();
            expect(result).toStrictEqual(mockResponse);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.issuerTransfer(ts_mockito_1.deepEqual({ fromAddress: 'add1', toAddress: 'add2', namespace: 'BOE', classId: 'GBP', amount: 100 }))).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.issuerTransfer({ fromAddress: 'add1', toAddress: 'add2', namespace: 'BOE', classId: 'GBP', amount: 100 });
            ts_mockito_1.verify(mockRestService.issuerTransfer(ts_mockito_1.deepEqual({ fromAddress: 'add1', toAddress: 'add2', namespace: 'BOE', classId: 'GBP', amount: 100 }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('null', () => {
        it('calls the socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { status: 'OK', id: 1, message: { messagetype: 'request', request: null, requestid: 1, status: 'OK', data: {} } };
            ts_mockito_1.when(mockWalletNodeClient.nullTxPayload(ts_mockito_1.anything())).thenResolve(mockResponse);
            const result = yield sut.null({ address: 'add1', walletId: 1 });
            ts_mockito_1.verify(mockWalletNodeClient.nullTxPayload(ts_mockito_1.deepEqual({ address: 'add1', walletid: 1 }))).once();
            expect(result).toStrictEqual(mockResponse);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.null(ts_mockito_1.deepEqual({ address: 'add1', walletId: 1 }))).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.null({ address: 'add1', walletId: 1 });
            ts_mockito_1.verify(mockRestService.null(ts_mockito_1.deepEqual({ address: 'add1', walletId: 1 }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('registerAsset', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.registerAsset(ts_mockito_1.anything())).thenResolve('holders');
            const result = yield sut.registerAsset({ address: 'add1', walletId: 1, namespace: 'BOE', classId: 'GBP' });
            ts_mockito_1.verify(mockRestService.registerAsset(ts_mockito_1.deepEqual({ address: 'add1', walletId: 1, namespace: 'BOE', classId: 'GBP' }))).once();
            expect(result).toBe('holders');
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.registerAsset(ts_mockito_1.anything())).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.registerAsset({ address: 'add1', walletId: 1, namespace: 'BOE', classId: 'GBP' });
            ts_mockito_1.verify(mockRestService.registerAsset(ts_mockito_1.deepEqual({ address: 'add1', walletId: 1, namespace: 'BOE', classId: 'GBP' }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('registerNamespace', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.registerNamespace(ts_mockito_1.anything())).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.registerNamespace({ address: 'add1', walletid: 1, namespace: 'BOE' });
            ts_mockito_1.verify(mockRestService.registerNamespace(ts_mockito_1.deepEqual({ address: 'add1', walletid: 1, namespace: 'BOE' }))).once();
            expect(result).toBe('holders');
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.registerNamespace(ts_mockito_1.anything())).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.registerNamespace({ address: 'add1', walletid: 1, namespace: 'BOE' });
            ts_mockito_1.verify(mockRestService.registerNamespace(ts_mockito_1.deepEqual({ address: 'add1', walletid: 1, namespace: 'BOE' }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('transferFromMany', () => {
        it('calls the socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { status: 'OK', id: 1, message: { messagetype: 'request', request: null, requestid: 1, status: 'OK', data: {} } };
            ts_mockito_1.when(mockWalletNodeClient.transferFromManyPayload(ts_mockito_1.anything())).thenResolve(mockResponse);
            const result = yield sut.transferFromMany({ address: 'add1', namespace: 'BOE', walletId: 1, amount: 100, classId: 'GBP', transfers: [{ address: 'add1', amount: 100 }] });
            ts_mockito_1.verify(mockWalletNodeClient.transferFromManyPayload(ts_mockito_1.deepEqual({ address: 'add1', namespace: 'BOE', walletId: 1, amount: 100, classId: 'GBP', transfers: ts_mockito_1.deepEqual([{ address: 'add1', amount: 100 }]) }))).once();
            expect(result).toStrictEqual(mockResponse);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.transferFromMany(ts_mockito_1.anything())).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.transferFromMany({ address: 'add1', namespace: 'BOE', walletId: 1, amount: 100, classId: 'GBP', transfers: [{ address: 'add1', amount: 100 }] });
            ts_mockito_1.verify(mockRestService.transferFromMany(ts_mockito_1.deepEqual({ address: 'add1', namespace: 'BOE', walletId: 1, amount: 100, classId: 'GBP', transfers: ts_mockito_1.deepEqual([{ address: 'add1', amount: 100 }]) }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('createTx', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.createTx(ts_mockito_1.anything())).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.createTx({ address: 'add1', namespace: 'BOE', walletId: 1, amount: 100, classId: 'GBP', transfers: [{ address: 'add1', amount: 100 }] });
            ts_mockito_1.verify(mockRestService.createTx(ts_mockito_1.deepEqual({ address: 'add1', namespace: 'BOE', walletId: 1, amount: 100, classId: 'GBP', transfers: ts_mockito_1.deepEqual([{ address: 'add1', amount: 100 }]) }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('unencumber', () => {
        it('calls rest by default', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.unencumber(ts_mockito_1.anything())).thenResolve('holders');
            const result = yield sut.unencumber({ address: 'add1', namespace: 'BOE', walletId: 1, amount: 100, classId: 'GBP', reference: 'ref1', timestamp: 1234 });
            ts_mockito_1.verify(mockRestService.unencumber(ts_mockito_1.deepEqual({ address: 'add1', namespace: 'BOE', walletId: 1, amount: 100, classId: 'GBP', reference: 'ref1', timestamp: 1234 }))).once();
            expect(result).toBe('holders');
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockRestService.unencumber(ts_mockito_1.anything())).thenResolve('holders');
            sut.switchToRpc();
            const result = yield sut.unencumber({ address: 'add1', namespace: 'BOE', walletId: 1, amount: 100, classId: 'GBP', reference: 'ref1', timestamp: 1234 });
            ts_mockito_1.verify(mockRestService.unencumber(ts_mockito_1.deepEqual({ address: 'add1', namespace: 'BOE', walletId: 1, amount: 100, classId: 'GBP', reference: 'ref1', timestamp: 1234 }))).once();
            expect(result).toBe('holders');
        }));
    });
    describe('getTxs', () => {
        it('calls the socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { Status: 'OK', Data: '' };
            ts_mockito_1.when(mockMembernodeClient.getTxs()).thenResolve(mockResponse);
            const result = yield sut.getTxs();
            ts_mockito_1.verify(mockMembernodeClient.getTxs()).once();
            expect(result).toStrictEqual(mockResponse);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockJwtService.call(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenResolve('holders');
            stateService.token = '';
            sut.switchToRpc();
            const result = yield sut.getTxs();
            ts_mockito_1.verify(mockJwtService.call(ts_mockito_1.deepEqual({ topic: 'repgettxs' }), 'request', 'member')).once();
            expect(result).toBe('holders');
        }));
    });
    describe('getTx', () => {
        it('calls the socket by default', () => __awaiter(void 0, void 0, void 0, function* () {
            const mockResponse = { Status: 'OK', Data: '' };
            ts_mockito_1.when(mockMembernodeClient.getTx(1, 'contractA')).thenResolve(mockResponse);
            const result = yield sut.getTx(1, 'contractA');
            ts_mockito_1.verify(mockMembernodeClient.getTx(1, 'contractA')).once();
            expect(result).toStrictEqual(mockResponse);
        }));
        it('calls rest', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockJwtService.call(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenResolve('holders');
            stateService.token = '';
            sut.switchToRpc();
            const result = yield sut.getTx(1, 'contractA');
            ts_mockito_1.verify(mockJwtService.call(ts_mockito_1.deepEqual({ topic: 'repgettx', walletId: 1, txHash: 'contractA' }), 'request', 'member')).once();
            expect(result).toBe('holders');
        }));
    });
    describe('txReceipt', () => {
        it('throws if invalid tx', () => {
            expect(sut.txReceipt({})).rejects.toThrow();
        });
        it('calls the wallet socket - variant A', () => {
            sut.txReceipt({ hash: 'abc' });
            ts_mockito_1.verify(mockWalletNodeClient.txReceipt(ts_mockito_1.deepEqual({ message: { data: { hash: 'abc' } } }))).once();
        });
        it('calls the wallet socket - variant B', () => {
            sut.txReceipt({ data: { hash: 'abc' } });
            ts_mockito_1.verify(mockWalletNodeClient.txReceipt(ts_mockito_1.deepEqual({ message: { data: { hash: 'abc' } } }))).once();
        });
        it('calls the wallet socket - variant C', () => {
            sut.txReceipt({ message: { data: { hash: 'abc' } } });
            ts_mockito_1.verify(mockWalletNodeClient.txReceipt(ts_mockito_1.deepEqual({ message: { data: { hash: 'abc' } } }))).once();
        });
    });
    describe('createContract', () => {
        beforeEach(() => {
            mockConnectionService = {
                events$: new rxjs_1.Subject(),
                walletSocket: ts_mockito_1.instance(mockWalletNodeClient),
                memberSocket: ts_mockito_1.instance(mockMembernodeClient),
            };
            sut = new client_1.SetlClient(new state_1.StateService(), mockConnectionService, ts_mockito_1.instance(mockRestService), ts_mockito_1.instance(mockJwtService), 'localhost');
        });
        it('Throws if parties do not balance', () => __awaiter(void 0, void 0, void 0, function* () {
            yield expect(sut.createContract({
                address: 'ADD1',
                walletId: 1,
                expirySeconds: 30,
                parties: [
                    {
                        address: 'ADD1',
                        partyIdentifier: 'Seller',
                        payList: [
                            {
                                address: 'ADD1',
                                namespace: 'BOE',
                                instrument: 'GBP',
                                quantity: 100,
                            }
                        ],
                        receiveList: [
                            {
                                address: 'ADD2',
                                namespace: 'BOE',
                                instrument: 'GBP',
                                quantity: 95,
                            }
                        ]
                    }
                ]
            })).rejects.toMatchObject({ message: /Receive is -5/ });
        }));
        it('Calls walletnode client if contract is OK', () => __awaiter(void 0, void 0, void 0, function* () {
            yield sut.createContract({
                address: 'ADD1',
                walletId: 1,
                expirySeconds: 30,
                parties: [
                    {
                        address: 'ADD1',
                        partyIdentifier: 'Seller',
                        payList: [
                            {
                                address: 'ADD1',
                                namespace: 'BOE',
                                instrument: 'GBP',
                                quantity: 100,
                            }
                        ],
                        receiveList: [
                            {
                                address: 'ADD2',
                                namespace: 'BOE',
                                instrument: 'GBP',
                                quantity: 100,
                            }
                        ]
                    }
                ]
            });
            ts_mockito_1.verify(mockWalletNodeClient.createContract(ts_mockito_1.anything())).once();
        }));
    });
});
