"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const CommonFunctions = __importStar(require("../../src/lib/common-functions/common-functions"));
const path = __importStar(require("path"));
const mongoose_helper_1 = require("../../src/lib/mongoose-report-manager/mongoose-helper");
const server_1 = __importDefault(require("../../src/lib/server/server"));
const chai_1 = __importDefault(require("chai"));
const chai_http_1 = __importDefault(require("chai-http"));
chai_1.default.use(chai_http_1.default);
const expect = chai_1.default.expect;
const userProperties = {
    disableLog: true,
    jsonDir: `${process.cwd()}/test/unit/data/jsons`,
    metadata: [
        { name: 'browser', value: 'chrome 81' }
    ],
    mongoDb: {
        collections: {
            features: 'Features',
            outputs: 'Outputs',
            reports: 'Reports',
            scenarios: 'Scenarios',
            steps: 'Steps'
        },
        dbHost: 'localhost',
        dbName: 'multiple-cucumber-html-reports',
        dbPort: 27017,
    },
    navigateToFeatureIfThereIsOnlyOne: false,
    openReportInBrowser: true,
    server: {
        serverPort: 3100
    }
};
const mongooseHelper = new mongoose_helper_1.MongooseHelper(userProperties);
const server = new server_1.default();
describe('server.ts', () => {
    before(() => {
        server.startServer();
    });
    describe('Happy flows', () => {
        it('returns an html page', async () => {
            // When
            const res = await chai_1.default.request(server.app).get('/');
            expect(res).to.have.status(200);
            expect(res.body).to.be.an('object');
            expect(res).to.have.property('type', 'text/html');
        });
        it('returns a report from the database', async () => {
            // Given
            const jsonFile = path.resolve(process.cwd(), './test/unit/data/enriched-joined-cucumber-jsons/enriched-output.json');
            const reportSaved = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile);
            const id = await mongooseHelper.insertReport(reportSaved);
            // When
            const res = await chai_1.default.request(server.app).get('/generateReport').query({ id: id.toString() });
            // Then
            expect(res).to.have.status(200);
            expect(res.body).to.have.property('htmlreport');
            // expect( await fse.pathExists( ( res.body as Models.ResponseBody ).htmlreport ) ).to.equal( true );
            expect(res).to.have.property('type', 'application/json');
            await mongooseHelper.deleteReportById(id);
        });
        it('updates the datatable with ascending order', async () => {
            const jsonFile = path.resolve(process.cwd(), './test/unit/data/enriched-joined-cucumber-jsons/enriched-output.json');
            const reportSaved = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile);
            const id = await mongooseHelper.insertReport(reportSaved);
            // When
            const res = await chai_1.default.request(server.app)
                .get('/mongo/get/datatable')
                .query({ id: id.toString() })
                .query({ 'columns[0]': [{ 'data': '_id', 'name': '', 'orderable': 'true', 'regex': 'false', 'search': { 'searchable': 'true', 'value': '' } }] })
                .query({ 'columns[1]': [{ 'data': 'name', 'name': '', 'orderable': 'true', 'search': { 'regex': 'false', 'value': '' }, 'searchable': 'true' }] })
                .query({ 'columns[2]': [{ 'data': 'results.executionDate', 'name': '', 'orderable': 'true', 'search': { 'regex': 'false', 'value': '' }, 'searchable': 'true' }] })
                .query({ 'columns[3]': [{ 'data': 'results.executionTime', 'name': '', 'orderable': 'true', 'search': { 'regex': 'false', 'value': '' }, 'searchable': 'true' }] })
                .query({ 'columns[4]': [{ 'data': '4', 'name': '', 'orderable': 'true', 'search': { 'regex': 'false', 'value': '' }, 'searchable': 'true' }] })
                .query({ 'order[0]': { 'column': '1', 'dir': 'asc' } })
                .query({ 'search': [{ 'regex': 'false', 'value': '' }] });
            // Then
            expect(res).to.have.status(200);
            expect(res.body).to.have.property('data');
            expect(res.body.data).not.to.equal(null);
            expect(res.body.data[0]).to.have.property('results');
            await mongooseHelper.deleteReportById(id);
        });
        it('updates the datatable with descending order', async () => {
            const jsonFile = path.resolve(process.cwd(), './test/unit/data/enriched-joined-cucumber-jsons/enriched-output.json');
            const reportSaved = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile);
            const id = await mongooseHelper.insertReport(reportSaved);
            // When
            const res = await chai_1.default.request(server.app)
                .get('/mongo/get/datatable')
                .query({ id: id.toString() })
                .query({ 'columns[0]': [{ 'data': '_id', 'name': '', 'orderable': 'true', 'search': { 'regex': 'false', 'value': '' }, 'searchable': 'true' }] })
                .query({ 'columns[1]': [{ 'data': 'results.name', 'name': '', 'orderable': 'true', 'search': { 'regex': 'false', 'value': '' }, 'searchable': 'true' }] })
                .query({ 'columns[2]': [{ 'data': 'results.executionDate', 'name': '', 'orderable': 'true', 'search': { 'regex': 'false', 'value': '' }, 'searchable': 'true' }] })
                .query({ 'columns[3]': [{ 'data': 'results.executionTime', 'name': '', 'orderable': 'true', 'search': { 'regex': 'false', 'value': '' }, 'searchable': 'true' }] })
                .query({ 'columns[4]': [{ 'data': '4', 'name': '', 'orderable': 'true', 'search': { 'regex': 'false', 'value': '' }, 'searchable': 'true' }] })
                .query({ 'order[0]': { 'column': '1', 'dir': 'desc' } })
                .query({ 'search': [{ 'regex': 'false', 'value': '' }] });
            // Then
            expect(res).to.have.status(200);
            expect(res.body).to.have.property('data');
            expect(res.body.data).not.to.equal(null);
            expect(res.body.data[0]).to.have.property('results');
            await mongooseHelper.deleteReportById(id);
        });
    });
    after(() => {
        server.closeServer();
    });
});
//# sourceMappingURL=server-spec.js.map