"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const ws_1 = tslib_1.__importDefault(require("ws"));
const server_commands_1 = require("../../../shared/server-commands");
function buildWebSocket(server, path) {
    return new ws_1.default('ws://' + server.host + path);
}
function expectErrorOrTimeout(server, path, expectedTimeout) {
    return new Promise((res, rej) => {
        const ws = buildWebSocket(server, path);
        ws.on('error', () => res());
        const timeout = setTimeout(() => res(), expectedTimeout);
        ws.on('open', () => {
            clearTimeout(timeout);
            return rej(new Error('Connect did not timeout'));
        });
    });
}
describe('Test plugin websocket', function () {
    let server;
    const basePaths = [
        '/plugins/test-websocket/ws/',
        '/plugins/test-websocket/0.0.1/ws/'
    ];
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(30000);
            server = yield (0, server_commands_1.createSingleServer)(1);
            yield (0, server_commands_1.setAccessTokensToServers)([server]);
            yield server.plugins.install({ path: server_commands_1.PluginsCommand.getPluginTestPath('-websocket') });
        });
    });
    it('Should not connect to the websocket without the appropriate path', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const paths = [
                '/plugins/unknown/ws/',
                '/plugins/unknown/0.0.1/ws/'
            ];
            for (const path of paths) {
                yield expectErrorOrTimeout(server, path, 1000);
            }
        });
    });
    it('Should not connect to the websocket without the appropriate sub path', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            for (const path of basePaths) {
                yield expectErrorOrTimeout(server, path + '/unknown', 1000);
            }
        });
    });
    it('Should connect to the websocket and receive pong', function (done) {
        const ws = buildWebSocket(server, basePaths[0]);
        ws.on('open', () => ws.send('ping'));
        ws.on('message', data => {
            if (data.toString() === 'pong')
                return done();
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=plugin-websocket.js.map