"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 chai = __importStar(require("chai"));
const path = __importStar(require("path"));
const mongoose_helper_1 = require("../../src/lib/mongoose-report-manager/mongoose-helper");
const mongodb_1 = require("mongodb");
const chai_as_promised_1 = __importDefault(require("chai-as-promised"));
chai.should();
chai.use(chai_as_promised_1.default);
const expect = chai.expect;
describe('mongoose-helper', () => {
    const userProperties = {
        jsonDir: '',
        mongoDb: {
            collections: {
                features: 'Features',
                outputs: 'Outputs',
                reports: 'Reports',
                scenarios: 'Scenarios',
                steps: 'Steps',
            },
            dbHost: 'localhost',
            dbName: 'multiple-cucumber-html-reports',
            dbPort: 27017,
        },
    };
    const mongooseHelper = new mongoose_helper_1.MongooseHelper(userProperties);
    const jsonFile1 = path.resolve(process.cwd(), './test/unit/data/enriched-joined-cucumber-jsons/enriched-output1.json');
    const jsonFile3 = path.resolve(process.cwd(), './test/unit/data/enriched-joined-cucumber-jsons/enriched-output-with-errors.json');
    const jsonFile2 = path.resolve(process.cwd(), './test/unit/data/enriched-joined-cucumber-jsons/enriched-output.json');
    describe('Happy paths', () => {
        it('can save a report', async () => {
            // Given
            const jsonFile = path.resolve(process.cwd(), './test/unit/data/enriched-joined-cucumber-jsons/enriched-output.json');
            const expectedReport = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile);
            const id = await mongooseHelper.insertReport(expectedReport);
            // When
            const reportRecovered = await mongooseHelper.getReportById(id);
            // Then
            expect(reportRecovered).to.eql(expectedReport);
        });
        it('can get a report with is as string', async () => {
            // Given
            const jsonFile = path.resolve(process.cwd(), './test/unit/data/enriched-joined-cucumber-jsons/enriched-output.json');
            const expectedReport = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile);
            const id = await mongooseHelper.insertReport(expectedReport);
            // When
            const reportRecovered = await mongooseHelper.getReportById(id.toHexString());
            // Then
            expect(reportRecovered).to.eql(expectedReport);
        });
        it('can return all the reports ordered and filtered by feature name', async () => {
            // Given
            const reportSaved1 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile1);
            const reportSaved2 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile2);
            const id1 = await mongooseHelper.insertReport(reportSaved1);
            const id2 = await mongooseHelper.insertReport(reportSaved2);
            // When
            const results = await mongooseHelper.getAllTheElementsOrderedAndFiltered('', -1, "feature:Feature with special characters ^'@#");
            // Then
            expect(results.length).to.equal(1);
            await mongooseHelper.deleteReportById(id1.toHexString());
            await mongooseHelper.deleteReportById(id2.toHexString());
        });
        it('can return all the reports filtered by scenario name', async () => {
            // Given
            const reportSaved1 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile1);
            const reportSaved2 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile2);
            const id1 = await mongooseHelper.insertReport(reportSaved1);
            const id2 = await mongooseHelper.insertReport(reportSaved2);
            // When
            const results = await mongooseHelper.getAllTheElementsOrderedAndFiltered('', -1, 'scenario:Scenario with special characters:{&/%');
            // Then
            expect(results.length).to.equal(1);
            await mongooseHelper.deleteReportById(id1);
            await mongooseHelper.deleteReportById(id2);
        });
        it('can return all the reports filtered by step name', async () => {
            // Given
            const reportSaved1 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile1);
            const reportSaved2 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile2);
            const id1 = await mongooseHelper.insertReport(reportSaved1);
            const id2 = await mongooseHelper.insertReport(reportSaved2);
            // When
            const results = await mongooseHelper.getAllTheElementsOrderedAndFiltered('', -1, 'step:Step with special characters Ñ)/&%$');
            // Then
            expect(results.length).to.equal(1);
            await mongooseHelper.deleteReportById(id1);
            await mongooseHelper.deleteReportById(id2);
        });
        it('can return all the reports ordered by results.executionDate', async () => {
            // Given
            const reportSaved1 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile1);
            const reportSaved2 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile2);
            const id1 = await mongooseHelper.insertReport(reportSaved1);
            const id2 = await mongooseHelper.insertReport(reportSaved2);
            // When
            const resultsAsc = await mongooseHelper.getAllTheElementsOrderedAndFiltered('results.executionDate', 1, '');
            const resultsDesc = await mongooseHelper.getAllTheElementsOrderedAndFiltered('results.executionDate', -1, '');
            const resultsOrderedAsc = resultsAsc.sort((a, b) => a.results.overview.date.getTime() - b.results.overview.date.getTime());
            const resultsOrderedDesc = resultsDesc.sort((a, b) => b.results.overview.date.getTime() - a.results.overview.date.getTime());
            // Then
            expect(resultsAsc).to.be.deep.equal(resultsOrderedAsc);
            expect(resultsDesc).to.be.deep.equal(resultsOrderedDesc);
            await mongooseHelper.deleteReportById(id1);
            await mongooseHelper.deleteReportById(id2);
        });
        it('can return all the reports ordered by results.executionTime', async () => {
            // Given
            const reportSaved1 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile1);
            const reportSaved2 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile2);
            const id1 = await mongooseHelper.insertReport(reportSaved1);
            const id2 = await mongooseHelper.insertReport(reportSaved2);
            // When
            const resultsAsc = await mongooseHelper.getAllTheElementsOrderedAndFiltered('results.overview.executionDate', 1, '');
            const resultsDesc = await mongooseHelper.getAllTheElementsOrderedAndFiltered('results.overview.executionDate', -1, '');
            const resultsOrderedAsc = resultsAsc.sort((a, b) => a.results.overview.date.getTime() - b.results.overview.date.getTime());
            const resultsOrderedDesc = resultsDesc.sort((a, b) => b.results.overview.date.getTime() - a.results.overview.date.getTime());
            // Then
            expect(resultsAsc).to.be.deep.equal(resultsOrderedAsc);
            expect(resultsDesc).to.be.deep.equal(resultsOrderedDesc);
            await mongooseHelper.deleteReportById(id1);
            await mongooseHelper.deleteReportById(id2);
        });
        it('can return all the elements', async () => {
            // Given
            const reportSaved1 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile1);
            const reportSaved2 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile2);
            const id1 = await mongooseHelper.insertReport(reportSaved1);
            const id2 = await mongooseHelper.insertReport(reportSaved2);
            // When
            const results = await mongooseHelper.getAllTheElementsOrderedAndFiltered('', -1, '');
            // Then
            expect(results.length).to.be.least(2);
            await mongooseHelper.deleteReportById(id1);
            await mongooseHelper.deleteReportById(id2);
        });
        it('can get database size', async () => {
            // Given
            const reportSaved1 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile1);
            const reportSaved2 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile2);
            const id1 = await mongooseHelper.insertReport(reportSaved1);
            const id2 = await mongooseHelper.insertReport(reportSaved2);
            // When
            const results = await mongooseHelper.getDatabaseSize();
            // Then
            expect(results).to.be.least(5.269391059875488);
            await mongooseHelper.deleteReportById(id1);
            await mongooseHelper.deleteReportById(id2);
        });
        it('can delete a report', async () => {
            // Given
            const reportSaved1 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile1);
            const reportSaved2 = await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile2);
            const id1 = await mongooseHelper.insertReport(reportSaved1);
            const id2 = await mongooseHelper.insertReport(reportSaved2);
            const oId2 = new mongodb_1.ObjectId(id2);
            // When
            const results = await mongooseHelper.getDatabaseSize();
            // Then
            expect(results).to.be.least(5.269391059875488);
            await mongooseHelper.deleteReportById(id1);
            await mongooseHelper.deleteReportById(oId2);
        });
    });
    describe('Failures', () => {
        // beforeEach( () => sinon.stub( console, 'log' ) );
        it('connection object returns an error when it cannot connect', async () => {
            // Given
            userProperties.mongoDb.dbPort = 25;
            // When
            await mongooseHelper.getDatabaseSize().catch((err) => {
                // Then
                const errorMessage = 'connect ECONNREFUSED 127.0.0.1:25';
                expect(err.message).to.be.equal(errorMessage);
            });
        });
        it('returns an error when parsing an incorrect json', async () => {
            // Given
            // When
            await CommonFunctions.fileSystemFunctions.readJsonExtendedReportFile(jsonFile3)
                .catch((err) => {
                // Then
                const errorMessage = `The file ${jsonFile3} is not a valid json`;
                expect(err.message).to.contain(errorMessage);
            });
        });
    });
});
//# sourceMappingURL=mongoose-helper-spec.js.map