"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAddress = exports.stopTime = exports.expectToHaveEqualMembers = exports.expectToBeInRange = exports.waitUntil = exports.makeSignal = exports.describeAcrossBackends = void 0;
const events_1 = require("events");
function describeAcrossBackends(topic, runTests) {
    describe(topic, () => {
        describe("Redis", () => {
            runTests("Redis");
        });
        describe("Mock", () => {
            runTests("Mock");
        });
    });
}
exports.describeAcrossBackends = describeAcrossBackends;
function makeSignal() {
    const emitter = new events_1.EventEmitter();
    function waitForSignal(key = "") {
        return new Promise((resolve) => {
            emitter.on(key, resolve);
        });
    }
    waitForSignal.signal = (key = "") => {
        emitter.emit(key);
    };
    return waitForSignal;
}
exports.makeSignal = makeSignal;
function removeFirstStackLine(string) {
    return string.replace(/\n.*\n/, "");
}
function waitUntil(predicate, butMax, interval = 20) {
    const potentialError = new Error(`Predicate was not fulfilled on time (${predicate.toString()})`);
    potentialError.showDiff = false;
    potentialError.stack = removeFirstStackLine(potentialError.stack);
    return new Promise((resolve, reject) => {
        const check = setInterval(() => {
            if (predicate()) {
                clearInterval(check);
                clearTimeout(max);
                resolve();
            }
        }, interval);
        const max = setTimeout(() => {
            clearInterval(check);
            reject(potentialError);
        }, butMax);
    });
}
exports.waitUntil = waitUntil;
function expectToBeInRange(value, [from, to]) {
    expect(value).toBeGreaterThanOrEqual(from);
    expect(value).toBeLessThanOrEqual(to);
}
exports.expectToBeInRange = expectToBeInRange;
function expectToHaveEqualMembers(actual, expected) {
    expect(actual).toHaveLength(expected.length);
    for (const e of expected) {
        expect(actual).toContainEqual(e);
    }
}
exports.expectToHaveEqualMembers = expectToHaveEqualMembers;
async function stopTime(doIt) {
    const start = Date.now();
    await doIt();
    const end = Date.now();
    return end - start;
}
exports.stopTime = stopTime;
function getAddress(server) {
    let { address, port } = server.address();
    if (address.startsWith("::")) {
        address = "localhost";
    }
    return `http://${address}:${port}`;
}
exports.getAddress = getAddress;
//# sourceMappingURL=util.js.map