const common = require('./common');
const { expect, assert } = require('chai');

const emptyVersionsMetadata = {};

const versionsMetadata = {
  "versions": [
    {
      "version": "118.0.0-SNAPSHOT.13",
      "releaseDate": "01/01/2019 00:00",
      "envTarget": "DEV"
    },
    {
      "version": "117.0.0-TST.0",
      "releaseDate": "01/01/2019 00:00",
      "envTarget": "TST"
    }
  ]
};

const emptyEnvsMetadata = {};

const envsMetadata = {
  "DEV": {
      "version": "118.0.0-SNAPSHOT.13",
      "logicalVersion": "PI9.IT3",
      "releaseDate": "15/11/2019 17:39",
      "infos": "118.0.0-SNAPSHOT.13 - 15/11/2019 17:39",
      "dependencies": {
      }
  },
  "TST": {
      "version": "117.0.0-TST.0",
      "logicalVersion": "PI9.IT3",
      "releaseDate": "16/11/2019 01:01",
      "infos": "117.0.0-TST.1 - 16/11/2019 01:01",
      "dependencies": {
      }
  },
  "INT": {
      "version": "117.0.0-INT.0",
      "logicalVersion": "PI9.IT3",
      "releaseDate": "15/11/2019 07:42",
      "infos": "117.0.0-INT.0 - 15/11/2019 07:42",
      "dependencies": {
      }
  },
  "TRN": {
      "version": "113.0.0-TRN.0",
      "releaseDate": "11/11/2019 22:02",
      "infos": "113.0.0-TRN.0 - 11/11/2019 22:02",
      "dependencies": {
      }
  },
  "DLT": {
      "version": "113.0.0-DLT.1",
      "releaseDate": "11/11/2019 22:32",
      "infos": "113.0.0-DLT.1 - 11/11/2019 22:32",
      "dependencies": {
      }
  },
  "ACC": {
      "version": "117.0.0-ACC.0",
      "logicalVersion": "PI9.IT3",
      "releaseDate": "15/11/2019 14:50",
      "infos": "117.0.0-ACC.0 - 15/11/2019 14:50",
      "dependencies": {
      }
  },
  "PROD": {
      "version": "114.0.1",
      "logicalVersion": "PI9.IT3",
      "releaseDate": "14/11/2019 17:31",
      "infos": "114.0.1 - 14/11/2019 17:31",
      "dependencies": {
      }
  }
};

describe('[CSDR-version-app-env-target] - getNewVersionDefault()', () => {

  it('should fail if envTarget is empty/null/undefined', () => {
    return common.getNewVersionDefault()
      .catch((e) => {
        expect(e).not.to.be.undefined;
      })
  })

  /*
  * Versions table
  *
  * INT 1.0.0-INT.0
  * DEV 2.0.0-SNP.0 = INT+1
  * TST 2.0.0-TST.0 = DEV
  * INT 2.0.0-INT.0 = TST
  * --------------------
  * DEV 3.0.0-SNP.0 = INT+1
  * TST 3.0.0-TST.0 = INT+1 / SAME MAJOR +1
  * DEV 3.0.0-SNP.1 = INT+1 / SAME MAJOR +1
  * DEV 3.0.0-SNP.2 = INT+1 / SAME MAJOR +1
  * TST 3.0.0-TST.1 = DEV / SAME MAJOR +1
  * DEV 3.0.0-SNP.3 = INT+1
  * TST 3.0.0-TST.2 = DEV / SAME MAJOR +1
  * INT 3.0.0-INT.0 = TST
  * DEV 4.0.0.SNP.0 = INT+1
  * ACC 3.0.0-ACC.0 = INT
  * PRD 3.0.0       = ACC
  * TRN 3.0.0-TRN.0 = PROD
  * DLT 3.0.0-DLT.0 = PROD
  */

  it('version for DEV should initialize when no envsMetadata and no versionsMetadata', () => {
    return common.getNewVersionDefault('DEV', emptyVersionsMetadata, emptyEnvsMetadata)
      .then((version) => {
        expect(version).to.equal('1.0.0-SNAPSHOT.0');
      })
  })

  it('version for DEV should return INT raw major + 1', () => {
    return common.getNewVersionDefault('DEV', versionsMetadata, envsMetadata)
      .then((version) => {
        expect(version).to.equal('118.0.0-SNAPSHOT.14');
      })
  })

  it('version for TST should return DEV raw major', () => {
    return common.getNewVersionDefault('TST', versionsMetadata, envsMetadata)
      .then((version) => {
        expect(version).to.equal('118.0.0-TST.0');
      })
  })

  it('version for INT should return TST raw major', () => {
    return common.getNewVersionDefault('INT', versionsMetadata, envsMetadata)
      .then((version) => {
        expect(version).to.equal('117.0.0-INT.0');
      })
  })

  it('version for ACC should return INT raw major', () => {
    return common.getNewVersionDefault('ACC', versionsMetadata, envsMetadata)
      .then((version) => {
        expect(version).to.equal('117.0.0-ACC.0');
      })
  })

  it('version for PROD should return ACC raw major', () => {
    return common.getNewVersionDefault('PROD', versionsMetadata, envsMetadata)
      .then((version) => {
        expect(version).to.equal('117.0.0');
      })
  })

  it('version for PROD should return ACC raw major, increment if x.0.0 already exits to next patch version x.0.1', () => {
    const versionsMetadata = {
      "versions": [
        {
          "version": "117.0.0",
          "releaseDate": "01/01/2019 00:00",
          "envTarget": "PROD"
        },
      ]
    };
    return common.getNewVersionDefault('PROD', versionsMetadata, envsMetadata)
      .then((version) => {
        expect(version).to.equal('117.0.1');
      })
  })

  it('version for PROD should return ACC raw major, increment if x.0.1 already exist to next patch version x.0.2', () => {
    const versionsMetadata = {
      "versions": [
        {
          "version": "117.0.0",
          "releaseDate": "01/01/2019 00:00",
          "envTarget": "PROD"
        },
        {
          "version": "117.0.1",
          "releaseDate": "01/01/2019 00:00",
          "envTarget": "PROD"
        }
      ]
    };
    return common.getNewVersionDefault('PROD', versionsMetadata, envsMetadata)
      .then((version) => {
        expect(version).to.equal('117.0.2');
      })
  })

  it('version for DLT should return ACC raw major', () => {
    return common.getNewVersionDefault('DLT', versionsMetadata, envsMetadata)
      .then((version) => {
        expect(version).to.equal('117.0.0-DLT.0');
      })
  })

  it('version for TRN should return ACC raw major', () => {
    return common.getNewVersionDefault('TRN', versionsMetadata, envsMetadata)
      .then((version) => {
        expect(version).to.equal('117.0.0-TRN.0');
      })
  })

});
