"use strict";
/**
 * Tests ConfLoader
 *
 *  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 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 General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var chai = require('chai');
var expect = chai.expect;
var ConfLoader_1 = require("../../src/conf/ConfLoader");
var Store = require('../../').store.MemoryStore;
chai.use(require('chai-as-promised'));
describe('ConfLoader', function () {
    it("loads Store'd configuration from file", function () {
        var expected_path = '/foo/bar/baz.json';
        var expected_data = '{ "foo": "bar" }';
        var fs = {
            readFile: function (path, encoding, callback) {
                expect(path).to.equal(expected_path);
                expect(encoding).to.equal('utf8');
                callback(null, expected_data);
            },
        };
        return expect(new ConfLoader_1.ConfLoader(fs, Store).fromFile(expected_path).then(function (conf) { return conf.get('foo'); })).to.eventually.deep.equal(JSON.parse(expected_data).foo);
    });
    it('fails on read error', function () {
        var expected_err = Error('rejected');
        var fs = {
            readFile: function (_, __, callback) {
                callback(expected_err, null);
            },
        };
        return expect(new ConfLoader_1.ConfLoader(fs, Store).fromFile('')).to.eventually.be.rejectedWith(expected_err);
    });
    it('can override #parseConfData for custom parser', function () {
        var result = { foo: {} };
        var input = 'foo';
        var fs = {
            readFile: function (_, __, callback) {
                callback(null, input);
            },
        };
        var sut = new (/** @class */ (function (_super) {
            __extends(class_1, _super);
            function class_1() {
                return _super !== null && _super.apply(this, arguments) || this;
            }
            class_1.prototype.parseConfData = function (given_input) {
                expect(given_input).to.equal(input);
                return Promise.resolve(result);
            };
            return class_1;
        }(ConfLoader_1.ConfLoader)))(fs, Store);
        return expect(sut.fromFile('').then(function (conf) { return conf.get('foo'); })).to.eventually.equal(result.foo);
    });
    it('rejects promise on parsing error', function () {
        var expected_err = SyntaxError('test parsing error');
        var fs = {
            readFile: function (_, __, callback) {
                // make async so that we clear the stack, and therefore
                // try/catch
                process.nextTick(function () { return callback(null, ''); });
            },
        };
        var sut = new (/** @class */ (function (_super) {
            __extends(class_2, _super);
            function class_2() {
                return _super !== null && _super.apply(this, arguments) || this;
            }
            class_2.prototype.parseConfData = function (_given_input) {
                throw expected_err;
            };
            return class_2;
        }(ConfLoader_1.ConfLoader)))(fs, Store);
        return expect(sut.fromFile('')).to.eventually.be.rejectedWith(expected_err);
    });
    it('rejects promise on Store ctor error', function () {
        var expected_err = Error('test Store ctor error');
        var fs = {
            readFile: function (_, __, callback) { return callback(null, ''); },
        };
        var badstore = function () {
            throw expected_err;
        };
        return expect(new ConfLoader_1.ConfLoader(fs, badstore).fromFile('')).to.eventually.be.rejectedWith(expected_err);
    });
    it('rejects promise on bad fs call', function () {
        return expect(new ConfLoader_1.ConfLoader({}, Store).fromFile('')).to.eventually.be
            .rejected;
    });
});
//# sourceMappingURL=data:application/json;base64, 