// SPDX-License-Identifier: Apache-2.0
import { expect } from 'chai';
import { SemanticVersion } from '../../../../src/business/utils/semantic-version.js';
import { IllegalArgumentError } from '../../../../src/core/errors/illegal-argument-error.js';
describe('SemanticVersion', () => {
    describe('constructor', () => {
        it('should create a SemanticVersion instance with a valid SemanticVersion<string>', () => {
            const semVersion = '1.0.0';
            const version = new SemanticVersion(semVersion);
            expect(version.toString()).to.equal(semVersion);
        });
        it('should create a SemanticVersion instance with a valid numeric version', () => {
            const version = new SemanticVersion(42);
            expect(version.major).to.equal(42);
        });
        it('should throw a RangeError for an invalid SemanticVersion<string>', () => {
            expect(() => new SemanticVersion(null)).to.throw(IllegalArgumentError, 'Invalid semantic version: null');
            expect(() => new SemanticVersion('invalid')).to.throw(IllegalArgumentError, 'Invalid semantic version: invalid');
        });
        it('should throw a RangeError for an invalid numeric version', () => {
            expect(() => new SemanticVersion(-1)).to.throw(IllegalArgumentError, 'Invalid semantic version: -1');
        });
    });
    describe('equals', () => {
        it('should return true for equal SemanticVersion<string> versions', () => {
            const version1 = new SemanticVersion('1.0.0');
            const version2 = new SemanticVersion('1.0.0');
            expect(version1.equals(version2)).to.be.true;
        });
        it('should return false for different SemanticVersion<string> versions', () => {
            const version1 = new SemanticVersion('1.0.0');
            const version2 = new SemanticVersion('2.0.0');
            expect(version1.equals(version2)).to.be.false;
        });
        it('should return true for equal numeric versions', () => {
            const version1 = new SemanticVersion(42);
            const version2 = new SemanticVersion(42);
            expect(version1.equals(version2)).to.be.true;
        });
        it('should return false for different numeric versions', () => {
            const version1 = new SemanticVersion(42);
            const version2 = new SemanticVersion(43);
            expect(version1.equals(version2)).to.be.false;
        });
    });
    describe('compare', () => {
        it('should return 0 for equal SemanticVersion<string> versions', () => {
            const version1 = new SemanticVersion('1.0.0');
            const version2 = new SemanticVersion('1.0.0');
            expect(version1.compare(version2)).to.equal(0);
            expect(version1.lessThan(version2)).to.be.false;
            expect(version1.lessThanOrEqual(version2)).to.be.true;
            expect(version1.greaterThan(version2)).to.be.false;
            expect(version1.greaterThanOrEqual(version2)).to.be.true;
            expect(version1.equals(version2)).to.be.true;
        });
        it('should return -1 when the first SemanticVersion<string> is less than the second', () => {
            const version1 = new SemanticVersion('3.14.1');
            const version2 = new SemanticVersion('4.3.0');
            expect(version1.compare(version2)).to.equal(-1);
            expect(version1.lessThan(version2)).to.be.true;
            expect(version1.lessThanOrEqual(version2)).to.be.true;
        });
        it('should return 1 when the first SemanticVersion<string> is greater than the second', () => {
            const version1 = new SemanticVersion('2.0.0');
            const version2 = new SemanticVersion('1.2.3');
            expect(version1.compare(version2)).to.equal(1);
            expect(version1.greaterThan(version2)).to.be.true;
            expect(version1.greaterThanOrEqual(version2)).to.be.true;
        });
        it('should return 0 for equal numeric versions', () => {
            const version1 = new SemanticVersion(42);
            const version2 = new SemanticVersion(42);
            expect(version1.compare(version2)).to.equal(0);
        });
        it('should return -1 when the first numeric version is less than the second', () => {
            const version1 = new SemanticVersion(42);
            const version2 = new SemanticVersion(43);
            expect(version1.compare(version2)).to.equal(-1);
        });
        it('should return 1 when the first numeric version is greater than the second', () => {
            const version1 = new SemanticVersion(43);
            const version2 = new SemanticVersion(42);
            expect(version1.compare(version2)).to.equal(1);
            expect(version1.greaterThan(version2)).to.be.true;
            expect(version1.greaterThanOrEqual(version2)).to.be.true;
        });
        it('v3.14.2+gc309b6f is less than v4.1.3+gc94d381', () => {
            const version1 = new SemanticVersion('3.14.2+gc309b6f');
            const version2 = new SemanticVersion('4.1.3+gc94d381');
            expect(version1.compare(version2)).to.equal(-1);
            expect(version1.lessThan(version2)).to.be.true;
            expect(version1.lessThanOrEqual(version2)).to.be.true;
        });
        it('v0.0.1-alpha is less than v0.0.1', () => {
            const version1 = new SemanticVersion('0.0.1-alpha');
            const version2 = new SemanticVersion('0.0.1');
            expect(version1.compare(version2)).to.equal(-1);
            expect(version1.lessThan(version2)).to.be.true;
            expect(version1.lessThanOrEqual(version2)).to.be.true;
        });
        it('v1.0.0-beta.1 is less than v1.0.0-beta.2', () => {
            const version1 = new SemanticVersion('1.0.0-beta.1');
            const version2 = new SemanticVersion('1.0.0-beta.2');
            expect(version1.compare(version2)).to.equal(-1);
            expect(version1.lessThan(version2)).to.be.true;
            expect(version1.lessThanOrEqual(version2)).to.be.true;
        });
        it('v1.0.0-beta.2 is less than v1.0.0', () => {
            const version1 = new SemanticVersion('1.0.0-beta.2');
            const version2 = new SemanticVersion('1.0.0');
            expect(version1.compare(version2)).to.equal(-1);
            expect(version1.lessThan(version2)).to.be.true;
            expect(version1.lessThanOrEqual(version2)).to.be.true;
        });
        it('3.14.2+gc309b6f is less than 3.15.0', () => {
            const version1 = new SemanticVersion('3.14.2+gc309b6f');
            const version2 = new SemanticVersion('3.15.0');
            expect(version1.compare(version2)).to.equal(-1);
            expect(version1.lessThan(version2)).to.be.true;
            expect(version1.lessThanOrEqual(version2)).to.be.true;
        });
        it('3.14.2+gc309b6f is equal to 3.14.2', () => {
            const version1 = new SemanticVersion('3.14.2+gc309b6f');
            const version2 = new SemanticVersion('3.14.2');
            expect(version1.compare(version2)).to.equal(0);
            expect(version1.equals(version2)).to.be.true;
        });
        it('0.28.1-rc.1 < 0.28.1', () => {
            const version1 = new SemanticVersion('0.28.1-rc.1');
            const version2 = new SemanticVersion('0.28.1');
            expect(version1.compare(version2)).to.equal(-1);
            expect(version1.lessThan(version2)).to.be.true;
            expect(version1.lessThanOrEqual(version2)).to.be.true;
        });
        it('v0.72.0-rc.4 > v0.72.0-0 (numeric identifiers have lower precedence than alphanumeric)', () => {
            const version1 = new SemanticVersion('v0.72.0-rc.4');
            const version2 = new SemanticVersion('v0.72.0-0');
            expect(version1.compare(version2)).to.equal(1);
            expect(version1.greaterThan(version2)).to.be.true;
            expect(version1.greaterThanOrEqual(version2)).to.be.true;
            expect(version2.lessThan(version1)).to.be.true;
            expect(version2.lessThanOrEqual(version1)).to.be.true;
        });
        it('should follow semver §11 pre-release precedence ordering', () => {
            // From semver.org: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta
            //                  < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0
            const versions = [
                new SemanticVersion('1.0.0-alpha'),
                new SemanticVersion('1.0.0-alpha.1'),
                new SemanticVersion('1.0.0-alpha.beta'),
                new SemanticVersion('1.0.0-beta'),
                new SemanticVersion('1.0.0-beta.2'),
                new SemanticVersion('1.0.0-beta.11'),
                new SemanticVersion('1.0.0-rc.1'),
                new SemanticVersion('1.0.0'),
            ];
            for (let index = 0; index < versions.length - 1; index++) {
                expect(versions[index].lessThan(versions[index + 1]), `${versions[index]} should be less than ${versions[index + 1]}`).to.be.true;
            }
        });
    });
    describe('toString', () => {
        it('should return the string representation of a SemanticVersion<string> version', () => {
            const version = new SemanticVersion('1.0.0');
            expect(version.toString()).to.equal('1.0.0');
        });
        it('should return the string representation of a numeric version', () => {
            const version = new SemanticVersion(42);
            expect(version.toString()).to.equal('42');
        });
        it('should return the string representation of a single number string', () => {
            const version = new SemanticVersion('42');
            expect(version.toString()).to.equal('42.0.0');
        });
        it('should return the string representation as v3.14.2+gc309b6f for a version with build metadata', () => {
            const version = new SemanticVersion('3.14.2+gc309b6f');
            expect(version.toString()).to.equal('3.14.2+gc309b6f');
        });
    });
});
//# sourceMappingURL=semantic-version.test.js.map