"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("events");
const chai_1 = require("chai");
const sinon_1 = require("sinon");
const _1 = require("./");
const mock_splits_updated_init_json_1 = __importDefault(require("../test/fixtures/mock-splits-updated-init.json"));
const mockOnConnectInitResponse = mock_splits_updated_init_json_1.default;
describe('client/index', () => {
    const sandbox = (0, sinon_1.createSandbox)();
    beforeEach(() => {
        sandbox.restore();
        sandbox.stub(process, 'exit');
        class FakeClient extends events_1.EventEmitter {
            constructor() {
                super(...arguments);
                this.connect = sandbox.spy();
                this.disconnect = sandbox.spy();
                this.disconnected = false;
                this.io = { on: sandbox.spy() };
            }
        }
        sandbox.stub(_1.Client, 'ioclient').returns(new FakeClient());
    });
    it('exists', () => {
        (0, chai_1.expect)(_1.connect).to.be.a('function');
    });
    it('stubs connect call', () => __awaiter(void 0, void 0, void 0, function* () {
        console.log('try');
        const connection = yield (0, _1.connect)();
        console.log('no?');
        (0, chai_1.expect)(connection.socket.disconnected).to.eql(false);
        (0, chai_1.expect)(connection.socket.connect).to.be.a('function');
        (0, chai_1.expect)(connection.socket.disconnect).to.be.a('function');
        (0, chai_1.expect)(connection.socket.emit).to.be.a('function');
        (0, chai_1.expect)(connection.socket.connect.getCall(0)).to.be.null;
        (0, chai_1.expect)(connection.state).to.eql(_1.ClientState.CLIENT_CONNECTING);
    }));
    it('set state on successful connection', () => __awaiter(void 0, void 0, void 0, function* () {
        const connection = yield (0, _1.connect)();
        (0, chai_1.expect)(connection.state).to.eql(_1.ClientState.CLIENT_CONNECTING);
        connection.socket.emit('connect');
        (0, chai_1.expect)(connection.state).to.eql(_1.ClientState.CLIENT_OK);
    }));
    it('stubs process.exit', () => {
        process.exit(420);
        (0, chai_1.expect)(process.exit.getCalls()).to.have.lengthOf(1);
    });
    it('remembers api_version on connect', () => __awaiter(void 0, void 0, void 0, function* () {
        const connection = yield (0, _1.connect)();
        const splitEvents = [];
        connection.on('splits-updated', (data) => {
            splitEvents.push(data);
        });
        connection.socket.emit('connect');
        (0, chai_1.expect)(connection.lastApiVersionConnected).to.be.undefined;
        (0, chai_1.expect)(process.exit.getCalls()).to.have.lengthOf(0);
        connection.socket.emit('splits-updated', mockOnConnectInitResponse);
        (0, chai_1.expect)(connection.lastApiVersionConnected).to.eql('1.4.13');
        (0, chai_1.expect)(process.exit.getCalls()).to.have.lengthOf(0);
        connection.socket.emit('splits-updated', mockOnConnectInitResponse);
        connection.socket.emit('splits-updated', { mockOnConnectInitResponse, api_version: 'banana' });
        (0, chai_1.expect)(splitEvents).to.have.lengthOf(2);
        (0, chai_1.expect)(process.exit.getCalls()).to.have.lengthOf(1);
        (0, chai_1.expect)(process.exit.getCall(0).args[0]).to.eql(69);
    }));
});
//# sourceMappingURL=index.spec.js.map