"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 ts_mockito_1 = require("ts-mockito");
const wallet_node_client_1 = require("./wallet-node-client");
const socket_client_1 = require("./socket-client");
const callback_manager_1 = require("./callback-manager");
const lodash_1 = require("lodash");
const fetch_json_poster_1 = require("./services/fetch-json-poster");
class NullLogger {
    info(...msg) { }
    error(...msg) { }
    debug(...msg) { }
}
const mockSocket = ts_mockito_1.mock(socket_client_1.SocketClient);
const mockCallback = ts_mockito_1.mock(callback_manager_1.CallbackManager);
const mockLogger = ts_mockito_1.mock(NullLogger);
const mockPoster = ts_mockito_1.mock(fetch_json_poster_1.FetchJsonPoster);
function expectedContract(overrides = {}) {
    const valid = {
        requestid: 2,
        MessageType: 'tx',
        MessageBody: {
            txtype: 'conew',
            walletid: 10,
            address: 'address1',
            contractdata: {
                __function: 'dvp_uk',
                parties: [
                    2,
                    [
                        'partyA',
                        'address1',
                        [
                            [
                                'address2',
                                'NS1',
                                'ASS1',
                                10,
                                '',
                                '',
                                false,
                                '',
                                '',
                            ]
                        ],
                        [],
                        '',
                        '',
                        false,
                        '',
                    ],
                    [
                        'partyB',
                        'address2',
                        [],
                        [
                            [
                                'address2',
                                'NS1',
                                'ASS1',
                                10
                            ]
                        ],
                        '',
                        '',
                        false,
                        '',
                    ]
                ],
                authorisations: [],
                parameters: [],
                encumbrance: [
                    false,
                    '',
                ],
                addencumbrances: [],
                events: ['commit', 'expiry'],
                expiry: ts_mockito_1.anyNumber(),
                startdate: ts_mockito_1.anyNumber(),
                protocol: 'dvp',
                metadata: '',
                deletedelayoncomplete: 3600,
                autosign: true,
            }
        }
    };
    return lodash_1.merge(valid, overrides);
}
describe('WalletNodeClient', () => {
    let sut;
    beforeEach(() => {
        ts_mockito_1.reset(mockSocket);
        ts_mockito_1.reset(mockCallback);
        ts_mockito_1.reset(mockLogger);
        sut = new wallet_node_client_1.WalletNodeClient(ts_mockito_1.instance(mockSocket), ts_mockito_1.instance(mockCallback), ts_mockito_1.instance(mockPoster), ts_mockito_1.instance(mockLogger));
    });
    describe('createContract', () => {
        it('creates a basic contract', () => __awaiter(void 0, void 0, void 0, function* () {
            const parties = [
                {
                    partyIdentifier: 'partyA',
                    address: 'address1',
                    payList: [
                        {
                            address: 'address2',
                            namespace: 'NS1',
                            instrument: 'ASS1',
                            quantity: 10,
                            metadata: '',
                            issuance: false,
                        }
                    ],
                    receiveList: [],
                    metadata: '',
                },
                {
                    partyIdentifier: 'partyB',
                    address: 'address2',
                    payList: [],
                    receiveList: [
                        {
                            address: 'address2',
                            namespace: 'NS1',
                            instrument: 'ASS1',
                            quantity: 10,
                        }
                    ],
                    metadata: '',
                }
            ];
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.createContract({ walletId: 10, address: 'address1', parties, expirySeconds: 3600 * 12 });
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual(expectedContract()))).once();
        }));
        it('adds encumbrance', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.createContract({ walletId: 10, address: 'address1', parties: [], expirySeconds: 3600 * 12, encumbrance: 'encref' });
            const expected = expectedContract({ MessageBody: { contractdata: { encumbrance: [true, 'encref'] } } });
            expected.MessageBody.contractdata.parties = [0];
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual(expected))).once();
        }));
        it('adds addencumbrance', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.createContract({
                walletId: 10,
                address: 'address1',
                parties: [],
                expirySeconds: 3600 * 12,
                addencumbrance: [
                    {
                        address: 'encaddress',
                        fullAssetId: 'NS1|ASS1',
                        beneficiaries: [],
                        amount: 10,
                        reference: 'ref1',
                        administrators: [
                            {
                                address: 'adminref',
                                starttime: 0,
                                endtime: 0
                            }
                        ]
                    }
                ]
            });
            const expected = expectedContract({ MessageBody: { contractdata: { addencumbrances: [['encaddress', 'NS1|ASS1', 'ref1', 10, [], [['adminref', 0, 0]]]] } } });
            expected.MessageBody.contractdata.parties = [0];
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual(expected))).once();
        }));
    });
    describe('getHoldings', () => {
        it('Calls socket', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.holdings({
                walletId: 3,
            });
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'request',
                MessageBody: {
                    topic: 'holdings',
                    walletid: 3,
                    walletId: undefined,
                }
            }))).once();
        }));
    });
    describe('unencumberAsset', () => {
        it('Calls socket', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.unencumberAsset(3, 'ADDRESS1', 'ADDRESS2', 'REF', 'NS1', 'ASS1', 10);
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'tx',
                MessageBody: {
                    txtype: 'unenc',
                    walletid: 3,
                    address: 'ADDRESS1',
                    subjectaddress: 'ADDRESS2',
                    reference: 'REF',
                    namespace: 'NS1',
                    instrument: 'ASS1',
                    amount: 10
                }
            }))).once();
        }));
    });
    describe('encumberAsset', () => {
        it('Calls socket', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            const beneficiaries = [
                ['ADDRESS3', 1, 2],
                ['ADDRESS4', 3, 4]
            ];
            const administrators = [
                ['ADDRESS5', 5, 6],
                ['ADDRESS6', 7, 8]
            ];
            yield sut.encumberAsset(3, 'ADDRESS1', 'ADDRESS2', 'REF', 'NS1', 'ASS1', 10, beneficiaries, administrators, true, '{"name":"Stuart"}');
            const expectedBeneficiaries = [
                ['ADDRESS3', 1, 2],
                ['ADDRESS4', 3, 4]
            ];
            const expectedAdministrators = [
                ['ADDRESS5', 5, 6],
                ['ADDRESS6', 7, 8]
            ];
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'tx',
                MessageBody: {
                    txtype: 'encum',
                    walletid: 3,
                    address: 'ADDRESS1',
                    subjectaddress: 'ADDRESS2',
                    reference: 'REF',
                    namespace: 'NS1',
                    instrument: 'ASS1',
                    amount: 10,
                    beneficiaries: expectedBeneficiaries,
                    administrators: expectedAdministrators,
                    iscumulative: true,
                    metadata: '{"name":"Stuart"}'
                }
            }))).once();
        }));
    });
    describe('issueAsset', () => {
        it('Calls the socket', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.issueAsset(3, 'ADDRESS1', 'NS1', 'ASS1', 10);
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'tx',
                MessageBody: {
                    txtype: 'asiss',
                    walletid: 3,
                    address: 'ADDRESS1',
                    namespace: 'NS1',
                    instrument: 'ASS1',
                    amount: 10,
                }
            }))).once();
        }));
    });
    describe('registerAsset', () => {
        it('Calls the socket', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.registerAsset(3, 'ADDRESS1', 'NS1', 'ASS1', '{"name":"Stuart"}');
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'tx',
                MessageBody: {
                    txtype: 'asreg',
                    walletid: 3,
                    address: 'ADDRESS1',
                    namespace: 'NS1',
                    instrument: 'ASS1',
                    metadata: '{"name":"Stuart"}'
                }
            }))).once();
        }));
    });
    describe('registerNamespace', () => {
        it('Calls the socket', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.registerNamespace(3, 'ADDRESS1', 'NS1', '{"name":"Stuart"}');
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'tx',
                MessageBody: {
                    txtype: 'nsreg',
                    walletid: 3,
                    address: 'ADDRESS1',
                    name: 'NS1',
                    metadata: '{"name":"Stuart"}'
                }
            }))).once();
        }));
    });
    describe('contracts', () => {
        it('Calls the socket', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.contracts(3);
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'request',
                MessageBody: {
                    topic: 'contract',
                    walletid: 3,
                }
            }))).once();
        }));
    });
    describe('addresses', () => {
        it('Calls the socket', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: [
                    ['ADDRESS1', 'PUB1'],
                    ['ADDRESS2', 'PUB2']
                ] }));
            yield sut.addresses(3);
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'request',
                MessageBody: {
                    topic: 'addresses',
                    walletid: 3,
                }
            }))).once();
        }));
    });
    describe('instruments', () => {
        it('Calls the socket', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: [
                    ['ADDRESS1', 'NS1', 'ASS1', '1'],
                    ['ADDRESS2', 'NS2', 'ASS2', '2']
                ] }));
            yield sut.instruments(3);
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'request',
                MessageBody: {
                    topic: 'instruments',
                    walletid: 3,
                }
            }))).once();
        }));
        it('Transforms the response', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: [
                    ['ADDRESS1', 'NS1', 'ASS1', '1'],
                    ['ADDRESS2', 'NS2', 'ASS2', '2']
                ] }));
            const response = yield sut.instruments(3);
            expect(response).toEqual([
                {
                    address: 'ADDRESS1',
                    namespace: 'NS1',
                    instrument: 'ASS1',
                    metadata: '1',
                },
                {
                    address: 'ADDRESS2',
                    namespace: 'NS2',
                    instrument: 'ASS2',
                    metadata: '2',
                }
            ]);
        }));
    });
    describe('namespaces', () => {
        it('calls the socket - no params', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.namespaces();
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'request',
                MessageBody: {
                    topic: 'issuers',
                    walletid: undefined,
                    namespace: undefined,
                    address: undefined
                }
            }))).once();
        }));
        it('calls the socket - walletid', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.namespaces(3);
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'request',
                MessageBody: {
                    topic: 'issuers',
                    walletid: 3,
                    namespace: undefined,
                    address: undefined
                }
            }))).once();
        }));
        it('calls the socket - walletid, namespace', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.namespaces(3, 'NS1');
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'request',
                MessageBody: {
                    topic: 'issuers',
                    walletid: 3,
                    namespace: 'NS1',
                    address: undefined
                }
            }))).once();
        }));
        it('calls the socket - walletid, namespace', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.namespaces(3, 'NS1', 'ADDRESS1');
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'request',
                MessageBody: {
                    topic: 'issuers',
                    walletid: 3,
                    namespace: 'NS1',
                    address: 'ADDRESS1'
                }
            }))).once();
        }));
    });
    describe('newAddress', () => {
        it('calls the socket', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.newAddress({ walletId: 3, creator: '', metadata: '', keyType: '' });
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'tx',
                MessageBody: {
                    txtype: 'newaddress',
                    walletid: 3,
                    walletId: undefined,
                    creator: '',
                    metadata: '',
                    keyType: '',
                }
            }))).once();
        }));
    });
    describe('subscribe', () => {
        it('calls the socket', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.subscribe('block', 'blockupdates');
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'subscribe',
                MessageBody: {
                    topic: ['block', 'blockupdates'],
                }
            }))).once();
        }));
    });
    describe('state', () => {
        it('calls the socket', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.state();
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'request',
                MessageBody: {
                    topic: 'state',
                }
            }))).once();
        }));
    });
    describe('authenticate', () => {
        it('calls the socket', () => __awaiter(void 0, void 0, void 0, function* () {
            ts_mockito_1.when(mockCallback.uniqueIdValue).thenReturn(2);
            ts_mockito_1.when(mockCallback.addHandler(2, ts_mockito_1.anyFunction())).thenCall((requestId, cb) => cb(1, { status: 'OK', data: { error: '' } }));
            yield sut.authenticate(10, 'APIKEY');
            ts_mockito_1.verify(mockSocket.sendRequest(ts_mockito_1.deepEqual({
                requestid: 2,
                MessageType: 'authenticate',
                MessageBody: {
                    userid: 10,
                    apikey: 'APIKEY'
                }
            }))).once();
        }));
    });
    describe('connect', () => {
        it('calls the socket', () => {
            sut.connect();
            ts_mockito_1.verify(mockSocket.connect()).once();
        });
    });
});
