"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai = __importStar(require("chai"));
const fs = __importStar(require("fs"));
const fse = __importStar(require("fs-extra"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const sinon = __importStar(require("sinon"));
const GenerateReport_1 = require("../../src/lib/generate-report/GenerateReport");
const readDirAsync = fs.promises.readdir;
const statAsync = fs.promises.stat;
const { expect } = chai;
describe('GenerateReport.ts', () => {
    const getReportPath = async () => {
        const tempDir = path.join(os.tmpdir(), 'multiple_cucumber_report/');
        const foldersInTempDir = await readDirAsync(tempDir);
        const stats = await Promise.all(foldersInTempDir.map(async (folder) => {
            const stat = await statAsync(path.join(tempDir, folder));
            return { folder: tempDir + folder, stat };
        }));
        return stats.sort((a, b) => b.stat.mtime.getTime() - a.stat.mtime.getTime())[0].folder;
    };
    const emptyDestinationFolder = async (reportPath) => {
        if (await fse.pathExists(reportPath)) {
            await fse.remove(reportPath);
        }
        await fse.mkdir(reportPath);
    };
    describe('Happy flows', () => {
        it('should create a report from the merged found json files with metadata', async () => {
            // Given
            const reportPath = `${process.cwd()}/dist/test/reports/.tmp4/`;
            if (!await fse.pathExists(reportPath)) {
                await fse.mkdirp(reportPath);
            }
            await emptyDestinationFolder(reportPath);
            const featuresFolder = path.resolve(reportPath, 'features/');
            if (!await fse.pathExists(featuresFolder)) {
                await fse.mkdir(featuresFolder);
            }
            const options = {
                date: new Date(),
                expandStepsNotPassed: false,
                featuresFolder: `${process.cwd()}/test/unit/data/features`,
                isLinkToServer: true,
                jsonDir: `${process.cwd()}/test/unit/data/cucumber-report-jsons/`,
                metadata: [{
                        name: 'browser',
                        value: 'Chrome 84'
                    }],
                openReportInBrowser: true,
                projectName: 'You can adjust this report name',
                reportPath,
                saveCollectedJSON: true,
                saveEnrichedJSON: true,
                saveReportInMongoDb: true,
                server: {
                    serverPort: 3100
                },
                showExecutionTime: true,
            };
            // When
            await new GenerateReport_1.GenerateReport().generate(options);
            // Then
            expect(await fse.pathExists(path.join(reportPath, 'index.html'))).to.equal(true);
            expect(await fse.pathExists(path.join(reportPath, 'merged-output.json'))).to.equal(true);
            expect(await fse.pathExists(path.join(reportPath, 'enriched-output.json'))).to.equal(true);
        });
        it('should create a report from the merged found json files without metadata', async () => {
            // Given
            const options = {
                disableLog: true,
                isLinkToServer: false,
                jsonDir: `${process.cwd()}/test/unit/data/cucumber-report-jsons/`,
                navigateToFeatureIfThereIsOnlyOne: true,
                openReportInBrowser: false,
                saveCollectedJSON: false,
                saveReportInMongoDb: false,
                showExecutionTime: false
            };
            // When
            await new GenerateReport_1.GenerateReport().generate(options);
            // Then
            const reportPath = await getReportPath();
            expect(await fse.pathExists(path.join(reportPath, 'index.html'))).to.equal(true);
            sinon.restore();
        });
    });
    describe('failures', () => {
        it('should throw an error when no options are provided', async () => {
            // Given
            const options = null;
            // When
            await new GenerateReport_1.GenerateReport().generate(options).catch((error) => {
                // Then
                expect(error.message).to.equal('Options need to be provided.');
            });
        });
        it('should throw an error when the json dir si not provided', async () => {
            // Given
            const reportPath = `${process.cwd()}/dist/test/reports/.tmp6/`;
            const options = {
                jsonDir: '',
                metadata: [
                    { name: 'browser', value: 'chrome 81' }
                ],
                projectName: 'You can adjust this report name',
                reportPath
            };
            // When
            await new GenerateReport_1.GenerateReport().generate(options).catch((error) => {
                // Then
                expect(error.message).to.equal('A path which holds the JSON files should be provided.');
            });
        });
        it('should throw an error when the report folder is invalid', async () => {
            // Given
            const reportPath = '/home/demian/test/test2';
            const options = {
                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,
                reportPath
            };
            // When
            await new GenerateReport_1.GenerateReport().generate(options).catch((error) => {
                // THen
                expect(error.message).to.equal(`The path provided: ${options.reportPath} is invalid`);
            });
        });
    });
});
//# sourceMappingURL=GenerateReport-spec.js.map