"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 models_1 = require("../../models");
const services_1 = require("../../services");
const token_1 = require("./token");
let mockConnectionService = ts_mockito_1.mock(services_1.ConnectionService);
let stateService;
beforeEach(() => {
    ts_mockito_1.reset(mockConnectionService);
    stateService = new services_1.StateService();
});
describe('getToken', () => {
    it('calls the connection service', () => __awaiter(void 0, void 0, void 0, function* () {
        ts_mockito_1.when(mockConnectionService.send(ts_mockito_1.deepEqual({ username: 'stuart', password: 'letmein' }), ts_mockito_1.anything())).thenResolve({ token: 'abc' });
        ts_mockito_1.when(mockConnectionService.send(ts_mockito_1.deepEqual({}), ts_mockito_1.anything())).thenResolve([{ userID: 10, apiKey: 'api' }]);
        const result = yield token_1.getToken(ts_mockito_1.instance(mockConnectionService), stateService, { username: 'stuart', password: 'letmein' });
        ts_mockito_1.verify(mockConnectionService.send(ts_mockito_1.deepEqual({ username: 'stuart', password: 'letmein' }), ts_mockito_1.deepEqual(models_1.apiConfig.token))).once();
        expect(result).toStrictEqual({ token: 'abc' });
    }));
    it('sets the state', () => __awaiter(void 0, void 0, void 0, function* () {
        ts_mockito_1.when(mockConnectionService.send(ts_mockito_1.deepEqual({ username: 'stuart', password: 'letmein' }), ts_mockito_1.anything())).thenResolve({ token: 'abc' });
        ts_mockito_1.when(mockConnectionService.send(ts_mockito_1.deepEqual({}), ts_mockito_1.anything())).thenResolve([{ userID: 10, apiKey: 'api' }]);
        yield token_1.getToken(ts_mockito_1.instance(mockConnectionService), stateService, { username: 'stuart', password: 'letmein' });
        expect(stateService.userId).toBe(10);
        expect(stateService.apiKey).toBe('api');
    }));
    it('inits sockets', () => __awaiter(void 0, void 0, void 0, function* () {
        ts_mockito_1.when(mockConnectionService.send(ts_mockito_1.deepEqual({ username: 'stuart', password: 'letmein' }), ts_mockito_1.anything())).thenResolve({ token: 'abc' });
        ts_mockito_1.when(mockConnectionService.send(ts_mockito_1.deepEqual({}), ts_mockito_1.anything())).thenResolve([{ userID: 10, apiKey: 'api' }]);
        yield token_1.getToken(ts_mockito_1.instance(mockConnectionService), stateService, { username: 'stuart', password: 'letmein' });
        ts_mockito_1.verify(mockConnectionService.initSockets()).once();
    }));
});
