"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.expectLogContain = exports.checkBadSortPagination = exports.checkBadCountPagination = exports.checkBadStartPagination = exports.expectEndWith = exports.expectNotStartWith = exports.expectStartWith = exports.testFileExistsOrNot = exports.expectLogDoesNotContain = exports.testImage = exports.dateIsValid = void 0;
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const core_utils_1 = require("../../../shared/core-utils");
const server_commands_1 = require("../../../shared/server-commands");
function dateIsValid(dateString, interval = 300000) {
    const dateToCheck = new Date(dateString);
    const now = new Date();
    return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval;
}
exports.dateIsValid = dateIsValid;
function expectStartWith(str, start) {
    (0, chai_1.expect)(str.startsWith(start), `${str} does not start with ${start}`).to.be.true;
}
exports.expectStartWith = expectStartWith;
function expectNotStartWith(str, start) {
    (0, chai_1.expect)(str.startsWith(start), `${str} does not start with ${start}`).to.be.false;
}
exports.expectNotStartWith = expectNotStartWith;
function expectEndWith(str, end) {
    (0, chai_1.expect)(str.endsWith(end), `${str} does not end with ${end}`).to.be.true;
}
exports.expectEndWith = expectEndWith;
function expectLogDoesNotContain(server, str) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const content = yield server.servers.getLogContent();
        (0, chai_1.expect)(content.toString()).to.not.contain(str);
    });
}
exports.expectLogDoesNotContain = expectLogDoesNotContain;
function expectLogContain(server, str) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const content = yield server.servers.getLogContent();
        (0, chai_1.expect)(content.toString()).to.contain(str);
    });
}
exports.expectLogContain = expectLogContain;
function testImage(url, imageName, imageHTTPPath, extension = '.jpg') {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const res = yield (0, server_commands_1.makeGetRequest)({
            url,
            path: imageHTTPPath,
            expectedStatus: 200
        });
        const body = res.body;
        const data = yield (0, fs_extra_1.readFile)((0, path_1.join)((0, core_utils_1.root)(), 'server', 'tests', 'fixtures', imageName + extension));
        const minLength = data.length - ((40 * data.length) / 100);
        const maxLength = data.length + ((40 * data.length) / 100);
        (0, chai_1.expect)(body.length).to.be.above(minLength, 'the generated image is way smaller than the recorded fixture');
        (0, chai_1.expect)(body.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture');
    });
}
exports.testImage = testImage;
function testFileExistsOrNot(server, directory, filePath, exist) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const base = server.servers.buildDirectory(directory);
        (0, chai_1.expect)(yield (0, fs_extra_1.pathExists)((0, path_1.join)(base, filePath))).to.equal(exist);
    });
}
exports.testFileExistsOrNot = testFileExistsOrNot;
function checkBadStartPagination(url, path, token, query = {}) {
    return (0, server_commands_1.makeGetRequest)({
        url,
        path,
        token,
        query: Object.assign(Object.assign({}, query), { start: 'hello' }),
        expectedStatus: 400
    });
}
exports.checkBadStartPagination = checkBadStartPagination;
function checkBadCountPagination(url, path, token, query = {}) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        yield (0, server_commands_1.makeGetRequest)({
            url,
            path,
            token,
            query: Object.assign(Object.assign({}, query), { count: 'hello' }),
            expectedStatus: 400
        });
        yield (0, server_commands_1.makeGetRequest)({
            url,
            path,
            token,
            query: Object.assign(Object.assign({}, query), { count: 2000 }),
            expectedStatus: 400
        });
    });
}
exports.checkBadCountPagination = checkBadCountPagination;
function checkBadSortPagination(url, path, token, query = {}) {
    return (0, server_commands_1.makeGetRequest)({
        url,
        path,
        token,
        query: Object.assign(Object.assign({}, query), { sort: 'hello' }),
        expectedStatus: 400
    });
}
exports.checkBadSortPagination = checkBadSortPagination;
//# sourceMappingURL=checks.js.map