"use strict";
/**
 * Test DataAPI backed by tokens for logging and precedence
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of liza.
 *
 *  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/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var TokenedDataApi_1 = require("../../../src/server/dapi/TokenedDataApi");
var Token_1 = require("../../../src/server/token/Token");
var ContextError_1 = require("../../../src/error/ContextError");
var chai_1 = require("chai");
describe('TokenedDataApi', function () {
    var expected_ns = 'foo_ns';
    [
        [
            'creates token and returns data if last_created',
            true,
            function (e) { return chai_1.expect(e).to.equal(null); },
        ],
        [
            'creates token and does not callback if not last_created',
            false,
            function (e) {
                chai_1.expect(e).to.be.instanceof(Error);
                // this awkwardness can be mitigated in TS 3.7
                // (see https://github.com/microsoft/TypeScript/pull/32695)
                if (e instanceof Error) {
                    chai_1.expect(e.message).to.contain('superceded');
                    chai_1.expect(ContextError_1.hasContext(e)).to.be.true;
                    if (ContextError_1.hasContext(e)) {
                        chai_1.expect(e.context.id).to.equal(expected_ns);
                    }
                }
            },
        ],
    ].forEach(function (_a) {
        var label = _a[0], last_created = _a[1], expected_err = _a[2];
        return it(label, function (done) {
            var expected_data = { given: 'data' };
            var dapi_ret_data = [{ return: 'data' }];
            var stub_tok = createStubToken(last_created);
            var tok_completed = false;
            var tok_ackd = false;
            var mock_tstore = new (/** @class */ (function () {
                function class_1() {
                }
                class_1.prototype.lookupToken = function () {
                    return Promise.reject(Error('not used'));
                };
                class_1.prototype.createToken = function () {
                    return Promise.resolve(stub_tok);
                };
                class_1.prototype.completeToken = function (given_tok, given_data) {
                    chai_1.expect(given_tok).to.equal(stub_tok);
                    chai_1.expect(given_data).to.equal(JSON.stringify(dapi_ret_data));
                    var ret = Object.create(stub_tok);
                    ret.state = Token_1.TokenState.DONE;
                    tok_completed = true;
                    return Promise.resolve(ret);
                };
                class_1.prototype.acceptToken = function () {
                    chai_1.expect(tok_completed).to.be.true;
                    chai_1.expect(last_created).to.be.true;
                    tok_ackd = true;
                    return Promise.resolve(Object.create(stub_tok));
                };
                class_1.prototype.killToken = function () {
                    chai_1.expect(tok_completed).to.be.true;
                    chai_1.expect(last_created).to.be.false;
                    tok_ackd = true;
                    return Promise.resolve(Object.create(stub_tok));
                };
                return class_1;
            }()))();
            var mock_dapi = new (/** @class */ (function () {
                function class_2() {
                }
                class_2.prototype.request = function (given_data, callback, given_id) {
                    chai_1.expect(given_data).to.equal(expected_data);
                    chai_1.expect(given_id).to.equal(expected_ns);
                    callback(null, dapi_ret_data);
                    return this;
                };
                return class_2;
            }()))();
            var ctor = function (ns) {
                chai_1.expect(ns).to.equal(expected_ns);
                return mock_tstore;
            };
            var callback = function (e, data) {
                chai_1.expect(tok_ackd).to.be.true;
                expected_err(e);
                chai_1.expect(data).to.equal(last_created ? dapi_ret_data : null);
                done();
            };
            new TokenedDataApi_1.TokenedDataApi(mock_dapi, ctor).request(expected_data, callback, expected_ns);
        });
    });
    it('propagates dapi request errors', function (done) {
        var expected_err = Error('test dapi error');
        var stub_tok = createStubToken(true);
        var mock_tstore = new (/** @class */ (function () {
            function class_3() {
            }
            class_3.prototype.lookupToken = function () {
                return Promise.reject(Error('not used'));
            };
            class_3.prototype.createToken = function () {
                return Promise.resolve(stub_tok);
            };
            class_3.prototype.completeToken = function () {
                return Promise.reject(Error('not used'));
            };
            class_3.prototype.acceptToken = function () {
                return Promise.reject(Error('not used'));
            };
            class_3.prototype.killToken = function () {
                return Promise.reject(Error('not used'));
            };
            return class_3;
        }()))();
        var mock_dapi = new (/** @class */ (function () {
            function class_4() {
            }
            class_4.prototype.request = function (_, callback) {
                callback(expected_err, null);
                return this;
            };
            return class_4;
        }()))();
        var callback = function (e, data) {
            chai_1.expect(data).to.equal(null);
            chai_1.expect(e).to.equal(expected_err);
            done();
        };
        new TokenedDataApi_1.TokenedDataApi(mock_dapi, function () { return mock_tstore; }).request({}, callback, expected_ns);
    });
});
function createStubToken(last_created) {
    return {
        id: 'dummy-id',
        state: Token_1.TokenState.ACTIVE,
        timestamp: 0,
        data: '',
        last_mismatch: false,
        last_created: last_created,
    };
}
//# sourceMappingURL=data:application/json;base64, 