"use strict";
/**
 * Delta publisher test
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var DeltaPublisher_1 = require("../../src/system/DeltaPublisher");
var events_1 = require("events");
var ContextError_1 = require("../../src/error/ContextError");
var AmqpError_1 = require("../../src/error/AmqpError");
var chai_1 = require("chai");
chai_1.use(require('chai-as-promised'));
describe('server.DeltaPublisher', function () {
    describe('#publish', function () {
        it('sends a message', function () {
            var publish_called = false;
            var delta = createMockDelta();
            var bucket = createMockBucketData();
            var ratedata = createMockBucketData();
            var emitter = new events_1.EventEmitter();
            var conn = createMockAmqpConnection();
            var writer = createMockWriter();
            var meta = {
                id: 123,
                entity_name: 'Some Agency',
                entity_id: 234,
                startDate: 345,
                lastUpdate: 456,
            };
            conn.getAmqpChannel = function () {
                return {
                    publish: function (_, __, buf, ___) {
                        chai_1.expect(buf instanceof Buffer).to.be.true;
                        publish_called = true;
                        return true;
                    },
                };
            };
            var sut = new DeltaPublisher_1.DeltaPublisher(emitter, ts_ctor, conn, writer);
            return chai_1.expect(sut.publish(meta, delta, bucket, ratedata))
                .to.eventually.deep.equal(undefined)
                .then(function (_) {
                chai_1.expect(publish_called).to.be.true;
            });
        });
        [
            [
                'Throws an error when publishing was unsuccessful',
                function () {
                    return {
                        publish: function (_, __, _buf, ___) {
                            return false;
                        },
                    };
                },
                Error,
                'Delta publish failed',
            ],
            [
                'Throws an error when no amqp channel is found',
                function () {
                    return undefined;
                },
                AmqpError_1.AmqpError,
                'Error sending message: No channel',
            ],
        ].forEach(function (_a) {
            var label = _a[0], getChannelF = _a[1], error_type = _a[2], err_msg = _a[3];
            return it(label, function () {
                var delta = createMockDelta();
                var bucket = createMockBucketData();
                var ratedata = createMockBucketData();
                var emitter = new events_1.EventEmitter();
                var conn = createMockAmqpConnection();
                var writer = createMockWriter();
                var meta = {
                    id: 123,
                    entity_name: 'Some Agency',
                    entity_id: 234,
                    startDate: 345,
                    lastUpdate: 456,
                };
                var expected = {
                    doc_id: meta.id,
                    quote_id: meta.id,
                    delta_type: delta.type,
                    delta_ts: delta.timestamp,
                };
                conn.getAmqpChannel = getChannelF;
                var result = new DeltaPublisher_1.DeltaPublisher(emitter, ts_ctor, conn, writer).publish(meta, delta, bucket, ratedata);
                return Promise.all([
                    chai_1.expect(result).to.eventually.be.rejectedWith(error_type, err_msg),
                    result.catch(function (e) {
                        if (!ContextError_1.hasContext(e)) {
                            return chai_1.expect.fail();
                        }
                        return chai_1.expect(e.context).to.deep.equal(expected);
                    }),
                ]);
            });
        });
        it('writer#write rejects', function () {
            var delta = createMockDelta();
            var bucket = createMockBucketData();
            var ratedata = createMockBucketData();
            var emitter = new events_1.EventEmitter();
            var conn = createMockAmqpConnection();
            var writer = createMockWriter();
            var error = new Error('Bad thing happened');
            var meta = {
                id: 123,
                entity_name: 'Some Agency',
                entity_id: 234,
                startDate: 345,
                lastUpdate: 456,
            };
            writer.write = function (_, __, ___, ____, _____) {
                return Promise.reject(error);
            };
            var result = new DeltaPublisher_1.DeltaPublisher(emitter, ts_ctor, conn, writer).publish(meta, delta, bucket, ratedata);
            return Promise.all([
                chai_1.expect(result).to.eventually.be.rejectedWith(error),
                result.catch(function (e) {
                    return chai_1.expect(e).to.deep.equal(error);
                }),
            ]);
        });
    });
});
function ts_ctor() {
    return Math.floor(new Date().getTime() / 1000);
}
function createMockAmqpConnection() {
    return {
        connect: function () { },
        getExchangeName: function () {
            'Foo';
        },
    };
}
function createMockBucketData() {
    return {
        foo: ['bar', 'baz'],
    };
}
function createMockDelta() {
    return {
        type: 'data',
        timestamp: 123123123,
        data: {},
    };
}
function createMockWriter() {
    return {
        write: function (_, __, ___, ____, _____) {
            return Promise.resolve(Buffer.from(''));
        },
    };
}
//# sourceMappingURL=data:application/json;base64, 