"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 chai = __importStar(require("chai"));
const fse = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
chai.should();
const collect_jsons_1 = require("../../src/lib/collect-jsons/collect-jsons");
const chai_as_promised_1 = __importDefault(require("chai-as-promised"));
chai.use(chai_as_promised_1.default);
const expect = chai.expect;
describe('CollectJsons.ts', () => {
    describe('Happy flows', () => {
        it('should return an output from the merged found json files', async () => {
            // Given
            const outputReportPath = process.cwd() + '/dist/test/reports/.tmp1/';
            if (await fse.pathExists(outputReportPath)) {
                await fse.remove(outputReportPath);
            }
            await fse.mkdir(outputReportPath);
            const pathJsonsToBeJoined = path.resolve(process.cwd(), './test/unit/data/cucumber-report-jsons');
            const resultsPath = path.resolve(process.cwd(), './test/unit/data/joined-cucumber-jsons/merged-output.json');
            const userProperties = {
                jsonDir: pathJsonsToBeJoined,
                reportPath: outputReportPath,
                saveCollectedJSON: true,
            };
            const collectionJsons = new collect_jsons_1.CollectJsons(userProperties);
            // When
            const jsonRead = await collectionJsons.createJoinedJson();
            // await fse.writeJson( '/home/demian/Downloads/merged-output.json', jsonRead );
            const jsonExpected = await fse.readJson(resultsPath);
            // Then
            expect(jsonRead).to.be.deep.equal(jsonExpected);
        });
        it('should return an output from the merged found json files and add the provided metadata', async () => {
            // Given
            const metadata = [
                {
                    name: 'browser',
                    value: 'chrome 1',
                },
                {
                    name: 'platform',
                    value: 'Ubuntu 16.04',
                },
            ];
            const pathJsonsToBeJoined = path.resolve(process.cwd(), './test/unit/data/cucumber-report-jsons');
            const resultsPath = path.resolve(process.cwd(), './test/unit/data/joined-cucumber-jsons/provided-metadata.json');
            const userProperties = {
                jsonDir: pathJsonsToBeJoined,
                metadata,
                reportPath: resultsPath,
            };
            const collectionJsons = new collect_jsons_1.CollectJsons(userProperties);
            // When
            const jsonRead = await collectionJsons.createJoinedJson();
            // await fse.writeJson( '/home/demian/Downloads/provided-metadata.json', jsonRead );
            // Then
            const jsonExpected = await fse.readJson(resultsPath);
            expect(jsonRead).to.be.deep.equal(jsonExpected);
        });
        it('should return no report from incorrect files', async () => {
            const pathJsonsToBeJoined = path.resolve(process.cwd(), './test/unit/data/no-jsons');
            const userProperties = { jsonDir: pathJsonsToBeJoined };
            // When
            await new collect_jsons_1.CollectJsons(userProperties).createJoinedJson().catch((error) => {
                expect(error.message).equal(`No JSON files found in '${pathJsonsToBeJoined}'. NO REPORT CAN BE CREATED!`);
            });
        });
    });
    describe('failures', () => {
        it('should throw an error when the json folder does not exist', async () => {
            const pathJsonsToBeJoined = path.resolve(process.cwd(), './test/unit/data/bla');
            const userProperties = { jsonDir: pathJsonsToBeJoined };
            await new collect_jsons_1.CollectJsons(userProperties).createJoinedJson().catch((error) => {
                expect(error.message).equal(`The path provided: ${pathJsonsToBeJoined} is invalid`);
            });
        });
        it('should throw an error when no json files could be found', async () => {
            const pathJsonsToBeJoined = path.resolve(process.cwd(), './test/unit/data/no-jsons');
            const userProperties = { jsonDir: pathJsonsToBeJoined };
            await new collect_jsons_1.CollectJsons(userProperties).createJoinedJson().catch((error) => {
                expect(error.message).equal(`No JSON files found in '${pathJsonsToBeJoined}'. NO REPORT CAN BE CREATED!`);
            });
        });
    });
});
//# sourceMappingURL=collect-jsons-spec.js.map